How to write the coding problem with python?
Question:
Three empty cans can be exchanged for a new one. Suppose you have N cans of soda, try to use the program to solve how many cans of soda you can drink in the end?
Input description: Input a positive integer N. ex.5 / ex.100
Output description: The maximum number of sodas that can be drunk, and must have a newline character at the end. ex.7 / ex.149
`
n = int(input())
a = n-3
sum = 0
while a > 2 :
sum += 1
a -= 3
print(f'{n+sum}')
if a == 2 :
print(f'{n+sum+1}')
`
I used while to finish the code which is on above, but I input 5 and output 6,and it is actually to be 7.The other side, I input 100 and output 132. Actually, the correct answer is 149.
Answers:
You can try this way –
def get_total_cans(n):
s = n # we can get at least n cans
while n > 2:
s += 1
n -= 3 # 3 exchanged
n += 1 # got 1 for the exchange
return s
n = int(input())
print(get_total_cans(n))
The logic is simple and comments are added to explain.
In your code, the first thing to notice it generates wrong output when n is less than 3. For example for n = 2, your output is 3 which is not possible. Also in the while loop you are decrementing a
by 3 for the exchange but you fail to add 1 to a
for the one soda you can exchanging 3 empty cans. That is the issue. My above code addresses those issues
Here’s a recursive approach:
def dr_cans(full, empty=0):
# if we have at least 3 empty cans, exchange them for a full one
if empty >=3:
return dr_cans(full+1,empty-3)
# no full cans, and not enough empty ones
if full == 0:
return 0
# at least one full can: drink it and gain an empty one
return 1 + dr_cans(full-1, empty+1)
If I understand corretly the question is as follows:
Let k
be the index of the exchange process. Since not all N
can be divided by 3 we have N[k] = floor(M/3)
, where M=N[k-1]+R[k-1]
new cans in each step. Plus some R[k] = M%3
leftover cans, where %
is the modulo operator…
With this is should be quite easy…
def compute_num_cans(empty_cans: int, exchange: int = 3) -> tuple:
"""
:param empty_cans: The number of cans to exchange
:return: tuple of (full_cans, empty_cans), where the empty cans are < exchange rate
"""
leftovers = empty_cans % exchange
full = empty_cans // exchange
return full, leftovers
EXCHANGE = 3
NUM_CANS = 51
print(f'Start with {NUM_CANS} and an exchange rate of {EXCHANGE}:1')
current_cans = NUM_CANS
drunk_cans = NUM_CANS
leftovers = 0
steps = 0
while current_cans >= EXCHANGE:
full, leftovers = compute_num_cans(current_cans, exchange=EXCHANGE)
current_cans = full + leftovers
drunk_cans += full
steps += 1
print(f'Cans drunk: {drunk_cans}, leftover cans: {leftovers}.')
print(f'A total of {steps} exchanges was needed.')
This yields as output
# Start with 51 and an exchange rate of 3:1
# Cans drunk: 76, leftover cans: 0.
# A total of 4 exchanges was needed.
General Answer:- Accepted also if we change the numExchange also.
Code:-
def numWaterBottles(numBottles: int, numExchange: int) -> int:
ans=numBottles #Initial bottles he/she will drink
while numBottles>=numExchange: #If numBottles<numExchange exit the while loop
remainder=numBottles%numExchange #remaining bottles which is not change
numBottles//=numExchange #The bottles which are changed
ans+=numBottles #The bottles which are changed added to the answer
numBottles+=remainder #Remaining bottles==The bottles which is not change+The bottles which are changed
return ans #Return The answer
print(numWaterBottles(5,3))
print(numWaterBottles(100,3))
print(numWaterBottles(32,4)) #numexchange when different
Output:-
7
149
42
Three empty cans can be exchanged for a new one. Suppose you have N cans of soda, try to use the program to solve how many cans of soda you can drink in the end?
Input description: Input a positive integer N. ex.5 / ex.100
Output description: The maximum number of sodas that can be drunk, and must have a newline character at the end. ex.7 / ex.149
`
n = int(input())
a = n-3
sum = 0
while a > 2 :
sum += 1
a -= 3
print(f'{n+sum}')
if a == 2 :
print(f'{n+sum+1}')
`
I used while to finish the code which is on above, but I input 5 and output 6,and it is actually to be 7.The other side, I input 100 and output 132. Actually, the correct answer is 149.
You can try this way –
def get_total_cans(n):
s = n # we can get at least n cans
while n > 2:
s += 1
n -= 3 # 3 exchanged
n += 1 # got 1 for the exchange
return s
n = int(input())
print(get_total_cans(n))
The logic is simple and comments are added to explain.
In your code, the first thing to notice it generates wrong output when n is less than 3. For example for n = 2, your output is 3 which is not possible. Also in the while loop you are decrementing a
by 3 for the exchange but you fail to add 1 to a
for the one soda you can exchanging 3 empty cans. That is the issue. My above code addresses those issues
Here’s a recursive approach:
def dr_cans(full, empty=0):
# if we have at least 3 empty cans, exchange them for a full one
if empty >=3:
return dr_cans(full+1,empty-3)
# no full cans, and not enough empty ones
if full == 0:
return 0
# at least one full can: drink it and gain an empty one
return 1 + dr_cans(full-1, empty+1)
If I understand corretly the question is as follows:
Let k
be the index of the exchange process. Since not all N
can be divided by 3 we have N[k] = floor(M/3)
, where M=N[k-1]+R[k-1]
new cans in each step. Plus some R[k] = M%3
leftover cans, where %
is the modulo operator…
With this is should be quite easy…
def compute_num_cans(empty_cans: int, exchange: int = 3) -> tuple:
"""
:param empty_cans: The number of cans to exchange
:return: tuple of (full_cans, empty_cans), where the empty cans are < exchange rate
"""
leftovers = empty_cans % exchange
full = empty_cans // exchange
return full, leftovers
EXCHANGE = 3
NUM_CANS = 51
print(f'Start with {NUM_CANS} and an exchange rate of {EXCHANGE}:1')
current_cans = NUM_CANS
drunk_cans = NUM_CANS
leftovers = 0
steps = 0
while current_cans >= EXCHANGE:
full, leftovers = compute_num_cans(current_cans, exchange=EXCHANGE)
current_cans = full + leftovers
drunk_cans += full
steps += 1
print(f'Cans drunk: {drunk_cans}, leftover cans: {leftovers}.')
print(f'A total of {steps} exchanges was needed.')
This yields as output
# Start with 51 and an exchange rate of 3:1
# Cans drunk: 76, leftover cans: 0.
# A total of 4 exchanges was needed.
General Answer:- Accepted also if we change the numExchange also.
Code:-
def numWaterBottles(numBottles: int, numExchange: int) -> int:
ans=numBottles #Initial bottles he/she will drink
while numBottles>=numExchange: #If numBottles<numExchange exit the while loop
remainder=numBottles%numExchange #remaining bottles which is not change
numBottles//=numExchange #The bottles which are changed
ans+=numBottles #The bottles which are changed added to the answer
numBottles+=remainder #Remaining bottles==The bottles which is not change+The bottles which are changed
return ans #Return The answer
print(numWaterBottles(5,3))
print(numWaterBottles(100,3))
print(numWaterBottles(32,4)) #numexchange when different
Output:-
7
149
42