Skip to content

Commit ae3dc6e

Browse files
committed
codingbat additions
1 parent 5e17cab commit ae3dc6e

8 files changed

Lines changed: 147 additions & 0 deletions

File tree

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/usr/bin/env python3
2+
"""
3+
Given a day of the week encoded as 0=Sun, 1=Mon, 2=Tue, ...6=Sat, and a boolean indicating
4+
if we are on vacation, return a string of the form "7:00" indicating when the alarm clock
5+
should ring. Weekdays, the alarm should be "7:00" and on the weekend it should be "10:00".
6+
Unless we are on vacation -- then on weekdays it should be "10:00" and weekends it should be "off".
7+
"""
8+
def alarm_clock(day, vacation):
9+
if not vacation:
10+
if 1 <= day <= 5:
11+
return '7:00'
12+
return '10:00'
13+
14+
if 1 <= day <= 5:
15+
return '10:00'
16+
return 'off'
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#!/usr/bin/env python3
2+
"""
3+
You are driving a little too fast, and a police officer stops you. Write code to compute the result, encoded as an int value: 0=no ticket, 1=small ticket, 2=big ticket. If speed is 60 or less, the result is 0. If speed is between 61 and 80 inclusive, the result is 1. If speed is 81 or more, the result is 2. Unless it is your birthday -- on that day, your speed can be 5 higher in all cases.
4+
"""
5+
6+
def caught_speeding(speed, is_birthday):
7+
if is_birthday:
8+
speed -= 5
9+
10+
if speed <= 60:
11+
return 0
12+
if 60 < speed <= 80:
13+
return 1
14+
return 2
15+
16+
if __name__ == "__main__":
17+
# some tests
18+
caught_speeding(60, False) is 0
19+
caught_speeding(65, False) is 1
20+
caught_speeding(65, True) is 0
21+
caught_speeding(80, False) is 1
22+
caught_speeding(85, False) is 2
23+
caught_speeding(85, True) is 1
24+
caught_speeding(70, False) is 1
25+
caught_speeding(75, False) is 1
26+
caught_speeding(75, True) is 0
27+
caught_speeding(40, False) is 0
28+
caught_speeding(40, True) is 0
29+
caught_speeding(90, False) is 2
30+
print("All tests passed")
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#!/usr/bin/env python3
2+
"""
3+
You and your date are trying to get a table at a restaurant. The parameter "you" is the stylishness of your clothes, in the range 0..10, and "date" is the stylishness of your date's clothes. The result getting the table is encoded as an int value with 0=no, 1=maybe, 2=yes. If either of you is very stylish, 8 or more, then the result is 2 (yes). With the exception that if either of you has style of 2 or less, then the result is 0 (no). Otherwise the result is 1 (maybe).
4+
"""
5+
def date_fashion(you, date):
6+
if (you <= 2) or (date <= 2):
7+
return 0
8+
elif (you >= 8) or (date >= 8):
9+
return 2
10+
else:
11+
return 1
12+
13+
print(date_fashion(10,5))
14+
15+
if __name__ == "__main__":
16+
# some tests
17+
18+
assert date_fashion(5, 10) is 2
19+
assert date_fashion(5, 2) is 0
20+
assert date_fashion(5, 5) is 1
21+
assert date_fashion(3, 3) is 1
22+
assert date_fashion(10, 2) is 0
23+
assert date_fashion(2, 9) is 0
24+
assert date_fashion(9, 9) is 2
25+
assert date_fashion(10, 5) is 2
26+
assert date_fashion(2, 2) is 0
27+
assert date_fashion(3, 7) is 1
28+
assert date_fashion(2, 7) is 0
29+
assert date_fashion(6, 2) is 0
30+
31+
print("All tests passed")
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/usr/bin/env python3
2+
"""
3+
Given a number n, return True if n is in the range 1..10, inclusive. Unless outside_mode
4+
is True, in which case return True if the number is less or equal to 1, or greater or equal to 10.
5+
"""
6+
def in1to10(n, outside_mode):
7+
if not outside_mode:
8+
return n in range(1, 11)
9+
return n <= 1 or n >= 10
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/usr/bin/env python3
2+
"""
3+
The number 6 is a truly great number. Given two int values, a and b, return True if either
4+
one is 6. Or if their sum or difference is 6. Note: the function abs(num) computes the
5+
absolute value of a number.
6+
"""
7+
def love6(a, b):
8+
return a == 6 or b == 6 or (a + b) == 6 or abs(a - b) == 6
9+
What, this looks ugly you say? I completely agree, and there is a much more pleasant solution:
10+
11+
def love6(a, b):
12+
return 6 in [a, b, a + b, abs(a - b)]
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/usr/bin/env python3
2+
"""
3+
Given a non-negative number "num", return True if num is within 2 of a multiple of 10.
4+
Note: (a % b) is the remainder of dividing a by b, so (7 % 5) is 2.
5+
"""
6+
def near_ten(num):
7+
# return 0 <= (num % 10) <= 2 or 8 <= (num % 10) <= 10
8+
return num % 10 in [0,1,2,8,9,10]
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/usr/bin/env python3
2+
def sorta_sum(a, b):
3+
if 10 <= a + b < 20:
4+
return 20
5+
return a + b
6+
7+
if __name__ == "__main__":
8+
assert sorta_sum(3, 4) is 7
9+
assert sorta_sum(9, 4) is 20
10+
assert sorta_sum(10, 11) is 21
11+
assert sorta_sum(12, -3) is 9
12+
assert sorta_sum(-3, 12) is 9
13+
assert sorta_sum(4, 5) is 9
14+
assert sorta_sum(4, 6) is 20
15+
assert sorta_sum(14, 7) is 21
16+
assert sorta_sum(14, 6) is 20
17+
print("All tests passed")
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#!/usr/bin/env python3
2+
"""
3+
The squirrels in Palo Alto spend most of the day playing. In particular, they play if the temperature is between 60 and 90 (inclusive). Unless it is summer, then the upper limit is 100 instead of 90. Given an int temperature and a boolean is_summer, return True if the squirrels play and False otherwise.
4+
"""
5+
def squirrel_play(temp, is_summer):
6+
return (temp >= 60 and temp <= 90) if not is_summer else (temp >= 60 and temp <= 100)
7+
8+
if __name__ == "__main__":
9+
# some tests
10+
assert squirrel_play(70, False) is True
11+
assert squirrel_play(95, False) is False
12+
assert squirrel_play(95, True) is True
13+
assert squirrel_play(90, False) is True
14+
assert squirrel_play(90, True) is True
15+
assert squirrel_play(50, False) is False
16+
assert squirrel_play(50, True) is False
17+
assert squirrel_play(100, False) is False
18+
assert squirrel_play(100, True) is True
19+
assert squirrel_play(105, True) is False
20+
assert squirrel_play(59, False) is False
21+
assert squirrel_play(59, True) is False
22+
assert squirrel_play(60, False) is True
23+
24+
print("All tests passed")

0 commit comments

Comments
 (0)