Skip to article frontmatterSkip to article content

Exercise 3 - Functions

  • 🏆 20 points available
  • ✏️ Last updated on 9/7/2025

▶️ First, run the code cell below to import unittest, the module used for the 🧭 Check Your Work boxes and the autograder.

# DO NOT MODIFY THE CODE IN THIS CELL
import unittest
tc = unittest.TestCase()

🎯 Challenge 1: Find the Smaller Number

👇 Tasks

Create a function named find_smaller that takes two numbers as input (parameters).

  1. The function should return the smaller of the two values.
  2. If both values are equal, return either one.
  3. Do not use Python’s built-in min() function.

💡 Expected Output

7
-10
0
# YOUR CODE BEGINS

# YOUR CODE ENDS

print(find_smaller(9, 7))
print(find_smaller(-10, -5))
print(find_smaller(0, 0))

🧭 Check Your Work

Run the code cell below to test your solution.

  • ✔️ If the code cell runs without errors, you’re good to move on.
  • ❌ If the code cell produces an error, review your code and fix any mistakes.
_test_case = 'find-smaller'
_points = 3

tc.assertEqual(find_smaller(5, 5), 5)
tc.assertEqual(find_smaller(8, 5), 5)
tc.assertEqual(find_smaller(-7, -2), -7)

🎯 Challenge 2: Find the Largest Number in a List

👇 Tasks

Create a function named find_largest that takes one parameter of type list.

  1. The list will only contain numbers.
  2. You can assume the list will always have at least one element.
  3. The find_largest function should return the largest value in the list.

⚔️ Special Requirement

Do not use Python’s built-in max() function.

🧭 Tips

  1. Create a variable named largest inside the function. Initialize it with the first element of the list.
  2. Use a for loop to iterate through the list. For each value, update largest if value > largest.

🔑 Expected Output

8
5
10
# YOUR CODE BEGINS

# YOUR CODE ENDS

print(find_largest([5, 3, 8]))
print(find_largest([5, -7, -4]))
print(find_largest([9, 5, 4, 1, 3, 7, 0, 10]))

🧭 Check Your Work

Run the code cell below to test your solution.

  • ✔️ If the code cell runs without errors, you’re good to move on.
  • ❌ If the code cell produces an error, review your code and fix any mistakes.
_test_case = 'find-the-largest-number-in-a-list'
_points = 4

tc_input_lists = [
    [4, 7, 3],
    [-1, -12, -3, -4, -5],
    [0, 0]
]

for l in tc_input_lists:
    tc.assertEqual(find_largest(l), max(l), f'find_largest({l}) should print out {max(l)}, not {find_largest(l)}.')

🎯 Challenge 3: Multiples of Four Checker

👇 Tasks

Create a function named is_multiple_of_four that takes one numeric parameter.

  1. The function should return True if the input is a multiple of 4.
  2. Otherwise, it should return False.

💡 Expected Output

False
True
False
True
# YOUR CODE BEGINS

# YOUR CODE ENDS

print(is_multiple_of_four(3))
print(is_multiple_of_four(8))
print(is_multiple_of_four(-10))
print(is_multiple_of_four(-4))

🧭 Check Your Work

Run the code cell below to test your solution.

  • ✔️ If the code cell runs without errors, you’re good to move on.
  • ❌ If the code cell produces an error, review your code and fix any mistakes.
_test_case = 'multiples-of-four-checker'
_points = 3

tc.assertEqual(is_multiple_of_four(7), False)
tc.assertEqual(is_multiple_of_four(-16), True)
tc.assertEqual(is_multiple_of_four(4), True)

🎯 Challenge 4: Calculate Revenue from an Order

👇 Tasks

  1. Create a function named get_revenue that takes one parameter of type dict (dictionary).
  2. The input dictionary will contain details about a product order with three keys: product, quantity, and unit_price.
  3. The get_revenue function should return the product of quantity and unit_price.

🔑 Expected Output

40.0
45.0
149.9
o1 = {"product": "Tape", "quantity": 20, "unit_price": 2.0}
o2 = {"product": "Ruler", "quantity": 30, "unit_price": 1.5}
o3 = {"product": "Flash Cards", "quantity": 10, "unit_price": 14.99}

# YOUR CODE BEGINS

# YOUR CODE ENDS

print(get_revenue(o1))
print(get_revenue(o2))
print(get_revenue(o3))

🧭 Check Your Work

Run the code cell below to test your solution.

  • ✔️ If the code cell runs without errors, you’re good to move on.
  • ❌ If the code cell produces an error, review your code and fix any mistakes.
_test_case = 'calculate-revenue-from-order'
_points = 5

tc.assertEqual(get_revenue({"quantity": 15, "unit_price": 3.0}), 45.0)
tc.assertEqual(get_revenue({"quantity": 0, "unit_price": 45.0}), 0)

🎯 Challenge 5: Convert USD to INR with a Fee

👇 Tasks

You are working as a bank teller responsible for converting United States Dollars (USD $) to Indian Rupees (INR ₹).

Create a function named usd_to_inr_with_fee that takes the following three parameters (in this order):

  1. amount: The amount in USD (can be int or float).
  2. rate: The conversion rate between USD and INR. For example, a rate of 90 means that 100 USD converts to 9000 INR (assuming the fee is already paid).
  3. fee: A fixed currency conversion fee in USD (applies regardless of the amount).

The function should:

  • First subtract the fee from amount.
  • Multiply the result by the rate.
  • Return the converted amount in INR.

Mathematical formula:

(amountfee)×rate(amount - fee) \times rate

🔑 Expected Output

8000
10620
19700
# YOUR CODE BEGINS

# YOUR CODE ENDS

print(usd_to_inr_with_fee(105, 80, 5))  # amount = 105,
                                        # rate = 80
                                        # fee = 5
print(usd_to_inr_with_fee(120, 90, 2))
print(usd_to_inr_with_fee(200, 100, 3))

🧭 Check Your Work

Run the code cell below to test your solution.

  • ✔️ If the code cell runs without errors, you’re good to move on.
  • ❌ If the code cell produces an error, review your code and fix any mistakes.
_test_case = 'convert-usd-to-inr-with-a-fee'
_points = 5

tc.assertAlmostEqual(usd_to_inr_with_fee(5550, 70, 4), 388220)
tc.assertAlmostEqual(usd_to_inr_with_fee(2255, 71, 15), 159040)
tc.assertAlmostEqual(usd_to_inr_with_fee(2421, 72, 10), 173592)