File tree 12 files changed +228
-0
lines changed
12 files changed +228
-0
lines changed Original file line number Diff line number Diff line change
1
+ # Given an int n, return the absolute difference between n and 21,
2
+ # except return double the absolute difference if n is over 21.
3
+
4
+
5
+ # diff21(19) → 2
6
+ # diff21(10) → 11
7
+ # diff21(21) → 0
8
+
9
+ def diff21 (n ):
10
+ d = 21 - n
11
+ if n > 21 :
12
+ return abs (d )* 2
13
+ else :
14
+ return abs (d )
15
+
16
+
17
+ # Official Solution:
18
+ # def diff21(n):
19
+ # if n <= 21:
20
+ # return 21 - n
21
+ # else:
22
+ # return (n - 21) * 2
Original file line number Diff line number Diff line change
1
+ # Given a string, we'll say that the front is the first 3 chars of the string.
2
+ # If the string length is less than 3, the front is whatever is there.
3
+ # Return a new string which is 3 copies of the front.
4
+
5
+
6
+ # front3('Java') → 'JavJavJav'
7
+ # front3('Chocolate') → 'ChoChoCho'
8
+ # front3('abc') → 'abcabcabc'
9
+
10
+ def front3 (str ):
11
+ if len (str ) <= 3 :
12
+ return str * 3
13
+ else :
14
+ return str [:3 ] * 3
15
+
16
+
17
+ # Official Solution:
18
+ # def front3(str):
19
+ # # Figure the end of the front
20
+ # front_end = 3
21
+ # if len(str) < front_end:
22
+ # front_end = len(str)
23
+ # front = str[:front_end]
24
+ # return front + front + front
25
+
26
+ # # Could omit the if logic, and write simply front = str[:3]
27
+ # # since the slice is silent about out-of-bounds conditions.
Original file line number Diff line number Diff line change
1
+ # Given a string, return a new string where the first and last chars have been exchanged.
2
+
3
+
4
+ # front_back('code') → 'eodc'
5
+ # front_back('a') → 'a'
6
+ # front_back('ab') → 'ba'
7
+
8
+ def front_back (str ):
9
+ if len (str ) <= 1 :
10
+ return str
11
+ middle = str [1 : len (str )- 1 ]
12
+ return str [- 1 ] + middle + str [0 ]
Original file line number Diff line number Diff line change
1
+ # Given 2 ints, a and b, return True if one if them is 10 or if their sum is 10.
2
+
3
+
4
+ # makes10(9, 10) → True
5
+ # makes10(9, 9) → False
6
+ # makes10(1, 9) → True
7
+
8
+ def makes10 (a , b ):
9
+ return a == 10 or b == 10 or a + b == 10
10
+
Original file line number Diff line number Diff line change
1
+ # Given a non-empty string and an int n,
2
+ # 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
+
7
+ # missing_char('kitten', 1) → 'ktten'
8
+ # missing_char('kitten', 0) → 'itten'
9
+ # missing_char('kitten', 4) → 'kittn'
10
+
11
+ def missing_char (str , n ):
12
+ part1 = str [:n ]
13
+ part2 = str [n + 1 :]
14
+
15
+ return part1 + part2
Original file line number Diff line number Diff line change
1
+ # We have two monkeys, a and b, and the parameters a_smile and b_smile indicate if each is smiling.
2
+ # We are in trouble if they are both smiling or if neither of them is smiling.
3
+ # Return True if we are in trouble.
4
+
5
+ # monkey_trouble(True, True) → True
6
+ # monkey_trouble(False, False) → True
7
+ # monkey_trouble(True, False) → False
8
+
9
+ def monkey_trouble (a_smile , b_smile ):
10
+ if a_smile and b_smile is True :
11
+ return True
12
+ elif not a_smile and not b_smile is True :
13
+ return True
14
+ else :
15
+ return False
16
+
17
+
18
+ # def monkey_trouble(a_smile, b_smile):
19
+ # if a_smile and b_smile:
20
+ # return True
21
+ # if not a_smile and not b_smile:
22
+ # return True
23
+ # return False
24
+ # ## The above can be shortened to:
25
+ # ## return ((a_smile and b_smile) or (not a_smile and not b_smile))
26
+ # ## Or this very short version (think about how this is the same as the above)
27
+ # ## return (a_smile == b_smile)
Original file line number Diff line number Diff line change
1
+ # Given an int n, return True if it is within 10 of 100 or 200.
2
+ # Note: abs(num) computes the absolute value of a number.
3
+
4
+
5
+ # near_hundred(93) → True
6
+ # near_hundred(90) → True
7
+ # near_hundred(89) → False
8
+
9
+ def near_hundred (n ):
10
+ return 90 <= n <= 110 or 190 <= n <= 210
Original file line number Diff line number Diff line change
1
+ # Given a string, return a new string where "not " has been added to the front.
2
+ # However, if the string already begins with "not", return the string unchanged.
3
+
4
+
5
+ # not_string('candy') → 'not candy'
6
+ # not_string('x') → 'not x'
7
+ # not_string('not bad') → 'not bad'
8
+
9
+ def not_string (str ):
10
+ if not str .startswith ("not" ):
11
+ return "not " + str
12
+ else :
13
+ return str
14
+
15
+
16
+ # Official Solution:
17
+ # def not_string(str):
18
+ # if len(str) >= 3 and str[:3] == "not":
19
+ # return str
20
+ # return "not " + str
21
+ # # str[:3] goes from the start of the string up to but not
22
+ # # including index 3
Original file line number Diff line number Diff line change
1
+ # We have a loud talking parrot.
2
+ # 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
+
7
+ # parrot_trouble(True, 6) → True
8
+ # parrot_trouble(True, 7) → False
9
+ # parrot_trouble(False, 6) → False
10
+
11
+ def parrot_trouble (talking , hour ):
12
+ return (talking and (hour < 7 or hour > 20 ))
Original file line number Diff line number Diff line change
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
+
6
+ # pos_neg(1, -1, False) → True
7
+ # pos_neg(-1, 1, False) → True
8
+ # pos_neg(-4, -5, True) → True
9
+
10
+ def pos_neg (a , b , negative ):
11
+ if negative is True and not (a < 0 and b < 0 ):
12
+ return False
13
+ elif negative is True and (a < 0 and b < 0 ):
14
+ return True
15
+ else :
16
+ return (a < 0 and b > 0 ) or (a > 0 and b < 0 )
17
+
18
+
19
+ # Official Solution:
20
+ # def pos_neg(a, b, negative):
21
+ # if negative:
22
+ # return (a < 0 and b < 0)
23
+ # else:
24
+ # return ((a < 0 and b > 0) or (a > 0 and b < 0))
Original file line number Diff line number Diff line change
1
+ # The parameter weekday is True if it is a weekday, and the parameter vacation is True if we are on vacation.
2
+ # We sleep in if it is not a weekday or we're on vacation.
3
+ # Return True if we sleep in.
4
+
5
+ # sleep_in(False, False) → True
6
+ # sleep_in(True, False) → False
7
+ # sleep_in(False, True) → True
8
+
9
+ def sleep_in (weekday , vacation ):
10
+ if vacation == True :
11
+ return True
12
+ else :
13
+ if weekday == True :
14
+ return False
15
+ else :
16
+ return True
17
+
18
+ # def sleep_in(weekday, vacation):
19
+ # if not weekday or vacation:
20
+ # return True
21
+ # else:
22
+ # return False
23
+ # # This can be shortened to: return(not weekday or vacation)
Original file line number Diff line number Diff line change
1
+ # Given two int values, return their sum.
2
+ # Unless the two values are the same, then return double their sum.
3
+
4
+
5
+ # sum_double(1, 2) → 3
6
+ # sum_double(3, 2) → 5
7
+ # sum_double(2, 2) → 8
8
+
9
+ def sum_double (a , b ):
10
+ if a == b :
11
+ return (a + b )* 2
12
+ else :
13
+ return a + b
14
+
15
+
16
+ # Official Solution:
17
+ # def sum_double(a, b):
18
+ # # Store the sum in a local variable
19
+ # sum = a + b
20
+
21
+ # # Double it if a and b are the same
22
+ # if a == b:
23
+ # sum = sum * 2
24
+ # return sum
You can’t perform that action at this time.
0 commit comments