The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Python Programming
|
Need help with this problem. ( Python 3.2.1.1)
Discuss Need help with this problem. ( Python 3.2.1.1) in the Python Programming forum on Dev Shed. Need help with this problem. ( Python 3.2.1.1) Python Programming forum discussing coding techniques, tips and tricks, and Zope related information. Python was designed from the ground up to be a completely object-oriented programming language.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

February 15th, 2013, 02:48 PM
|
|
Registered User
|
|
Join Date: Feb 2013
Posts: 2
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?
|

February 15th, 2013, 04:39 PM
|
|
Contributing User
|
|
Join Date: Nov 2012
Posts: 43
Time spent in forums: 15 h 59 m 28 sec
Reputation Power: 1
|
|
|
The link doesn't seem to work.
|

February 15th, 2013, 04:41 PM
|
|
Registered User
|
|
Join Date: Feb 2013
Posts: 2
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
|

February 15th, 2013, 05:57 PM
|
 |
Contributing User
|
|
|
|
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.
|

February 15th, 2013, 09:37 PM
|
|
Contributing User
|
|
Join Date: May 2009
Posts: 291
  
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)
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|