Skip to content

Commit b20f12b

Browse files
committed
add solutions to warmup1,2,string1,list1
1 parent 0012506 commit b20f12b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+375
-9
lines changed

python/list-1/common_end.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
"""
2+
Given 2 arrays of ints, a and b, return True if they have the same first element
3+
or they have the same last element. Both arrays will be length 1 or more.
4+
"""
5+
6+
def common_end(a, b):
7+
return a[0] == b[0] or a[-1] == b[-1]

python/list-1/first_last6.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
"""
2+
Given an array of ints, return True if 6 appears as either the first
3+
or last element in the array. The array will be length 1 or more.
4+
"""
5+
6+
def first_last6(nums):
7+
return 6 == nums[0] or 6 == nums[-1]

python/list-1/has23.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
"""
2+
Given an int array length 2, return True if it contains a 2 or a 3.
3+
"""
4+
5+
def has23(nums):
6+
return 2 in nums or 3 in nums

python/list-1/make_ends.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
"""
2+
Given an array of ints, return a new array length 2 containing the
3+
first and last elements from the original array. The original
4+
array will be length 1 or more.
5+
"""
6+
7+
def make_ends(nums):
8+
return [nums[0], nums[-1]]

python/list-1/make_pi.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
"""
2+
Return an int array length 3 containing the first 3 digits of pi, {3, 1, 4}.
3+
"""
4+
5+
def make_pi():
6+
return [3,1,4]

python/list-1/max_end3.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
"""
2+
Given an array of ints length 3, figure out which is larger, the
3+
first or last element in the array, and set all the other elements
4+
to be that value. Return the changed array.
5+
"""
6+
7+
def max_end3(nums):
8+
return [nums[-1]] * 3 if nums[-1] > nums[0] else [nums[0]] * 3

python/list-1/middle_way.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
"""
2+
Given 2 int arrays, a and b, each length 3, return a new
3+
array length 2 containing their middle elements.
4+
"""
5+
6+
def middle_way(a, b):
7+
return [a[len(a)/2], b[len(b)/2]]

python/list-1/reverse3.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
"""
2+
Given an array of ints length 3, return a new array with the elements
3+
in reverse order, so {1, 2, 3} becomes {3, 2, 1}.
4+
"""
5+
6+
def reverse3(nums):
7+
return nums[::-1]

python/list-1/rotate_left3.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
"""
2+
Given an array of ints length 3, return an array with the elements
3+
"rotated left" so {1, 2, 3} yields {2, 3, 1}.
4+
"""
5+
6+
def rotate_left3(nums):
7+
return nums[1:] + nums[0:1]

python/list-1/same_first_last.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
"""
2+
Given an array of ints, return True if the array is length 1 or
3+
more, and the first element and the last element are equal.
4+
"""
5+
6+
def same_first_last(nums):
7+
return len(nums) >= 1 and nums[0] == nums[-1]

python/list-1/sum2.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
"""
2+
Given an array of ints, return the sum of the first 2 elements in
3+
the array. If the array length is less than 2, just sum up the elements
4+
that exist, returning 0 if the array is length 0.
5+
"""
6+
7+
def sum2(nums):
8+
return sum(nums[0:2]) if len(nums) > 2 else sum(nums)

python/list-1/sum3.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
"""
2+
Given an array of ints length 3, return the sum of all the elements.
3+
"""
4+
5+
def sum3(nums):
6+
return sum(nums)

python/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
4+
40 and 60, inclusive. Unless it is the weekend, in which case there
5+
is no upper bound on the number of cigars. Return True if the party
6+
with the given values is successful, or False otherwise.
7+
"""
8+
9+
def cigar_party(cigars, is_weekend):
10+
if is_weekend:
11+
return cigars >= 40
12+
return 60 >= cigars >= 40

python/logic-1/date_fashion.py

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

python/logic-2/make_chocolate.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,12 @@
66

77
def make_chocolate(small, big, goal):
88

9-
# check if enough chocolate in the first place
9+
# check if enough chocolate in the first place
10+
if goal > big * 5 + small or goal % 5 > small:
11+
return -1
1012

11-
if goal > big * 5 + small or goal % 5 > small:
12-
return -1
13+
# if enough, figure out how many big is needed then small
14+
elif goal // 5 > big:
15+
return goal - big * 5
1316

14-
# if enough, figure out how many big is needed then small
15-
16-
elif goal // 5 > big:
17-
return goal - big * 5
18-
19-
return goal - (goal // 5) * 5
17+
return goal - (goal // 5) * 5

python/string-1/combo_string.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
"""
2+
Given 2 strings, a and b, return a string of the form short+long+short,
3+
with the shorter string on the outside and the longer string on the inside.
4+
The strings will not be the same length, but they may be empty (length 0).
5+
"""
6+
7+
def combo_string(a, b):
8+
return "{}{}{}".format(a,b,a) if len(a) < len(b) else "{}{}{}".format(b,a,b)

python/string-1/extra_end.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
"""
2+
Given a string, return a new string made of 3 copies of the last 2
3+
chars of the original string. The string length will be at least 2.
4+
"""
5+
6+
def extra_end(str):
7+
return str[-2:] * 3

python/string-1/first_half.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
"""
2+
Given a string of even length, return the first half.
3+
So the string "WooHoo" yields "Woo".
4+
"""
5+
6+
def first_half(str):
7+
return str[:len(str) / 2]

python/string-1/first_two.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 the string made of its first two chars,
3+
so the String "Hello" yields "He". If the string is shorter
4+
than length 2, return whatever there is, so "X" yields "X",
5+
and the empty string "" yields the empty string "".
6+
"""
7+
8+
def first_two(str):
9+
return str if len(str) < 2 else str[:2]

python/string-1/hello_name.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
"""
2+
Given a string name, e.g. "Bob", return a greeting of the form "Hello Bob!".
3+
"""
4+
5+
def hello_name(name):
6+
return "Hello {}!".format(name)

python/string-1/left2.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
"""
2+
Given a string, return a "rotated left 2" version where the first 2
3+
chars are moved to the end. The string length will be at least 2.
4+
"""
5+
6+
def left2(str):
7+
return str[2:] + str[:2]

python/string-1/make_abba.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
"""
2+
Given two strings, a and b, return the result of putting them
3+
together in the order abba, e.g. "Hi" and "Bye" returns "HiByeByeHi".
4+
"""
5+
6+
def make_abba(a, b):
7+
return "{}{}{}{}".format(a,b,b,a)

python/string-1/make_out_word.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
"""
2+
Given an "out" string length 4, such as "<<>>", and
3+
a word, return a new string where the word is in
4+
the middle of the out string, e.g. "<<word>>".
5+
"""
6+
7+
def make_out_word(out, word):
8+
return out[:len(out) / 2] + word + out[len(out) / 2:]

python/string-1/make_tags.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
"""
2+
The web is built with HTML strings like "<i>Yay</i>" which draws
3+
Yay as italic text. In this example, the "i" tag makes <i> and
4+
</i> which surround the word "Yay". Given tag and word strings,
5+
create the HTML string with tags around the word, e.g. "<i>Yay</i>".
6+
"""
7+
8+
def make_tags(tag, word):
9+
return "<{}>{}</{}>".format(tag,word,tag)

python/string-1/non_start.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
"""
2+
Given 2 strings, return their concatenation, except omit
3+
the first char of each. The strings will be at least length 1.
4+
"""
5+
6+
def non_start(a, b):
7+
return a[1:] + b[1:]

python/string-1/without_end.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
"""
2+
Given a string, return a version without the first and last char,
3+
so "Hello" yields "ell". The string length will be at least 2.
4+
"""
5+
6+
def without_end(str):
7+
return str[1:-1]

python/warmup-1/diff21.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
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+
6+
def diff21(n):
7+
return 21 - n if n <= 21 else 2 * (n - 21)

python/warmup-1/front3.py

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

python/warmup-1/front_back.py

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

python/warmup-1/makes10.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
"""
2+
Given 2 ints, a and b, return True if one if them is 10 or if their sum is 10.
3+
"""
4+
5+
def makes10(a, b):
6+
return a == 10 or b == 10 or a + b == 10

python/warmup-1/missing_char.py

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

python/warmup-1/monkey_trouble.py

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

python/warmup-1/near_hundred.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
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+
6+
def near_hundred(n):
7+
return abs(n - 100) <= 10 or abs(n - 200) <= 10

python/warmup-1/not_string.py

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

python/warmup-1/parrot_trouble.py

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

python/warmup-1/pos_neg.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
"""
2+
Given 2 int values, return True if one is negative and one
3+
is positive. Except if the parameter "negative" is True,
4+
then return True only if both are negative.
5+
"""
6+
7+
def pos_neg(a, b, negative):
8+
return a < 0 and b < 0 if negative else a * b < 0

python/warmup-1/sleep_in.py

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

python/warmup-1/sum_double.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
"""
2+
Given two int values, return their sum. Unless the two
3+
values are the same, then return double their sum.
4+
"""
5+
6+
def sum_double(a, b):
7+
return 4 * a if a == b else a + b

python/warmup-2/array123.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
"""
2+
Given an array of ints, return True if the sequence of numbers 1, 2, 3 appears in the array somewhere.
3+
"""
4+
5+
def array123(nums):
6+
for i in range(len(nums)-2):
7+
if nums[i] == 1 and nums[i+1] == 2 and nums[i+2] == 3:
8+
return True
9+
return False

python/warmup-2/array_count9.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
"""
2+
Given an array of ints, return the number of 9's in the array.
3+
"""
4+
5+
def array_count9(nums):
6+
return nums.count(9)

python/warmup-2/array_front9.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
"""
2+
Given an array of ints, return True if one of the first 4 elements
3+
in the array is a 9. The array length may be less than 4.
4+
"""
5+
6+
def array_front9(nums):
7+
return 9 in nums[0:4]

python/warmup-2/front_times.py

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

python/warmup-2/last2.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
"""
2+
Given a string, return the count of the number of times
3+
that a substring length 2 appears in the string and also
4+
as the last 2 chars of the string, so "hixxxhi" yields 1
5+
(we won't count the end substring).
6+
"""
7+
8+
def last2(str):
9+
if len(str) < 3:
10+
return 0
11+
12+
count = 0
13+
for i in range(len(str)-2):
14+
if str[i:i+2] == str[-2:]:
15+
count +=1
16+
return count

0 commit comments

Comments
 (0)