diff --git a/codingbat_exercise_scraper.py b/codingbat_exercise_scraper.py new file mode 100644 index 0000000..7cf8569 --- /dev/null +++ b/codingbat_exercise_scraper.py @@ -0,0 +1,79 @@ +""" +This script gets all the exercises in codingbat and creates a base file in the appropriate folder to edit. + +You need to install requests and bs4. + +You can do the following to install: +pip install bs4 +pip install requests + +Author: Ha Min Ko +""" + +import requests, bs4 +import os +from os.path import exists + +urlbase = 'https://codingbat.com' +url = urlbase + '/python' + +cwd = os.getcwd() +res = requests.get(url) +res.raise_for_status() + +html = bs4.BeautifulSoup(res.text, features='html.parser') + +# We get the link for each main page. +for link in html.select('div[class=summ] > a'): + name = link.text.lower() + folder = cwd + '\\' + name + os.chdir(folder) + + url1 = urlbase + link['href'] + + res1 = requests.get(url1) + html1 = bs4.BeautifulSoup(res1.text, features='html.parser') + + # We get the link for each exercise. + for link1 in html1.select('td[width="200"] > a'): + # print(link1.text) + name1 = link1.text + + url2 = urlbase + link1['href'] + res2 = requests.get(url2) + + html2 = bs4.BeautifulSoup(res2.text, features='html.parser') + # print(html2) + + title = name1 + '.py' + + # Check if file already exists. + if (exists(title)): + print(title, 'exists already.') + else: + function_definer = html2.select('div[id=ace_div]')[0].text.strip() + function_description = html2.select('p[class=max2]')[0].text.strip() + function_test = [] + for br in html2.find_all('br'): + following = br.nextSibling + if name1 in following: + function_test.append(following) + + # Create a file that can be written to. + print('Creating file', title, 'in folder', name ) + outputfile = open(title, 'w') + + # Write data to the file: + outputfile.write(function_definer) + outputfile.write('\n\t"""') + outputfile.write('\n\t' + function_description) + outputfile.write('\n\t"""') + outputfile.write('\n\tpass') + outputfile.write('\n') + for test in function_test: + test = test.split('→')[0].strip() + outputfile.write('\nprint(' + test + ')') + outputfile.write('\n') + + # Close the file after writing to it + outputfile.close() diff --git a/list-1/common_end.py b/list-1/common_end.py new file mode 100644 index 0000000..6d85484 --- /dev/null +++ b/list-1/common_end.py @@ -0,0 +1,9 @@ +def common_end(a, b): + """ + Given 2 arrays of ints, a and b, return True if they have the same first element or they have the same last element. Both arrays will be length 1 or more. + """ + return a[-1] == b[-1] or a[0] == b[0] + +print(common_end([1, 2, 3], [7, 3])) +print(common_end([1, 2, 3], [7, 3, 2])) +print(common_end([1, 2, 3], [1, 3])) diff --git a/list-1/has23.py b/list-1/has23.py new file mode 100644 index 0000000..c79a3dc --- /dev/null +++ b/list-1/has23.py @@ -0,0 +1,9 @@ +def has23(nums): + """ + Given an int array length 2, return True if it contains a 2 or a 3. + """ + return 2 in nums or 3 in nums + +print(has23([2, 5])) +print(has23([4, 3])) +print(has23([4, 5])) diff --git a/list-1/make_ends.py b/list-1/make_ends.py new file mode 100644 index 0000000..fbb08d8 --- /dev/null +++ b/list-1/make_ends.py @@ -0,0 +1,9 @@ +def make_ends(nums): + """ + Given an array of ints, return a new array length 2 containing the first and last elements from the original array. The original array will be length 1 or more. + """ + return [nums[0], nums[-1]] + +print(make_ends([1, 2, 3])) +print(make_ends([1, 2, 3, 4])) +print(make_ends([7, 4, 6, 2])) diff --git a/list-1/make_pi.py b/list-1/make_pi.py new file mode 100644 index 0000000..f59b128 --- /dev/null +++ b/list-1/make_pi.py @@ -0,0 +1,7 @@ +def make_pi(): + """ + Return an int array length 3 containing the first 3 digits of pi, {3, 1, 4}. + """ + return [3, 1, 4] + +print(make_pi()) diff --git a/list-1/max_end3.py b/list-1/max_end3.py new file mode 100644 index 0000000..aa6b865 --- /dev/null +++ b/list-1/max_end3.py @@ -0,0 +1,9 @@ +def max_end3(nums): + """ + Given an array of ints length 3, figure out which is larger, the first or last element in the array, and set all the other elements to be that value. Return the changed array. + """ + return [max(nums[0], nums[-1]) for x in nums] + +print(max_end3([1, 2, 3])) +print(max_end3([11, 5, 9])) +print(max_end3([2, 11, 3])) diff --git a/list-1/middle_way.py b/list-1/middle_way.py new file mode 100644 index 0000000..67e5a0d --- /dev/null +++ b/list-1/middle_way.py @@ -0,0 +1,9 @@ +def middle_way(a, b): + """ + Given 2 int arrays, a and b, each length 3, return a new array length 2 containing their middle elements. + """ + return [a[1], b[1]] + +print(middle_way([1, 2, 3], [4, 5, 6])) +print(middle_way([7, 7, 7], [3, 8, 0])) +print(middle_way([5, 2, 9], [1, 4, 5])) diff --git a/list-1/reverse3.py b/list-1/reverse3.py new file mode 100644 index 0000000..5b56645 --- /dev/null +++ b/list-1/reverse3.py @@ -0,0 +1,9 @@ +def reverse3(nums): + """ + Given an array of ints length 3, return a new array with the elements in reverse order, so {1, 2, 3} becomes {3, 2, 1}. + """ + return nums[::-1] + +print(reverse3([1, 2, 3])) +print(reverse3([5, 11, 9])) +print(reverse3([7, 0, 0])) \ No newline at end of file diff --git a/list-1/rotate_left3.py b/list-1/rotate_left3.py new file mode 100644 index 0000000..77fc596 --- /dev/null +++ b/list-1/rotate_left3.py @@ -0,0 +1,9 @@ +def rotate_left3(nums): + """ + Given an array of ints length 3, return an array with the elements "rotated left" so {1, 2, 3} yields {2, 3, 1}. + """ + return nums[1:] + nums[:1] + +print(rotate_left3([1, 2, 3])) +print(rotate_left3([5, 11, 9])) +print(rotate_left3([7, 0, 0])) diff --git a/list-1/same_first_last.py b/list-1/same_first_last.py new file mode 100644 index 0000000..5bc04cd --- /dev/null +++ b/list-1/same_first_last.py @@ -0,0 +1,9 @@ +def same_first_last(nums): + """ + Given an array of ints, return True if the array is length 1 or more, and the first element and the last element are equal. + """ + return len(nums) >= 1 and nums[0] == nums[-1] + +print(same_first_last([1, 2, 3])) +print(same_first_last([1, 2, 3, 1])) +print(same_first_last([1, 2, 1])) diff --git a/list-1/sum2.py b/list-1/sum2.py new file mode 100644 index 0000000..61b4e86 --- /dev/null +++ b/list-1/sum2.py @@ -0,0 +1,9 @@ +def sum2(nums): + """ + Given an array of ints, return the sum of the first 2 elements in the array. If the array length is less than 2, just sum up the elements that exist, returning 0 if the array is length 0. + """ + return sum(nums[0:2]) + +print(sum2([1, 2, 3])) +print(sum2([1, 1])) +print(sum2([1, 1, 1, 1])) diff --git a/list-1/sum3.py b/list-1/sum3.py new file mode 100644 index 0000000..603b9b3 --- /dev/null +++ b/list-1/sum3.py @@ -0,0 +1,9 @@ +def sum3(nums): + """ + Given an array of ints length 3, return the sum of all the elements. + """ + return sum(nums) + +print(sum3([1, 2, 3])) +print(sum3([5, 11, 2])) +print(sum3([7, 0, 0])) diff --git a/list-2/big_diff.py b/list-2/big_diff.py new file mode 100644 index 0000000..765843e --- /dev/null +++ b/list-2/big_diff.py @@ -0,0 +1,9 @@ +def big_diff(nums): + """ + Given an array length 1 or more of ints, return the difference between the largest and smallest values in the array. Note: the built-in min(v1, v2) and max(v1, v2) functions return the smaller or larger of two values. + """ + return max(nums) - min(nums) + +print(big_diff([10, 3, 5, 6])) +print(big_diff([7, 2, 10, 9])) +print(big_diff([2, 10, 7, 2])) diff --git a/list-2/centered_average.py b/list-2/centered_average.py new file mode 100644 index 0000000..0645a38 --- /dev/null +++ b/list-2/centered_average.py @@ -0,0 +1,10 @@ +def centered_average(nums): + """ + Return the "centered" average of an array of ints, which we'll say is the mean average of the values, except ignoring the largest and smallest values in the array. If there are multiple copies of the smallest value, ignore just one copy, and likewise for the largest value. Use int division to produce the final average. You may assume that the array is length 3 or more. + """ + sort_nums = sorted(nums) + return sum(sort_nums[1:-1])/len(sort_nums[1:-1]) + +print(centered_average([1, 2, 3, 4, 100])) +print(centered_average([1, 1, 5, 5, 10, 8, 7])) +print(centered_average([-10, -4, -2, -4, -2, 0])) diff --git a/list-2/has22.py b/list-2/has22.py new file mode 100644 index 0000000..7b80a32 --- /dev/null +++ b/list-2/has22.py @@ -0,0 +1,12 @@ +def has22(nums): + """ + Given an array of ints, return True if the array contains a 2 next to a 2 somewhere. + """ + for i in range(len(nums) - 1): + if nums[i] == 2 and nums[i+1] == 2: + return True + return False + +print(has22([1, 2, 2])) +print(has22([1, 2, 1, 2])) +print(has22([2, 1, 2])) diff --git a/list-2/sum13.py b/list-2/sum13.py new file mode 100644 index 0000000..bef462d --- /dev/null +++ b/list-2/sum13.py @@ -0,0 +1,19 @@ +def sum13(nums): + """ + Return the sum of the numbers in the array, returning 0 for an empty array. Except the number 13 is very unlucky, so it does not count and numbers that come immediately after a 13 also do not count. + """ + total = 0 + is_after_13 = 0 + for i in range(len(nums)): + if is_after_13 == 0 and nums[i] != 13: + total += nums[i] + elif nums[i] == 13: + is_after_13 = 1 + else: + is_after_13 = 0 + return total + + +print(sum13([1, 2, 2, 1])) +print(sum13([1, 1])) +print(sum13([1, 2, 2, 1, 13])) diff --git a/list-2/sum67.py b/list-2/sum67.py new file mode 100644 index 0000000..a8b7862 --- /dev/null +++ b/list-2/sum67.py @@ -0,0 +1,18 @@ +def sum67(nums): + """ + Return the sum of the numbers in the array, except ignore sections of numbers starting with a 6 and extending to the next 7 (every 6 will be followed by at least one 7). Return 0 for no numbers. + """ + total = 0 + is_after_6 = 0 + for i in range(len(nums)): + if nums[i] == 6: + is_after_6 = 1 + elif nums[i] == 7 and is_after_6 == 1: + is_after_6 = 0 + elif is_after_6 != 1: + total += nums[i] + return total + +print(sum67([1, 2, 2])) +print(sum67([1, 2, 2, 6, 99, 99, 7])) +print(sum67([1, 1, 6, 7, 2])) diff --git a/logic-1/alarm_clock.py b/logic-1/alarm_clock.py new file mode 100644 index 0000000..e7e8936 --- /dev/null +++ b/logic-1/alarm_clock.py @@ -0,0 +1,16 @@ +def alarm_clock(day, vacation): + """ + Given a day of the week encoded as 0=Sun, 1=Mon, 2=Tue, ...6=Sat, and a boolean indicating if we are on vacation, return a string of the form "7:00" indicating when the alarm clock should ring. Weekdays, the alarm should be "7:00" and on the weekend it should be "10:00". Unless we are on vacation -- then on weekdays it should be "10:00" and weekends it should be "off". + """ + if day in [1,2,3,4,5]: + if vacation: + return '10:00' + return '7:00' + else: + if vacation: + return 'off' + return '10:00' + +print(alarm_clock(1, False)) +print(alarm_clock(5, False)) +print(alarm_clock(0, False)) diff --git a/logic-1/caught_speeding.py b/logic-1/caught_speeding.py new file mode 100644 index 0000000..f8d874f --- /dev/null +++ b/logic-1/caught_speeding.py @@ -0,0 +1,22 @@ +def caught_speeding(speed, is_birthday): + """ + You are driving a little too fast, and a police officer stops you. Write code to compute the result, encoded as an int value: 0=no ticket, 1=small ticket, 2=big ticket. If speed is 60 or less, the result is 0. If speed is between 61 and 80 inclusive, the result is 1. If speed is 81 or more, the result is 2. Unless it is your birthday -- on that day, your speed can be 5 higher in all cases. + """ + if not is_birthday: + if speed <= 60: + return 0 + elif speed <= 80: + return 1 + else: + return 2 + else: + if speed <= 65: + return 0 + elif speed <= 85: + return 1 + else: + return 2 + +print(caught_speeding(60, False)) +print(caught_speeding(65, False)) +print(caught_speeding(65, True)) diff --git a/logic-1/date_fashion.py b/logic-1/date_fashion.py new file mode 100644 index 0000000..22e5e2b --- /dev/null +++ b/logic-1/date_fashion.py @@ -0,0 +1,14 @@ +def date_fashion(you, date): + """ + You and your date are trying to get a table at a restaurant. The parameter "you" is the stylishness of your clothes, in the range 0..10, and "date" is the stylishness of your date's clothes. The result getting the table is encoded as an int value with 0=no, 1=maybe, 2=yes. If either of you is very stylish, 8 or more, then the result is 2 (yes). With the exception that if either of you has style of 2 or less, then the result is 0 (no). Otherwise the result is 1 (maybe). + """ + if you <=2 or date <= 2: + return 0 + elif you >= 8 or date >= 8: + return 2 + else: + return 1 + +print(date_fashion(5, 10)) +print(date_fashion(5, 2)) +print(date_fashion(5, 5)) diff --git a/logic-1/in1to10.py b/logic-1/in1to10.py new file mode 100644 index 0000000..549b1a1 --- /dev/null +++ b/logic-1/in1to10.py @@ -0,0 +1,9 @@ +def in1to10(n, outside_mode): + """ + Given a number n, return True if n is in the range 1..10, inclusive. Unless outside_mode is True, in which case return True if the number is less or equal to 1, or greater or equal to 10. + """ + return (n in range(1, 11)) is not outside_mode or (n not in range(2, 10)) is outside_mode + +print(in1to10(5, False)) +print(in1to10(11, False)) +print(in1to10(11, True)) diff --git a/logic-1/love6.py b/logic-1/love6.py new file mode 100644 index 0000000..e4cd553 --- /dev/null +++ b/logic-1/love6.py @@ -0,0 +1,9 @@ +def love6(a, b): + """ + The number 6 is a truly great number. Given two int values, a and b, return True if either one is 6. Or if their sum or difference is 6. Note: the function abs(num) computes the absolute value of a number. + """ + return a == 6 or b == 6 or a+b == 6 or abs(a-b) == 6 + +print(love6(6, 4)) +print(love6(4, 5)) +print(love6(1, 5)) diff --git a/logic-1/near_ten.py b/logic-1/near_ten.py new file mode 100644 index 0000000..2bb427a --- /dev/null +++ b/logic-1/near_ten.py @@ -0,0 +1,9 @@ +def near_ten(num): + """ + Given a non-negative number "num", return True if num is within 2 of a multiple of 10. Note: (a % b) is the remainder of dividing a by b, so (7 % 5) is 2. See also: Introduction to Mod + """ + return num%10 <= 2 or num%10 >= 8 + +print(near_ten(12)) +print(near_ten(17)) +print(near_ten(19)) diff --git a/logic-1/sorta_sum.py b/logic-1/sorta_sum.py new file mode 100644 index 0000000..96e42d5 --- /dev/null +++ b/logic-1/sorta_sum.py @@ -0,0 +1,12 @@ +def sorta_sum(a, b): + """ + Given 2 ints, a and b, return their sum. However, sums in the range 10..19 inclusive, are forbidden, so in that case just return 20. + """ + if a + b in range(10, 20): + return 20 + else: + return a + b + +print(sorta_sum(3, 4)) +print(sorta_sum(9, 4)) +print(sorta_sum(10, 11)) diff --git a/logic-1/squirrel_play.py b/logic-1/squirrel_play.py new file mode 100644 index 0000000..bab3d4b --- /dev/null +++ b/logic-1/squirrel_play.py @@ -0,0 +1,9 @@ +def squirrel_play(temp, is_summer): + """ + The squirrels in Palo Alto spend most of the day playing. In particular, they play if the temperature is between 60 and 90 (inclusive). Unless it is summer, then the upper limit is 100 instead of 90. Given an int temperature and a boolean is_summer, return True if the squirrels play and False otherwise. + """ + return temp in range (60, 91) or ((temp in range(60, 101)) and is_summer) + +print(squirrel_play(70, False)) +print(squirrel_play(95, False)) +print(squirrel_play(95, True)) diff --git a/logic-2/close_far.py b/logic-2/close_far.py new file mode 100644 index 0000000..3a9a0a4 --- /dev/null +++ b/logic-2/close_far.py @@ -0,0 +1,9 @@ +def close_far(a, b, c): + """ + Given three ints, a b c, return True if one of b or c is "close" (differing from a by at most 1), while the other is "far", differing from both other values by 2 or more. Note: abs(num) computes the absolute value of a number. + """ + return (abs(a - b) <= 1 and abs(a - c) >= 2 or abs(a - c) <= 1 and abs(a - b) >= 2) and abs(b - c) >= 2 + +print(close_far(1, 2, 10)) +print(close_far(1, 2, 3)) +print(close_far(4, 1, 3)) diff --git a/logic-2/lucky_sum.py b/logic-2/lucky_sum.py new file mode 100644 index 0000000..080472c --- /dev/null +++ b/logic-2/lucky_sum.py @@ -0,0 +1,14 @@ +def lucky_sum(a, b, c): + """ + Given 3 int values, a b c, return their sum. However, if one of the values is 13 then it does not count towards the sum and values to its right do not count. So for example, if b is 13, then both b and c do not count. + """ + val = 0 + for num in [a, b, c]: + if num == 13: + break + val += num + return val + +print(lucky_sum(1, 2, 3)) +print(lucky_sum(1, 2, 13)) +print(lucky_sum(1, 13, 3)) diff --git a/logic-2/make_bricks.py b/logic-2/make_bricks.py new file mode 100644 index 0000000..88295d8 --- /dev/null +++ b/logic-2/make_bricks.py @@ -0,0 +1,9 @@ +def make_bricks(small, big, goal): + """ + We want to make a row of bricks that is goal inches long. We have a number of small bricks (1 inch each) and big bricks (5 inches each). Return True if it is possible to make the goal by choosing from the given bricks. This is a little harder than it looks and can be done without any loops. See also: Introduction to MakeBricks + """ + return small*1 + big*5 >= goal and goal%5 <= small + +print(make_bricks(3, 1, 8)) +print(make_bricks(3, 1, 9)) +print(make_bricks(3, 2, 10)) diff --git a/logic-2/make_chocolate.py b/logic-2/make_chocolate.py new file mode 100644 index 0000000..17b2b90 --- /dev/null +++ b/logic-2/make_chocolate.py @@ -0,0 +1,15 @@ +def make_chocolate(small, big, goal): + """ + We want make a package of goal kilos of chocolate. We have small bars (1 kilo each) and big bars (5 kilos each). Return the number of small bars to use, assuming we always use big bars before small bars. Return -1 if it can't be done. + """ + if small*1 + big*5 >= goal and goal%5 <= small: + if goal//5 >= big: + return goal - 9//5 * big * 5 + else: + return goal%5 + else: + return -1 + +print(make_chocolate(4, 1, 9)) +print(make_chocolate(4, 1, 10)) +print(make_chocolate(4, 1, 7)) diff --git a/logic-2/no_teen_sum.py b/logic-2/no_teen_sum.py new file mode 100644 index 0000000..e9f779d --- /dev/null +++ b/logic-2/no_teen_sum.py @@ -0,0 +1,15 @@ +def no_teen_sum(a, b, c): + """ + Given 3 int values, a b c, return their sum. However, if any of the values is a teen -- in the range 13..19 inclusive -- then that value counts as 0, except 15 and 16 do not count as a teens. Write a separate helper "def fix_teen(n):"that takes in an int value and returns that value fixed for the teen rule. In this way, you avoid repeating the teen code 3 times (i.e. "decomposition"). Define the helper below and at the same indent level as the main no_teen_sum(). + """ + return fix_teen(a) + fix_teen(b) + fix_teen(c) + +def fix_teen(n): + if n in range(13, 20) and (n != 15 and n !=16): + return 0 + else: + return n + +print(no_teen_sum(1, 2, 3)) +print(no_teen_sum(2, 13, 1)) +print(no_teen_sum(2, 1, 14)) diff --git a/logic-2/round_sum.py b/logic-2/round_sum.py new file mode 100644 index 0000000..0d6f3d2 --- /dev/null +++ b/logic-2/round_sum.py @@ -0,0 +1,15 @@ +def round_sum(a, b, c): + """ + For this problem, we'll round an int value up to the next multiple of 10 if its rightmost digit is 5 or more, so 15 rounds up to 20. Alternately, round down to the previous multiple of 10 if its rightmost digit is less than 5, so 12 rounds down to 10. Given 3 ints, a b c, return the sum of their rounded values. To avoid code repetition, write a separate helper "def round10(num):" and call it 3 times. Write the helper entirely below and at the same indent level as round_sum(). + """ + return round10(a) + round10(b) + round10(c) + +def round10(num): + if num%10 >= 5: + return num + 10 - num%10 + else: + return num - num%10 + +print(round_sum(16, 17, 18)) +print(round_sum(12, 13, 14)) +print(round_sum(6, 4, 4)) diff --git a/string-1/combo_string.py b/string-1/combo_string.py new file mode 100644 index 0000000..b1ab906 --- /dev/null +++ b/string-1/combo_string.py @@ -0,0 +1,15 @@ +def combo_string(a, b): + """ + Given 2 strings, a and b, return a string of the form short+long+short, with the shorter string on the outside and the longer string on the inside. The strings will not be the same length, but they may be empty (length 0). + """ + if len(a) > len(b): + shorter = b + longer = a + else: + shorter = a + longer = b + return shorter+longer+shorter + +print(combo_string('Hello', 'hi')) +print(combo_string('hi', 'Hello')) +print(combo_string('aaa', 'b')) diff --git a/string-1/extra_end.py b/string-1/extra_end.py new file mode 100644 index 0000000..36b144d --- /dev/null +++ b/string-1/extra_end.py @@ -0,0 +1,9 @@ +def extra_end(str): + """ + Given a string, return a new string made of 3 copies of the last 2 chars of the original string. The string length will be at least 2. + """ + return str[-2:]*3 + +print(extra_end('Hello')) +print(extra_end('ab')) +print(extra_end('Hi')) diff --git a/string-1/first_half.py b/string-1/first_half.py new file mode 100644 index 0000000..f5ae147 --- /dev/null +++ b/string-1/first_half.py @@ -0,0 +1,9 @@ +def first_half(str): + """ + Given a string of even length, return the first half. So the string "WooHoo" yields "Woo". + """ + return str[0:len(str)/2] + +print(first_half('WooHoo')) +print(first_half('HelloThere')) +print(first_half('abcdef')) diff --git a/string-1/first_two.py b/string-1/first_two.py new file mode 100644 index 0000000..878b00b --- /dev/null +++ b/string-1/first_two.py @@ -0,0 +1,9 @@ +def first_two(str): + """ + Given a string, return the string made of its first two chars, so the String "Hello" yields "He". If the string is shorter than length 2, return whatever there is, so "X" yields "X", and the empty string "" yields the empty string "". + """ + return str[0:2] + +print(first_two('Hello')) +print(first_two('abcdefg')) +print(first_two('ab')) diff --git a/string-1/left2.py b/string-1/left2.py new file mode 100644 index 0000000..188bf09 --- /dev/null +++ b/string-1/left2.py @@ -0,0 +1,9 @@ +def left2(str): + """ + Given a string, return a "rotated left 2" version where the first 2 chars are moved to the end. The string length will be at least 2. + """ + return str[2:] + str[0:2] + +print(left2('Hello')) +print(left2('java')) +print(left2('Hi')) diff --git a/string-1/make_abba.py b/string-1/make_abba.py new file mode 100644 index 0000000..5c47412 --- /dev/null +++ b/string-1/make_abba.py @@ -0,0 +1,9 @@ +def make_abba(a, b): + """ + Given two strings, a and b, return the result of putting them together in the order abba, e.g. "Hi" and "Bye" returns "HiByeByeHi". + """ + return a + b + b + a + +print(make_abba('Hi', 'Bye')) +print(make_abba('Yo', 'Alice')) +print(make_abba('What', 'Up')) diff --git a/string-1/make_out_word.py b/string-1/make_out_word.py new file mode 100644 index 0000000..13e9971 --- /dev/null +++ b/string-1/make_out_word.py @@ -0,0 +1,9 @@ +def make_out_word(out, word): + """ + Given an "out" string length 4, such as "<<>>", and a word, return a new string where the word is in the middle of the out string, e.g. "<>". + """ + return out[0:2] + word + out[2:] + +print(make_out_word('<<>>', 'Yay')) +print(make_out_word('<<>>', 'WooHoo')) +print(make_out_word('[[]]', 'word')) diff --git a/string-1/make_tags.py b/string-1/make_tags.py new file mode 100644 index 0000000..c6cc2fe --- /dev/null +++ b/string-1/make_tags.py @@ -0,0 +1,9 @@ +def make_tags(tag, word): + """ + The web is built with HTML strings like "Yay" which draws Yay as italic text. In this example, the "i" tag makes and which surround the word "Yay". Given tag and word strings, create the HTML string with tags around the word, e.g. "Yay". + """ + return '<' + tag + '>' + word + '' + +print(make_tags('i', 'Yay')) +print(make_tags('i', 'Hello')) +print(make_tags('cite', 'Yay')) diff --git a/string-1/non_start.py b/string-1/non_start.py new file mode 100644 index 0000000..9f3017d --- /dev/null +++ b/string-1/non_start.py @@ -0,0 +1,9 @@ +def non_start(a, b): + """ + Given 2 strings, return their concatenation, except omit the first char of each. The strings will be at least length 1. + """ + return a[1:] + b[1:] + +print(non_start('Hello', 'There')) +print(non_start('java', 'code')) +print(non_start('shotl', 'java')) diff --git a/string-1/without_end.py b/string-1/without_end.py new file mode 100644 index 0000000..8099e4f --- /dev/null +++ b/string-1/without_end.py @@ -0,0 +1,9 @@ +def without_end(str): + """ + Given a string, return a version without the first and last char, so "Hello" yields "ell". The string length will be at least 2. + """ + return str[1:len(str)-1] + +print(without_end('Hello')) +print(without_end('java')) +print(without_end('coding')) diff --git a/string-2/cat_dog.py b/string-2/cat_dog.py new file mode 100644 index 0000000..93d6998 --- /dev/null +++ b/string-2/cat_dog.py @@ -0,0 +1,9 @@ +def cat_dog(str): + """ + Return True if the string "cat" and "dog" appear the same number of times in the given string. + """ + return str.count('dog') == str.count('cat') + +print(cat_dog('catdog')) +print(cat_dog('catcat')) +print(cat_dog('1cat1cadodog')) diff --git a/string-2/count_code.py b/string-2/count_code.py new file mode 100644 index 0000000..c1a55e1 --- /dev/null +++ b/string-2/count_code.py @@ -0,0 +1,13 @@ +def count_code(str): + """ + Return the number of times that the string "code" appears anywhere in the given string, except we'll accept any letter for the 'd', so "cope" and "cooe" count. + """ + count = 0 + for i in range(0, len(str) - 3): + if str[i] == "c" and str[i+1] == "o" and str[i+3] == "e": + count += 1 + return count + +print(count_code('aaacodebbb')) +print(count_code('codexxcode')) +print(count_code('cozexxcope')) diff --git a/string-2/count_hi.py b/string-2/count_hi.py new file mode 100644 index 0000000..d5a40aa --- /dev/null +++ b/string-2/count_hi.py @@ -0,0 +1,9 @@ +def count_hi(str): + """ + Return the number of times that the string "hi" appears anywhere in the given string. + """ + return str.count('hi') + +print(count_hi('abc hi ho')) +print(count_hi('ABChi hi')) +print(count_hi('hihi')) diff --git a/string-2/end_other.py b/string-2/end_other.py new file mode 100644 index 0000000..34b8128 --- /dev/null +++ b/string-2/end_other.py @@ -0,0 +1,9 @@ +def end_other(a, b): + """ + Given two strings, return True if either of the strings appears at the very end of the other string, ignoring upper/lower case differences (in other words, the computation should not be "case sensitive"). Note: s.lower() returns the lowercase version of a string. + """ + return b.lower() == a.lower()[-len(b):] or a.lower() == b.lower()[-len(a):] + +print(end_other('Hiabc', 'abc')) +print(end_other('AbC', 'HiaBc')) +print(end_other('abc', 'abXabc')) diff --git a/string-2/xyz_there.py b/string-2/xyz_there.py new file mode 100644 index 0000000..d4b7ba3 --- /dev/null +++ b/string-2/xyz_there.py @@ -0,0 +1,14 @@ +def xyz_there(str): + """ + Return True if the given string contains an appearance of "xyz" where the xyz is not directly preceeded by a period (.). So "xxyz" counts but "x.xyz" does not. + """ + if str[:3] == 'xyz': + return True + for i in range(len(str) - 3): + if str[i] != '.' and str[i+1:i+4] == 'xyz': + return True + return False + +print(xyz_there('abcxyz')) +print(xyz_there('abc.xyz')) +print(xyz_there('xyz.abc')) diff --git a/warmup-1/diff21.py b/warmup-1/diff21.py new file mode 100644 index 0000000..daf8904 --- /dev/null +++ b/warmup-1/diff21.py @@ -0,0 +1,11 @@ +def diff21(n): + """ + Given an int n, return the absolute difference between n and 21, except return double the absolute difference if n is over 21. + """ + if n> 21: + return (n- 21) * 2 + return 21 - n + +print(diff21(19)) +print(diff21(10)) +print(diff21(21)) diff --git a/warmup-1/front3.py b/warmup-1/front3.py new file mode 100644 index 0000000..43185e1 --- /dev/null +++ b/warmup-1/front3.py @@ -0,0 +1,9 @@ +def front3(str): + """ + Given a string, we'll say that the front is the first 3 chars of the string. If the string length is less than 3, the front is whatever is there. Return a new string which is 3 copies of the front. + """ + return str[0:3]*3 + +print(front3('Java')) +print(front3('Chocolate')) +print(front3('abc')) diff --git a/warmup-1/front_back.py b/warmup-1/front_back.py new file mode 100644 index 0000000..55455b7 --- /dev/null +++ b/warmup-1/front_back.py @@ -0,0 +1,11 @@ +def front_back(str): + """ + Given a string, return a new string where the first and last chars have been exchanged. + """ + if len(str) > 1: + return str[-1] + str[1:-1] + str[0] + return str + +print(front_back('code')) +print(front_back('a')) +print(front_back('ab')) diff --git a/warmup-1/makes10.py b/warmup-1/makes10.py new file mode 100644 index 0000000..19d80d7 --- /dev/null +++ b/warmup-1/makes10.py @@ -0,0 +1,9 @@ +def makes10(a, b): + """ + Given 2 ints, a and b, return True if one if them is 10 or if their sum is 10. + """ + return a == 10 or b == 10 or a + b == 10 + +print(makes10(9, 10)) +print(makes10(9, 9)) +print(makes10(1, 9)) diff --git a/warmup-1/missing_char.py b/warmup-1/missing_char.py new file mode 100644 index 0000000..8b4272b --- /dev/null +++ b/warmup-1/missing_char.py @@ -0,0 +1,9 @@ +def missing_char(str, n): + """ + Given a non-empty string and an int n, return a new string where the char at index n has been removed. The value of n will be a valid index of a char in the original string (i.e. n will be in the range 0..len(str)-1 inclusive). + """ + return str[:n] + str[n + 1:] + +print(missing_char('kitten', 1)) +print(missing_char('kitten', 0)) +print(missing_char('kitten', 4)) diff --git a/warmup-1/monkey_trouble.py b/warmup-1/monkey_trouble.py new file mode 100644 index 0000000..31dc6bf --- /dev/null +++ b/warmup-1/monkey_trouble.py @@ -0,0 +1,9 @@ +def monkey_trouble(a_smile, b_smile): + """ + We have two monkeys, a and b, and the parameters a_smile and b_smile indicate if each is smiling. We are in trouble if they are both smiling or if neither of them is smiling. Return True if we are in trouble. + """ + return a_smile == b_smile + +print(monkey_trouble(True, True)) +print(monkey_trouble(False, False)) +print(monkey_trouble(True, False)) diff --git a/warmup-1/near_hundred.py b/warmup-1/near_hundred.py new file mode 100644 index 0000000..e9218c2 --- /dev/null +++ b/warmup-1/near_hundred.py @@ -0,0 +1,9 @@ +def near_hundred(n): + """ + Given an int n, return True if it is within 10 of 100 or 200. Note: abs(num) computes the absolute value of a number. + """ + return abs(200 - n) <= 10 or abs(100 - n) <= 10 + +print(near_hundred(93)) +print(near_hundred(90)) +print(near_hundred(89)) diff --git a/warmup-1/not_string.py b/warmup-1/not_string.py new file mode 100644 index 0000000..aa2092c --- /dev/null +++ b/warmup-1/not_string.py @@ -0,0 +1,11 @@ +def not_string(str): + """ + Given a string, return a new string where "not " has been added to the front. However, if the string already begins with "not", return the string unchanged. + """ + if 'not' == str[:3]: + return str + return 'not ' + str + +print(not_string('candy')) +print(not_string('x')) +print(not_string('not bad')) diff --git a/warmup-1/parrot_trouble.py b/warmup-1/parrot_trouble.py new file mode 100644 index 0000000..031f50e --- /dev/null +++ b/warmup-1/parrot_trouble.py @@ -0,0 +1,9 @@ +def parrot_trouble(talking, hour): + """ + We have a loud talking parrot. The "hour" parameter is the current hour time in the range 0..23. We are in trouble if the parrot is talking and the hour is before 7 or after 20. Return True if we are in trouble. + """ + return talking and (hour < 7 or hour> 20) + +print(parrot_trouble(True, 6)) +print(parrot_trouble(True, 7)) +print(parrot_trouble(False, 6)) diff --git a/warmup-1/pos_neg.py b/warmup-1/pos_neg.py new file mode 100644 index 0000000..93282b2 --- /dev/null +++ b/warmup-1/pos_neg.py @@ -0,0 +1,12 @@ +def pos_neg(a, b, negative): + """ + Given 2 int values, return True if one is negative and one is positive. Except if the parameter "negative" is True, then return True only if both are negative. + """ + if negative: + return a < 0 and b < 0 + else : + return (a < 0 and b > 0) or (a > 0 and b < 0) + +print(pos_neg(1, -1, False)) +print(pos_neg(-1, 1, False)) +print(pos_neg(-4, -5, True)) diff --git a/warmup-1/sum_double.py b/warmup-1/sum_double.py new file mode 100644 index 0000000..eef5080 --- /dev/null +++ b/warmup-1/sum_double.py @@ -0,0 +1,11 @@ +def sum_double(a, b): + """ + Given two int values, return their sum. Unless the two values are the same, then return double their sum. + """ + if a == b: + return (a + b ) * 2 + return a + b + +print(sum_double(1, 2)) +print(sum_double(3, 2)) +print(sum_double(2, 2)) diff --git a/warmup-2/array123.py b/warmup-2/array123.py new file mode 100644 index 0000000..8ecca64 --- /dev/null +++ b/warmup-2/array123.py @@ -0,0 +1,12 @@ +def array123(nums): + """ + Given an array of ints, return True if the sequence of numbers 1, 2, 3 appears in the array somewhere. + """ + for i in range(len(nums)- 2): + if nums[i] == 1 and nums[i + 1] == 2 and nums[i + 2] == 3: + return True + return False + +print(array123([1, 1, 2, 3, 1])) +print(array123([1, 1, 2, 4, 1])) +print(array123([1, 1, 2, 1, 2, 3])) diff --git a/warmup-2/array_count9.py b/warmup-2/array_count9.py new file mode 100644 index 0000000..98b5807 --- /dev/null +++ b/warmup-2/array_count9.py @@ -0,0 +1,9 @@ +def array_count9(nums): + """ + Given an array of ints, return the number of 9's in the array. + """ + return nums.count(9) + +print(array_count9([1, 2, 9])) +print(array_count9([1, 9, 9])) +print(array_count9([1, 9, 9, 3, 9])) diff --git a/warmup-2/array_front9.py b/warmup-2/array_front9.py new file mode 100644 index 0000000..6df18a1 --- /dev/null +++ b/warmup-2/array_front9.py @@ -0,0 +1,9 @@ +def array_front9(nums): + """ + Given an array of ints, return True if one of the first 4 elements in the array is a 9. The array length may be less than 4. + """ + return 9 in nums[:4] + +print(array_front9([1, 2, 9, 3, 4])) +print(array_front9([1, 2, 3, 4, 9])) +print(array_front9([1, 2, 3, 4, 5])) diff --git a/warmup-2/front_times.py b/warmup-2/front_times.py new file mode 100644 index 0000000..cc2a3c2 --- /dev/null +++ b/warmup-2/front_times.py @@ -0,0 +1,9 @@ +def front_times(str, n): + """ + Given a string and a non-negative int n, we'll say that the front of the string is the first 3 chars, or whatever is there if the string is less than length 3. Return n copies of the front; + """ + return str[:3] * n + +print(front_times('Chocolate', 2)) +print(front_times('Chocolate', 3)) +print(front_times('Abc', 3)) diff --git a/warmup-2/last2.py b/warmup-2/last2.py new file mode 100644 index 0000000..8eb43f8 --- /dev/null +++ b/warmup-2/last2.py @@ -0,0 +1,17 @@ +def last2(str): + """ + Given a string, return the count of the number of times that a substring length 2 appears in the string and also as the last 2 chars of the string, so "hixxxhi" yields 1 (we won't count the end substring). + """ + if len(str) < 2: + return 0 + last2 = str[-2:] + count = 0 + for i in range(len(str) - 2): + if str[i] == last2[0] and str[i + 1] == last2[1]: + count += 1 + return count + + +print(last2('hixxhi')) +print(last2('xaxxaxaxx')) +print(last2('axxxaaxx')) diff --git a/warmup-2/string_bits.py b/warmup-2/string_bits.py new file mode 100644 index 0000000..a0c8023 --- /dev/null +++ b/warmup-2/string_bits.py @@ -0,0 +1,12 @@ +def string_bits(str): + """ + Given a string, return a new string made of every other char starting with the first, so "Hello" yields "Hlo". + """ + return_str = '' + for i in range(0, len(str), 2): + return_str += str[i] + return return_str + +print(string_bits('Hello')) +print(string_bits('Hi')) +print(string_bits('Heeololeo')) diff --git a/warmup-2/string_match.py b/warmup-2/string_match.py new file mode 100644 index 0000000..b3ab02d --- /dev/null +++ b/warmup-2/string_match.py @@ -0,0 +1,13 @@ +def string_match(a, b): + """ + Given 2 strings, a and b, return the number of the positions where they contain the same length 2 substring. So "xxcaazz" and "xxbaaz" yields 3, since the "xx", "aa", and "az" substrings appear in the same place in both strings. + """ + count = 0 + for i in range(0, len(a) - 1): + if a[i:i+2] == b[i:i+2]: + count += 1 + return count + +print(string_match('xxcaazz', 'xxbaaz')) +print(string_match('abc', 'abc')) +print(string_match('abc', 'axc')) diff --git a/warmup-2/string_splosion.py b/warmup-2/string_splosion.py new file mode 100644 index 0000000..99e9a5f --- /dev/null +++ b/warmup-2/string_splosion.py @@ -0,0 +1,12 @@ +def string_splosion(str): + """ + Given a non-empty string like "Code" return a string like "CCoCodCode". + """ + string = '' + for i in range(len(str)): + string += str[0:i+1] + return string + +print(string_splosion('Code')) +print(string_splosion('abc')) +print(string_splosion('ab'))