Exercises: calculators

Tags

Remember

Exercise

Jape Joint

The Jape Joint is a small goat-only comedy club. It has 40 small stalls that can hold up to three goats. Small stall tickets are $39.95 each. It also has 20 big stalls that can hold up to six. Tickets are $99.95 for the bigs.

Write a program to compute sales, and warn the user if stalls are oversold. Here's some I/O. Assume users don't make mistakes.

  • Jape Joint Sales
  • ==== ===== =====
  •  
  • Big stalls sold? 1
  • Small stalls sold? 1
  •  
  • Big stall sales: $ 99.95
  • Small stall sales: $ 39.95
  • Total: $ 139.9

Some more:

  • Jape Joint Sales
  • ==== ===== =====
  •  
  • Big stalls sold? 21
  • Warning: big stalls oversold!
  • Small stalls sold? 41
  • Warning: small stalls oversold!
  •  
  • Big stall sales: $ 2098.95
  • Small stall sales: $ 1637.95
  • Total: $ 3736.9

Upload a zip of your project folder. The usual coding standards apply.

Exercise

Goat bento boxes

Cthulhu runs Best Bento Boxes, supplying bento boxes to goat parties, meetings, whatevs. Write a program to work out a price for an event.

There are three sizes:

  • Great Goat Gobble Box: $32.95 each
  • Medium Mandy's Munch Box: $26.95 each
  • Sue's Small Snack Pack: $17.95

There's a delivery fee:

  • 10 or fewer boxes: $39.95
  • 11 to 20 boxes: $59.95
  • More than 20 boxes: Free delivery

Here's some I/O. Assume users make no mistakes.

  • Best Bento Boxes
  • ==== ===== =====
  •  
  • How many of each type do you need?
  • Great Goat Gobble Box: 1
  • Medium Mandy's Munch Box: 1
  • Sue's Small Snack Pack: 1
  •  
  • Boxes: $ 77.85
  • Delivery: $ 39.95
  • Total price: $ 117.8

More:

  • Best Bento Boxes
  • ==== ===== =====
  •  
  • How many of each type do you need?
  • Great Goat Gobble Box: 10
  • Medium Mandy's Munch Box: 10
  • Sue's Small Snack Pack: 10
  •  
  • Boxes: $ 778.5
  • Delivery: $ 0
  • Total price: $ 778.5

Round the total to two decimal places. (Leaving off cents if it's zero is OK.)

Upload a zip of your project folder. The usual coding standards apply.

Exercise

Bouc Bus

Philippe Fallop
Philippe Fallop, by Teagan

Philippe Fallop runs Bouc Bus, a French goat bus company. Write a program to price a bus trip, based on the number of goats, and the length of the trip.

Philippe has three buses:

  • La petite carotte, seats up to 10 goats. Bus fee is €250, plus €4.80 per kilometer.
  • La moyenne oignon, seats up to 40 goats. 🚌 fee is €375, plus €7.50 per kilometer.
  • La grosse patate, seats up to 80 goats. 🚍 fee is €525, plus €9.20 per kilometer.
  • La infini racine de yucca, seats as many goats as you want. 🚎 fee is €39,345,231, plus €1,332 per kilometer.

Here's some sample I/O:

  • Bouc Bus
  • - - - -
  • How many goats on the trip? 1
  • How far is the trip (kilometers)? 1
  •  
  • Bus: La petite carotte
  • Bus fee: 250
  • Distance fee: 4.8
  • - - - - - - - - - - - -
  • Total: 254.8

More:

  • Bouc Bus
  • - - - -
  • How many goats on the trip? 15
  • How far is the trip (kilometers)? 120
  •  
  • Bus: La moyenne oignon
  • Bus fee: 375
  • Distance fee: 900.0
  • - - - - - - - - - - - -
  • Total: 1275.0

One more. When La infini racine de yucca is used, output a warning, as seen below.

  • Bouc Bus
  • - - - -
  • How many goats on the trip? 1000
  • How far is the trip (kilometers)? 1233
  •  
  • Bus: La infini racine de yucca
  • Bus fee: 39345231
  • Distance fee: 1642356.0
  • - - - - - - - - - - - -
  • Total: 40987587.0
  •  
  •  
  • WARNING! Time distortion is not covered by insurance.

Other things:

  • Assume users make no input errors. That's later in the course.
  • Add comments for IPO.
  • Remember the warning when selecting La infini racine de yucca.

Upload your solution here, not to Moodle. The usual coding standards apply.

Exercise

Wellermen

There once was a ship that put to sea
The name of the ship
Was the Billy o' Tea
The winds blew hard
Her bow dipped down
O blow, my bully boys, blow
Huuh!

Soon may the Wellerman come
To bring us sugar and tea and rum
One day, when the tonguin' is done
We'll take our leave and go.

Make a program to take orders for the Weller Brothers, a whaling supply company in New Zealand. We'll only have the most important supplies: sugar, tea, and rum. We'll make the measurement units easier to deal with as well. Because, sanity. They used units like bushels in those days. You wouldn't believe the twisted currency system.

There are bulk discounts.

  • Sugar is $1.8 per kilo for less than 10 kilos, $1.6 for less than 50, and $1.4 for 50 or more.
  • Tea is $5.2 per kilo for less than 5 kilos, $5.1 for less than 10, and $4.8 for 10 or more.
  • Rum is $6.7 per liter for less than 20 liters, $6.4 for less than 50, and $6.2 for 50 or more.

Output the price for each item, and the total.

Also, output "You're gonna need more rum, mate!" if the number of liters of rum is less than the sum of the number of kilos of tea and sugar. E.g., if rum is 20, tea is 12, and sugar is 10, output the message. If rum is 25, tea is 12, and sugar is 10, do not output the message.

I/O sample:

  • Weller Bros Order Form
  • - - - - - - - - - - -
  •  
  • How many kilos of sugar? 1
  • How many kilos of tea? 1
  • How many liters of rum? 1
  •  
  • Sugar total: 1.8
  • Tea total: 5.2
  • Rum price: 6.7
  •  
  • Total price: 13.7
  •  
  • You're gonna need more rum, mate!

Another:

  • Weller Bros Order Form
  • - - - - - - - - - - -
  •  
  • How many kilos of sugar? 10
  • How many kilos of tea? 10
  • How many liters of rum? 20
  •  
  • Sugar total: 16.0
  • Tea total: 48.0
  • Rum price: 128.0
  •  
  • Total price: 192.0

Notes:

  • Assume all user input is correct. Validation is the next part of the course.
  • Users might input fractional amounts, like 3.8 kilos.
  • Output the blank lines, the underline, and the prompts exactly.
  • Add IPO comments.
  • Hint: I had a variable named sugar_price_per_kilo. You don't have to.

Upload your solution here, not to Moodle. The usual coding standards apply.

Exercise

Flash Mobs for Hope

Mary Louise Porter runs Flash Mobs for Hope, small flash mobs that sing at farmer's markets and such. Write a program to help Mary work out the cost of supplies for an event.

To help the singers relax before a gig, she gives them small packs of special gummies. The number of gummies depends on the type of singer:

  • Sopranos are flighty. They get three packs each.
  • Altos aren't as bad, but being soprano-adjacent, they need extra. They get two packs each.
  • Tenors and basses are rock solid. They get one pack each.

The price of a pack depends on the number MLP orders.

  • 20 or more packs: $8.50 each
  • 10 to 19: $8.80 each
  • Less than 10: $9.10 each

Shipping is free if MLP orders 25 or more packs. Otherwise, shipping is $0.50 per pack.

If there are too many sopranos, they get over-excited. Sometimes they need to be tranquilized. MLP's assistant Wendell has a dart gun for those cases.

If there are more sopranos than all the others put together, output a message saying Wendell should bring the dart gun. He'll need two more darts than the number of sopranos. He's a good shot, but they run around when excited.

Here's some sample I/O:

  • Flash Mobs for Hope
  • - - - - - - - - - -
  •  
  • How many sopranos? 1
  • How many altos? 1
  • How many tenors? 1
  • How many basses? 1
  •  
  • Gummy packs needed: 7
  • Gummies price: 63.7
  • - - - - - - - - - -
  • Total: 67.2

Another sample:

  • Flash Mobs for Hope
  • - - - - - - - - - -
  •  
  • How many sopranos? 5
  • How many altos? 2
  • How many tenors? 1
  • How many bass? 1
  •  
  • !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  • WARNING! Too many sopranos!
  • Have Wendell bring the dart gun with 7 darts.
  • !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  •  
  • Gummy packs needed: 21
  • Gummies price: 178.5
  • - - - - - - - - - -
  • Total: 189.0

Other things:

  • Assume users make no input errors. That's later in the course.
  • Round numbers to at most two decimal places.
  • Add comments for IPO.
  • Format the output exactly, with the blank lines, underlining, etc.

Upload your solution here, not to Moodle. The usual coding standards apply.

Exercise

Overrun with Carnimon

North Korea bans cell phones, so up to a few years ago it was overrun with Pokemon. Nobody to catch one, let alone them all.

The solution was to release Carnimon, creatures that feed on Pokemon. There are three species: Burner, Biter, and Borer. It worked. Pokemon are not clogging the streets, interrupting theoretical food deliveries.

Carnimon

Of course, now the Carnimon are out of control. (Sigh)

Dear Leader has ordered that peasants..., er, patriotic heroes and righteous toilers (PHARTs) (which is everyone but him and his special people, who keep the PHARTs from escaping) catch Carnimon per week, using Caricubes, like Pokeballs but made in North Korea, rather than made by immoral foreigners who don't control their PHARTs.

PHARTs who don't meet their quota will be punished. They'll be forced to read a regular programming textbook. So, you know, motivation.

Carnicubes are all the same size, one cubic gan. That's the standard unit of length, named for the reported length of Dear Leader's great grandmother's liver. A Carnicube can hold one Burner, three Biters, or four Borers. You can't mix species in a Carnicube or they'll carn each other. DL doesn't want that, since he plans Carnimon to be the main source of protein in PHART diets. And they'd better not complain! DL wants them silent.

Being a tough but fair leader, DL won't make every PHART catch Carimon. He'll exempt those who are too young, or who are about to contribute their bodies to enriching NK's fields, in a program called Mulch Mom for Might. He'll exempt his PHART-squeezing pals, too, of course. He needs them to keep the plebs having the right PHARTy thoughts.

Write a program to estimate how many Carnicubes DL will need each week, based on:

  • The number of PHARTs who'll participate.
  • The number of each type of Carnimon each PHART will catch.
  • Expected percentage of Carnicube loss, through poor PHARTmanship, falling off the back of a buffalo cart, or whatever.
  • Whether Dear Leader is happy today.

Here's some I/O. Oh, were it says "volunteer?" In DearLeaderLand, volunteering is not voluntary.

The simplest case:

  • Carnicube Count
  • ======== =====
  •  
  • Hail Dear Leader, from whom all good flows!
  •  
  • How many PHARTs will volunteer for this glorious work? 1
  •  
  • Tell me about the weekly quotas for each PHART.
  • Burners? 1
  • Biters? 1
  • Borers? 1
  •  
  • Tell me about Carnicube loss.
  • Expected Carnicube loss (%)? 100
  •  
  • Tell me about Dear Leader
  • Is Dear Leader happy today (y/n)? y
  •  
  • ==== Results ====
  •  
  • Dear Leader is happy! All rejoice!
  •  
  • PHARTs: 1
  •  
  • Cubes needed for each PHART
  • - - - - - - - - - - - - -
  •  
  • To capture burners: 1 per PHART
  • To capture biters: 1 per PHART
  • To capture borers: 1 per PHART
  •  
  • Total cubes needed before accounting for loss: 3 cubes
  • Cube loss allowance: 3 cubes
  •  
  • Total cubes needed: 6
  •  
  • Hail Dear Leader, from whom all good flows!

Notice the line To capture biters: 1 per PHART. The quota is one biter, and you can put three biters into a cube, so that's 0.33 cubes, right? No, you need to go up to the next highest number. So they need one cube each, even though most of it will be MT.

The easiest way to do that is to use the ceil() function. For example:

  1. import math
  2. yummy_pi = 3.14159
  3. round_me_up_some_pi = math.ceil(yummy_pi)
  4. print(round_me_up_some_pi)

This prints 4.

ceil() isn't a built-in function. It's in the math library. (Yay! Math!) You import the library (line 1) and tell your program to use ceil() in that library (line 3).

Give ceil() a number, and it will return the next largest whole number. Or just the number, if it's already whole.

Here's some stuff from my solution:

  1. biter_cubes_per_phart = math.ceil(biters_per_phart / 3)
  2. ...
  3. lost_cubes = cubes_before_loss * (loss_percent/100)
  4. lost_cubes = math.ceil(lost_cubes)

You can fit three biters in a cube. The first line works out the number of cubes you'll need for the number of biters you have, then rounds up.

The last two lines do the same thing for the lost cube allowance.

Another example:

  • Carnicube Count
  • ======== =====
  •  
  • Hail Dear Leader, from whom all good flows!
  •  
  • How many PHARTs will volunteer for this glorious work? 100
  •  
  • Tell me about the weekly quotas for each PHART.
  • Burners? 2
  • Biters? 7
  • Borers? 9
  •  
  • Tell me about Carnicube loss.
  • Expected Carnicube loss (%)? 10
  •  
  • Tell me about Dear Leader
  • Is Dear Leader happy today (y/n)? y
  •  
  • ==== Results ====
  •  
  • Dear Leader is happy! All rejoice!
  •  
  • PHARTs: 100
  •  
  • Cubes needed for each PHART
  • - - - - - - - - - - - - -
  •  
  • To capture burners: 2 per PHART
  • To capture biters: 3 per PHART
  • To capture borers: 3 per PHART
  •  
  • Total cubes needed before accounting for loss: 800 cubes
  • Cube loss allowance: 80 cubes
  •  
  • Total cubes needed: 880
  •  
  • Hail Dear Leader, from whom all good flows!

The number of cubes per PHART are rounded up as expected.

Here's a more realistic run:

  • Carnicube Count
  • ========= =====
  •  
  • Hail Dear Leader, from whom all good flows!
  •  
  • How many PHARTs will volunteer for this glorious work? 15000000
  •  
  • Tell me about the weekly quotas for each PHART.
  • Burners? 2
  • Biters? 5
  • Borers? 12
  •  
  • Tell me about Carnicube loss.
  • Expected Carnicube loss (%)? 10
  •  
  • Tell me about Dear Leader
  • Is Dear Leader happy today (y/n)? n
  •  
  • ==== Results ====
  •  
  • Dear Leader is unhappy. His PHARTs are failing him.
  •  
  • PHARTs: 15000000
  •  
  • Cubes needed for each PHART
  • - - - - - - - - - - - - -
  •  
  • To capture burners: 4 per PHART
  • To capture biters: 4 per PHART
  • To capture borers: 6 per PHART
  •  
  • Total cubes needed before accounting for loss: 210000000 cubes
  • Cube loss allowance: 21000000 cubes
  •  
  • Total cubes needed: 231000000
  •  
  • Hail Dear Leader, from whom all good flows!

All those 0s in the large numbers makes it easy for readers to make mistakes. What I'd do for realsies is add , separators, like humans normally do. You can google that if you want.

Submit your solution through this site, not Moodle. The usual rules apply.