Skip to content

Commit 637eef5

Browse files
Jem ChangJem Chang
Jem Chang
authored and
Jem Chang
committed
Adding Warmup-1
0 parents  commit 637eef5

14 files changed

+134
-0
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
## Practicing Python Programming in [CodingBat](http://codingbat.com/python)
2+
1. Warmup-1
3+
2. Warmup-2
4+
3. Logic-1
5+
4. Logic-2
6+
5. String-1
7+
6. String-2
8+
7. List-1
9+
8. List-2

Warmup-1/.DS_Store

6 KB
Binary file not shown.

Warmup-1/diff21.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
"""
2+
Given an int n, return the absolute difference between n and 21,
3+
except return double the absolute difference if n is over 21.
4+
"""
5+
def diff21(n):
6+
if n > 21:
7+
return 2*abs(n-21)
8+
else:
9+
return abs(n-21)

Warmup-1/front3.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
"""
2+
Given a string, we'll say that the front is the first 3 chars of the string.
3+
If the string length is less than 3, the front is whatever is there.
4+
Return a new string which is 3 copies of the front.
5+
"""
6+
def front3(str):
7+
if len(str) < 3:
8+
return str+str+str
9+
else:
10+
return str[0:3]+str[0:3]+str[0:3]

Warmup-1/front_back.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
"""
2+
Given a string, return a new string where the first and last chars have been exchanged.
3+
"""
4+
def front_back(x):
5+
if len(x) <= 1:
6+
return x
7+
mid = x[1:len(x)-1]
8+
return x[-1] + mid + x[0]
9+

Warmup-1/makes10.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 True if one if them is 10 or if their sum is 10.
3+
"""
4+
def makes10(a, b):
5+
if a == 10 or b == 10 or a+b == 10:
6+
return True
7+
else:
8+
return False

Warmup-1/missing_char.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
"""
2+
Given a non-empty string and an int n, return a new string where the char at index n has been removed.
3+
The value of n will be a valid index of a char in the original string
4+
(i.e. n will be in the range 0..len(str)-1 inclusive).
5+
"""
6+
def missing_char(x, n):
7+
allstr = str()
8+
for i in range(len(x)):
9+
if i == n: continue
10+
allstr = allstr + x[i]
11+
return allstr

Warmup-1/monkey_trouble.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
"""
2+
We have two monkeys, a and b, and the parameters a_smile and b_smile indicate if each is smiling.
3+
We are in trouble if they are both smiling or if neither of them is smiling. Return True if we are in trouble.
4+
"""
5+
def monkey_trouble(a_smile, b_smile):
6+
if a_smile == True and b_smile == True:
7+
return True
8+
elif a_smile == False and b_smile == False:
9+
return True
10+
else:
11+
return False

Warmup-1/near_hundred.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
"""
2+
Given an int n, return True if it is within 10 of 100 or 200.
3+
Note: abs(num) computes the absolute value of a number.
4+
"""
5+
def near_hundred(n):
6+
if n >= (100-10) and n <= (100+10):
7+
return True
8+
elif n >= (200-10) and n <= (200+10):
9+
return True
10+
else:
11+
return False

Warmup-1/not_string.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
"""
2+
Given a string, return a new string where "not " has been added to the front.
3+
However, if the string already begins with "not", return the string unchanged.
4+
"""
5+
def not_string(str):
6+
if str.startswith('not'):
7+
return str
8+
else:
9+
return 'not '+str

Warmup-1/parrot_trouble.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
"""
2+
We have a loud talking parrot. The "hour" parameter is the current hour time in the range 0..23.
3+
We are in trouble if the parrot is talking and the hour is before 7 or after 20.
4+
Return True if we are in trouble.
5+
"""
6+
def parrot_trouble(talking, hour):
7+
if talking == True and (hour < 7 or hour > 20):
8+
return True
9+
else:
10+
return False

Warmup-1/pos_neg.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
"""
2+
Given 2 int values, return True if one is negative and one is positive.
3+
Except if the parameter "negative" is True, then return True only if both are negative.
4+
"""
5+
def pos_neg(a, b, negative):
6+
if a > 0 and b < 0:
7+
if negative == False:
8+
return True
9+
if negative == True:
10+
return False
11+
if a < 0 and b > 0:
12+
if negative == True:
13+
return False
14+
if negative == False:
15+
return True
16+
if a < 0 and b < 0 and negative == True:
17+
return True
18+
else:
19+
return False

Warmup-1/sleep_in.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
"""
2+
The parameter weekday is True if it is a weekday, and the parameter vacation is True if we are on vacation.
3+
We sleep in if it is not a weekday or we're on vacation. Return True if we sleep in.
4+
"""
5+
def sleep_in(weekday, vacation):
6+
if weekday == True and vacation != True:
7+
return False
8+
if weekday != True and vacation == True:
9+
return True
10+
if weekday == True and vacation == True:
11+
return True
12+
if weekday == False and vacation == False:
13+
return True

Warmup-1/sum_double.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
def sum_double(a, b):
2+
if a !=b:
3+
return a+b
4+
else:
5+
return 2*(a+b)

0 commit comments

Comments
 (0)