Courses‎ > ‎Intro to CS - Full Year‎ > ‎Spring‎ > ‎Dyrland-Weaver‎ > ‎Work‎ > ‎

Work 20: 3/26

posted Mar 26, 2019, 11:20 AM by JonAlf Dyrland-Weaver   [ updated Mar 26, 2019, 12:14 PM ]
Write the following python functions, use elif in each, trying to minimize the length of each conditional test used:
  • sale_price
    • Takes 2 arguments, the first represents the original price of an item, the second represents the number of weeks it has been on sale.
    • Returns the current price of the item based on how many weeks it has been on sale.
      • Items on sale for 0 weeks will cost the full amount
      • Items on sale for 1 week will cost 75% of the full amount
      • Items on sale for 2 weeks will cost 50% of the full amount
      • Items on sale for 3 or more weeks will cost 25% of the full amount.
    • Examples
      • sale_price(200, 0) ==> 200
      • sale_price(200, 1) ==> 150
      • sale_price(200, 2) ==> 100
      • sale_price(200, 3) ==> 50
  • production
    • Takes 2 arguments, the first represents an hour of the day between 6 and 20 (8pm in 24 hour time format), the second represents the number of workers at a factory.
    • Returns the number of widgets produced by the factory in the given hour by the given number of employees.
    • The amount of widgets a worker makes is based on the time of day
      • Between 6 and 10 (including 6 but excluding 10) each worker can make 30 widgets in an hour.
      • Between 10 and 14 (including 10 but excluding 14) each worker can make 40 widgets in an hour.
      • Between 14 and 20 (including 14 but excluding 20) each worker can make 35 widgets in an hour.
    • You can assume that only valid times are provided for the hour parameter.
    • Examples
      • production(8, 2) ==> 60
      • production(13, 4) ==> 160
      • production(17, 1) ==> 35
  • letter_grade
    • Returns a letter grade based on a numeric grade out of 100 using the following scale:
      • grade >= 90 ==> e
      • 90 > grade >= 75 ==> s
      • 75 > grade >= 65 ==> n
      • 65 > grade ==> u
  • is_leap_year
    • Takes a single parameter representing a year.
    • Returns True if the year is a leap year, and False if it is not
    • The rules for leap year-ness are as follows
      • Years that are divisible by 4 are leap years, except:
      • Years that are divisible by 100 are not leap years except:
      • Years that are divisible by 400 are leap years.
    • Examples
      • is_leap_year(2012) ==> True
      • is_leap_year(1900) ==> False
      • is_leap_year(2000) ==> True
      • is_leap_year(1983) ==> False
submit this as elif
Comments