Python Forum
I am getting the wrong answer, and not sure why
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
I am getting the wrong answer, and not sure why
#1
This is my code:

def triangles():
    N = [1]
    while True:
        yield N
        N.insert(0,0)
        N.append(0)
        N = [N[i]+N[i+1] for i in range(len(N)-1)]

n = 0
results = []
for t in triangles():
    results.append(t)
    n = n + 1
    if n == 10:
        break

for t in results:
    print(t)
The answer I want to get:

[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]

The output of that code:
[0, 1, 0]
[0, 1, 1, 0]
[0, 1, 2, 1, 0]
[0, 1, 3, 3, 1, 0]
[0, 1, 4, 6, 4, 1, 0]
[0, 1, 5, 10, 10, 5, 1, 0]
[0, 1, 6, 15, 20, 15, 6, 1, 0]
[0, 1, 7, 21, 35, 35, 21, 7, 1, 0]
[0, 1, 8, 28, 56, 70, 56, 28, 8, 1, 0]
[1, 9, 36, 84, 126, 126, 84, 36, 9, 1]

I try to debug that code,The value of 'results' changes from [[1]] into [[0,1]] when the N.insert(0,0) is ran, why?
[Image: 2c45be315c6034a8bbdb67c1dc13495409237612.jpg]
Reply
#2
The next step
[Image: bf35b119ebc4b74508736756d8fc1e178b82156f.jpg]
Reply
#3
N is a list and is therefore mutable.

In triangles() you hand back an object in line 4 (yield N), but then in line 5 you modify that object. If the caller didn't make a copy yet, their copy of the object is changed as well.

A simple change here would be to return a copy of the list so that the caller's object can't be modified later by the function.

yield list(N)
Reply
#4
This is a fun problem.

Triangles has a list N which it yields. A new N is generated each time triangles is called. HOWEVER, before the new N is generated you insert and append 0 to the old N.

This code prints the desired output because it prints the result of triangles() before they get "corrupted".
def triangles():
    N = [1]
    while True:
        yield N
        N.insert(0,0)
        N.append(0)
        N = [N[i]+N[i+1] for i in range(len(N)-1)]
 
for i, t in enumerate(triangles()):
    print(t)
    if i >= 5:
        break
An easy fix to your problem is to create to create a new N at the start of each iteration so you don't mess up the previous.
def triangles():
    N = [1]
    while True:
        yield N
        N = (0, *N, 0)
        N = [N[i]+N[i+1] for i in range(len(N)-1)]

# Or this one that uses zip and temporary lists.
def triangles():
    N = [1]
    while True:
        yield N
        N = [p + n for p, n in zip([0]+N, N+[0])]
riskeay likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Am I wrong or is Udemy wrong? String Slicing! Mavoz 3 2,553 Nov-05-2022, 11:33 AM
Last Post: Mavoz
  why don't i get the answer i want CompleteNewb 12 3,394 Sep-04-2021, 03:59 PM
Last Post: CompleteNewb
  Make the answer of input int and str enderfran2006 2 2,008 Oct-12-2020, 09:44 AM
Last Post: DeaD_EyE
  Keeps looping even after correct answer mcesmcsc 2 1,909 Dec-12-2019, 04:27 PM
Last Post: mcesmcsc
  python gives wrong string length and wrong character thienson30 2 3,000 Oct-15-2019, 08:54 PM
Last Post: Gribouillis
  I'm getting a wrong answer don't know where the bug is 357mag 4 2,804 Jul-07-2019, 11:21 PM
Last Post: DeaD_EyE
  How to answer subprocess prompt Monty 8 17,368 Feb-14-2018, 09:59 AM
Last Post: wavic

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020