├── .gitattributes ├── .gitignore └── Python ├── List-1 ├── common_end.py ├── first_last6.py ├── has23.py ├── make_ends.py ├── make_pi.py ├── max_end3.py ├── middle_way.py ├── reverse3.py ├── rotate_left3.py ├── same_first_last.py ├── sum2.py └── sum3.py ├── List-2 ├── big_diff.py ├── centered_average.py ├── count_evens.py ├── has22.py ├── sum13.py └── sum67.py ├── Logic-1 ├── alarm_clock.py ├── caught_speeding.py ├── cigar_party.py ├── date_fashion.py ├── in1to10.py ├── love6.py ├── near_ten.py ├── sorta_sum.py └── squirrel_play.py ├── Logic-2 ├── close_far.py ├── lone_sum.py ├── lucky_sum.py ├── make_bricks.py ├── no_teen_sum.py └── round_sum.py ├── String-1 ├── combo_string.py ├── extra_end.py ├── first_half.py ├── first_two.py ├── hello_name.py ├── left2.py ├── make_abba.py ├── make_out_word.py ├── make_tags.py ├── non_start.py └── without_end.py ├── String-2 ├── cat_dog.py ├── count_code.py ├── count_hi.py ├── double_char.py ├── end_other.py └── xyz_there.py ├── Warmup-1 ├── diff21.py ├── front3.py ├── front_back.py ├── makes10.py ├── missing_char.py ├── monkey_trouble.py ├── near_hundred.py ├── not_string.py ├── parrot_trouble .py ├── pos_neg.py ├── sleep_in.py └── sum_double.py └── Warmup-2 ├── array123.py ├── array_count9.py ├── array_front9.py ├── front_times.py ├── last2.py ├── string_bits.py ├── string_match.py ├── string_splosion.py └── string_times.py /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | 4 | # Custom for Visual Studio 5 | *.cs diff=csharp 6 | *.sln merge=union 7 | *.csproj merge=union 8 | *.vbproj merge=union 9 | *.fsproj merge=union 10 | *.dbproj merge=union 11 | 12 | # Standard to msysgit 13 | *.doc diff=astextplain 14 | *.DOC diff=astextplain 15 | *.docx diff=astextplain 16 | *.DOCX diff=astextplain 17 | *.dot diff=astextplain 18 | *.DOT diff=astextplain 19 | *.pdf diff=astextplain 20 | *.PDF diff=astextplain 21 | *.rtf diff=astextplain 22 | *.RTF diff=astextplain 23 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ################# 2 | ## Eclipse 3 | ################# 4 | 5 | *.pydevproject 6 | .project 7 | .metadata 8 | bin/ 9 | tmp/ 10 | *.tmp 11 | *.bak 12 | *.swp 13 | *~.nib 14 | local.properties 15 | .classpath 16 | .settings/ 17 | .loadpath 18 | 19 | # External tool builders 20 | .externalToolBuilders/ 21 | 22 | # Locally stored "Eclipse launch configurations" 23 | *.launch 24 | 25 | # CDT-specific 26 | .cproject 27 | 28 | # PDT-specific 29 | .buildpath 30 | 31 | 32 | ################# 33 | ## Visual Studio 34 | ################# 35 | 36 | ## Ignore Visual Studio temporary files, build results, and 37 | ## files generated by popular Visual Studio add-ons. 38 | 39 | # User-specific files 40 | *.suo 41 | *.user 42 | *.sln.docstates 43 | 44 | # Build results 45 | [Dd]ebug/ 46 | [Rr]elease/ 47 | *_i.c 48 | *_p.c 49 | *.ilk 50 | *.meta 51 | *.obj 52 | *.pch 53 | *.pdb 54 | *.pgc 55 | *.pgd 56 | *.rsp 57 | *.sbr 58 | *.tlb 59 | *.tli 60 | *.tlh 61 | *.tmp 62 | *.vspscc 63 | .builds 64 | *.dotCover 65 | 66 | ## TODO: If you have NuGet Package Restore enabled, uncomment this 67 | #packages/ 68 | 69 | # Visual C++ cache files 70 | ipch/ 71 | *.aps 72 | *.ncb 73 | *.opensdf 74 | *.sdf 75 | 76 | # Visual Studio profiler 77 | *.psess 78 | *.vsp 79 | 80 | # ReSharper is a .NET coding add-in 81 | _ReSharper* 82 | 83 | # Installshield output folder 84 | [Ee]xpress 85 | 86 | # DocProject is a documentation generator add-in 87 | DocProject/buildhelp/ 88 | DocProject/Help/*.HxT 89 | DocProject/Help/*.HxC 90 | DocProject/Help/*.hhc 91 | DocProject/Help/*.hhk 92 | DocProject/Help/*.hhp 93 | DocProject/Help/Html2 94 | DocProject/Help/html 95 | 96 | # Click-Once directory 97 | publish 98 | 99 | # Others 100 | [Bb]in 101 | [Oo]bj 102 | sql 103 | TestResults 104 | *.Cache 105 | ClientBin 106 | stylecop.* 107 | ~$* 108 | *.dbmdl 109 | Generated_Code #added for RIA/Silverlight projects 110 | 111 | # Backup & report files from converting an old project file to a newer 112 | # Visual Studio version. Backup files are not needed, because we have git ;-) 113 | _UpgradeReport_Files/ 114 | Backup*/ 115 | UpgradeLog*.XML 116 | 117 | 118 | 119 | ############ 120 | ## Windows 121 | ############ 122 | 123 | # Windows image file caches 124 | Thumbs.db 125 | 126 | # Folder config file 127 | Desktop.ini 128 | 129 | 130 | ############# 131 | ## Python 132 | ############# 133 | 134 | *.py[co] 135 | 136 | # Packages 137 | *.egg 138 | *.egg-info 139 | dist 140 | build 141 | eggs 142 | parts 143 | bin 144 | var 145 | sdist 146 | develop-eggs 147 | .installed.cfg 148 | 149 | # Installer logs 150 | pip-log.txt 151 | 152 | # Unit test / coverage reports 153 | .coverage 154 | .tox 155 | 156 | #Translations 157 | *.mo 158 | 159 | #Mr Developer 160 | .mr.developer.cfg 161 | 162 | # Mac crap 163 | .DS_Store 164 | -------------------------------------------------------------------------------- /Python/List-1/common_end.py: -------------------------------------------------------------------------------- 1 | """ 2 | 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. 3 | 4 | common_end([1, 2, 3], [7, 3]) → True 5 | common_end([1, 2, 3], [7, 3, 2]) → False 6 | common_end([1, 2, 3], [1, 3]) → True 7 | """ 8 | 9 | def common_end(a, b): 10 | return a[0] == b[0] or a[-1] == b[-1] -------------------------------------------------------------------------------- /Python/List-1/first_last6.py: -------------------------------------------------------------------------------- 1 | """ 2 | 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. 3 | 4 | first_last6([1, 2, 6]) → True 5 | first_last6([6, 1, 2, 3]) → True 6 | first_last6([3, 2, 1]) → False 7 | """ 8 | 9 | def first_last6(nums): 10 | return 6 in (nums[0], nums[-1]) -------------------------------------------------------------------------------- /Python/List-1/has23.py: -------------------------------------------------------------------------------- 1 | """ 2 | Given an int array length 2, return True if it contains a 2 or a 3. 3 | 4 | has23([2, 5]) → True 5 | has23([4, 3]) → True 6 | has23([4, 5]) → False 7 | """ 8 | 9 | def has23(nums): 10 | return 2 in nums or 3 in nums -------------------------------------------------------------------------------- /Python/List-1/make_ends.py: -------------------------------------------------------------------------------- 1 | """ 2 | 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. 3 | 4 | make_ends([1, 2, 3]) → [1, 3] 5 | make_ends([1, 2, 3, 4]) → [1, 4] 6 | make_ends([7, 4, 6, 2]) → [7, 2] 7 | """ 8 | 9 | def make_ends(nums): 10 | return [nums[0], nums[-1]] -------------------------------------------------------------------------------- /Python/List-1/make_pi.py: -------------------------------------------------------------------------------- 1 | """ 2 | Return an int array length 3 containing the first 3 digits of pi, {3, 1, 4}. 3 | 4 | make_pi() → [3, 1, 4] 5 | """ 6 | 7 | def make_pi(): 8 | return [3, 1, 4] -------------------------------------------------------------------------------- /Python/List-1/max_end3.py: -------------------------------------------------------------------------------- 1 | """ 2 | Given an array of ints length 3, figure out which is larger between the first and last elements in the array, and set all the other elements to be that value. Return the changed array. 3 | 4 | max_end3([1, 2, 3]) → [3, 3, 3] 5 | max_end3([11, 5, 9]) → [11, 11, 11] 6 | max_end3([2, 11, 3]) → [3, 3, 3] 7 | """ 8 | 9 | def max_end3(nums): 10 | return [nums[0]] * 3 if nums[0] >= nums[-1] else [nums[-1]] * 3 -------------------------------------------------------------------------------- /Python/List-1/middle_way.py: -------------------------------------------------------------------------------- 1 | """ 2 | Given 2 int arrays, a and b, each length 3, return a new array length 2 containing their middle elements. 3 | 4 | middle_way([1, 2, 3], [4, 5, 6]) → [2, 5] 5 | middle_way([7, 7, 7], [3, 8, 0]) → [7, 8] 6 | middle_way([5, 2, 9], [1, 4, 5]) → [2, 4] 7 | """ 8 | 9 | def middle_way(a, b): 10 | return [a[1], b[1]] -------------------------------------------------------------------------------- /Python/List-1/reverse3.py: -------------------------------------------------------------------------------- 1 | """ 2 | 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}. 3 | 4 | reverse3([1, 2, 3]) → [3, 2, 1] 5 | reverse3([5, 11, 9]) → [9, 11, 5] 6 | reverse3([7, 0, 0]) → [0, 0, 7] 7 | """ 8 | 9 | def reverse3(nums): 10 | return nums[::-1] -------------------------------------------------------------------------------- /Python/List-1/rotate_left3.py: -------------------------------------------------------------------------------- 1 | """ 2 | Given an array of ints length 3, return an array with the elements "rotated left" so {1, 2, 3} yields {2, 3, 1}. 3 | 4 | rotate_left3([1, 2, 3]) → [2, 3, 1] 5 | rotate_left3([5, 11, 9]) → [11, 9, 5] 6 | rotate_left3([7, 0, 0]) → [0, 0, 7] 7 | """ 8 | 9 | def rotate_left3(nums): 10 | return [nums[1], nums[2], nums[0]] -------------------------------------------------------------------------------- /Python/List-1/same_first_last.py: -------------------------------------------------------------------------------- 1 | """ 2 | Given an array of ints, return True if the array is length 1 or more, and the first element and the last element are the same. 3 | 4 | same_first_last([1, 2, 3]) → False 5 | same_first_last([1, 2, 3, 1]) → True 6 | same_first_last([1, 2, 1]) → True 7 | """ 8 | 9 | def same_first_last(nums): 10 | return len(nums) > 0 and nums[0] == nums[-1] -------------------------------------------------------------------------------- /Python/List-1/sum2.py: -------------------------------------------------------------------------------- 1 | """ 2 | 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. 3 | 4 | sum2([1, 2, 3]) → 3 5 | sum2([1, 1]) → 2 6 | sum2([1, 1, 1, 1]) → 2 7 | """ 8 | 9 | def sum2(nums): 10 | return nums[0] + nums[1] if len(nums) > 1 else sum(nums) -------------------------------------------------------------------------------- /Python/List-1/sum3.py: -------------------------------------------------------------------------------- 1 | """ 2 | Given an array of ints length 3, return the sum of all the elements. 3 | 4 | sum3([1, 2, 3]) → 6 5 | sum3([5, 11, 2]) → 18 6 | sum3([7, 0, 0]) → 7 7 | """ 8 | 9 | def sum3(nums): 10 | return sum(nums) -------------------------------------------------------------------------------- /Python/List-2/big_diff.py: -------------------------------------------------------------------------------- 1 | """ 2 | Given an array length 1 or more of ints, 3 | return the difference between the largest and smallest values in the array. 4 | Note: the built-in min(v1, v2) and max(v1, v2) functions 5 | return the smaller or larger of two values. 6 | 7 | big_diff([10, 3, 5, 6]) → 7 8 | big_diff([7, 2, 10, 9]) → 8 9 | big_diff([2, 10, 7, 2]) → 8 10 | """ 11 | 12 | 13 | def big_diff(nums): 14 | return max(nums) - min(nums) 15 | -------------------------------------------------------------------------------- /Python/List-2/centered_average.py: -------------------------------------------------------------------------------- 1 | """ 2 | Return the "centered" average of an array of ints, 3 | which we'll say is the mean average of the values, 4 | except ignoring the largest and smallest values in the array. 5 | If there are multiple copies of the smallest value, ignore just one copy, 6 | and likewise for the largest value. 7 | Use int division to produce the final average. 8 | You may assume that the array is length 3 or more. 9 | 10 | centered_average([1, 2, 3, 4, 100]) → 3 11 | centered_average([1, 1, 5, 5, 10, 8, 7]) → 5 12 | centered_average([-10, -4, -2, -4, -2, 0]) → -3 13 | """ 14 | 15 | 16 | def centered_average(nums): 17 | small = min(nums) 18 | big = max(nums) 19 | return (sum(nums) - small - big) / (len(nums) - 2) 20 | -------------------------------------------------------------------------------- /Python/List-2/count_evens.py: -------------------------------------------------------------------------------- 1 | """ 2 | Return the number of even ints in the given array. Note: the % "mod" operator computes the remainder, e.g. 5 % 2 is 1. 3 | 4 | count_evens([2, 1, 2, 3, 4]) → 3 5 | count_evens([2, 2, 0]) → 3 6 | count_evens([1, 3, 5]) → 0 7 | """ 8 | 9 | 10 | def count_evens(nums): 11 | return sum(1 for n in nums if n % 2 == 0) 12 | -------------------------------------------------------------------------------- /Python/List-2/has22.py: -------------------------------------------------------------------------------- 1 | """ 2 | Given an array of ints, 3 | return True if the array contains a 2 next to a 2 somewhere. 4 | 5 | has22([1, 2, 2]) → True 6 | has22([1, 2, 1, 2]) → False 7 | has22([2, 1, 2]) → False 8 | """ 9 | 10 | 11 | def has22(nums): 12 | for i in range(len(nums) - 1): 13 | if nums[i] == 2 and nums[i + 1] == 2: 14 | return True 15 | 16 | return False 17 | -------------------------------------------------------------------------------- /Python/List-2/sum13.py: -------------------------------------------------------------------------------- 1 | """ 2 | Return the sum of the numbers in the array, 3 | returning 0 for an empty array. 4 | Except the number 13 is very unlucky, 5 | so it does not count and numbers that come immediately 6 | after a 13 also do not count. 7 | 8 | sum13([1, 2, 2, 1]) → 6 9 | sum13([1, 1]) → 2 10 | sum13([1, 2, 2, 1, 13]) → 6 11 | """ 12 | 13 | 14 | def sum13(nums): 15 | 16 | total = 0 17 | i = 0 18 | 19 | while i < len(nums): 20 | if nums[i] == 13: 21 | i += 2 22 | continue 23 | total += nums[i] 24 | i += 1 25 | 26 | return total 27 | -------------------------------------------------------------------------------- /Python/List-2/sum67.py: -------------------------------------------------------------------------------- 1 | """ 2 | Return the sum of the numbers in the array, 3 | except ignore sections of numbers starting with a 6 and 4 | extending to the next 7 (every 6 will be followed by at least one 7). 5 | Return 0 for no numbers. 6 | 7 | sum67([1, 2, 2]) → 5 8 | sum67([1, 2, 2, 6, 99, 99, 7]) → 5 9 | sum67([1, 1, 6, 7, 2]) → 4 10 | """ 11 | 12 | 13 | def sum67(nums): 14 | record = True 15 | total = 0 16 | 17 | for n in nums: 18 | if n == 6: 19 | record = False 20 | 21 | if record: 22 | total += n 23 | continue 24 | 25 | if n == 7: 26 | record = True 27 | 28 | return total 29 | -------------------------------------------------------------------------------- /Python/Logic-1/alarm_clock.py: -------------------------------------------------------------------------------- 1 | """ 2 | 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". 3 | 4 | alarm_clock(1, False) → '7:00' 5 | alarm_clock(5, False) → '7:00' 6 | alarm_clock(0, False) → '10:00' 7 | """ 8 | 9 | 10 | def alarm_clock(day, vacation): 11 | week_day = day not in (0, 6) 12 | 13 | if vacation: 14 | return '10:00' if week_day else 'off' 15 | 16 | return '7:00' if week_day else '10:00' 17 | -------------------------------------------------------------------------------- /Python/Logic-1/caught_speeding.py: -------------------------------------------------------------------------------- 1 | """ 2 | 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. 3 | 4 | caught_speeding(60, False) → 0 5 | caught_speeding(65, False) → 1 6 | caught_speeding(65, True) → 0 7 | """ 8 | 9 | 10 | def caught_speeding(speed, is_birthday): 11 | 12 | if is_birthday: 13 | speed -= 5 14 | 15 | if speed <= 60: 16 | return 0 17 | 18 | return 1 if 61 <= speed <= 80 else 2 19 | -------------------------------------------------------------------------------- /Python/Logic-1/cigar_party.py: -------------------------------------------------------------------------------- 1 | """ 2 | 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. 3 | 4 | cigar_party(30, False) → False 5 | cigar_party(50, False) → True 6 | cigar_party(70, True) → True 7 | """ 8 | 9 | 10 | def cigar_party(cigars, is_weekend): 11 | return cigars >= 40 if is_weekend else 40 <= cigars <= 60 12 | -------------------------------------------------------------------------------- /Python/Logic-1/date_fashion.py: -------------------------------------------------------------------------------- 1 | """ 2 | 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). 3 | 4 | date_fashion(5, 10) → 2 5 | date_fashion(5, 2) → 0 6 | date_fashion(5, 5) → 1 7 | """ 8 | 9 | 10 | def date_fashion(you, date): 11 | if you <= 2 or date <= 2: 12 | return 0 13 | 14 | if you >= 8 or date >= 8: 15 | return 2 16 | 17 | return 1 18 | -------------------------------------------------------------------------------- /Python/Logic-1/in1to10.py: -------------------------------------------------------------------------------- 1 | """ 2 | Given a number n, return True if n is in the range 1..10, inclusive. Unless "outsideMode" is True, in which case return True if the number is less or equal to 1, or greater or equal to 10. 3 | 4 | in1to10(5, False) → True 5 | in1to10(11, False) → False 6 | in1to10(11, True) → True 7 | """ 8 | 9 | 10 | def in1to10(n, outside_mode): 11 | if outside_mode: 12 | return n <= 1 or n >= 10 13 | return 1 <= n <= 10 14 | -------------------------------------------------------------------------------- /Python/Logic-1/love6.py: -------------------------------------------------------------------------------- 1 | """ 2 | 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. 3 | 4 | love6(6, 4) → True 5 | love6(4, 5) → False 6 | love6(1, 5) → True 7 | """ 8 | 9 | 10 | def love6(a, b): 11 | return a == 6 or b == 6 or abs(a - b) == 6 or a + b == 6 12 | -------------------------------------------------------------------------------- /Python/Logic-1/near_ten.py: -------------------------------------------------------------------------------- 1 | """ 2 | 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. 3 | 4 | near_ten(12) → True 5 | near_ten(17) → False 6 | near_ten(19) → True 7 | """ 8 | 9 | 10 | def near_ten(num): 11 | num_mod_10 = num % 10 12 | return num_mod_10 <= 2 or num_mod_10 >= 8 13 | -------------------------------------------------------------------------------- /Python/Logic-1/sorta_sum.py: -------------------------------------------------------------------------------- 1 | """ 2 | 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. 3 | 4 | sorta_sum(3, 4) → 7 5 | sorta_sum(9, 4) → 20 6 | sorta_sum(10, 11) → 21 7 | """ 8 | 9 | 10 | def sorta_sum(a, b): 11 | total = a + b 12 | return 20 if 10 <= total <= 19 else total 13 | -------------------------------------------------------------------------------- /Python/Logic-1/squirrel_play.py: -------------------------------------------------------------------------------- 1 | """ 2 | 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. 3 | 4 | squirrel_play(70, False) → True 5 | squirrel_play(95, False) → False 6 | squirrel_play(95, True) → True 7 | """ 8 | 9 | 10 | def squirrel_play(temp, is_summer): 11 | return 60 <= temp <= (100 if is_summer else 90) 12 | -------------------------------------------------------------------------------- /Python/Logic-2/close_far.py: -------------------------------------------------------------------------------- 1 | """ 2 | Given three ints, a b c, return True if one of b or c is "close" 3 | (differing from a by at most 1), 4 | while the other is "far", differing from both other values by 2 or more. 5 | Note: abs(num) computes the absolute value of a number. 6 | 7 | close_far(1, 2, 10) → True 8 | close_far(1, 2, 3) → False 9 | close_far(4, 1, 3) → True 10 | """ 11 | 12 | 13 | def close_far(a, b, c): 14 | a_b_diff = abs(a - b) 15 | a_c_diff = abs(a - c) 16 | b_c_diff = abs(b - c) 17 | 18 | return ( 19 | (a_b_diff <= 1 and a_c_diff >= 2 and b_c_diff >= 2) != 20 | (a_c_diff <= 1 and a_b_diff >= 2 and b_c_diff >= 2) 21 | ) 22 | -------------------------------------------------------------------------------- /Python/Logic-2/lone_sum.py: -------------------------------------------------------------------------------- 1 | """ 2 | Given 3 int values, a b c, return their sum. 3 | However, if one of the values is the same as another of the values, 4 | it does not count towards the sum. 5 | 6 | lone_sum(1, 2, 3) → 6 7 | lone_sum(3, 2, 3) → 2 8 | lone_sum(3, 3, 3) → 0 9 | """ 10 | 11 | 12 | def lone_sum(a, b, c): 13 | nums = (a, b, c) 14 | return sum(n for n in nums if nums.count(n) == 1) 15 | -------------------------------------------------------------------------------- /Python/Logic-2/lucky_sum.py: -------------------------------------------------------------------------------- 1 | """ 2 | Given 3 int values, a b c, return their sum. 3 | However, if one of the values is 13 then it does not count towards the sum and 4 | values to its right do not count. 5 | So for example, if b is 13, then both b and c do not count. 6 | 7 | lucky_sum(1, 2, 3) → 6 8 | lucky_sum(1, 2, 13) → 3 9 | lucky_sum(1, 13, 3) → 1 10 | """ 11 | 12 | 13 | def lucky_sum(a, b, c): 14 | total = 0 15 | 16 | for n in (a, b, c): 17 | if n != 13: 18 | total += n 19 | else: 20 | break 21 | 22 | return total 23 | -------------------------------------------------------------------------------- /Python/Logic-2/make_bricks.py: -------------------------------------------------------------------------------- 1 | """ 2 | We want to make a row of bricks that is goal inches long. 3 | We have a number of small bricks (1 inch each) and big bricks (5 inches each). 4 | Return True if it is possible to make the goal by choosing from the given bricks. 5 | This is a little harder than it looks and can be done without any loops. 6 | 7 | make_bricks(3, 1, 8) → True 8 | make_bricks(3, 1, 9) → False 9 | make_bricks(3, 2, 10) → True 10 | """ 11 | 12 | 13 | def make_bricks(small, big, goal): 14 | big_needed = min(big, goal // 5) 15 | return goal - (big_needed * 5) <= small 16 | -------------------------------------------------------------------------------- /Python/Logic-2/no_teen_sum.py: -------------------------------------------------------------------------------- 1 | """ 2 | Given 3 int values, a b c, return their sum. 3 | However, if any of the values is a teen -- in the range 13..19 inclusive -- 4 | then that value counts as 0, except 15 and 16 do not count as a teens. 5 | Write a separate helper "def fix_teen(n):" 6 | that takes in an int value and returns that value fixed for the teen rule. 7 | In this way, you avoid repeating the teen code 3 times (i.e. "decomposition"). 8 | Define the helper below and at the same indent level as the main no_teen_sum(). 9 | 10 | no_teen_sum(1, 2, 3) → 6 11 | no_teen_sum(2, 13, 1) → 3 12 | no_teen_sum(2, 1, 14) → 3 13 | """ 14 | 15 | 16 | def no_teen_sum(a, b, c): 17 | nums = (a, b, c) 18 | return sum(fix_teen(n) for n in nums) 19 | 20 | 21 | def fix_teen(n): 22 | return 0 if n not in (15, 16) and 13 <= n <= 19 else n 23 | -------------------------------------------------------------------------------- /Python/Logic-2/round_sum.py: -------------------------------------------------------------------------------- 1 | """ 2 | For this problem, we'll round an int value up to the next multiple of 10 if its rightmost digit is 5 or more, 3 | so 15 rounds up to 20. Alternately, round down to the previous multiple of 10 if its rightmost digit is less than 5, 4 | so 12 rounds down to 10. Given 3 ints, a b c, return the sum of their rounded values. 5 | To avoid code repetition, write a separate helper "def round10(num):" and call it 3 times. 6 | Write the helper entirely below and at the same indent level as round_sum(). 7 | 8 | round_sum(16, 17, 18) → 60 9 | round_sum(12, 13, 14) → 30 10 | round_sum(6, 4, 4) → 10 11 | """ 12 | 13 | 14 | def round_sum(a, b, c): 15 | rounded_values = [int(round(num, -1)) for num in (a, b, c)] 16 | return sum(rounded_values) 17 | -------------------------------------------------------------------------------- /Python/String-1/combo_string.py: -------------------------------------------------------------------------------- 1 | """ 2 | Given 2 strings, a and b, 3 | return a string of the form short+long+short, 4 | with the shorter string on the outside and the longer string on the inside. 5 | The strings will not be the same length, but they may be empty (length 0). 6 | 7 | combo_string('Hello', 'hi') → 'hiHellohi' 8 | combo_string('hi', 'Hello') → 'hiHellohi' 9 | combo_string('aaa', 'b') → 'baaab' 10 | """ 11 | 12 | 13 | def combo_string(a, b): 14 | return b + a + b if len(a) > len(b) else a + b + a 15 | -------------------------------------------------------------------------------- /Python/String-1/extra_end.py: -------------------------------------------------------------------------------- 1 | """ 2 | Given a string, return a new string made of 3 copies of the last 2 chars of the original string. 3 | The string length will be at least 2. 4 | 5 | extra_end('Hello') → 'lololo' 6 | extra_end('ab') → 'ababab' 7 | extra_end('Hi') → 'HiHiHi' 8 | """ 9 | 10 | 11 | def extra_end(str): 12 | return str[-2:] * 3 13 | -------------------------------------------------------------------------------- /Python/String-1/first_half.py: -------------------------------------------------------------------------------- 1 | """ 2 | Given a string of even length, return the first half. So the string "WooHoo" yields "Woo". 3 | 4 | first_half('WooHoo') → 'Woo' 5 | first_half('HelloThere') → 'Hello' 6 | first_half('abcdef') → 'abc' 7 | """ 8 | 9 | 10 | def first_half(str): 11 | return str[:len(str) / 2] 12 | -------------------------------------------------------------------------------- /Python/String-1/first_two.py: -------------------------------------------------------------------------------- 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 than length 2, 4 | return whatever there is, so "X" yields "X", 5 | and the empty string "" yields the empty string "". 6 | 7 | first_two('Hello') → 'He' 8 | first_two('abcdefg') → 'ab' 9 | first_two('ab') → 'ab' 10 | """ 11 | 12 | 13 | def first_two(str): 14 | return str[:2] 15 | -------------------------------------------------------------------------------- /Python/String-1/hello_name.py: -------------------------------------------------------------------------------- 1 | """ 2 | Given a string name, e.g. "Bob", return a greeting of the form "Hello Bob!". 3 | 4 | hello_name('Bob') → 'Hello Bob!' 5 | hello_name('Alice') → 'Hello Alice!' 6 | hello_name('X') → 'Hello X!' 7 | """ 8 | 9 | 10 | def hello_name(name): 11 | return 'Hello ' + name + '!' 12 | -------------------------------------------------------------------------------- /Python/String-1/left2.py: -------------------------------------------------------------------------------- 1 | """ 2 | Given a string, return a "rotated left 2" version where the first 2 chars 3 | are moved to the end. The string length will be at least 2. 4 | 5 | left2('Hello') → 'lloHe' 6 | left2('java') → 'vaja' 7 | left2('Hi') → 'Hi' 8 | """ 9 | 10 | 11 | def left2(str): 12 | return str[2:] + str[:2] 13 | -------------------------------------------------------------------------------- /Python/String-1/make_abba.py: -------------------------------------------------------------------------------- 1 | """ 2 | Given two strings, a and b, 3 | return the result of putting them together in the order abba, 4 | e.g. "Hi" and "Bye" returns "HiByeByeHi". 5 | 6 | make_abba('Hi', 'Bye') → 'HiByeByeHi' 7 | make_abba('Yo', 'Alice') → 'YoAliceAliceYo' 8 | make_abba('x', 'y') → 'xyyx' 9 | """ 10 | 11 | 12 | def make_abba(a, b): 13 | return a + b * 2 + a 14 | -------------------------------------------------------------------------------- /Python/String-1/make_out_word.py: -------------------------------------------------------------------------------- 1 | """ 2 | Given an "out" string length 4, such as "<<>>", and a word, 3 | return a new string where the word is in the middle of the out string, e.g. "<>". 4 | 5 | make_out_word('<<>>', 'Yay') → '<>' 6 | make_out_word('<<>>', 'WooHoo') → '<>' 7 | make_out_word('[[]]', 'word') → '[[word]]' 8 | """ 9 | 10 | 11 | def make_out_word(out, word): 12 | return out[:2] + word + out[2:] 13 | -------------------------------------------------------------------------------- /Python/String-1/make_tags.py: -------------------------------------------------------------------------------- 1 | """ 2 | The web is built with HTML strings like "Yay" which draws Yay as italic text. 3 | In this example, the "i" tag makes and which surround the word "Yay". 4 | Given tag and word strings, create the HTML string with tags around the word, e.g. "Yay". 5 | 6 | make_tags('i', 'Yay') → 'Yay' 7 | make_tags('i', 'Hello') → 'Hello' 8 | make_tags('cite', 'Yay') → 'Yay' 9 | """ 10 | 11 | 12 | def make_tags(tag, word): 13 | return '<{0}>{1}'.format(tag, word) 14 | -------------------------------------------------------------------------------- /Python/String-1/non_start.py: -------------------------------------------------------------------------------- 1 | """ 2 | Given 2 strings, return their concatenation, 3 | except omit the first char of each. The strings will be at least length 1. 4 | 5 | non_start('Hello', 'There') → 'ellohere' 6 | non_start('java', 'code') → 'avaode' 7 | non_start('shotl', 'java') → 'hotlava' 8 | """ 9 | 10 | 11 | def non_start(a, b): 12 | return a[1:] + b[1:] 13 | -------------------------------------------------------------------------------- /Python/String-1/without_end.py: -------------------------------------------------------------------------------- 1 | """ 2 | Given a string, return a version without the first and last char, so "Hello" yields "ell". 3 | The string length will be at least 2. 4 | 5 | without_end('Hello') → 'ell' 6 | without_end('java') → 'av' 7 | without_end('coding') → 'odin' 8 | """ 9 | 10 | 11 | def without_end(str): 12 | return str[1:-1] 13 | -------------------------------------------------------------------------------- /Python/String-2/cat_dog.py: -------------------------------------------------------------------------------- 1 | """ 2 | Return True if the string "cat" and "dog" appear 3 | the same number of times in the given string. 4 | 5 | cat_dog('catdog') → True 6 | cat_dog('catcat') → False 7 | cat_dog('1cat1cadodog') → True 8 | """ 9 | 10 | 11 | def cat_dog(str): 12 | return str.count('cat') == str.count('dog') 13 | -------------------------------------------------------------------------------- /Python/String-2/count_code.py: -------------------------------------------------------------------------------- 1 | """ 2 | Return the number of times that 3 | the string "code" appears anywhere in the given string, 4 | except we'll accept any letter for the 'd', so "cope" and "cooe" count. 5 | 6 | count_code('aaacodebbb') → 1 7 | count_code('codexxcode') → 2 8 | count_code('cozexxcope') → 2 9 | """ 10 | 11 | 12 | def count_code(str): 13 | return sum(1 for i in range(len(str) - 3) 14 | if str[i:i + 2] == 'co' and str[i + 3] == 'e' 15 | ) 16 | -------------------------------------------------------------------------------- /Python/String-2/count_hi.py: -------------------------------------------------------------------------------- 1 | """ 2 | Return the number of times that the string "hi" 3 | appears anywhere in the given string. 4 | 5 | count_hi('abc hi ho') → 1 6 | count_hi('ABChi hi') → 2 7 | count_hi('hihi') → 2 8 | """ 9 | 10 | 11 | def count_hi(str): 12 | return str.count('hi') 13 | -------------------------------------------------------------------------------- /Python/String-2/double_char.py: -------------------------------------------------------------------------------- 1 | """ 2 | Given a string, return a string where for every char in the original, there are two chars. 3 | 4 | double_char('The') → 'TThhee' 5 | double_char('AAbb') → 'AAAAbbbb' 6 | double_char('Hi-There') → 'HHii--TThheerree' 7 | """ 8 | 9 | 10 | def double_char(str): 11 | return ''.join(char * 2 for char in str) 12 | -------------------------------------------------------------------------------- /Python/String-2/end_other.py: -------------------------------------------------------------------------------- 1 | """ 2 | Given two strings, return True if either of the strings appears 3 | at the very end of the other string, ignoring upper/lower case differences 4 | (in other words, the computation should not be "case sensitive"). 5 | Note: s.lower() returns the lowercase version of a string. 6 | 7 | end_other('Hiabc', 'abc') → True 8 | end_other('AbC', 'HiaBc') → True 9 | end_other('abc', 'abXabc') → True 10 | """ 11 | 12 | 13 | def end_other(a, b): 14 | a = a.lower() 15 | b = b.lower() 16 | return (b.endswith(a) or a.endswith(b)) 17 | -------------------------------------------------------------------------------- /Python/String-2/xyz_there.py: -------------------------------------------------------------------------------- 1 | """ 2 | Return True if the given string contains an appearance of "xyz" 3 | where the xyz is not directly preceeded by a period (.). 4 | So "xxyz" counts but "x.xyz" does not. 5 | 6 | xyz_there('abcxyz') → True 7 | xyz_there('abc.xyz') → False 8 | xyz_there('xyz.abc') → True 9 | """ 10 | 11 | 12 | def xyz_there(str): 13 | return str.count('.xyz') != str.count('xyz') 14 | -------------------------------------------------------------------------------- /Python/Warmup-1/diff21.py: -------------------------------------------------------------------------------- 1 | """ 2 | Given an int n, return the absolute difference between n and 21, except return double the absolute difference if n is over 21. 3 | 4 | diff21(19) → 2 5 | diff21(10) → 11 6 | diff21(21) → 0 7 | """ 8 | 9 | def diff21(n): 10 | difference = abs(21-n) 11 | return difference * 2 if n > 21 else difference -------------------------------------------------------------------------------- /Python/Warmup-1/front3.py: -------------------------------------------------------------------------------- 1 | """ 2 | 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. 3 | 4 | front3('Java') → 'JavJavJav' 5 | front3('Chocolate') → 'ChoChoCho' 6 | front3('abc') → 'abcabcabc' 7 | """ 8 | 9 | def front3(str): 10 | return str[:3] * 3 -------------------------------------------------------------------------------- /Python/Warmup-1/front_back.py: -------------------------------------------------------------------------------- 1 | """ 2 | Given a string, return a new string where the first and last chars have been exchanged. 3 | 4 | front_back('code') → 'eodc' 5 | front_back('a') → 'a' 6 | front_back('ab') → 'ba' 7 | """ 8 | 9 | def front_back(str): 10 | return str if len(str) == 1 else str[-1:] + str[1:-1] + str[:1] -------------------------------------------------------------------------------- /Python/Warmup-1/makes10.py: -------------------------------------------------------------------------------- 1 | """ 2 | Given 2 ints, a and b, return True if one if them is 10 or if their sum is 10. 3 | 4 | makes10(9, 10) → True 5 | makes10(9, 9) → False 6 | makes10(1, 9) → True 7 | """ 8 | 9 | def makes10(a, b): 10 | return a+b == 10 or a == 10 or b == 10 -------------------------------------------------------------------------------- /Python/Warmup-1/missing_char.py: -------------------------------------------------------------------------------- 1 | """ 2 | 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). 3 | 4 | missing_char('kitten', 1) → 'ktten' 5 | missing_char('kitten', 0) → 'itten' 6 | missing_char('kitten', 4) → 'kittn' 7 | """ 8 | 9 | def missing_char(str, n): 10 | return str[:n] + str[n+1:] -------------------------------------------------------------------------------- /Python/Warmup-1/monkey_trouble.py: -------------------------------------------------------------------------------- 1 | """ 2 | 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. 3 | 4 | monkey_trouble(True, True) → True 5 | monkey_trouble(False, False) → True 6 | monkey_trouble(True, False) → False 7 | """ 8 | 9 | def monkey_trouble(a_smile, b_smile): 10 | return a_smile == b_smile -------------------------------------------------------------------------------- /Python/Warmup-1/near_hundred.py: -------------------------------------------------------------------------------- 1 | """ 2 | 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. 3 | 4 | near_hundred(93) → True 5 | near_hundred(90) → True 6 | near_hundred(89) → False 7 | """ 8 | 9 | def near_hundred(n): 10 | return abs(100 - n) <= 10 or abs(200 - n) <= 10 -------------------------------------------------------------------------------- /Python/Warmup-1/not_string.py: -------------------------------------------------------------------------------- 1 | """ 2 | 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. 3 | 4 | not_string('candy') → 'not candy' 5 | not_string('x') → 'not x' 6 | not_string('not bad') → 'not bad' 7 | """ 8 | 9 | def not_string(str): 10 | return str if 'not' in str[:3] else 'not ' + str -------------------------------------------------------------------------------- /Python/Warmup-1/parrot_trouble .py: -------------------------------------------------------------------------------- 1 | """ 2 | 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. 3 | 4 | parrot_trouble(True, 6) → True 5 | parrot_trouble(True, 7) → False 6 | parrot_trouble(False, 6) → False 7 | """ 8 | 9 | def parrot_trouble(talking, hour): 10 | return talking and (hour < 7 or hour > 20) 11 | -------------------------------------------------------------------------------- /Python/Warmup-1/pos_neg.py: -------------------------------------------------------------------------------- 1 | """ 2 | Given 2 int values, return True if one is negative and one is positive. Unless the parameter "negative" is True, then they both must be negative. 3 | 4 | pos_neg(1, -1, False) → True 5 | pos_neg(-1, 1, False) → True 6 | pos_neg(1, 1, False) → False 7 | """ 8 | 9 | def pos_neg(a, b, negative): 10 | return a < 0 and b < 0 if negative else (a < 0) ^ (b < 0) -------------------------------------------------------------------------------- /Python/Warmup-1/sleep_in.py: -------------------------------------------------------------------------------- 1 | """ 2 | 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. 3 | 4 | sleep_in(False, False) → True 5 | sleep_in(True, False) → False 6 | sleep_in(False, True) → True 7 | """ 8 | 9 | def sleep_in(weekday, vacation): 10 | return not weekday or vacation -------------------------------------------------------------------------------- /Python/Warmup-1/sum_double.py: -------------------------------------------------------------------------------- 1 | """ 2 | Given two int values, return their sum. Unless the two values are the same, then return double their sum. 3 | 4 | sum_double(1, 2) → 3 5 | sum_double(3, 2) → 5 6 | sum_double(2, 2) → 8 7 | """ 8 | 9 | def sum_double(a, b): 10 | sum = a + b 11 | return sum * 2 if a == b else sum -------------------------------------------------------------------------------- /Python/Warmup-2/array123.py: -------------------------------------------------------------------------------- 1 | """ 2 | Given an array of ints, return True if .. 1, 2, 3, .. appears in the array somewhere. 3 | 4 | array123([1, 1, 2, 3, 1]) → True 5 | array123([1, 1, 2, 4, 1]) → False 6 | array123([1, 1, 2, 1, 2, 3]) → True 7 | """ 8 | 9 | def array123(nums): 10 | for i in range(len(nums)-2): 11 | if nums[i] == 1 and nums[i+1] == 2 and nums[i+2] == 3: 12 | return True 13 | return False -------------------------------------------------------------------------------- /Python/Warmup-2/array_count9.py: -------------------------------------------------------------------------------- 1 | """ 2 | Given an array of ints, return the number of 9's in the array. 3 | 4 | array_count9([1, 2, 9]) → 1 5 | array_count9([1, 9, 9]) → 2 6 | array_count9([1, 9, 9, 3, 9]) → 3 7 | """ 8 | 9 | def array_count9(nums): 10 | return nums.count(9) -------------------------------------------------------------------------------- /Python/Warmup-2/array_front9.py: -------------------------------------------------------------------------------- 1 | """ 2 | 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. 3 | 4 | array_front9([1, 2, 9, 3, 4]) → True 5 | array_front9([1, 2, 3, 4, 9]) → False 6 | array_front9([1, 2, 3, 4, 5]) → False 7 | """ 8 | 9 | def array_front9(nums): 10 | return 9 in nums[:4] -------------------------------------------------------------------------------- /Python/Warmup-2/front_times.py: -------------------------------------------------------------------------------- 1 | """ 2 | 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; 3 | 4 | front_times('Chocolate', 2) → 'ChoCho' 5 | front_times('Chocolate', 3) → 'ChoChoCho' 6 | front_times('Abc', 3) → 'AbcAbcAbc' 7 | """ 8 | 9 | def front_times(str, n): 10 | return str[:3] * n 11 | 12 | -------------------------------------------------------------------------------- /Python/Warmup-2/last2.py: -------------------------------------------------------------------------------- 1 | """ 2 | 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). 3 | 4 | last2('hixxhi') → 1 5 | last2('xaxxaxaxx') → 1 6 | last2('axxxaaxx') → 2 7 | """ 8 | 9 | def last2(str): 10 | last2 = str[-2:] 11 | return sum(1 for i in range(len(str)-2) if str[i:i+2] == last2) -------------------------------------------------------------------------------- /Python/Warmup-2/string_bits.py: -------------------------------------------------------------------------------- 1 | """ 2 | Given a string, return a new string made of every other char starting with the first, so "Hello" yields "Hlo". 3 | 4 | string_bits('Hello') → 'Hlo' 5 | string_bits('Hi') → 'H' 6 | string_bits('Heeololeo') → 'Hello' 7 | """ 8 | 9 | def string_bits(str): 10 | return str[::2] -------------------------------------------------------------------------------- /Python/Warmup-2/string_match.py: -------------------------------------------------------------------------------- 1 | """ 2 | 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. 3 | 4 | string_match('xxcaazz', 'xxbaaz') → 3 5 | string_match('abc', 'abc') → 2 6 | string_match('abc', 'axc') → 0 7 | """ 8 | 9 | def string_match(a, b): 10 | shorter = min(len(a), len(b)) 11 | return sum(1 for i in range(shorter-1) if a[i:i+2] == b[i:i+2]) -------------------------------------------------------------------------------- /Python/Warmup-2/string_splosion.py: -------------------------------------------------------------------------------- 1 | """ 2 | Given a non-empty string like "Code" return a string like "CCoCodCode". 3 | 4 | string_splosion('Code') → 'CCoCodCode' 5 | string_splosion('abc') → 'aababc' 6 | string_splosion('ab') → 'aab' 7 | """ 8 | 9 | def string_splosion(str): 10 | result = '' 11 | for i in range(len(str)): 12 | result += str[:i+1] 13 | return result -------------------------------------------------------------------------------- /Python/Warmup-2/string_times.py: -------------------------------------------------------------------------------- 1 | """ 2 | Given a string and a non-negative int n, return a larger string that is n copies of the original string. 3 | 4 | string_times('Hi', 2) → 'HiHi' 5 | string_times('Hi', 3) → 'HiHiHi' 6 | string_times('Hi', 1) → 'Hi' 7 | """ 8 | 9 | def string_times(str, n): 10 | return str * n --------------------------------------------------------------------------------