0

can someone explain this code to me in detail? im trying to grasp how recursion is working here especially how previous_lines is building up as a list. I'm getting confused to the point where n==2, I wonder why it doesn't show out of array error for previous_lines[i+1] where previous_lines does not even have 2 elements when n==2 is used. I surmise that previous_lines is only [1] at n==2

def pascal(n):
    if n == 1:
        return [1]
    else:
        line = [1]
        previous_line = pascal(n-1)
        for i in range(len(previous_line)-1):
            line.append(previous_line[i] + previous_line[i+1])
        line += [1]
    return line

print(pascal(6))
Psytho
  • 3,313
  • 2
  • 19
  • 27
  • Please format your code. It's not a python code now. – Psytho Jan 10 '18 at 10:05
  • I can't even read it. Is it exact code? Or have you deleted newlines and indentations? – gonczor Jan 10 '18 at 10:07
  • why have you deleted indenations? – Mohsen_Fatemi Jan 10 '18 at 10:09
  • let me use my pc – user9111001 Jan 10 '18 at 10:10
  • Hello, please take a time to go through the welcome tour to know your way around here , read how to create a [mcve] example and also check [ask] so you increase your chances to get feedback and useful answers. – garfbradaz Jan 10 '18 at 10:11
  • Hello, im extremely sorry for the gibberish. This app sucks at copypasting in android. anyway, can you all review this and explain it to me in detail ? – user9111001 Jan 10 '18 at 10:13
  • I'm getting confused to the point where n==2, I wonder why it doesn't show out of array error for previous_lines[i+1] where previous_lines does not even have 2 elements when n==2 is used. I surmise that previous_lines is only [1] – user9111001 Jan 10 '18 at 10:14
  • https://stackoverflow.com/questions/24093387/pascals-triangle-for-python – Alex Jan 10 '18 at 10:16

0 Answers0