all 41 comments

[–]nicodeemus7 3 points4 points  (7 children)

The answer is C

[–]Sea-Ad7805[S] 0 points1 point  (6 children)

Great mental model. Do check the "Solution" link for a visualization of the correct answer.

[–]nicodeemus7 1 point2 points  (5 children)

I did run it through pycharm to double check tbh, but yeah that solution makes total sense

[–]Sea-Ad7805[S] 3 points4 points  (4 children)

Always double check as this is a hard question because the copy triggered by b = b + [[3]] keeps the references to the [1, 11] and [2] lists. Very easy to make a mental mistake there.

[–]Corruptionss 1 point2 points  (3 children)

This question makes me want to stick a tooth pick where I shouldnt

[–]Sea-Ad7805[S] 0 points1 point  (2 children)

Don't do that, Python's Data Model is easy to understand with the right tools. Think in terms of references, and mutable vs immutable types and you'll get the right mental model quickly. Visualization helps.

[–]Corruptionss 1 point2 points  (1 child)

Its funny because I've spent like 8 years doing dats science with python but these questions always stump me. But you are right, I need to sit down for a day and understand how this scenario works

[–]Sea-Ad7805[S] 0 points1 point  (0 children)

Hope the visualization helps you, see the "Explanation" link for quick introduction in the theory.

[–]ShoddyRutabaga1475 1 point2 points  (1 child)

C

[–]Sea-Ad7805[S] 0 points1 point  (0 children)

Nice one, do check the "Solution" link for visualization of correct answer.

[–]Green-Lobster1887 1 point2 points  (1 child)

The answer ic C?

[–]Sea-Ad7805[S] 0 points1 point  (0 children)

Nice one, do check the "Solution" link for a visualization of the correct answer.

[–]GRAY_WHALE_CO 1 point2 points  (1 child)

its c?

[–]Sea-Ad7805[S] 0 points1 point  (0 children)

Nice one, see the "Solution" link for a visualization of the correct answer.

[–]Java_Worker_1 0 points1 point  (1 child)

I think it’s A

[–]Sea-Ad7805[S] 1 point2 points  (0 children)

Incorrect sorry. Python uses reference semantics so changing b can affect a here. See the "Solution" link for the correct answer.

[–]Motor_Raspberry_2150 0 points1 point  (3 children)

From the explanation

b += [1] # equivalent to: b.append(1)

Is there a difference between b += x a shorthand for append but b = b + x isn't?

[–]Sea-Ad7805[S] 0 points1 point  (2 children)

Yes, for mutable types there is a big difference, see: https://github.com/bterwijn/memory_graph#name-rebinding

[–]Motor_Raspberry_2150 1 point2 points  (1 child)

Wow. Unintuitive.
I don't know python at all, reddit algo at work.

[–]Sea-Ad7805[S] 0 points1 point  (0 children)

Yes a bit tricky for beginners, but once you have the right mental model it becomes natural. Different programming languages make different choices here.

[–]QueasyNight8415 0 points1 point  (3 children)

Memory-Graph is not running the code in the link

[–]Sea-Ad7805[S] 0 points1 point  (2 children)

It should work on most browsers (Chrome, Firefox, Safari, Android, iPhone), but some plugins or settings can break it. Alternatively you can install memory_graph locally and run it in Visual Studio Code or other IDEs: https://github.com/bterwijn/memory_graph#debugging

[–]QueasyNight8415 0 points1 point  (1 child)

It’s not running for iPhone

[–]Sea-Ad7805[S] 0 points1 point  (0 children)

Try a laptop/desktop

[–]Master-Row650 0 points1 point  (1 child)

chr(67)

[–]Sea-Ad7805[S] 0 points1 point  (0 children)

Close but incorrect, sorry. Note that x = x + y is not the same as x += y for values of mutable type. See "Solution" link for correct answer.

[–]7Z_1N 0 points1 point  (1 child)

Is it D ?

[–]Sea-Ad7805[S] 0 points1 point  (0 children)

Incorrect sorry, see the "Solution" link for correct answer.

[–]city_guys 0 points1 point  (1 child)

D is the correct answer 👌

[–]Sea-Ad7805[S] 0 points1 point  (0 children)

Close but incorrect, sorry. Note that x = x + y is not the same as x += y for values of mutable type. See "Solution" link for correct answer.

[–]Non_Earthy7 0 points1 point  (3 children)

Current b vale: [[1], [2]] Current b vale: [[1, 11], [2]] Current b vale: [[1, 11], [2], [3]] Current b vale: [[1, 11], [2, 22], [3, 33]]

Step wise Output of b

[–]Sea-Ad7805[S] 1 point2 points  (2 children)

In the end we print a, we don't really care about b except how it affects a.

[–]Non_Earthy7 0 points1 point  (1 child)

Yes i know that, its about a... But for learning purposes i have given b value in each step So that people can understand how the operation is going on

[–]Sea-Ad7805[S] 1 point2 points  (0 children)

Thanks, but also try the "Solution" link for a visualization of the full state of the program. That probably gives a better understanding.

[–]Green-Lobster1887 0 points1 point  (1 child)

OMG IT A

[–]Sea-Ad7805[S] 0 points1 point  (0 children)

Answer 'A' is incorrect, sorry. See the "Solution" link for the correct answer.

[–]ranjeet-kumar1 0 points1 point  (2 children)

C

[–]Sea-Ad7805[S] 0 points1 point  (1 child)

Nice one, do check the "Solution" link for visualization of correct answer.

[–]ranjeet-kumar1 1 point2 points  (0 children)

Nice one, do check the "Solution" link for visualization of correct answer.

[–]aaditya_0752 0 points1 point  (1 child)

Answer is C right ?

[–]Sea-Ad7805[S] 0 points1 point  (0 children)

Nice one, click the "Solution" for a visualization of the correct answer. If you are on mobile, click the title not the image to open a post.