Skip to content

Commit 0f8210f

Browse files
Jem ChangJem Chang
authored andcommitted
Adding Logic-1
1 parent 495adb9 commit 0f8210f

File tree

10 files changed

+118
-3
lines changed

10 files changed

+118
-3
lines changed

Logic-1/alarm_clock.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
"""
2+
Given a day of the week encoded as 0=Sun, 1=Mon, 2=Tue, ...6=Sat, and a boolean indicating if we are
3+
on vacation, 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+
def alarm_clock(day, vacation):
8+
if day == 6 or day == 0:
9+
if vacation == True:
10+
return 'off'
11+
else:
12+
return '10:00'
13+
elif vacation == True:
14+
return '10:00'
15+
return '7:00'

Logic-1/caught_speeding.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
"""
2+
You are driving a little too fast, and a police officer stops you.
3+
Write code to compute the result, encoded as an int value: 0=no ticket, 1=small ticket, 2=big ticket.
4+
If speed is 60 or less, the result is 0. If speed is between 61 and 80 inclusive, the result is 1.
5+
If speed is 81 or more, the result is 2. Unless it is your birthday --
6+
on that day, your speed can be 5 higher in all cases.
7+
"""
8+
def caught_speeding(speed, is_birthday):
9+
if (speed <=60 and is_birthday == False) or (speed <= 65 and is_birthday == True):
10+
return 0
11+
elif (speed <=80 and is_birthday == False) or (speed <= 85 and is_birthday == True):
12+
return 1
13+
elif (speed >=81 and is_birthday == False) or (speed >= 86 and is_birthday == True):
14+
return 2

Logic-1/cigar_party.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
"""
2+
When squirrels get together for a party, 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+
def cigar_party(cigars, is_weekend):
8+
if cigars >=40 and cigars <=60 and is_weekend == False:
9+
return True
10+
elif cigars >= 40 and is_weekend == True:
11+
return True
12+
return False

Logic-1/date_fashion.py

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

Logic-1/in1to10.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
"""
2+
Given a number n, 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+
def in1to10(n, outside_mode):
7+
if n >= 1 and n <= 10:
8+
return True
9+
if (n <= 1 or n >= 10) and outside_mode == True:
10+
return True
11+
return False

Logic-1/love6.py

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

Logic-1/near_ten.py

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

Logic-1/sorta_sum.py

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

Logic-1/squirrel_play.py

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

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
## Practicing Python Programming in [CodingBat](http://codingbat.com/python)
22
1. [Warmup-1](https://github.com/jemc36/CodingBat-Python/tree/master/Warmup-1)
33
2. [Warmup-2](https://github.com/jemc36/CodingBat-Python/tree/master/Warmup-2)
4-
3. Logic-1
4+
3. [Logic-1](https://github.com/jemc36/CodingBat-Python/tree/master/Logic-1)
55
4. Logic-2
6-
5. String-1
7-
6. String-2
6+
5. [String-1](https://github.com/jemc36/CodingBat-Python/tree/master/String-1)
7+
6. [String-2](https://github.com/jemc36/CodingBat-Python/tree/master/String-2)
88
7. List-1
99
8. List-2

0 commit comments

Comments
 (0)