diff --git a/list-1/common_end.py b/list-1/common_end.py new file mode 100644 index 0000000..395e67c --- /dev/null +++ b/list-1/common_end.py @@ -0,0 +1,11 @@ +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[0] == b[0] or a[-1] == b[-1] + +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])) \ No newline at end of file diff --git a/list-1/first_last6.py b/list-1/first_last6.py index 8e6cd71..ecf376b 100644 --- a/list-1/first_last6.py +++ b/list-1/first_last6.py @@ -1,8 +1,14 @@ def first_last6(nums): - """Given an array of ints, return True if 6 appears as either the first or last element in the array. The array will be length 1 or more.""" + """ + Given an array of ints, return True if 6 appears as either the first or last element in the array. + The array will be length 1 or more. + """ return nums[0] == 6 or nums[-1] == 6 print(first_last6([1, 2, 6])) print(first_last6([6, 1, 2, 3])) print(first_last6([13, 6, 1, 2, 3])) + +# Alternative soltion +# return (nums[0] == 6 or nums[-1] == 6) diff --git a/list-1/has23.py b/list-1/has23.py new file mode 100644 index 0000000..049ef54 --- /dev/null +++ b/list-1/has23.py @@ -0,0 +1,10 @@ +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])) \ No newline at end of file diff --git a/list-1/make_ends.py b/list-1/make_ends.py new file mode 100644 index 0000000..1136555 --- /dev/null +++ b/list-1/make_ends.py @@ -0,0 +1,10 @@ +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])) \ No newline at end of file diff --git a/list-1/make_pi.py b/list-1/make_pi.py new file mode 100644 index 0000000..7ff205b --- /dev/null +++ b/list-1/make_pi.py @@ -0,0 +1,8 @@ +def make_pi(): + """ + Return an int array length 3 containing the first 3 digits of pi, {3, 1, 4}. + """ + a = 3, 1, 4 + return list(a) + +print(make_pi()) diff --git a/list-1/max_end3.py b/list-1/max_end3.py new file mode 100644 index 0000000..80b0406 --- /dev/null +++ b/list-1/max_end3.py @@ -0,0 +1,20 @@ +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. + """ + if nums[0] > nums[2]: + return [nums[0]] * 3 + return [nums[2]] * 3 + + +print(max_end3([1, 2, 3])) +print(max_end3([11, 5, 9])) +print(max_end3([2, 11, 3])) + +# Alternative solution +# big = max(nums[0], nums[2]) +# nums[0] = big +# nums[1] = big +# nums[2] = big +# return nums \ No newline at end of file diff --git a/list-1/middle_way.py b/list-1/middle_way.py new file mode 100644 index 0000000..afdf093 --- /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])) \ No newline at end of file diff --git a/list-1/reverse3.py b/list-1/reverse3.py new file mode 100644 index 0000000..ec76552 --- /dev/null +++ b/list-1/reverse3.py @@ -0,0 +1,10 @@ +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[2]] + [nums[1]] + [nums[0]] + + +print(reverse3([1, 2, 3])) +print(reverse3([5, 11, 9])) +print(reverse3([7, 0, 0])) diff --git a/list-1/rotate_left3.py b/list-1/rotate_left3.py new file mode 100644 index 0000000..0a29942 --- /dev/null +++ b/list-1/rotate_left3.py @@ -0,0 +1,10 @@ +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[0]] + +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..e985990 --- /dev/null +++ b/list-1/same_first_last.py @@ -0,0 +1,13 @@ +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])) + +# Alternative solution +# return (len(nums) >= 1 and nums[0] == nums[-1]) \ No newline at end of file diff --git a/list-1/sum2.py b/list-1/sum2.py new file mode 100644 index 0000000..533cfb5 --- /dev/null +++ b/list-1/sum2.py @@ -0,0 +1,14 @@ +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. + """ + if len(nums) < 2: + return sum(nums) + return sum(nums[:2]) + + +print(sum2([1, 2, 3])) +print(sum2([1, 1])) +print(sum2([1, 1, 1, 1])) \ No newline at end of file diff --git a/list-1/sum3.py b/list-1/sum3.py new file mode 100644 index 0000000..52ccd15 --- /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])) \ No newline at end of file diff --git a/list-2/big_diff.py b/list-2/big_diff.py new file mode 100644 index 0000000..cd56fb0 --- /dev/null +++ b/list-2/big_diff.py @@ -0,0 +1,21 @@ +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. + """ + maximum, minimum = max(nums), min(nums) + return maximum - minimum + +print(big_diff([10, 3, 5, 6])) +print(big_diff([7, 2, 10, 9])) +print(big_diff([2, 10, 7, 2])) + +# With Loop +# maximum, minimum = 0, nums[0] +# for i in range(len(nums)-1): +# if max(nums[i:i+2]) > maximum: +# maximum = max(nums[i:i+2]) +# if min(nums[i:i+2]) < minimum: +# minimum = min(nums[i:i+2]) +# return maximum - minimum diff --git a/list-2/centered_average.py b/list-2/centered_average.py new file mode 100644 index 0000000..c9a327d --- /dev/null +++ b/list-2/centered_average.py @@ -0,0 +1,20 @@ +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. + """ + nums.sort() + if nums.count(nums[0]) > 0: + nums.remove(nums[0]) + if nums.count(nums[-1]) > 0: + nums.remove(nums[-1]) + return sum(nums) // len(nums) + + +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])) \ No newline at end of file diff --git a/list-2/count_evens.py b/list-2/count_evens.py index dee910d..f5d2311 100644 --- a/list-2/count_evens.py +++ b/list-2/count_evens.py @@ -1,7 +1,6 @@ def count_evens(nums): - """Return the number of even ints in the given array. - - Note: the % "mod" operator computes the remainder, e.g. 5 % 2 is 1. + """ + Return the number of even ints in the given array. """ evens = 0 for number in nums: diff --git a/list-2/has22.py b/list-2/has22.py new file mode 100644 index 0000000..d1b79d9 --- /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:i+2] == [2, 2]: + return True + return False + +print(has22([1, 2, 2])) +print(has22([1, 2, 1, 2])) +print(has22([2, 1, 2])) \ No newline at end of file diff --git a/list-2/sum13.py b/list-2/sum13.py new file mode 100644 index 0000000..e4b6c63 --- /dev/null +++ b/list-2/sum13.py @@ -0,0 +1,20 @@ +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. + """ + result = 0 + for i in range(len(nums)): + if i == len(nums) - 1: + if nums[i] != 13: + result += nums[i] + elif nums[i] == 13: + if nums[i+1] != 13: + result -= nums[i+1] + else: + result += nums[i] + return result + +print(sum13([1, 2, 2, 1])) +print(sum13([1, 1])) +print(sum13([1, 2, 2, 1, 13])) \ No newline at end of file diff --git a/list-2/sum67.py b/list-2/sum67.py new file mode 100644 index 0000000..90b3ca4 --- /dev/null +++ b/list-2/sum67.py @@ -0,0 +1,21 @@ +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. + """ + result = 0 + sum_off = False + for number in nums: + if number == 6: + sum_off = True + elif number == 7 and sum_off: + sum_off = False + elif not sum_off: + result += number + return result + + +print(sum67([1, 2, 2])) +print(sum67([1, 2, 2, 6, 99, 99, 7])) +print(sum67([1, 1, 6, 7, 2])) \ No newline at end of file diff --git a/logic-1/alarm_clock.py b/logic-1/alarm_clock.py new file mode 100644 index 0000000..5af3233 --- /dev/null +++ b/logic-1/alarm_clock.py @@ -0,0 +1,19 @@ +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 vacation: + if day == 0 or day == 6: + return "off" + return "10:00" + elif day == 0 or day == 6: + return "10:00" + return "7:00" + +print(alarm_clock(1, False)) +print(alarm_clock(5, False)) +print(alarm_clock(0, False)) \ No newline at end of file diff --git a/logic-1/caught_speeding.py b/logic-1/caught_speeding.py new file mode 100644 index 0000000..b40cf8f --- /dev/null +++ b/logic-1/caught_speeding.py @@ -0,0 +1,23 @@ +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 is_birthday: + if speed <= 65: + return 0 + elif 66 <= speed <= 85: + return 1 + return 2 + elif speed <= 60: + return 0 + elif 61 <= speed <= 80: + return 1 + return 2 + +print(caught_speeding(60, False)) +print(caught_speeding(65, False)) +print(caught_speeding(65, True)) \ No newline at end of file diff --git a/logic-1/cigar_party.py b/logic-1/cigar_party.py index 31854ba..3510c78 100644 --- a/logic-1/cigar_party.py +++ b/logic-1/cigar_party.py @@ -1,9 +1,19 @@ def cigar_party(cigars, is_weekend): - """When squirrels get together for a party, they like to have cigars. A squirrel party is successful when the number of cigars is between 40 and 60, inclusive. Unless it is the weekend, in which case there is no upper bound on the number of cigars. Return True if the party with the given values is successful, or False otherwise.""" - return is_weekend or (not is_weekend and 40 <= cigars <= 60) + """ + When squirrels get together for a party, they like to have cigars. + A squirrel party is successful when the number of cigars is between 40 and 60, inclusive. + Unless it is the weekend, in which case there is no upper bound on the number of cigars. + Return True if the party with the given values is successful, or False otherwise. + """ + return is_weekend and cigars >= 40 or (not is_weekend and 40 <= cigars <= 60) print(cigar_party(30, False)) -print(cigar_party(40, False)) print(cigar_party(50, False)) print(cigar_party(70, True)) + +# Alternative solution +# if is_weekend: +# return (cigars >= 40) +# else: +# return (cigars >= 40 and cigars <= 60) diff --git a/logic-1/date_fashion.py b/logic-1/date_fashion.py new file mode 100644 index 0000000..a84dd13 --- /dev/null +++ b/logic-1/date_fashion.py @@ -0,0 +1,25 @@ +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 + return 1 + +print(date_fashion(5, 10)) +print(date_fashion(5, 2)) +print(date_fashion(5, 5)) + +# Alternative solution +# if (you <= 2) or (date <= 2): +# return 0 +# elif (you >= 8) or (date >= 8): +# return 2 +# else: +# return 1 \ No newline at end of file diff --git a/logic-1/in1to10.py b/logic-1/in1to10.py new file mode 100644 index 0000000..6a9cab8 --- /dev/null +++ b/logic-1/in1to10.py @@ -0,0 +1,13 @@ +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. + """ + if outside_mode: + return n <= 1 or n >= 10 + return 1 <= n <= 10 + +print(in1to10(5, False)) +print(in1to10(11, False)) +print(in1to10(11, True)) \ No newline at end of file diff --git a/logic-1/love6.py b/logic-1/love6.py new file mode 100644 index 0000000..d805175 --- /dev/null +++ b/logic-1/love6.py @@ -0,0 +1,12 @@ +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)) \ No newline at end of file diff --git a/logic-1/near_ten.py b/logic-1/near_ten.py new file mode 100644 index 0000000..6e989a3 --- /dev/null +++ b/logic-1/near_ten.py @@ -0,0 +1,10 @@ +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. + """ + return num % 10 <= 2 or num % 10 >= 8 + +print(near_ten(12)) +print(near_ten(17)) +print(near_ten(19)) \ No newline at end of file diff --git a/logic-1/sorta_sum.py b/logic-1/sorta_sum.py new file mode 100644 index 0000000..b476dd2 --- /dev/null +++ b/logic-1/sorta_sum.py @@ -0,0 +1,18 @@ +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 10 <= sum([a, b]) <= 19: + return 20 + return sum([a, b]) + +print(sorta_sum(3, 4)) +print(sorta_sum(9, 4)) +print(sorta_sum(10, 11)) + +# Alternative solution +# sum = a + b +# if sum >= 10 and sum <= 19: +# return 20 +# return sum \ No newline at end of file diff --git a/logic-1/squirrel_play.py b/logic-1/squirrel_play.py new file mode 100644 index 0000000..04eb368 --- /dev/null +++ b/logic-1/squirrel_play.py @@ -0,0 +1,12 @@ +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 is_summer and 60 <= temp <= 100 or (not is_summer and 60 <= temp <= 90) + +print(squirrel_play(70, False)) +print(squirrel_play(95, False)) +print(squirrel_play(95, True)) \ No newline at end of file diff --git a/logic-2/close_far.py b/logic-2/close_far.py new file mode 100644 index 0000000..e4117cc --- /dev/null +++ b/logic-2/close_far.py @@ -0,0 +1,11 @@ +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. + """ + return (abs(b - a) <= 1 and abs(c - b) >= 2 and abs(c - a) >= 2 + or abs(c - a) <= 1 and abs(b - c) >= 2 and abs(b - a) >= 2) + +print(close_far(1, 2, 10)) +print(close_far(1, 2, 3)) +print(close_far(4, 1, 3)) \ No newline at end of file diff --git a/logic-2/lone_sum.py b/logic-2/lone_sum.py index b11fb00..0657f34 100644 --- a/logic-2/lone_sum.py +++ b/logic-2/lone_sum.py @@ -1,9 +1,12 @@ def lone_sum(a, b, c): - """Given 3 int values, a b c, return their sum. However, if one of the values is the same as another of the values, it does not count towards the sum.""" - l = [a, b, c] + """ + Given 3 int values, a b c, return their sum. + However, if one of the values is the same as another of the values, it does not count towards the sum. + """ + values = [a, b, c] result = 0 - for x in l: - if l.count(x) == 1: + for x in values: + if values.count(x) == 1: result += x return result @@ -11,3 +14,13 @@ def lone_sum(a, b, c): print(lone_sum(1, 2, 3)) print(lone_sum(3, 2, 3)) print(lone_sum(3, 3, 3)) + +# Alternative solution +# sum = 0 +# if a != b and a != c: +# sum += a +# if b != a and b != c: +# sum += b +# if c != a and c != b: +# sum += c +# return sum diff --git a/logic-2/lucky_sum.py b/logic-2/lucky_sum.py new file mode 100644 index 0000000..5d7fd56 --- /dev/null +++ b/logic-2/lucky_sum.py @@ -0,0 +1,18 @@ +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. + """ + values = [a, b, c] + result = 0 + for x in values: + if x == 13: + break + result += x + return result + + +print(lucky_sum(1, 2, 3)) +print(lucky_sum(1, 2, 13)) +print(lucky_sum(1, 13, 3)) \ No newline at end of file diff --git a/logic-2/make_bricks.py b/logic-2/make_bricks.py new file mode 100644 index 0000000..3d37138 --- /dev/null +++ b/logic-2/make_bricks.py @@ -0,0 +1,20 @@ +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 b_brick 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. + """ + if small + big * 5 < goal: + return False + elif small == 0: + return goal % 5 == 0 + elif big == 0: + return True + else: + return (goal - small) % 5 == 0 or goal % 5 <= small or goal - big * 5 == 0 or goal - small == 0 + + +print(make_bricks(3, 1, 8)) +print(make_bricks(3, 1, 9)) +print(make_bricks(3, 2, 10)) \ No newline at end of file diff --git a/logic-2/make_chocolate.py b/logic-2/make_chocolate.py new file mode 100644 index 0000000..df1efb3 --- /dev/null +++ b/logic-2/make_chocolate.py @@ -0,0 +1,21 @@ +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 + big * 5 < goal: + return -1 + elif goal - big * 5 == 0: + return 0 + elif goal - big * 5 > 0: + return goal - big * 5 + elif goal - big * 5 < 0 and goal % 5 <= small: + return goal % 5 + return -1 + + +print(make_chocolate(4, 1, 9)) +print(make_chocolate(4, 1, 10)) +print(make_chocolate(4, 1, 7)) \ No newline at end of file diff --git a/logic-2/no_teen_sum.py b/logic-2/no_teen_sum.py new file mode 100644 index 0000000..9f5d87c --- /dev/null +++ b/logic-2/no_teen_sum.py @@ -0,0 +1,20 @@ +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. + """ + return fix_teen(a) + fix_teen(b) + fix_teen(c) + +def fix_teen(n): + """ + Takes an an int value and returns that value fixed for the teen rule. + """ + teens = [13, 14, 17, 18, 19] + if n in teens: + n = 0 + return n + +print(no_teen_sum(1, 2, 3)) +print(no_teen_sum(2, 13, 1)) +print(no_teen_sum(2, 1, 14)) \ No newline at end of file diff --git a/logic-2/round_sum.py b/logic-2/round_sum.py new file mode 100644 index 0000000..28d1f23 --- /dev/null +++ b/logic-2/round_sum.py @@ -0,0 +1,32 @@ +def round_sum(a, b, c): + """ + Round an int value up to next multiple of 10 if its rightmost digit is 5 or more, so 15 rounds up to 20. + Round down to 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. + """ + return round10(a) + round10(b) + round10(c) + +def round10(num): + """ + Rounds num to the nearest next multiple of 10 + """ + if num % 10 >= 5: + num += 10 - num % 10 + else: + num -= num % 10 + return num + + +print(round_sum(16, 17, 18)) +print(round_sum(12, 13, 14)) +print(round_sum(6, 4, 4)) + +# Alternative solution: +# def round_sum(a, b, c): +# return round10(a) + round10(b) + round10(c) +# def round10(num): +# mod = num % 10 +# num -= mod +# if mod >= 5: +# num += 10 +# return num \ No newline at end of file diff --git a/string-1/combo_string.py b/string-1/combo_string.py new file mode 100644 index 0000000..91eb00c --- /dev/null +++ b/string-1/combo_string.py @@ -0,0 +1,13 @@ +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): + return b + a + b + return a + b + a + +print(combo_string('Hello', 'hi')) +print(combo_string('hi', 'Hello')) +print(combo_string('aaa', 'b')) \ No newline at end of file diff --git a/string-1/extra_end.py b/string-1/extra_end.py new file mode 100644 index 0000000..18d95a4 --- /dev/null +++ b/string-1/extra_end.py @@ -0,0 +1,14 @@ +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')) + +# Alternative solution +# end = str[-2:] +# return end + end + end \ No newline at end of file diff --git a/string-1/first_half.py b/string-1/first_half.py new file mode 100644 index 0000000..b02887e --- /dev/null +++ b/string-1/first_half.py @@ -0,0 +1,10 @@ +def first_half(str): + """ + Given a string of even length, return the first half. + So the string "WooHoo" yields "Woo". + """ + return str[:int(len(str)/2)] + +print(first_half('WooHoo')) +print(first_half('HelloThere')) +print(first_half('abcdef')) \ No newline at end of file diff --git a/string-1/first_two.py b/string-1/first_two.py new file mode 100644 index 0000000..a8d7127 --- /dev/null +++ b/string-1/first_two.py @@ -0,0 +1,17 @@ +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[:2] + +print(first_two('Hello')) +print(first_two('abcdefg')) +print(first_two('ab')) + +# Alternative solution +# if len(str) >= 2 +# return str[:2] +# else: +# return str \ No newline at end of file diff --git a/string-1/hello_name.py b/string-1/hello_name.py index 1b199be..32081d6 100644 --- a/string-1/hello_name.py +++ b/string-1/hello_name.py @@ -1,5 +1,7 @@ def hello_name(name): - """Given a string name, e.g. "Bob", return a greeting of the form "Hello Bob!".""" + """ + Given a string name, e.g. "Bob", return a greeting of the form "Hello Bob!". + """ return 'Hello ' + name + '!' diff --git a/string-1/left2.py b/string-1/left2.py new file mode 100644 index 0000000..09c6ca4 --- /dev/null +++ b/string-1/left2.py @@ -0,0 +1,10 @@ +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[:2] + +print(left2('Hello')) +print(left2('java')) +print(left2('Hi')) \ No newline at end of file diff --git a/string-1/make_abba.py b/string-1/make_abba.py new file mode 100644 index 0000000..97ecf2a --- /dev/null +++ b/string-1/make_abba.py @@ -0,0 +1,10 @@ +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')) \ No newline at end of file diff --git a/string-1/make_out_word.py b/string-1/make_out_word.py new file mode 100644 index 0000000..34b5acc --- /dev/null +++ b/string-1/make_out_word.py @@ -0,0 +1,10 @@ +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[:2] + word + out[2:] + +print(make_out_word('<<>>', 'Yay')) +print(make_out_word('<<>>', 'WooHoo')) +print(make_out_word('[[]]', 'word')) \ No newline at end of file diff --git a/string-1/make_tags.py b/string-1/make_tags.py new file mode 100644 index 0000000..14c8d25 --- /dev/null +++ b/string-1/make_tags.py @@ -0,0 +1,15 @@ +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')) + +# My other solution (not accepted by the website; perhaps f-string is not supported) +# return f"<{tag}>{word}" \ No newline at end of file diff --git a/string-1/non_start.py b/string-1/non_start.py new file mode 100644 index 0000000..ba497ec --- /dev/null +++ b/string-1/non_start.py @@ -0,0 +1,10 @@ +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')) \ No newline at end of file diff --git a/string-1/without_end.py b/string-1/without_end.py new file mode 100644 index 0000000..4550522 --- /dev/null +++ b/string-1/without_end.py @@ -0,0 +1,10 @@ +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:-1] + +print(without_end('Hello')) +print(without_end('java')) +print(without_end('coding')) \ No newline at end of file diff --git a/string-2/cat_dog.py b/string-2/cat_dog.py new file mode 100644 index 0000000..2714de1 --- /dev/null +++ b/string-2/cat_dog.py @@ -0,0 +1,15 @@ +def cat_dog(str): + """ + Return True if the string "cat" and "dog" appear the same number of times in the given string. + """ + cat_count, dog_count = 0, 0 + for i in range(len(str)-2): + if "cat" in str[i:i+3]: + cat_count += 1 + elif "dog" in str[i:i+3]: + dog_count += 1 + return cat_count == dog_count + +print(cat_dog('catdog')) +print(cat_dog('catcat')) +print(cat_dog('1cat1cadodog')) \ No newline at end of file diff --git a/string-2/count_code.py b/string-2/count_code.py new file mode 100644 index 0000000..1c897e3 --- /dev/null +++ b/string-2/count_code.py @@ -0,0 +1,14 @@ +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. + """ + code_count = 0 + for i in range(len(str)-3): + if "co" in str[i:i+2] and "e" in str[i+3]: + code_count += 1 + return code_count + +print(count_code('aaacodebbb')) +print(count_code('codexxcode')) +print(count_code('cozexxcope')) \ No newline at end of file diff --git a/string-2/count_hi.py b/string-2/count_hi.py new file mode 100644 index 0000000..7e67cfa --- /dev/null +++ b/string-2/count_hi.py @@ -0,0 +1,21 @@ +def count_hi(str): + """ + Return the number of times that the string "hi" appears anywhere in the given string. + """ + hi_count = 0 + for i in range(len(str)-1): + if "hi" in str[i:i+2]: + hi_count += 1 + return hi_count + + +print(count_hi('abc hi ho')) +print(count_hi('ABChi hi')) +print(count_hi('hihi')) + +# Alternative solution +# sum = 0 +# for i in range(len(str)-1) +# if str[i:i+2] == 'hi' +# sum = sum + 1 +# return sum \ No newline at end of file diff --git a/string-2/double_char.py b/string-2/double_char.py index 1b82b02..3484d08 100644 --- a/string-2/double_char.py +++ b/string-2/double_char.py @@ -1,5 +1,7 @@ def double_char(str): - """Given a string, return a string where for every char in the original, there are two chars.""" + """ + Given a string, return a string where for every char in the original, there are two chars. + """ new_str = '' for letter in str: new_str += letter * 2 @@ -8,4 +10,10 @@ def double_char(str): print(double_char('The')) print(double_char('AAbb')) -print(double_char('Hi, there')) +print(double_char('Hi-There')) + +# Alternative solution +# result = "" +# for i in range(len(str)) +# result += str[i] + str[i] +# return result diff --git a/string-2/end_other.py b/string-2/end_other.py new file mode 100644 index 0000000..e51ca68 --- /dev/null +++ b/string-2/end_other.py @@ -0,0 +1,16 @@ +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"). + """ + return a[-len(b):].lower() == b.lower() or b[-len(a):].lower() == a.lower() + + +print(end_other('Hiabc', 'abc')) +print(end_other('AbC', 'HiaBc')) +print(end_other('abc', 'abXabc')) + +# Alternative solution +# a = a.lower() +# b = b.lower() +# return (b.endswith(a) or a.endswith(b)) \ No newline at end of file diff --git a/string-2/xyz_there.py b/string-2/xyz_there.py new file mode 100644 index 0000000..dfe123e --- /dev/null +++ b/string-2/xyz_there.py @@ -0,0 +1,19 @@ +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. + """ + xyz_count = 0 + if len(str) == 3: + return "xyz" in str + for i in range(len(str)-3): + if "xyz" in str[i:i+4] and "." not in str[i]: + xyz_count += 1 + elif "xyz" in str[i:i+4] and "." in str[i]: + xyz_count -= 1 + return xyz_count > 0 + + +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..ec1f57a --- /dev/null +++ b/warmup-1/diff21.py @@ -0,0 +1,19 @@ +def diff21(n): + """ + Given an int n, return the absolute difference between b and 21, + except return double the absolute difference if n is over 21. + """ + abs_diff_n_21 = abs(n - 21) + if n >= 21: + abs_diff_n_21 = abs_diff_n_21 * 2 + return abs_diff_n_21 + +print(diff21(19)) +print(diff21(10)) +print(diff21(21)) + +# Alternative solution: +# if n <= 21: +# return 21 - n +# else: +# return (n - 21) * 2 \ No newline at end of file diff --git a/warmup-1/front3.py b/warmup-1/front3.py new file mode 100644 index 0000000..4f4b79e --- /dev/null +++ b/warmup-1/front3.py @@ -0,0 +1,20 @@ +def front3(str): + """ + Given a string, front is first 3 chars of string. + If string length is less than 3, front is whatever is there. + Return a new strings which is 3 copies of the front + """ + if len(str) <= 3: + return str * 3 + return (str[:3] * 3) + +print(front3("Java")) +print(front3("Chocolate")) +print(front3("abc")) + +# Alternative solution +# front_end = 3 +# if len(str) < front_end: +# front_end = len(str) +# front = str[:front_end] +# return front + front + front \ No newline at end of file diff --git a/warmup-1/front_back.py b/warmup-1/front_back.py new file mode 100644 index 0000000..76741f2 --- /dev/null +++ b/warmup-1/front_back.py @@ -0,0 +1,17 @@ +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 + return (str[-1] + str[1:-1] + str[0]) + +print(front_back("code")) +print(front_back("a")) +print(front_back("ab")) + +# Alternative solution: +# if len(str) <= 1: +# return str +# mid = str[1:len(str)-1] +# return str[len(str)-1] + mid + str[0] \ No newline at end of file diff --git a/warmup-1/makes10.py b/warmup-1/makes10.py new file mode 100644 index 0000000..1a5a762 --- /dev/null +++ b/warmup-1/makes10.py @@ -0,0 +1,12 @@ +def makes10(a, b): + """ + Given 2 ints, a and b, return True if one of 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)) + +# Alternative solution: +# return (a == 10 or b == 10 or a + b == 10) \ No newline at end of file diff --git a/warmup-1/missing_char.py b/warmup-1/missing_char.py new file mode 100644 index 0000000..6202f9e --- /dev/null +++ b/warmup-1/missing_char.py @@ -0,0 +1,15 @@ +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 values of n will be a valid index of a char in the original string (i.e. n will be in the range 0 to 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)) + +# Alternative solution: +# front = str[:n] +# back = str[n+1:] +# return front + back \ No newline at end of file diff --git a/warmup-1/monkey_trouble.py b/warmup-1/monkey_trouble.py new file mode 100644 index 0000000..b92cd86 --- /dev/null +++ b/warmup-1/monkey_trouble.py @@ -0,0 +1,31 @@ +def monkey_trouble(a_smile, b_smile): + """ + Parameters a_smile and b_smile indicate if each is smiling. + Returns True if both are smiling or if neither are smiling. + """ + if a_smile: + if b_smile: + return True + else: + return False + else: + if not b_smile: + return True + else: + return False + +print(monkey_trouble(True, True)) +print(monkey_trouble(False, False)) +print(monkey_trouble(True, False)) + +# Alternative solutions: +#1. +# if a_smile and b_smile: +# return True +# if not a_smile and not b_smile +# return True +# return False +#2. +# return ((a_smile and b_smile) or (not a_smile and not b_smile)) +#3. +# return (a_smile == b_smile) diff --git a/warmup-1/near_hundred.py b/warmup-1/near_hundred.py new file mode 100644 index 0000000..f6e5302 --- /dev/null +++ b/warmup-1/near_hundred.py @@ -0,0 +1,12 @@ +def near_hundred(n): + """ + Return True if int n is within 10 of 100 or 200. + """ + return (abs(n - 100) <= 10 or abs(n - 200) <= 10) + +print(near_hundred(93)) +print(near_hundred(90)) +print(near_hundred(89)) + +# Alternative solution: +# return ((abs(100 - n) <= 10) or (abs(200 - n) <= 10)) \ No newline at end of file diff --git a/warmup-1/not_string.py b/warmup-1/not_string.py new file mode 100644 index 0000000..f698d28 --- /dev/null +++ b/warmup-1/not_string.py @@ -0,0 +1,17 @@ +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" in str[0:3]: + return str + return ("not " + str) + +print(not_string("candy")) +print(not_string("x")) +print(not_string("not bad")) + +# Alternative solution: +# if len(str) >= 3 and str[:3] == "not" +# return str +# return "not " + str diff --git a/warmup-1/parrot_trouble.py b/warmup-1/parrot_trouble.py new file mode 100644 index 0000000..e1aba99 --- /dev/null +++ b/warmup-1/parrot_trouble.py @@ -0,0 +1,15 @@ +def parrot_trouble(talking, hour): + """ + The hour parameter is the current hour time in the range 0-23. + Returns True if the parrot is talking and the hour is before 7 or after 20. + """ + if talking and (hour < 7 or hour > 20): + return True + return False + +print(parrot_trouble(True, 6)) +print(parrot_trouble(True, 7)) +print(parrot_trouble(False, 6)) + +# Alternative solution: +# return (talking and (hour < 7 or hour > 20)) \ No newline at end of file diff --git a/warmup-1/pos_neg.py b/warmup-1/pos_neg.py new file mode 100644 index 0000000..bfa4a4e --- /dev/null +++ b/warmup-1/pos_neg.py @@ -0,0 +1,18 @@ +def pos_neg(a, b, negative): + """ + Given 2 int values (a and b), returns True if one is negative and one is positive. + Except if parameter negative is True, then return True only if both are negative. + """ + if negative: + return (a < 0 and b < 0) + return ((a < 0) != (b < 0)) + +print(pos_neg(1, -1, False)) +print(pos_neg(-1, 1, False)) +print(pos_neg(-4, -5, True)) + +# Alternative solution: +# if negative: +# return (a < 0 and b < 0) +# else: +# return ((a < 0 and b > 0) or (a > 0 and b < 0)) diff --git a/warmup-1/sleep_in.py b/warmup-1/sleep_in.py index 0f1fe64..57cd5e0 100644 --- a/warmup-1/sleep_in.py +++ b/warmup-1/sleep_in.py @@ -1,10 +1,18 @@ def sleep_in(weekday, vacation): - """The parameter weekday is True if it is a weekday, and the parameter vacation is True if we are on vacation. We sleep in if it is not a weekday or we're on vacation. - - Return True if we sleep in.""" + """ + The parameter weekday is True if it is a weekday, and the parameter vacation is True if we are on vacation. + We sleep in if it is not a weekday or we're on vacation. + Return True if we sleep in. + """ return not weekday or vacation print(sleep_in(False, False)) print(sleep_in(True, False)) print(sleep_in(False, True)) + +# Alternative solution +# if not weekend or vacation: +# return True +# else: +# return False \ No newline at end of file diff --git a/warmup-1/sum_double.py b/warmup-1/sum_double.py new file mode 100644 index 0000000..a89b040 --- /dev/null +++ b/warmup-1/sum_double.py @@ -0,0 +1,19 @@ +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 2 * (a + b) + else: + return a + b + +print(sum_double(1, 2)) +print(sum_double(3, 2)) +print(sum_double(2, 2)) + +# alternative solution: +# sum = a + b +# if a == b: +# sum = sum * 2 +# return sum \ No newline at end of file diff --git a/warmup-2/array123.py b/warmup-2/array123.py new file mode 100644 index 0000000..11bf2a7 --- /dev/null +++ b/warmup-2/array123.py @@ -0,0 +1,18 @@ +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)): + if nums[i:i+3] == [1, 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])) + +# Alternative solution: +# 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 \ No newline at end of file diff --git a/warmup-2/array_count9.py b/warmup-2/array_count9.py new file mode 100644 index 0000000..ae266dd --- /dev/null +++ b/warmup-2/array_count9.py @@ -0,0 +1,20 @@ +def array_count9(nums): + """ + Given an array of ints, return the number of 9's in the array + """ + count = 0 + for int in nums: + if int == 9: + count +=1 + return count + +print(array_count9([1, 2, 9])) +print(array_count9([1, 9, 9])) +print(array_count9([1, 9, 9, 3, 9])) + +# Alternative solution: +# count = 0 +# for num in nums: +# if num == 9: +# count = count + 1 +# return count \ No newline at end of file diff --git a/warmup-2/array_front9.py b/warmup-2/array_front9.py new file mode 100644 index 0000000..51df3b5 --- /dev/null +++ b/warmup-2/array_front9.py @@ -0,0 +1,22 @@ +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 + """ + for int in nums[0:4]: + if int == 9: + return True + return False + +print(array_front9([1, 2, 9, 3, 4])) +print(array_front9([1, 2, 3, 4, 9])) +print(array_front9([1, 2, 3, 4, 5])) + +# Alternative solution: +# end = len(nums) +# if end > 4: +# end = 4 +# for i in range(end): +# if nums[i] == 9: +# return True +# return False \ No newline at end of file diff --git a/warmup-2/front_times.py b/warmup-2/front_times.py new file mode 100644 index 0000000..70ed32e --- /dev/null +++ b/warmup-2/front_times.py @@ -0,0 +1,21 @@ +def front_time(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_time("Chocolate", 2)) +print(front_time("Chocolate", 3)) +print(front_time("Abc", 3)) + +# Alternative solution: +# front_len = 3 +# if front_len > len(str): +# front_len = len(str) +# front = str[:front_len] +# result = "" +# for i in range(n): +# result = result + front +# return result \ No newline at end of file diff --git a/warmup-2/last2.py b/warmup-2/last2.py new file mode 100644 index 0000000..1e05b94 --- /dev/null +++ b/warmup-2/last2.py @@ -0,0 +1,26 @@ +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) + """ + sub_str = str[-2:] + count = 0 + for i in range(len(str)-2): + if str[i:i+2] == sub_str: + count += 1 + return count + +print(last2("hixxhi")) +print(last2("xaxxaxaxx")) +print(last2("axxxaxx")) + +# Alternative solution: +# if len(str) < 2 +# return 0 +# last2 = str[len(str)-2:] +# count = 0 +# for i in range(len(str)-2) +# sub = str[i:i+2] +# if sub == last2: +# count = count + 1 \ No newline at end of file diff --git a/warmup-2/string_bits.py b/warmup-2/string_bits.py new file mode 100644 index 0000000..542ebd3 --- /dev/null +++ b/warmup-2/string_bits.py @@ -0,0 +1,20 @@ +def string_bits(str): + """ + Given a string, return a new string made of every other char starting with the first, + so "Hello" yields "Hlo" + """ + res = "" + for i in range(0, len(str), 2): + res += str[i] + return res + +print(string_bits("Hello")) +print(string_bits("Hi")) +print(string_bits("Heeololeo")) + +# Alternative solution: +# result = "" +# for i in range(len(str)): +# if i % 2 == 0: +# result = result + str[i] +# return result diff --git a/warmup-2/string_match.py b/warmup-2/string_match.py new file mode 100644 index 0000000..e52347b --- /dev/null +++ b/warmup-2/string_match.py @@ -0,0 +1,24 @@ +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 "xx", "aa", and "az" substrings appear in the same place in both strings + """ + count = 0 + for i in range(len(a)-1): # doesn't matter which string to use for length + 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")) + +# Alternative solution: +# shorter = min(len(a), len(b)) +# count = 0 +# for i in range(shorter-1) +# a_sub = a[i:i+2] +# b_sub = a[i:i+2] +# if a_sub == b_sub: +# count = count + 1 +# return count \ No newline at end of file diff --git a/warmup-2/string_splosion.py b/warmup-2/string_splosion.py new file mode 100644 index 0000000..5b66e22 --- /dev/null +++ b/warmup-2/string_splosion.py @@ -0,0 +1,19 @@ +def string_splosion(str): + """ + Given a non-empty string like "Code", + return a string string like "CCoCodCode" + """ + res = "" + for i in range(len(str)): + res += str[:i+1] + return res + +print(string_splosion("Code")) +print(string_splosion("abc")) +print(string_splosion("ab")) + +# Alternative solution: +# result = "" +# for i in range(len(str)): +# result = result + str[:i+1] +# return result \ No newline at end of file diff --git a/warmup-2/string_times.py b/warmup-2/string_times.py index 729ae48..40959b0 100644 --- a/warmup-2/string_times.py +++ b/warmup-2/string_times.py @@ -1,8 +1,16 @@ def string_times(str, n): - """Given a string and a non-negative int n, return a larger string that is n copies of the original string. """ + """ + Given a string and a non-negative int n, return a larger string that is n copies of the original string. + """ return str * n print(string_times('Hi', 2)) print(string_times('Hi', 3)) print(string_times('Hi', 1)) + +# Alternative solution: +# result = "" +# for i in range(n): +# result = result + str +# return result