Skip to content

Commit 27f82cf

Browse files
committed
Logic-1 directory added
1 parent 4382c01 commit 27f82cf

9 files changed

+168
-0
lines changed

Logic-1/alarm_clock.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Given a day of the week encoded as 0=Sun, 1=Mon, 2=Tue, ...6=Sat,
2+
# and a boolean indicating if we are on vacation,
3+
# return a string of the form "7:00" indicating when the alarm clock should ring.
4+
# Weekdays, the alarm should be "7:00" and on the weekend it should be "10:00".
5+
# Unless we are on vacation -- then on weekdays it should be "10:00" and weekends it should be "off".
6+
7+
8+
# alarm_clock(1, False) → '7:00'
9+
# alarm_clock(5, False) → '7:00'
10+
# alarm_clock(0, False) → '10:00'
11+
12+
def alarm_clock(day, vacation):
13+
if vacation:
14+
if day == 0 or day == 6:
15+
return "off"
16+
return "10:00"
17+
else:
18+
if day == 0 or day == 6:
19+
return "10:00"
20+
return "7:00"
21+

Logic-1/caught_speeding.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# You are driving a little too fast,
2+
# and a police officer stops you.
3+
# Write code to compute the result,
4+
# encoded as an int value: 0=no ticket, 1=small ticket, 2=big ticket.
5+
# If speed is 60 or less, the result is 0.
6+
# If speed is between 61 and 80 inclusive, the result is 1.
7+
# If speed is 81 or more, the result is 2.
8+
# Unless it is your birthday -- on that day, your speed can be 5 higher in all cases.
9+
10+
11+
# caught_speeding(60, False) → 0
12+
# caught_speeding(65, False) → 1
13+
# caught_speeding(65, True) → 0
14+
15+
def caught_speeding(speed, is_birthday):
16+
if is_birthday:
17+
if speed <= 65:
18+
return 0
19+
elif 66 <= speed <= 85:
20+
return 1
21+
else:
22+
return 2
23+
else:
24+
if speed <= 60:
25+
return 0
26+
elif 61 <= speed <= 80:
27+
return 1
28+
else:
29+
return 2
30+
31+
# def caught_speeding(speed, is_birthday):
32+
# if is_birthday:
33+
# inc = 5
34+
# else:
35+
# inc = 0
36+
# if speed <= 60 + inc:
37+
# return 0
38+
# elif 61 <= speed <=80 + inc:
39+
# return 1
40+
# elif speed >= 81 + inc:
41+
# return 2

Logic-1/cigar_party.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# When squirrels get together for a party,
2+
# they like to have cigars.
3+
# A squirrel party is successful when the number of cigars is between 40 and 60, inclusive.
4+
# Unless it is the weekend, in which case there is no upper bound on the number of cigars.
5+
# Return True if the party with the given values is successful, or False otherwise.
6+
7+
8+
# cigar_party(30, False) → False
9+
# cigar_party(50, False) → True
10+
# cigar_party(70, True) → Tru
11+
12+
def cigar_party(cigars, is_weekend):
13+
return cigars >= 40 and cigars <= 60 or is_weekend and cigars > 60

Logic-1/date_fashion.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# You and your date are trying to get a table at a restaurant.
2+
# The parameter "you" is the stylishness of your clothes,
3+
# in the range 0..10, and "date" is the stylishness of your date's clothes.
4+
# The result getting the table is encoded as an int value with 0=no, 1=maybe, 2=yes.
5+
# If either of you is very stylish, 8 or more, then the result is 2 (yes).
6+
# With the exception that if either of you has style of 2 or less,
7+
# then the result is 0 (no). Otherwise the result is 1 (maybe).
8+
9+
10+
# date_fashion(5, 10) → 2
11+
# date_fashion(5, 2) → 0
12+
# date_fashion(5, 5) → 1
13+
14+
def date_fashion(you, date):
15+
if you <= 2 or date <= 2:
16+
return 0
17+
elif you >= 8 or date >= 8:
18+
return 2
19+
return 1
20+

Logic-1/in1to10.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Given a number n,
2+
# return True if n is in the range 1..10, inclusive.
3+
# Unless outside_mode is True, in which case return True if the number is less or equal to 1,
4+
# or greater or equal to 10.
5+
6+
7+
# in1to10(5, False) → True
8+
# in1to10(11, False) → False
9+
# in1to10(11, True) → True
10+
11+
def in1to10(n, outside_mode):
12+
if outside_mode:
13+
return n <= 1 or n >= 10
14+
return 1 <= n <= 10

Logic-1/love6.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# The number 6 is a truly great number.
2+
# Given two int values, a and b, return True if either one is 6.
3+
# Or if their sum or difference is 6.
4+
# Note: the function abs(num) computes the absolute value of a number.
5+
6+
7+
# love6(6, 4) → True
8+
# love6(4, 5) → False
9+
# love6(1, 5) → True
10+
11+
def love6(a, b):
12+
if a == 6 or b == 6:
13+
return True
14+
elif a+b == 6 or abs(a-b) == 6:
15+
return True
16+
else:
17+
return False

Logic-1/near_ten.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Given a non-negative number "num",
2+
# return True if num is within 2 of a multiple of 10.
3+
# Note: (a % b) is the remainder of dividing a by b,
4+
# so (7 % 5) is 2. See also: Introduction to Mod
5+
6+
7+
# near_ten(12) → True
8+
# near_ten(17) → False
9+
# near_ten(19) → True
10+
11+
def near_ten(num):
12+
return (num % 10) <= 2 or (num % 10 ) >= 8

Logic-1/sorta_sum.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Given 2 ints, a and b, return their sum.
2+
# However, sums in the range 10..19 inclusive, are forbidden,
3+
# so in that case just return 20.
4+
5+
6+
# sorta_sum(3, 4) → 7
7+
# sorta_sum(9, 4) → 20
8+
# sorta_sum(10, 11) → 21
9+
10+
def sorta_sum(a, b):
11+
total_sum = a + b
12+
if 10 <= total_sum <= 19:
13+
return 20
14+
15+
return total_sum

Logic-1/squirrel_play.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# The squirrels in Palo Alto spend most of the day playing.
2+
# In particular, they play if the temperature is between 60 and 90 (inclusive).
3+
# Unless it is summer, then the upper limit is 100 instead of 90.
4+
# Given an int temperature and a boolean is_summer,
5+
# return True if the squirrels play and False otherwise.
6+
7+
8+
# squirrel_play(70, False) → True
9+
# squirrel_play(95, False) → False
10+
# squirrel_play(95, True) → True
11+
12+
def squirrel_play(temp, is_summer):
13+
if is_summer:
14+
return 60 <= temp <= 100
15+
return 60 <= temp <= 90

0 commit comments

Comments
 (0)