You are viewing a single comment's thread. Return to all comments →

This approach is clean and easy to follow, which is great.

However, it results in O(N) time (input of 10000 1 2 results in the loop running 9999 times.)

If you switch the loop to the following you end up with O( lg(N)) (or 14 in the worst case, since you are using integer math).

def chocolateFeast(cash, cost, wrapper_cost): chocolate = cash//cost wrappers = chocolate while wrappers // wrapper_cost > 0: new_chocolate = wrappers // wrapper_cost wrappers -= new_chocolate * wrapper_cost wrappers += new_chocolate chocolate += new_chocolate return chocolate

Hi Jason I kind of did the same thing what you did but mine didn't pass the test cases how whats the difference

import time

start_time = time.time()

def chocolateFeast1(he_has,cost,wrapper):

current_chocates = int(he_has / cost) current_wrappers = current_chocates while current_wrappers >= wrapper: current_chocates += 1 current_wrappers -= cost current_wrappers += 1 print(f"Took { time.time() - start_time}") print (current_chocates)

start_time = time.time() def chocolateFeast(cash, cost, wrapper_cost):

chocolate = cash//cost wrappers = chocolate while wrappers // wrapper_cost > 0: new_chocolate = wrappers // wrapper_cost wrappers -= new_chocolate * wrapper_cost wrappers += new_chocolate chocolate += new_chocolate print(f"Took { time.time() - start_time}") print (chocolate)

wrappers = wrappers % wrapper_cost instead of wrappers -= new_chocolate * wrapper_cost

was better understandable for me.

wrappers %= wrapper_cost would be even shorter

thanks.anyways, i got it.

Your code is flawless. Quick, simple and completely readable! Congratulations!

def chocolateFeast(n, c, m):

ch = n/c all_ch = ch while (ch>=1): all_ch += ch/m ch = ch/m + ch%m return all_ch

## Chocolate Feast

You are viewing a single comment's thread. Return to all comments →

This approach is clean and easy to follow, which is great.

However, it results in O(N) time (input of 10000 1 2 results in the loop running 9999 times.)

If you switch the loop to the following you end up with O( lg(N)) (or 14 in the worst case, since you are using integer math).

Hi Jason I kind of did the same thing what you did but mine didn't pass the test cases how whats the difference

import time

start_time = time.time()

def chocolateFeast1(he_has,cost,wrapper):

start_time = time.time() def chocolateFeast(cash, cost, wrapper_cost):

wrappers = wrappers % wrapper_cost instead of wrappers -= new_chocolate * wrapper_cost

was better understandable for me.

wrappers %= wrapper_cost would be even shorter

thanks.anyways, i got it.

Your code is flawless. Quick, simple and completely readable! Congratulations!

## see this

def chocolateFeast(n, c, m):