The concise 'how many beers' issue? - python-3.x

Where I'm at
I'm trying to figure out how many beers I can buy with 10 RMB after recycling every bottle I get. It's obvious to me that I'm doing something wrong, procedurally, but it's not occurring to me what that is. I'm currently reading "How To Think Like a Computer Scientist: Think Python" on chapter 9. I feel like this should be an easy program for me, but I'm not sure how to loop in the recycling portion of the app. What would be the most concise way to rinse and repeat beer purchases?
The question
Basically, one beer costs 2 RMB. 2 bins gets 1 RMB. 4 caps gets 1 RMB. I'm starting out with 10 RMB. How many beers can I buy (recycling all the bins and caps)?
#5 bottles 5 caps
#= 3 rmb + 1 caps 1 bottles
#6th bottle bought
#= 2rmb + 2 caps
#7th bottle bought
#= 0rmb + 3 caps 1 bottles.
import math
def countbeers(rmb):
beers = 0;
caps = 0;
bins = 0;
bcost = 2;
for i in range (0,rmb):
beers += 1/2
for i in range (0,math.floor(beers)):
caps += 1
bins += 1
rmb = rmb - bcost
for i in range (0,caps):
rmb += 1/4
for i in range (0,bins):
rmb += 1/2
# if rmb > 2 what goes here, trying to loop back through
return beers
print(countbeers(10))
Second attempt
#5 bottles 5 caps
#= 3 wallet + 1 caps 1 bottles
#6th bottle bought
#= 2wallet + 2 caps
#7th bottle bought
#= 0wallet + 3 caps 1 bottles.
import math
global beers
global caps
global bins
global bcost
beers = 0
caps = 0
bins = 0
bcost = 2
def buybeers(wallet):
beers = 0
for i in range (0,wallet):
beers += 1/2
wallet -= 2
return beers
def drinkbeers(beers):
for i in range (0,math.floor(beers)):
caps += 1
bins += 1
wallet = wallet - bcost
return wallet, caps, bins
def recycle(caps, bins):
for i in range (0,caps):
wallet += 1/4
for i in range (0,bins):
wallet += 1/2
return wallet
def maxbeers(wallet):
if wallet > 2:
buybeers(wallet)
if math.floor(beers) > 1:
drinkbeers(beers)
if caps > 4 | bins > 2:
recycle(caps, bins)
return wallet
wallet = int(input("How many wallet do you have?"))
maxbeers(wallet)
if wallet >= 2:
maxbeers(wallet)
elif wallet < 2:
print(beers)

Your main problem is that you are not looping. Every beer you bought from rmb gives you one more bottle, and one more cap. This new bottle and cap might be enough to earn you another rmb, which might be enough for another beer. Your implementation handles this to a limited extent, since you call maxbeers multiple times, but it will not give the correct answer if you give it a truckload of beers, i.e. 25656 bottles.
If you know the number of rmb you have, you can do the calculation by hand on paper and write this:
def maxbeers(rmb):
return 7 # totally correct, I promise. Checked this by hand.
but that's no fun. What if rmb is 25656?
Assuming we can exchange:
2 bottles -> 1 rmb
4 caps -> 1 rmb
2 rmb -> 1 beer + 1 cap + 1 bottle
we calculate it like this, through simulation:
def q(rmb):
beers = 0
caps = 0
bottles = 0
while rmb > 0:
# buy a beer with rmb
rmb -= 2
beers += 1
caps += 1
bottles += 1
# exchange all caps for rmb
while caps >= 4:
rmb += 1
caps -= 4
# exchange all bottles for rmb
while bottles >= 2:
rmb += 1
bottles -= 2
return beers
for a in range(20):
print("rmb:", a, "beers:", q(a))
Then we can buy 20525 beers.

Related

how to correctly move a sprite with the keys avoiding conflicts in pygame?

Is a simple game where a monkey throws bananas.
I want to launch the banana when the space bar is released and continue running.
When the space bar is held the banana must remain attached to the monkey until the space bar is released.
The first launch works properly, from the second onwards the bananas are not thrown correctly.
done=False
while not done:
for ev in pygame.event.get():
if ev.type == QUIT:
done = True
if ev.type ==KEYDOWN:
if ev.key== K_RIGHT:
move_right=True
if ev.key== K_LEFT:
move_left = True
if ev.key==K_SPACE:
space_down=True
still=True
elif ev.type ==KEYUP:
if ev.key== K_RIGHT:
move_right=False
if ev.key== K_LEFT:
move_left=False
if ev.key==K_SPACE:
still=False
space_down=False
launch=True
#Game logic
#move monkey
if move_right and monkey.rect.right<screen.get_width():
monkey.rect.x += 5
elif move_left and monkey.rect.left>0:
monkey.rect.x -= 5
#banana launch
if space_down:
banana=Throw_Objects()
banana.rect.midtop = monkey.rect.topright
space_down=False
if still and not space_down and move_right and monkey.rect.right < screen.get_width():
banana.rect.x += 5
if still and not space_down and move_left and monkey.rect.left > 0:
banana.rect.x -= 5
if launch:
for banana in all_object:
banana.rect.y -= 5
if banana.rect.top < 0:
banana.kill()
Any suggestions to make sure that the banana is launched when the space bar is released and when, as long as you press the space bar, the new banana will remain attached to the monkey without affecting the movement of the previous one?
Thank you
i resolded by adding a sprite group after launch in this way
if space_down:
banana=Throw_Objects()
banana.rect.midtop = monkey.rect.topright
space_down=False
if move_right and monkey.rect.right<screen.get_width():
monkey.rect.x += 5
if still:
banana.rect.x += 5
elif move_left and monkey.rect.left>0:
monkey.rect.x -= 5
if still:
banana.rect.x -= 5
if launch :
banana.rect.y -= 5
only_banana.add(banana)
launch=False
for banana in only_banana:
banana.rect.y -= 5
for banana in all_object:
if banana.rect.top < 0:
banana.kill()

Runtime for binary search

New to algorithms
Trying a Binary Search problem, below is my code -
def search(nums, target):
low = 0
high = len(nums) - 1
#a = sorted(nums) --> to sort the list if needed [ change nums to a ]
while low <= high:
middle = (low + high) // 2
if target == nums[middle]:
return middle
elif target > nums[middle]:
low = middle + 1
else:
high = middle - 1
else:
return -1
Code is running fine with a runtime of 402 ms. Then I checked the code of other people and one of them had
middle = low + (high - low) // 2
and their runtime is 110 ms. Can someone please explain why such a difference is there between the runtime?

Calculate probability of an event not by exclusion

I have some doubt with these kind of problems, example:
"If we asked 20,000 in a stadium to toss a coin 10 times, what it's the probability of at least one person getting 10 heads?"
I took this example from Practical Statistics for Data Scientist.
So, the probability of at least one person getting 10 heads it's calculated using: 1 - P(of nobody in the stadium getting 10 heads).
So we kind of doing an exclude procedure here, first I get the probability of the contrary event I am trying to measure, not the ACTUAL experiment I want to measure: at least one people getting 10 heads.
Why do we do it this way?
How can I calculate the probability of at least someone getting 10 heads but without passing through the probability of no one getting 10 heads?
As #Robert Dodier mentioned in the comments, the reason is that the calculations are simpler. I will use a stadium of 20 people instead of 20000 as an example:
Method 1:
Probability of not getting 10 heads for one individual
= 1 - probability of getting 10 heads
= 1 - 10!/(10!0!)*0.5^10*(1-0.5)^0
= 0.9990234375
Probability of at least one person in the stadium getting 10 heads
= 1 - P(of nobody in the stadium getting 10 heads)
= 1 - 0.9990234375**20 (because all coin tosses are independent)
= 0.019351109194852834
Method 2:
Probability of getting 10 heads for one individual
= 10!/(10!0!)*0.5^10*(1-0.5)^0
= 0.0009765625
Probability of exactly 1, 2, 3, etc. persons in the stadium getting 10 heads:
p1 = 20!/(1!19!)*0.0009765625^1*(1-0.0009765625)^(20-1) = 0.019172021325613825
p2 = 20!/(2!18!)*0.0009765625^2*(1-0.0009765625)^(20-2) = 0.00017803929872270904
p3 = 20!/(3!17!)*0.0009765625^3*(1-0.0009765625)^(20-3) = 1.0442187608370032e-06
p4 = 20!/(4!16!)*0.0009765625^4*(1-0.0009765625)^(20-4) = 4.338152232216289e-09
p5 = 20!/(5!15!)*0.0009765625^5*(1-0.0009765625)^(20-5) = 1.3569977656981548e-11
p6 = 20!/(6!14!)*0.0009765625^6*(1-0.0009765625)^(20-6) = 3.316221323798032e-14
p7 = 20!/(7!13!)*0.0009765625^7*(1-0.0009765625)^(20-7) = 6.483326146232712e-17
p8 = 20!/(8!12!)*0.0009765625^8*(1-0.0009765625)^(20-8) = 1.029853859983202e-19
p9 = 20!/(9!11!)*0.0009765625^9*(1-0.0009765625)^(20-9) = 1.342266353839299e-22
p10 = 20!/(10!10!)*0.0009765625^10*(1-0.0009765625)^(20-10) = 1.443297154665913e-25
p11 = 20!/(11!9!)*0.0009765625^11*(1-0.0009765625)^(20-11) = 1.2825887804726853e-28
p12 = 20!/(12!8!)*0.0009765625^12*(1-0.0009765625)^(20-12) = 9.403143551852531e-32
p13 = 20!/(13!7!)*0.0009765625^13*(1-0.0009765625)^(20-13) = 5.656451493707817e-35
p14 = 20!/(14!6!)*0.0009765625^14*(1-0.0009765625)^(20-14) = 2.7646390487330485e-38
p15 = 20!/(15!5!)*0.0009765625^15*(1-0.0009765625)^(20-15) = 1.0809927854283668e-41
p16 = 20!/(16!4!)*0.0009765625^16*(1-0.0009765625)^(20-16) = 3.3021529369146104e-45
p17 = 20!/(17!3!)*0.0009765625^17*(1-0.0009765625)^(20-17) = 7.59508466888531e-49
p18 = 20!/(18!2!)*0.0009765625^18*(1-0.0009765625)^(20-18) = 1.2373875315877011e-52
p19 = 20!/(19!1!)*0.0009765625^19*(1-0.0009765625)^(20-19) = 1.2732289258503896e-56
p20 = 20!/(20!0!)*0.0009765625^20*(1-0.0009765625)^(20-20) = 6.223015277861142e-61
Probability of at least one person in the stadium getting 10 heads
= p1 + p2 + p3 + p4 + p5 + p6 + p7 + p8 + p9 + p10 +
p11 + p12 + p13 + p14 + p15 + p16 + p17 + p18 + p19 + p20
= 0.01935110919485281
So the result is the same (the tiny difference is due to floating point precision), but as you can see the first calculation is slightly simpler for 20 people, never mind for 20000 ;)

Round up a decimal

I have a problem and i need your help!
Here is the code:
kg_lemons = float(input())
kg_sugar = float(input())
water = float(input())
total_lemon_juice = kg_lemons * 980 #in mililiters need to multiply by 1000
total_lemonade = total_lemon_juice + 5 * 1000 + (0.3 * kg_sugar)
cups_made = total_lemonade / 150
money_made = cups_made * 1.20
print(f'All cups sold: {cups_made:.2f}')
print(f'Money earned: {money_made:.2f}')
At then end, after I print it, it must shown the numbers:
All cups sold: 66
Money earned: 79.20
But I got:
All cups sold: 66.01
Money earned: 79.21
So I need to round it up to the second decimal (the lowest number). Should I use math.floor and, if so, how?

Python 3: How to exit loop without stopping function

I am a complete newbie and have tried solving this problem (with my own head and with online research) for the last 5 hours.
Below is a snippet of a function we have written to simulate a game. We want to offer the ooportunity to start a new round - meaning if a player hits "b", the game should start again at the beginning of the range (0, players). But right now it just goes onto the next player in the range (if player 1 enters "b", the program calls player 2)
players = input(4)
if players in range(3, 9):
for player in range(0, players):
sum_points = 0
throw_per_player_counter = 0
print("\nIt is player no.", player+1, "'s turn!\n")
print("\nPress 'return' to roll the dice.\n"
"To start a new round press 'b'.\n"
"Player", player+1)
roll_dice = input(">>> ")
if roll_dice == "b":
player = 0
throw_per_player_counter = 0
sum_points = 0
print("\n * A new round was started. * \n")
I have tried return and break, also tried to put it all in another while-loop... failed. Break and return just ended the function.
Any hints highly appreciated!
you could change the for loop to a while loop. instead of using a range, make player a counter
players = 4
if 3 <= players < 9:
player = 0 # here's where you make your counter
while player < players:
sum_points = 0
throw_per_player_counter = 0
print("\nIt is player no.", player+1, "'s turn!\n")
print("\nPress 'return' to roll the dice.\n"
"To start a new round press 'b'.\n"
"Player", player+1)
roll_dice = input(">>> ")
player += 1 # increment it
if roll_dice == "b":
player = 0 # now your reset should work
throw_per_player_counter = 0
sum_points = 0
print("\n * A new round was started. * \n")

Resources