Not Getting proper number traingle in python - python

i would like to print a some triangle of numbers. please have look at below code. Can anybody tell me where it's going wrong in following code??
x=0
while x<10:
y=x
while (y >= 0):
print y,
y=y-1
print x
x=x+1
output:
0 0
1 0 1
2 1 0 2
3 2 1 0 3
4 3 2 1 0 4
5 4 3 2 1 0 5
6 5 4 3 2 1 0 6
7 6 5 4 3 2 1 0 7
8 7 6 5 4 3 2 1 0 8
9 8 7 6 5 4 3 2 1 0 9
required o/p:
0
1 0
2 1 0
3 2 1 0
4 3 2 1 0
5 4 3 2 1 0
6 5 4 3 2 1 0
7 6 5 4 3 2 1 0
8 7 6 5 4 3 2 1 0
9 8 7 6 5 4 3 2 1 0
Thanks

You're getting an extra number at the end of each line because you're printing x when you don't need to. I assume you have that print statement there so you can force a line break between different rows of results. If all you want is to move the cursor down a line, you can use print with no arguments.
x=0
while x<10:
y=x
while (y >= 0):
print y,
y=y-1
print
x=x+1

Just change the print x to print. That will output a newline without printing the value of x, which is where the unwanted numbers come from:
x=0
while x<10:
y=x
while (y >= 0):
print y,
y=y-1
print
x=x+1
The output is:
0
1 0
2 1 0
3 2 1 0
4 3 2 1 0
5 4 3 2 1 0
6 5 4 3 2 1 0
7 6 5 4 3 2 1 0
8 7 6 5 4 3 2 1 0
9 8 7 6 5 4 3 2 1 0

Related

Code that prints out a right triangle. Each line has numbers and that at the end of each line is the sum of all numbers in it

What I have so far:
n=10
for i in range(1,n+1):
for j in range(0,i+1):
print(j,end=' ')
print('')
That prints out:
0 1
0 1 2
0 1 2 3
0 1 2 3 4
0 1 2 3 4 5
0 1 2 3 4 5 6
0 1 2 3 4 5 6 7
0 1 2 3 4 5 6 7 8
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9 10
The output should be:
0 1 1
0 1 2 3
0 1 2 3 6
0 1 2 3 4 10
0 1 2 3 4 5 15
0 1 2 3 4 5 6 21
0 1 2 3 4 5 6 7 28
0 1 2 3 4 5 6 7 8 36
0 1 2 3 4 5 6 7 8 9 45
0 1 2 3 4 5 6 7 8 9 10 55
At the end of each line is the sum of all the numbers in the line. For example, in line 5, the sum of all numbers in that line is 15.
As you can see I am missing the sum of all the numbers at the end of each line. Should I add another for-loop? Should I change the inner loop? I don't know what code I should use to get the sum of the numbers.
Thank you!
Perhaps the simplest way:
n=10
for i in range(1,n+1):
r = range(i+1)
print(*r, sum(r))
You can track your own sum, if you want to.
n=10
for i in range(1,n+1):
sumx = 0
for j in range(i+1):
sumx += j
print(j,end=' ')
print(sumx)
Or, microscopically better:
n=10
sumx = 0
for i in range(1,n+1):
for j in range(i+1):
print(j,end=' ')
sumx += i
print(sumx)

Simple Python number generation

I'm missing something very simple. I'm a new Python student, so bear with me.
for i in range(10):
print("\n")
for x in range(10):
print(x, end = " ")
Sample output:
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9
...
The "i" variable is setting the size of what I want to output, and in the above example I am showing the range of 0-9, 10 times. How would I alter this to display the following without using the print command by way of a string or an array? I'm trying to use two nested "for" statements, but I'm drawing a blank on what to use in place of the range command.
Desired output:
0 0 0 0 0 0 0 0 0 0
1 1 1 1 1 1 1 1 1 1
2 2 2 2 2 2 2 2 2 2
3 3 3 3 3 3 3 3 3 3
...
You are printing the variable from your inner loop (x) instead of the variable from the outer loop (i). Do it this way:
for i in range(10):
print("\n")
for x in range(10):
print(i, end = " ")
why to loop twice, we know str*n give n times str
>>> for x in range(10):
... print((str(x) + ' ')*10)
...
0 0 0 0 0 0 0 0 0 0
1 1 1 1 1 1 1 1 1 1
2 2 2 2 2 2 2 2 2 2
3 3 3 3 3 3 3 3 3 3
4 4 4 4 4 4 4 4 4 4
5 5 5 5 5 5 5 5 5 5
6 6 6 6 6 6 6 6 6 6
7 7 7 7 7 7 7 7 7 7
8 8 8 8 8 8 8 8 8 8
9 9 9 9 9 9 9 9 9 9
demo:
>>> 'a'*5
'aaaaa'
>>> 'a'*10
'aaaaaaaaaa'
>>> 'hello**'*10
'hello**hello**hello**hello**hello**hello**hello**hello**hello**hello**'
>>> '9'*10
'9999999999'
This also works:
for i in range(10):
print ' '.join(str(i) * 10)

Nested Loops Python

Hi I am working with nested loops but can not figure out a way to start a new row for my nested loop below:
for i in range(0,10):
for j in range(10):
print(i,end =" ")
Output
0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 5 5 6 6 6 6 6 6 6 6 6 6 7 7 7 7 7 7 7 7 7 7 8 8 8 8 8 8 8 8 8 8 9 9 9 9 9 9 9 9 9 9
The output I want is:
0 0 0 0 0 0 0 0 0 0
1 1 1 1 1 1 1 1 1 1
2 2 2 2 2 2 2 2 2 2
and so on
Thank you but I found the answer!
for i in range(0,10):
for j in range(10):
print(i, end=" ")
print()
This (and minor variations) also work:
for i in range(10):
print((('%s '%i)*10)[:-1])

Python: Replace a cell value in Dataframe with if statement

I have a matrix with that looks like this:
com 0 1 2 3 4 5
AAA 0 5 0 4 2 1 4
ABC 0 9 8 9 1 0 3
ADE 1 4 3 5 1 0 1
BCD 1 6 7 8 3 4 1
BCF 2 3 4 2 1 3 0 ...
Where AAA, ABC ... is the dataframe index. The dataframe columns are com 0 1 3 4 5 6
I want to set the cell values in my dataframe equal to 0 when the row values of com is equal the column "number". So for instance, the above matrix will look like:
com 0 1 2 3 4 5
AAA 0 0 0 4 2 1 4
ABC 0 0 8 9 1 0 3
ADE 1 4 0 5 1 0 1
BCD 1 6 0 8 3 4 1
BCF 2 3 4 0 1 3 0 ...
I tried to iterate over rows and use both .loc and .ix but no success.
Just require some numpy trick
In [22]:
print df
0 1 2 3 4 5
0 5 0 4 2 1 4
0 9 8 9 1 0 3
1 4 3 5 1 0 1
1 6 7 8 3 4 1
2 3 4 2 1 3 0
[5 rows x 6 columns]
In [23]:
#making a masking matrix, 0 where column and index values equal, 1 elsewhere, kind of the vectorized way of doing if TURE 0, else 1
print df*np.where(df.columns.values==df.index.values[..., np.newaxis], 0,1)
0 1 2 3 4 5
0 0 0 4 2 1 4
0 0 8 9 1 0 3
1 4 0 5 1 0 1
1 6 0 8 3 4 1
2 3 4 0 1 3 0
[5 rows x 6 columns]
I think this should work.
for line in range(len(matrix)):
matrix[matrix[line][0]+1]=0
NOTE
Depending on your matrix setup you may not need the +1
Basically it takes the first digit of each line in the matrix and uses that as the index of the value to change to 0
i.e. if the row was
c 0 1 2 3 4 5
AAA 4 3 2 3 9 5 9,
it would change the 5 below the number 4 to 0
c 0 1 2 3 4 5
AAA 4 3 2 3 9 0 9

How can I print this?

I am trying to teach myself python using interactivepython.org. I have come across a problem that I can not figure out. I have the slope and the spacing correct. I need it to print one less number every time. Could anybody help a newbie out?...
The code I have written:
numLines = 10
for i in range(numLines):
for k in range(i):
print(' ', end = ' ')
for j in range(1, numLines):
print(j, end = ' ')
print()
print(" ")
Prints:
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
Want to Print:
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8
0 1 2 3 4 5 6 7
0 1 2 3 4 5 6
0 1 2 3 4 5
0 1 2 3 4
0 1 2 3
0 1 2
0 1
0
Well, The above answers are perfectly fine. But this is my way of doing things... :)
Code:
l = map(str,range(0,10))
for i in range(10):
print ' '.join(l[:len(l)-i]).rjust(20)
Output:
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8
0 1 2 3 4 5 6 7
0 1 2 3 4 5 6
0 1 2 3 4 5
0 1 2 3 4
0 1 2 3
0 1 2
0 1
0
Hope this helps :)
Try this:
numLines = 10
for i in range(numLines, 0, -1):
for j in range(0, numLines - i):
print " ",
for k in range(0, i):
print k,
print
How about this
numLines = 10
for i in range(numLines):
print "".join (" " for j in range(i)) + " ".join (str(j) for j in range(numLines - i))
Output
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8
0 1 2 3 4 5 6 7
0 1 2 3 4 5 6
0 1 2 3 4 5
0 1 2 3 4
0 1 2 3
0 1 2
0 1
0
Never mind I figured it out. I had to decrement numLines by one inside the first for loop.

Categories