HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythonMinor

Cadbury problem solution in Python

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
cadburyproblemsolutionpython

Problem

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 \$P\le 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:

Number of children who will receive Cadbury bar from the carton.

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 5x3, 5x4, 6x3, 6x4.

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

Solution

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. :)

Code Snippets

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
a = 7
b = 10
for element in [a,b]:
  print (element)
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
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)

Context

StackExchange Code Review Q#142008, answer score: 2

Revisions (0)

No revisions yet.