Python Forum
Code challenge I need to solve for a Bootcamp
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Code challenge I need to solve for a Bootcamp
#1
CHALLENGE
A supermarket is doing a sale promotion of soft drinks. If one day you buy soda and take the empty hulls the next day, she exchanges each set of K empty bottles for a full bottle. A customer wants to make the most of this offer and buys several bottles on the first day of the promotion. Now, he wants to know how many bottles he will have at the end of the second day of the promotion if he uses it to the maximum.

INPUT
The first input line contains integer T (1 <=T <=10000), which indicates the number of test cases. On each of the following T lines come two integers N and K (1 <=K, N <= 10000), respectively the number of soft drinks purchased and the number of empty bottles to win a full.

OUTPUT
For each test case, print the number of bottles the customer will have on the second day if they make the most of the offer.

TEST - INPUT
3
7 4
4 7
4000 7

TEST - OUTPUT
4
4
574

---
START OF THE CODE
T = int(input())

for i in range(T):
---

def soda_promotion(n, k):
    bottles = n  # Initially, we have N purchased bottles
    empty_bottles = n  # Start with all bottles being empty
    
    while empty_bottles >= k:
        # Exchange K empty bottles for one full bottle
        exchanged_bottles = empty_bottles // k
        bottles += exchanged_bottles  # Add the exchanged bottles to the total
        empty_bottles = exchanged_bottles + (empty_bottles % k)  # Update the number of empty bottles
        
    return bottles

T = int(input())

for i in range(T):
    n, k = map(int, input().split())
    bottles = soda_promotion(n, k)
    print(bottles)
The code above is not giving the expected result:
Your Output:
9
4
4666
Reply
#2
You are supposed to calculate the number of bottles the customer has on the second day. You drink a bunch of soda and exchange the empties for new bottles. How many bottles do you have. One calculation. You don't need a loop for 1 calculation.

Remember that you don't have the bottles that you drank. Your turn those in.
You might want to look at divmod()
ramonrocha likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  PySpark Coding Challenge cpatte7372 4 6,114 Jun-25-2023, 12:56 PM
Last Post: prajwal_0078
  string format challenge jfc 2 1,795 Oct-23-2021, 10:30 AM
Last Post: ibreeden
Question Help with code to solve polynomials equations hiviera 1 1,805 Jul-31-2021, 01:56 AM
Last Post: hiviera
  I code a program to solve puzzle but i can't make it more dynamic. Shahmadhur13 5 2,764 Apr-18-2020, 10:05 AM
Last Post: Shahmadhur13
  Very difficult challenge for me cristfp 1 2,758 Apr-01-2019, 08:45 PM
Last Post: Yoriz
  Challenge with my string SpencerH 3 2,943 Oct-12-2018, 11:58 AM
Last Post: SpencerH

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020