Question: Hi, I have this example where two people share a chocolate bar and each of the squares has an integer on it. The first person
Hi, I have this example where two people share a chocolate bar and each of the squares has an integer on it.
The first person decides to share a contiguous segment of the bar selected such that the length of the segment matches the second persons birth month and the sum of the integers on the squares is equal to his birthday.
Determine how many ways she can divide the chocolate bar.
The correct code, written in python is:
def birthday(s, d, m):
start = 0
number_of_ways = 0
curr_sum = sum(s[0:m-1])
for end in range(m-1, len(s)):
curr_sum += s[end]
if curr_sum == d:
number_of_ways += 1
curr_sum -= s[start]
start += 1
return number_of_ways
I understand the first part of the code but not the second part begining with the for end in range.
Can someone please explain each line of code in that segment?
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
