4 Replies - 426 Views - Last Post: 17 April 2013 - 04:29 AM Rate Topic: -----

#1 uzerza  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 20-March 13

cant "join" a Python 3 "[list]"

Posted 16 April 2013 - 08:54 AM

Hi,

I am trying to print out pascals triangle based on a user input, and it works, but it prints out the data in a list, and when i try and convert the lists into a string so I can format it, it messes with the code and it changes the list in the loop.

Here is my code below, right now it will calculate up to the 8th row, and then it will print out all of the values, I dont want it to print out all the values at the end and I want to remove all of the brackets.

Can anyone tell me what Im doing wrong?


def triangle(n):
    if n == 0:
        return []
    elif n == 1:
        return [[1]]
    else:
        new_row = [1]
        result = triangle(n-1)
        last_row = result[-1]
        print(last_row)
        for i in range(len(last_row)-1):
            new_row.append(last_row[i] + last_row[i+1])
        new_row += [1]
        result.append(new_row)
        a=result[:]
    print(a)
print(triangle(9))





Any constructive thoughts would be appreciated, Thanks.

Is This A Good Question/Topic? 0
  • +

Replies To: cant "join" a Python 3 "[list]"

#2 Mekire  Icon User is offline

  • D.I.C Head

Reputation: 116
  • View blog
  • Posts: 212
  • Joined: 11-January 13

Re: cant "join" a Python 3 "[list]"

Posted 16 April 2013 - 11:16 PM

Not positive what you want the output to look like honestly. Also that code doesn't run.
If you want to print every element you have collected without [] that shouldn't be hard.

This might do something like you want (python 2 users will need to add from __future__ import print_function at the top of their code:
Spoiler

Output for a triangle of 20 rows:
Spoiler

-Mek

This post has been edited by Mekire: 16 April 2013 - 11:25 PM

Was This Post Helpful? 0
  • +
  • -

#3 uzerza  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 20-March 13

Re: cant "join" a Python 3 "[list]"

Posted 16 April 2013 - 11:40 PM

View PostMekire, on 16 April 2013 - 11:16 PM, said:

Not positive what you want the output to look like honestly. Also that code doesn't run.
If you want to print every element you have collected without [] that shouldn't be hard.

This might do something like you want (python 2 users will need to add from __future__ import print_function at the top of their code:
Spoiler

Output for a triangle of 20 rows:
Spoiler

-Mek


Hi Mek,

Thanks so much for your reply!
That is the output I was expecting, but only with one space in-between the numbers, which bring me back to my original problem, as I am confused where I can change the spacing.

Could you please let me know?

Thanks!
Was This Post Helpful? 0
  • +
  • -

#4 Mekire  Icon User is offline

  • D.I.C Head

Reputation: 116
  • View blog
  • Posts: 212
  • Joined: 11-January 13

Re: cant "join" a Python 3 "[list]"

Posted 17 April 2013 - 12:15 AM

If you want a single space, regardless of the size of the numbers, then you can certainly use join if you'd like.

Just change the pretty function to this:
def pretty(tri):
    for group in tri:
        print(" ".join([str(num) for num in group]))

Or if you don't want to use join:
def pretty(tri):
    for group in tri:
        for ele in group:
            print(ele,end=" ")
        print()

Because the numbers get large pretty quickly though, using a single space won't line your columns up very well.

-Mek
Was This Post Helpful? 0
  • +
  • -

#5 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5881
  • View blog
  • Posts: 12,758
  • Joined: 16-October 07

Re: cant "join" a Python 3 "[list]"

Posted 17 April 2013 - 04:29 AM

If you just want single spaces, you can actually do it as a one liner:
tri = triangle(20)
print '\n'.join(' '.join(str(c) for c in row) for row in tri)



Which is basically the join already offered.

For something slightly more interesting, you could do:
def pretty3(tri):
	def center(row):
		ofs = len(rows[-1])/2 - len(row)/2
		return (' ' * ofs) + row
	rows = [' '.join(str(c) for c in row) for row in tri]
	print '\n'.join(center(row) for row in rows)



Result:
                                               1
                                              1 1
                                             1 2 1
                                            1 3 3 1
                                           1 4 6 4 1
                                         1 5 10 10 5 1
                                       1 6 15 20 15 6 1
                                      1 7 21 35 35 21 7 1
                                    1 8 28 56 70 56 28 8 1
                                  1 9 36 84 126 126 84 36 9 1
                              1 10 45 120 210 252 210 120 45 10 1
                            1 11 55 165 330 462 462 330 165 55 11 1
                          1 12 66 220 495 792 924 792 495 220 66 12 1
                      1 13 78 286 715 1287 1716 1716 1287 715 286 78 13 1
                  1 14 91 364 1001 2002 3003 3432 3003 2002 1001 364 91 14 1
               1 15 105 455 1365 3003 5005 6435 6435 5005 3003 1365 455 105 15 1
           1 16 120 560 1820 4368 8008 11440 12870 11440 8008 4368 1820 560 120 16 1
       1 17 136 680 2380 6188 12376 19448 24310 24310 19448 12376 6188 2380 680 136 17 1
    1 18 153 816 3060 8568 18564 31824 43758 48620 43758 31824 18564 8568 3060 816 153 18 1
1 19 171 969 3876 11628 27132 50388 75582 92378 92378 75582 50388 27132 11628 3876 969 171 19 1



I kind of like it, but we can get more triangle shaped:
def pretty(tri):
	def center(s, size):
		s = str(s)
		ofs = size/2 - len(s)/2
		tail = size - (ofs + len(s))
		return (' ' * ofs) + s + (' ' * tail)
	itemSize = len(str(max(tri[-1]))) + 1
	rows = [''.join(center(c, itemSize) for c in row) for row in tri]
	print '\n'.join(center(row, len(rows[-1])) for row in rows)



Results:
                                                            1                                                           
                                                         1     1                                                        
                                                      1     2     1                                                     
                                                   1     3     3     1                                                  
                                                1     4     6     4     1                                               
                                             1     5    10    10     5     1                                            
                                          1     6    15    20    15     6     1                                         
                                       1     7    21    35    35    21     7     1                                      
                                    1     8    28    56    70    56    28     8     1                                   
                                 1     9    36    84    126   126   84    36     9     1                                
                              1    10    45    120   210   252   210   120   45    10     1                             
                           1    11    55    165   330   462   462   330   165   55    11     1                          
                        1    12    66    220   495   792   924   792   495   220   66    12     1                       
                     1    13    78    286   715  1287  1716  1716  1287   715   286   78    13     1                    
                  1    14    91    364  1001  2002  3003  3432  3003  2002  1001   364   91    14     1                 
               1    15    105   455  1365  3003  5005  6435  6435  5005  3003  1365   455   105   15     1              
            1    16    120   560  1820  4368  8008  11440 12870 11440 8008  4368  1820   560   120   16     1           
         1    17    136   680  2380  6188  12376 19448 24310 24310 19448 12376 6188  2380   680   136   17     1        
      1    18    153   816  3060  8568  18564 31824 43758 48620 43758 31824 18564 8568  3060   816   153   18     1     
   1    19    171   969  3876  11628 27132 50388 75582 92378 92378 75582 50388 27132 11628 3876   969   171   19     1  


Was This Post Helpful? 2
  • +
  • -

Page 1 of 1