Problem statement: A user is given n number of candies initially. There was a offer where on every 3 candy 1 more candy will be given for free. For eg: if he intially get 9 candy then he will get 3 free candy. and again on 3 free candy he will be given another free candy. so total candy will be 9+3+1. This look simple but when the initial candy count is bigger then it will need lot of calculation on finding free candies and again on free candies, user will receive more free candies and so on.
Using a simple program we will see how easily our script can solve this problem..
This also show the simplicity and flexiblity python provides for writing script (the conversion of int string etc).
Here it goes:
________________________________________________________________________________
"""
@Author: Madhusudan Vyas
@Objective: To find total and free chocalate with choc given initially by user and offere that on every 3 choc 1 chocalate is free.
"""
################################
class choc_count(object):
def find_free(self, n, newval):
while n/3>=1:
free_a = int( n/3)
newval = newval + int(free_a)
n = free_a
return newval
initial_choc = raw_input("Enter the number of chocalate given initially ")
n=int(initial_choc)
i = 0
newval = 0
freech = choc_count()
free_ch = freech.find_free(n, newval)
total = n+ free_ch
print "new val of free choc : ", free_ch
print "new val of total choc : ", total
Here on running this script, this prog will ask for providing the number of candies given in the begining and then it will show how many free candies it will give and the total candies including initial and free candies.
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Using a simple program we will see how easily our script can solve this problem..
This also show the simplicity and flexiblity python provides for writing script (the conversion of int string etc).
Here it goes:
________________________________________________________________________________
"""
@Author: Madhusudan Vyas
@Objective: To find total and free chocalate with choc given initially by user and offere that on every 3 choc 1 chocalate is free.
"""
################################
class choc_count(object):
def find_free(self, n, newval):
while n/3>=1:
free_a = int( n/3)
newval = newval + int(free_a)
n = free_a
return newval
initial_choc = raw_input("Enter the number of chocalate given initially ")
n=int(initial_choc)
i = 0
newval = 0
freech = choc_count()
free_ch = freech.find_free(n, newval)
total = n+ free_ch
print "new val of free choc : ", free_ch
print "new val of total choc : ", total
Here on running this script, this prog will ask for providing the number of candies given in the begining and then it will show how many free candies it will give and the total candies including initial and free candies.
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX