# Cadbury problem solution in Python

Code Review Asked on January 2, 2022

# Problem Statement

In a School, Chocolate bars have to be distributed to children waiting in a queue. Each Chocolate bar is rectangular in shape. Consider its side lengths are integer values.

The distribution procedure is as follows:-

If bar is not square in shape, then the largest possible square piece of Chocolate is broken and given to the first child in queue.
If bar is square in shape, then complete bar is given to the first child in queue.
Once a child receives his share of Chocolate, he leaves the queue. The remaining portion of the Chocolate bar is dealt in same fashion and the whole or a portion of it is given to the next child in the queue.

School has got a carton of Chocolate bars to be distributed among the children all over the School. The Chocolate bars in the carton are of different sizes. A bar of length i and breadth j is considered to be different from a bar of length j and breadth i. For every i such that $$M le i le N$$ and every j such that $$Ple j le Q$$ (where M, N, P and Q are integers). Each Chocolate bar in carton is unique in length (i) and breath(j).

Given the values of M, N, P and Q (where M, N values are the ranges for length of Chocolate and P, Q values are the ranges for breadth of the chocolate). Find the number of children who will receive Chocolate from the carton.

### Input Specification:

M, N, P, Q are of integer type (M, N values are the ranges for length of chocolate bar. P, Q values are the ranges for breadth of chocolate bar).

### Output Specification:

M = 5, N = 6, P = 3, Q=4 Here, i can be from 5 to 6 and j can be from 3 to 4. So the four bars will be in carton of sizes 5×3, 5×4, 6×3, 6×4.

First we choose a Cadbury bar of size 5×3 → first child would receive 3×3 portion ( remaining 2×3 portion ) → next child would receive 2×2 portion ( remaining 2×1 portion ) → now the remaining portion are 2 square pieces of (1×1), which can be given to 2 more children

So the Cadbury bar with the size of 5×3 can be distributed to 4 children.

Similarly we can find out number of children for rest of the combinations (i.e. 5×4, 6×3, 6×4) in the given range as follows

Can anyone suggest improvements?

m, n, p, q = raw_input().split(":")
ll = int
bb = int
count = 0

leng = [m, n]
brd = [p, q]
for l in leng:
for b in brd:
call(l, b)

def call(l, b):
#print l,b
global count
area = l * b
if l > b:
bb = l
rem = bb - b
#print rem
rem_part1 = rem
#print rem_part1
rem_part2 = b
#print rem_part2
l = rem_part1
#print l
b = rem_part2
#print b
if l != b:
if (l==1) or (b==1):
count += 1
count += (l*b)
#print count
return
else:
count += 1
#print count
call(l, b)
if l==b:
count+=2
#print count
return
elif b > l:
ll = b
#print ll
rem = ll - l
#print rem
rem_part1 = rem
#print rem_part1
rem_part2 = l
#print rem_part2
l = rem_part1
#print l
b = rem_part2
#print b
if l != b:
if (l==1) or (b==1):
count += 1
count += (l*b)
#print count
return
else:
count += 1
#print count
call(l, b)
if l==b:
count+=2
#print count
return
print count


# Style

1. Use names that have meaning (e.g. minimum_length instead of m)
2. Don't create needless aliases (e.g. for length in [minimum_length, maximum_length] instead of for l in leng)

Answered by Andrew Hoos on January 2, 2022

I made it a little shorter: The logic is also little more simple.

def TotalCount(M,N,P,Q):
count=0
for l in range(M,N+1):
for b in range(P,Q+1):
count +=CountPerChocolateBar(l,b)
return count


Your cadbury function stayed almost the same. But your version has a bug: if the difference between M and N, or P and Q is not one (in your example it was one), your code fails:

a = 7
b = 10
for element in [a,b]:
print (element)


result is 7, 10 . NOT the expected 7,8,9,10

def CountPerChocolateBar(l,b):

count = 0
while True:
longerr=max(l,b)
shorterr=min(l,b)
count+=1
diff=longer-shorter
if diff==0:
return count
else :
l=min(l,b)
b=diff


Your call function can be simplified a little: you can handle l>b ,b>l without branches, because it doesn't matter which one is bigger. You count the difference, increase the count variable, and based on the difference you return the count number or calculate the new b and l values. When the difference is 0, the while loop ends with the return statement

while True:

numbers=raw_input("Number: ")

M=int(numbers.split()[0])
N=int(numbers.split()[1])
P=int(numbers.split()[2])
Q=int(numbers.split()[3])
tc=TotalCount(M,N,P,Q)

print (tc)


You can test my solution with this little loop. :)

Answered by user3598726 on January 2, 2022

### Why so many aliases?

    ll = b

rem_part1 = rem

rem_part2 = l

l = rem_part1


Why do you continually assign x = y? Why don't you just keep each value in a single variable?

### Why so many un-descriptive looking names?

ll = int
bb = int


Also to be underlined ll that is LETTER L LETTER L looks really similar to 11 in some fonts that is NUMBER ONE NUMBER ONE especially at small font-sizes making it even more confusing to read the code.

rem = bb - b


This name is misleading rem stands for remainder, that is the result of division: from Wikipedia

In arithmetic, the remainder is the integer "left over" after dividing one integer by another to produce an integer quotient (integer division).

### Why is count global?

The count variable should be defined inside the function and returned out. While it does not really matter in such a small program, avoiding global variables is a good habit.

Answered by Caridorc on January 2, 2022

## Related Questions

### Parallelized web crawler using goroutines and channels

1  Asked on January 10, 2021 by snowbody

### Sudoku Game with automatically generated Sudokus

2  Asked on January 9, 2021 by philipp-wilhelm

### Type definitions for simple NPM package that generates OAuth 1.0a authorization header for Twitter API using native https nodejs module

1  Asked on January 9, 2021 by mauriciorobayo

### typescript : clean code remove or decrease alot if’s

1  Asked on January 5, 2021 by gabriel-costa

2  Asked on January 5, 2021 by guy-on-the-internet

### C# – name separation & last name in upper to Camel

2  Asked on January 3, 2021

### Simple Router class

1  Asked on January 3, 2021 by samayo

### Is this Java small pubsub memory implementation correct and effective?

1  Asked on January 2, 2021 by bruno-thomas

### Generate unique random strings considering collisions

0  Asked on December 30, 2020 by user2652379

### http url connection with kotlin on android

0  Asked on December 29, 2020

### Calculating a page size to execute a subset of jobs at a time

3  Asked on December 29, 2020 by user93068

### Decluttering quiz game

0  Asked on December 29, 2020 by kue

### Converting location names into country names in pandas dataframe

1  Asked on December 23, 2020

### Custom include? method for a substring

0  Asked on December 23, 2020 by michael-zech

### Avoiding Python tricks to find anagrams

4  Asked on December 21, 2020 by shubhamprashar

### Return correct path if windows or linux

1  Asked on December 17, 2020 by protractornewbie

### Scraping reddit using Python

0  Asked on December 14, 2020 by user00257

### Generic memoize utility function for pure functions

1  Asked on December 12, 2020 by ngoy-lufo