educative.io

Educative

The left part in solution 1

can someone explain how these below 2 lines work to get the product of left part:
result.append(currentproduct * left)
left = left * lst[i]

thanks in advance!


Course: Educative: Interactive Courses for Software Developers
Lesson: Educative: Interactive Courses for Software Developers

Hi @jessie_Zhao, Thanks for reaching out to us.
result.append(currentproduct * left) calculates the product of all the elements to the left of the current element. left = left * lst[i] is updating the variable left which holds the product of all values to the left of the ith index.

Hope it will help, Happy coding :slight_smile:

1 Like

thank you for commenting, but can you use a specific example list such as [1,2,3,4,5] to walk through the calculation? I can’t visualize the actual calculation based on the abstract idea. that’s where i stuck.


Course: Educative: Interactive Courses for Software Developers
Lesson: Educative: Interactive Courses for Software Developers

Hi @jessie_Zhao

My name is Shahrukh Naeem. I hope everything is going well with you. Thank you for reaching out about this. I will try my best to answer your query!

Lets take an example
Input list : [1, 2, 3, 4]
Result is an array which is empty initially.
Left variable stores 1 intially.
i is loop control variable which iterates the list elements.
When i = 0
currentproduct sets to 1.
First iteration of ele = list[0]= 1 β€”> currentproduct = 11 =1
Second iteration of ele = list[1]= 2 β€”> currentproduct = 1
2=2
Third iteration of ele = list[2]= 3 β€”> currentproduct = 23=6
Fourth iteration of ele = list[3]= 4 β€”> currentproduct = 6
4=24
currentproduct * left = 241=24 is appended in Result array at first index
left is 1
Updated left is 2
When i = 1
currentproduct resets to 1.
First iteration of ele = list[1]= 2 β€”> currentproduct = 1
2=2
Second iteration of ele = list[2]= 3 β€”> currentproduct = 23=6
Third iteration of ele = list[3]= 4 β€”> currentproduct = 6
4=24
currentproduct * left = 241=24 is appended in Result array at second index
left is 2
Updated left is 6
When i = 2
currentproduct resets to 1.
First iteration of ele = list[2]= 3 β€”> currentproduct = 1
3=3
Second iteration of ele = list[3]= 4 β€”> currentproduct = 34=12
currentproduct * left = 12
2=24 is appended in Result array at third index
left is 6
Updated left is 24
When i = 3
currentproduct resets to 1.
First iteration of ele = list[3]= 4 β€”> currentproduct = 14=4
currentproduct * left = 4
6 =24 is appended in Result array at fourth indexResult Array is [ 24, 24, 24 ,24 ]

I hope that this guide is helpful. Remember that I am always available via message to help you with any difficulty you might encounter.

Regards,

Happy Learning :slight_smile:

1 Like

Hi Shahrukh, thank you for elaborating it. it’s super helpful, though the result seems a bit different from in the solution provided. it helped!

2 Likes

Hi @jessie_Zhao

You are most welcome. I’m glad I could be a help to you today.

Cheers!

Hope you have a nice day.

Kind regards.