Trying to print a number triangle that does the following: numTri(6)
1
23
456
Edit:(all new line) What I have so far:
def numTri(n):
a = 0
for x in range(1,n+1):
a = 10*a + x
print a
Any hints? I don't want the answer. Some guidance would be well appreciated.
Woot this is my first post! (Edit: I posted the python code since someone else had posted the complete answer already). The following approaches this problem from a different perspective and needs only one loop. Hope this helps.
def numTri(n):
x = list(range(1,n+1)) #creates a list of numbers ([1],[2],...,[n])
i = 0
ln = 1
while i < n+1:
print(x[i:i+ln]) #prints a partition of the list of numbers
i += ln
ln += 1
NB: you may need to adjust the print function, I was using python 3.5
As you have said guidance.
def numTri(n):
a=1
col_per_row=1
while a<=n:
s=""
for y in range(1,col_per_row+1):
s+=str(a)
a=a+1
col_per_row=col_per_row+1
if(a==n+1):
print(s),
else:
print(s)
print(s,end="")
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.