Skip to content

Instantly share code, notes, and snippets.

@donburks
Last active May 15, 2022 03:20
Show Gist options
  • Save donburks/f0a19beaa5b7f011f5a2 to your computer and use it in GitHub Desktop.
Save donburks/f0a19beaa5b7f011f5a2 to your computer and use it in GitHub Desktop.
Poppin' Bottles

Poppin' Bottles

You've just been hired at Lighthouse Markets, a reputable (and thoroughly fictional) grocery store chain. One of the primary features of Lighthouse Markets is their recycling program on pop bottles. Here is how the program works:

  • For every two empty bottles, you can get one free (full) bottle of pop
  • For every four bottle caps, you can get one free (full) bottle of pop
  • Each bottle of pop costs $2 to purchase

Given these parameters, write a program so that you can figure out how many total bottles of pop can be redeemed given a customer investment.

Task 1

Figure out the algorithm that will calculate this. For example, given a $20 investment, the customer will get 10 bottles of pop. That gives a supply of both bottles and bottle caps that can be used to redeem for further bottles, which will then produce a further supply for recycling.

Task 2

Write a REPL that will prompt the user for the amount (in dollars) that the customer is going to spend. The REPL will then calculate the total number of bottles that the customer will receive and prompt for the next customer's amount.

Task 3

Enhance the output of your program so that once the amount has been given, it provides a breakdown of how many bottles were purchased, how many were obtained through bottle recycling, and how many were obtained through bottle cap recycling.

Task 4

Add to the output, so that the program will tell the customer how many bottles and bottle caps they will have left over. We have to upsell the customer on buying more pop after all!

TIP! Iterate, Iterate, Iterate

You hear this from us all the time, but build your code incrementally. Build it in small steps, and make sure you are confident and comfortable that the code is functioning correctly before you move on to the next step.

TIP! @instance variables

Your use of instance variables for keeping track of different variables will be very important here.

BONUS!

Write this same algorithm using recursion.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment