0

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.

user2736738
  • 30,591
  • 5
  • 42
  • 56
cbeckford1
  • 11
  • 4

2 Answers2

2

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

BigH
  • 340
  • 4
  • 6
  • Unfortunately this code does not seem to work, for the example in the question I get this output `>>> numTri(6) [1] [2, 3] [4, 5]` (newlines removed by commenting system) – Jonah Graham Oct 21 '15 at 14:37
  • Hi, Jonah. Try again after my edit, I had tested on larger numbers and it looked alright. Let me know if there is a problem. – BigH Oct 21 '15 at 15:52
  • It still prints `[2, 3]` rather than `23` as the OP asked for – Jonah Graham Oct 21 '15 at 16:44
  • The code does create a number triangle and within the comments I expressly said that it prints a partition of a list, "#prints a partition of the list of numbers", and made a note at the bottom that they may need to change the print statement, "NB: you may need to adjust the print function, I was using python 3.5", which is all that is needed to display the result without the brackets/commas. The OP did after-all ask for hints and the print function changes slightly with different versions of Python. – BigH Oct 21 '15 at 17:17
  • I have upvoted your answer now, I think you make a valid point. Let the OP ask more specific questions if they want to learn how to use join or similar. I had actually misunderstood your NB, I thought the two parts of your sentence were about the same thing, i.e. the print statement may need adjusting /because/ of python 3.5 – Jonah Graham Oct 21 '15 at 17:47
0

As you have said guidance.

Python code

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)
  • The comma after print statement is for avoiding the newline in Python-2
  • In Python-3 you can use print(s,end="")

1.How to print in python without newline or space?

Community
  • 1
  • 1
user2736738
  • 30,591
  • 5
  • 42
  • 56
  • thanks a ton! Now to figure out how to do this another way. – cbeckford1 Oct 21 '15 at 06:25
  • That Python code: 1) prints all in one line, 2) produces `n` lines instead of only counting up to `n`, 3) produces a space between each number, and 4) gives away the (incorrect) answer without further explanation. – TigerhawkT3 Oct 21 '15 at 06:26
  • @TigerhawkT3 if you remove the semicolon + comma, the loop prints according to rows; does this work, because the last print statement is printing according to the number of times the first loop iterates i.e. 5 times ? If this is the case, does the comma after (a) reserve each iteration in the second loop on a new line? Thus when the second print statement is elicited it prints each iteration n times according to the first loop while the print statement within the second loop holds and prints each number within the n rows? – cbeckford1 Oct 21 '15 at 06:47
  • The output of your code does not seem to produce the expected results. I get: `>>> numTri(6) 1 23 456 78910 1112131415 161718192021 ` (newlines removed by commenting system) – Jonah Graham Oct 21 '15 at 14:43
  • @coderredoc unfortunately you have gone in the wrong direction as the code no longer is syntactically valid as you are missing some indentation. – Jonah Graham Oct 21 '15 at 17:43
  • @JonahGraham.: Just comment that if the code works properly or not!! – user2736738 Oct 21 '15 at 17:51
  • Sadly, the code does not seem to produce the correct output :-(Although as @BigH mentions the OP asked for guidance and you have provided that. – Jonah Graham Oct 21 '15 at 17:51
  • I agree about unclear questions being a rathole. If you want to get diving in with me (fair enough if you don't) this is what I get: http://pastebin.com/WJiD9QaA – Jonah Graham Oct 21 '15 at 18:01
  • @JonahGraham.: Poor indentation killed me really..sorry..Have a look if you have time? Thanks for patiently commenting and .... – user2736738 Oct 21 '15 at 18:12
  • Looks right now. Of course, OP could add some detail of what should happen on `numTri(10)` as the number get more digits the triangle changes shape (i.e. not a triangle), but perhaps OP can add some more details and we can move on with our life – Jonah Graham Oct 21 '15 at 18:19
  • @JonahGraham.: I have learnt a great thing from you too.. you could have easily downvoted as the other guy had..but rather you just made me put the correct answer..thanks and yes as usual like many other question the question was vague and not that clear...On my part indentation was problem as I am doing it in a online editor. – user2736738 Oct 21 '15 at 18:22