Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old February 15th, 2013, 02:48 PM
Jokus Jokus is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 2 Jokus User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 37 m 14 sec
Reputation Power: 0
Need help with this problem. ( Python 3.2.1.1)

So I have this project for school. We are supposed to make :

h t t p : / / ed.edim.co/9437390/draw_with_loops.txt?Expires=1360962848&Signature=RYlWmjPnkWZTe9Seo7gesm6nucijqq3vVHhbJIr~s1lqNBQ4a9I7C9uVE9khvJniXarLYw1gH-iC1F-xTWjUtUGV2sc3CzYxQh9mozZVbZAA0TiAwK0BEXOt368gWB7GTb34G2zJe3934AfCnfnAGPgzbQYCL9JsYaulvFJwjkk_&Key-Pair-Id=APKAI3N2VAFIZ34RBHFA

I need to know how to make the third triangle, printing only 1 character at a time, and nesting atleast one loop, all in under 11 lines of code (Excluding the def triangle():, and triangle() ) Can anyone help?

Reply With Quote
  #2  
Old February 15th, 2013, 04:39 PM
eliskan eliskan is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 43 eliskan User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 15 h 59 m 28 sec
Reputation Power: 1
The link doesn't seem to work.

Reply With Quote
  #3  
Old February 15th, 2013, 04:41 PM
Jokus Jokus is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 2 Jokus User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 37 m 14 sec
Reputation Power: 0
you have to get rid of the spaces in the : h t t p : / /

it wouldn't let me post a link in my first post

Reply With Quote
  #4  
Old February 15th, 2013, 05:57 PM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,364 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 10 h 28 m 48 sec
Reputation Power: 383
The j (link) figure 3 triangle verb fits within the required number of lines.
Code:
   figure3triangle =: i. (< |)"0/ i:

   figure3triangle 8
1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 0 0 0 1 1 1 1 1 1 1
1 1 1 1 1 1 0 0 0 0 0 1 1 1 1 1 1
1 1 1 1 1 0 0 0 0 0 0 0 1 1 1 1 1
1 1 1 1 0 0 0 0 0 0 0 0 0 1 1 1 1
1 1 1 0 0 0 0 0 0 0 0 0 0 0 1 1 1
1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1

   '* ' ({~ figure3triangle) 8
        *        
       ***       
      *****      
     *******     
    *********    
   ***********   
  *************  
 *************** 
   
We can make it work in python!
Code:
import operator                                                 # line 1
integers = lambda y: list(range(y) if 0 <= y else reversed(range(-y)))          ; assert integers(8) == [0, 1, 2, 3, 4, 5, 6, 7]
steps = lambda y: list(range(-y, y + ((0 <= y) - (y < 0)), (0 <= y) - (y < 0))) ; assert [3, 2, 1, 0, -1, -2, -3] == steps(-3)
dyadicHook = lambda f, g: lambda x, y: f(x, g(y))               # line 4
table = lambda u: lambda x, y: [[u(X,Y) for Y in y] for X in x] # line 5
monadicFork = lambda f, g, h: lambda y: g(f(y), h(y))           # line 6
figure3triangle = monadicFork(integers, table(dyadicHook(operator.lt, operator.abs)), steps) # figure3triangle is a function
BOOLEAN_ARRAY = figure3triangle(8)                              # line 8
for ROW in BOOLEAN_ARRAY:                                       # line 9
    print(''.join('* '[ATOM] for ATOM in ROW))                  # line 10
#print('\n'.join(''.join('* '[ATOM] for ATOM in ROW) for ROW in BOOLEAN_ARRAY))  # replacement for lines 9 and 10
__________________
[code]Code tags[/code] are essential for python code!

Last edited by b49P23TIvg : February 15th, 2013 at 06:04 PM.

Reply With Quote
  #5  
Old February 15th, 2013, 09:37 PM
dwblas dwblas is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2009
Posts: 291 dwblas User rank is Corporal (100 - 500 Reputation Level)dwblas User rank is Corporal (100 - 500 Reputation Level)dwblas User rank is Corporal (100 - 500 Reputation Level)dwblas User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 3 Days 18 h 33 m 54 sec
Reputation Power: 7
The following does it in seven lines but does not nest one loop (note Python 2.7 print statement)
Code:
triangle_height=8
spaces = " " * triangle_height
to_print = "#"
for ctr in range(triangle_height):
    print "%s%s" % (spaces, to_print)
    to_print += "##"
    spaces = spaces[1:] 
So maybe something like the following. "Print one character at a time" could be done instead of packing the string if you suppress the space following and/or the new line of a normal print statement.
Code:
triangle_height=8
for ctr in range(triangle_height):
    spaces = ""
    for sp in range(ctr, triangle_height):
        spaces += " "
    stars = "*" + ("*" * ctr*2)     ## or a for() loop
    print "%s%s" % (spaces, stars) 

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Need help with this problem. ( Python 3.2.1.1)

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap