Skip to main content

Practice Problems: Basic Function Design (Pyret)

Tests are given for all practice problems for you to run against your own implementation.

Problem 1: Temperature Converter

Write a function that converts temperatures from Fahrenheit to Celsius. The conversion formula is: C = (F - 32) × 5/9

Template:

fun fahrenheit-to-celsius(fahrenheit :: Number) -> Number:
# Write your docstring here
# Write your implementation here
where:
fahrenheit-to-celsius(32) is 0
fahrenheit-to-celsius(212) is 100
fahrenheit-to-celsius(-40) is -40
end

Problem 2: Letter Grade Calculator

Write a function that converts a numerical percentage to a letter grade based on the following scale:

  • A: 90-100
  • B: 80-89
  • C: 70-79
  • D: 60-69
  • F: Below 60 Template:
fun percentage-to-grade(percentage :: Number) -> String:
# Write your docstring here
# Write your implementation here
where:
percentage-to-grade(100) is "A"
percentage-to-grade(90) is "A"
percentage-to-grade(89) is "B"
percentage-to-grade(80) is "B"
percentage-to-grade(79) is "C"
percentage-to-grade(70) is "C"
percentage-to-grade(69) is "D"
percentage-to-grade(60) is "D"
percentage-to-grade(55) is "F"
percentage-to-grade(0) is "F"
end

Problem 3: Circle Area Calculator

Write a function that calculates the area of a circle given its radius. Use the formula: Area = π × r^2 You can use 3.142 as the value for π. Template:

fun circle-area(radius :: Number) -> Number:
# Write your docstring here
# Write your implementation here
where:
circle-area(1) is 3.142
circle-area(2) is 12.568
circle-area(0) is 0
end

Problem 4: Password Strength Checker

Write a function that checks if a password meets basic strength requirements. A strong password must:

  • Be at least 8 characters long
  • Contain at least one digit

Template:

fun is-strong-password(password :: String) -> Boolean:
# Write your docstring here
# Write your implementation here
where:
is-strong-password("Password123") is true
is-strong-password("Pass1") is false
is-strong-password("password") is false
is-strong-password("PASSWORD123") is true
end