Multiply Integers
Use HackerRank to write a Python function that multiplies two integers.
Average
Write a function called average_four(value1, value2,
value3, value4)
, which takes 4 floating point values as
parameters. Your function should return average of the four
values.
Test Cases
print('Input: 0, 0, 0, 0\tActual:', average_four(0, 0, 0, 0), '\tExpected: 0.0') print('Input: 1, 1, 1, 1\tActual:', average_four(1, 1, 1, 1), '\tExpected: 1.0') print('Input: 1, 0, 0, 0\tActual:', average_four(1, 0, 0, 0), '\tExpected: 0.25') print('Input: 0, 1, 0, 0\tActual:', average_four(0, 1, 0, 0), '\tExpected: 0.25') print('Input: 0, 0, 1, 0\tActual:', average_four(0, 0, 1, 0), '\tExpected: 0.25') print('Input: 0, 0, 0, 1\tActual:', average_four(0, 0, 0, 1), '\tExpected: 0.25') print('Input: 1, 2, 3, 4\tActual:', average_four(1, 2, 3, 4), '\tExpected: 2.5')
Change
There are many people that would call me a lazy person. I don't find that offensive, but I think it's not a valid description. It's not that I'm lazy, I just try to be as efficient as possible. For example, in college a group of friends and I deduced the fewest amount of coins to carry that would produce any arbitrary amount of change. While a computation like that is still a little too complex, figuring out the fewest amount of coins for a fixed amount of change is possible.
Details
Create a Python program that defines the
function count_coins(cents)
that returns the fewest
number of coins needed to to make change. The
parameter cents, an integer between 0 and 100, is the
amount of change in cents. The function should return an
integer representing the fewest number of coins, using the
American monetary system consisting of quarters, dimes, nickles,
and pennies, necessary to make the specified amount of change.
Test Cases
print('Input: 0\tActual:', count_coins(0), '\tExpected: 0') print('Input: 1\tActual:', count_coins(1), '\tExpected: 1') print('Input: 2\tActual:', count_coins(2), '\tExpected: 2') print('Input: 5\tActual:', count_coins(5), '\tExpected: 1') print('Input: 10\tActual:', count_coins(10), '\tExpected: 1') print('Input: 15\tActual:', count_coins(15), '\tExpected: 2') print('Input: 16\tActual:', count_coins(16), '\tExpected: 3') print('Input: 25\tActual:', count_coins(25), '\tExpected: 1') print('Input: 50\tActual:', count_coins(50), '\tExpected: 2')
- While it might seem like you are going to use a loop for this function, you actually won't use a single loop.
- You can use integer division (//) to determine how many times a coin goes into the change amount evenly.
- Recall that you can use the mod (%) operator to determine the remainder of an integer division operator. Use this to determine the change amount the rest of the coins need to cover.
- Start with the largest coin first (quarters) and work your way down. This is known as a greedy approach, and is very common in Computer Science.
Challenge
When converting change to the fewest number of coins, what
amount of change requires the most coins? Write a program to
figure it out. Compute the fewest number of coins for all
amounts of change between 0 and 100. Use
the max
function to find the largest number of coins. Print the amount
of change and the number of coins.