September 25th, 2013, 12:11 AM

Write a program usiing nested loops to output a pyramid
I have written the following code using python 3.2 I am getting errors maybe someone could help fill me in as to what I am doing wrong any help would be much appreciate.
#This program will display a pyramid pattern
for i in range (0, i < n, i+1):
for l in range (n  i, l > 0, l1):
print(" ")
for j in range (0, j <= i, j+1):
print(format(1 < j,"3d"))
for k in range (i  1, k >= 0, k1):
print(format(1 < k,"3d"))
print()
This is what it's supposed to output:
#
# 1
# 1 2 1
# 1 2 4 2 1
# 1 2 4 8 4 2 1
# 1 2 4 8 16 8 4 2 1
# 1 2 4 8 16 32 16 8 4 2 1
# 1 2 4 8 16 32 64 32 16 8 4 2 1
# 1 2 4 8 16 32 64 128 64 32 16 8 4 2 1
It's actually supposed to look like a pyramid
September 25th, 2013, 01:54 AM

I believe it's pascals triangle but I am not sure
I really have this code messed up I saw the code in Java and tried to port it over to python and screwed it up big time if anyone can help I would really appreciate it. I have an assignment due and I have no clue as to what to do for the code.
Thanks
September 25th, 2013, 08:23 AM

The data you show is not Pascal's triangle but probably should have been.
Suppose you multiply (x+y) by (x+y)
Code:
(x+y)*(x+y) == x*(x+y) + y*(x+y) by distributing multiplication over addition.
==(x*x + x*y) + (y*x + y*y) same reasoning.
==(x*x + x*y) + (x*y + y*y) scalar multiplication commutes.
==x*x + x*y + x*y + y*y regrouping...
==x*x + (x*y + x*y) + y*y
==1 * (x*x) + 2 * (x*y) + 1 * (y*y)
giving coefficients 1 2 1
So far, so good. Let's write the next row:
Code:
(x+y)**3 == (x+y)*(x+y)*(x+y) definition of power
== (x+y) * (x*x + 2*x*y + y*y) substitution from previous result
== x*(x*x + 2*x*y + y*y) + y*(x*x + 2*x*y + y*y)
== (x x x + 2 x x y + x y y) + (x x y + 2 x y y + y y y)
== ... coefficients 1 3 3 1 and that's Pascal's triangle.
Let's look at the first row.
(x+y)^0 is 1.
And the second row of Pascal's triangle with coefficients 1 1:
(x + y)^1 == 1x + 1y
Awesome.
Write whatever data you need to a text file with appropriate rows,
With the python subprocess module load the file into some text editor commanding it to produce centered text, then save the file. Reread it into python and show on console. Not as dumb as it sounds because you can handle far more complex operations than centering this way.
Here's a j sentence (www.jsoftware.com) for your task.
Code:
f =: [: '_'&=`(,:&' ')} [: ": [: 0&=`(,:&_)} [: . [: : [: (, .@}:) [: . [: 2&^\. [: i. 
f 10
1
1 2 1
1 2 4 2 1
1 2 4 8 4 2 1
1 2 4 8 16 8 4 2 1
1 2 4 8 16 32 16 8 4 2 1
1 2 4 8 16 32 64 32 16 8 4 2 1
1 2 4 8 16 32 64 128 64 32 16 8 4 2 1
1 2 4 8 16 32 64 128 256 128 64 32 16 8 4 2 1
1 2 4 8 16 32 64 128 256 512 256 128 64 32 16 8 4 2 1
Here's the algorithm demonstrated with your n as 5. I'm sure you'll think of a shorter sequence of flip flops and transposes. I just kept sticking verbs together to make the result look closer and closer to the goal.
Code:
N =: 5 NB. your "n" is 5
f N NB. show that the verb works.
1
1 2 1
1 2 4 2 1
1 2 4 8 4 2 1
1 2 4 8 16 8 4 2 1
]A =: i.  N NB. start with a vector of integers in reverse order.
4 3 2 1 0
]B =: 2&^\.A NB. next raise 2 to the power of all suffixes of this vector.
16 8 4 2 1
8 4 2 1 0
4 2 1 0 0
2 1 0 0 0
1 0 0 0 0
[C =: . B NB. I wanted the 1 on top so I reversed the items of the matrix. if R is the rank of the array, "Items" have rank R1. Items are row vectors in this case.
1 0 0 0 0
2 1 0 0 0
4 2 1 0 0
8 4 2 1 0
16 8 4 2 1
NB. Next I observed that it would now be easier to work with items, and I saw a way forward.
NB. I'll have a numeric form of the triangle, sideways,
NB. if I concatenate the matrix C to itself, reversed.
NB. Except that I need to curtail it to avoid repeating
NB. the 16 8 4 2 1 vector.
[D =: (, .@}:) C
1 0 0 0 0
2 1 0 0 0
4 2 1 0 0
8 4 2 1 0
16 8 4 2 1
8 4 2 1 0
4 2 1 0 0
2 1 0 0 0
1 0 0 0 0
]E =: : D NB. Transpose to put in correct order.
1 2 4 8 16 8 4 2 1
0 1 2 4 8 4 2 1 0
0 0 1 2 4 2 1 0 0
0 0 0 1 2 1 0 0 0
0 0 0 0 1 0 0 0 0
]F =: . E NB. OOPS! Upside down. Reverse the items yet again.
0 0 0 0 1 0 0 0 0
0 0 0 1 2 1 0 0 0
0 0 1 2 4 2 1 0 0
0 1 2 4 8 4 2 1 0
1 2 4 8 16 8 4 2 1
Note 'finish'
We need a text representation. I replace for the 0 atoms, infinity,
which has the symbol _ . Format the matrix to convert it to text,
then replace _ with a space character.
Finally, I told j to give me a tacit verb with 13 :
"13 : n tacit verb if possible, otherwise equivalent to 3 : n"
)
f =: 13 :'''_''&=`(,:&'' '')}":0&=`(,:&_)}.:(,.@}:).2&^\.i.y'
f NB. show the definition of f
[: '_'&=`(,:&' ')} [: ": [: 0&=`(,:&_)} [: . [: : [: (, .@}:) [: . [: 2&^\. [: i. 
And that, friend, develops far more easily than writing it all out in python. (I notice that prefixes instead of suffixes would have shortened this. \. came to my head first and I went with it.)
Pascal's triangle version starter in numeric form:
Code:
(<.@::@:i.&.: .Rank 0 1 pt)8
0 0 0 1 0 0 0 0
0 0 0 1 1 0 0 0
0 0 1 2 1 0 0 0
0 0 1 3 3 1 0 0
0 1 4 6 4 1 0 0
0 1 5 10 10 5 1 0
1 6 15 20 15 6 1 0
1 7 21 35 35 21 7 1
pt
(!~ i.@>:)Rank 0~@:i.
Rank
"
Comments on this post
Last edited by b49P23TIvg; September 25th, 2013 at 08:56 AM.
[code]
Code tags[/code] are essential for python code and Makefiles!
September 25th, 2013, 01:15 PM

You are very knowledgeable and I thank you so much for your response. I on the other hand am a beginner, this is my 3rd week in an intro programming class. I worked on 5 programs last night and this one continues to stump me. I have to write it using a loop as per my lab requires it. I found some code in java and tried to port it over however I think I am missing a few things. Here is the code I rewrote in python:
#Program prints a number pyramid
row = 10
for i in range (1,numRow, i +=1):
num = 1
for j in range (0, j<numRow  i,j +1):
print(" ")
for j in range (numRowi+1, j<=numRow,j+1):
print(num," ")
num = num*2
num = num/2
for j in range(numRow+1, j<numRow+i,j+1):
num = num/2
print(num," ")
for j in range (numRow+i+1,j<=numRow*2,j+1):
print(" ")
print("\n")
This is the code in java I tried to port over:
int row = 10;
for(int i=1; i<=numRow ; i++){
int num = 1;
for(int j=0; j<numRow i; j++ ){
System.out.print(" ");
}
for(int j=numRowi+1; j<=numRow ; j++ ){
System.out.print(num+" ");
num=num*2;
}
num=num/2;
for(int j=numRow+1; j<numRow+i; j++ ){
num=num/2;
System.out.print(num+" ");
}
for(int j=numRow+i+1; j<=numRow*2; j++ ){
System.out.print(" ");
}
System.out.print("\n");
}
Any help with this would be greatly appreciated I have been racking my brain for hours trying to figure this out.
Thanks
September 25th, 2013, 01:51 PM

#Program prints a number pyramid
row = 10
num=1
numRow=8
i=1
j=1
num=2
for i in range (1,numRow, i + 1):
for j in range (0, j< numRow  i, j +1):
print(format("3d"))
for j in range (numRowi+1, j<=numRow,j+1):
print(format(num,"3d"))
num = num*2
num = num/2
for j in range(numRow+1, j<numRow+i,j+1):
num = num/2
print(format(num," 3d "))
for j in range (numRow+i+1,j<=numRow*2,j+1):
print(format("3d"))
print('\n')
So this is what I just tried and I am way off.... it just prints out a sequence of 3d in a column.... I am very lost apparently so if anyone can help I would really appreciate it.
September 25th, 2013, 02:48 PM

for i in range(): ~: for(initialize;condition;change)
The c/java for loop works like this:
Code:
for (init; cond; change) {statements;}
/* compiles the same as */
init;
while (cond) {
statements;
change;
}
The python range(start,stop,step) function does
>>> print(list(range(2,16,3)))
[2, 5, 8, 11, 14]
you've copied the java condition into the range stop value. See if you can finish the program:
Code:
n = 5
for i in range(n):
l = ni
while 0 < l:
print(' ', end = '')
l = 1
for j in range(i+1):
print('{:4d}'.format(2**j), end = '')
print()
Comments on this post
[code]
Code tags[/code] are essential for python code and Makefiles!
September 25th, 2013, 03:31 PM

You are printing powers of two, so to print the third line (I will leave the formatting and printing all the lines to you). You might start with understanding for loops http://en.wikibooks.org/wiki/NonPro..._2.6/For_Loops
Code:
level = 3
for ctr in range(level):
print 2**ctr,
## and reverse back down to zero
for reverse in range(level1):
ctr = 1
print 2**ctr,
print
September 26th, 2013, 02:44 PM

I have been working on this for two days trying to figure this out I wish I had a better understanding but I can't figure out how to write it so that it will print both sides of the pyramid. I can switch it around so it will go to the right side or visa versa but I don't know how to write it so that it will print both sides also if we are taking it to the power of two what do we do to count down from that to me it looks as if it's just dividing by two but I am not sure as to the logic. I know this shouldn't be so difficult but for some reason I am having so much trouble with this. I asked my prof for help in the lab but she wasn't much help at all. She just learned Python herself. So if you could help maybe explain it to me I would really appreciate it.
Thanks Again!!
September 26th, 2013, 06:07 PM

The java code you found multiplies by two for the left half + middle then divides by two for the right portion.
I used power instead, with the counters making the changes.
Sorry dude/dudette. You've got a lot of clues here. Not unusual for a professor to be reading only 2 days ahead in the text.
Comments on this post
[code]
Code tags[/code] are essential for python code and Makefiles!
September 27th, 2013, 12:41 AM

Code:
#Pyramid
n = 8
for i in range(8):
while i <= n:
num = 1
i+1
for j in range(ni+1):
while j <= n:
print(num," ")
num = num*2
for j in range(n+1):
while j < n+1:
num = num/2
print(num," ")
for j in range(n+i+1):
while j < n*2:
j+1
print(" ")
print()
So that is what I got which doesn't work. Am I atleast on the right track?
September 27th, 2013, 06:46 AM

You're not on the right track.
Use the python interpreter. Try experiments. Run this program and see what it does:
Code:
n = 5
for i in range(n):
l = ni
while 0 < l:
print(' ', end = '')
l = 1
for j in range(i+1):
print('{:4d}'.format(2**j), end = '')
print()
If you cannot run the program then we need to work on that, but that's more easily handled with someone looking over your shoulder than through this forum.
Now you must also try simple experiments in the python interpreter.
>>> for i in range(4): print(i)
...
>>> j = 7
>>> j + 1
>>> print(j)
>>> j = j+1
>>> print(j)
python is real. You can use it. It won't bite.
*I'd have you try dwblas's code also except that it's written for python version 2 and won't work in python3.
[code]
Code tags[/code] are essential for python code and Makefiles!
September 27th, 2013, 11:16 AM

Code:
#pyramid
n = 8
for i in range(n):
l = ni
while 0 < l:
print(' ', end = '')
l = 1
for j in range(i+1):
print('{:4d}'.format(2**j), end = '')
for j in range(i,0,1):
print('{:4d}'.format(2**(j1)), end = '')
print()
I got it !!!!!!!!!
I am so happy!!!!!!!!
Thanks so much for the tips!!!