Part I Exercises
1 Introduction to Programming Exercises . . . . . . . . . . . . . . . . . . . . 3
Exercise 1: Mailing Address . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
Exercise 2: Hello . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
Exercise 3: Area of a Room . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
Exercise 4: Area of a Field . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
Exercise 5: Bottle Deposits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
Exercise 6: Tax and Tip . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
Exercise 7: Sum of the First n Positive Integers . . . . . . . . . . . . . . . . 5
Exercise 8: Widgets and Gizmos . . . . . . . . . . . . . . . . . . . . . . . . . . 5
Exercise 9: Compound Interest. . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
Exercise 10: Arithmetic. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
Exercise 11: Fuel Efficiency . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
Exercise 12: Distance Between Two Points on Earth . . . . . . . . . . . . 6
Exercise 13: Making Change . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Exercise 14: Height Units . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Exercise 15: Distance Units . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
Exercise 16: Area and Volume. . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
Exercise 17: Heat Capacity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
Exercise 18: Volume of a Cylinder. . . . . . . . . . . . . . . . . . . . . . . . . 9
Exercise 19: Free Fall . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Exercise 20: Ideal Gas Law . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Exercise 21: Area of a Triangle . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
Exercise 22: Area of a Triangle (Again) . . . . . . . . . . . . . . . . . . . . . 10
Exercise 23: Area of a Regular Polygon . . . . . . . . . . . . . . . . . . . . . 10
Exercise 24: Units of Time . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
Exercise 25: Units of Time (Again) . . . . . . . . . . . . . . . . . . . . . . . . 11
Exercise 26: Current Time. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
Exercise 27: Body Mass Index. . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
Exercise 28: Wind Chill . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
Exercise 29: Celsius to Fahrenheit and Kelvin . . . . . . . . . . . . . . . . . 12
Exercise 30: Units of Pressure . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
Exercise 31: Sum of the Digits in an Integer . . . . . . . . . . . . . . . . . . 13
Exercise 32: Sort 3 Integers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
Exercise 33: Day Old Bread . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
2 If Statement Exercises. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
Exercise 34: Even or Odd? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
Exercise 35: Dog Years . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
Exercise 36: Vowel or Consonant. . . . . . . . . . . . . . . . . . . . . . . . . . 16
Exercise 37: Name that Shape . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
Exercise 38: Month Name to Number of Days. . . . . . . . . . . . . . . . . 16
Exercise 39: Sound Levels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
Exercise 40: Name that Triangle . . . . . . . . . . . . . . . . . . . . . . . . . . 17
Exercise 41: Note To Frequency . . . . . . . . . . . . . . . . . . . . . . . . . . 17
Exercise 42: Frequency To Note . . . . . . . . . . . . . . . . . . . . . . . . . . 18
Exercise 43: Faces on Money . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
Exercise 44: Date to Holiday Name . . . . . . . . . . . . . . . . . . . . . . . . 19
Exercise 45: What Color is that Square? . . . . . . . . . . . . . . . . . . . . . 20
Exercise 46: Season from Month and Day. . . . . . . . . . . . . . . . . . . . 20
Exercise 47: Birth Date to Astrological Sign . . . . . . . . . . . . . . . . . . 21
Exercise 48: Chinese Zodiac . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
Exercise 49: Richter Scale . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
Exercise 50: Roots of a Quadratic Function . . . . . . . . . . . . . . . . . . . 23
Exercise 51: Letter Grade to Grade Points . . . . . . . . . . . . . . . . . . . . 23
Exercise 52: Grade Points to Letter Grade . . . . . . . . . . . . . . . . . . . . 24
Exercise 53: Assessing Employees . . . . . . . . . . . . . . . . . . . . . . . . . 24
Exercise 54: Wavelengths of Visible Light . . . . . . . . . . . . . . . . . . . 24
Exercise 55: Frequency to Name . . . . . . . . . . . . . . . . . . . . . . . . . . 25
Exercise 56: Cell Phone Bill . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
Exercise 57: Is it a Leap Year? . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
Exercise 58: Next Day . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
Exercise 59: Is a License Plate Valid?. . . . . . . . . . . . . . . . . . . . . . . 26
Exercise 60: Roulette Payouts . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
3 Loop Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
Exercise 61: Average . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
Exercise 62: Discount Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
Exercise 63: Temperature Conversion Table . . . . . . . . . . . . . . . . . . 30
Exercise 64: No More Pennies . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
Exercise 65: Compute the Perimeter of a Polygon . . . . . . . . . . . . . . 30
Exercise 66: Compute a Grade Point Average . . . . . . . . . . . . . . . . . 31
Exercise 67: Admission Price. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
Exercise 68: Parity Bits. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
Exercise 69: Approximate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
Exercise 70: Caesar Cipher . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
Exercise 71: Square Root . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
Exercise 72: Is a String a Palindrome? . . . . . . . . . . . . . . . . . . . . . . 33
Exercise 73: Multiple Word Palindromes. . . . . . . . . . . . . . . . . . . . . 34
Exercise 74: Multiplication Table . . . . . . . . . . . . . . . . . . . . . . . . . . 34
Exercise 75: Greatest Common Divisor. . . . . . . . . . . . . . . . . . . . . . 35
Exercise 76: Prime Factors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
Exercise 77: Binary to Decimal . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
Exercise 78: Decimal to Binary . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
Exercise 79: Maximum Integer . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
Exercise 80: Coin Flip Simulation . . . . . . . . . . . . . . . . . . . . . . . . . 37
4 Function Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
Exercise 81: Compute the Hypotenuse . . . . . . . . . . . . . . . . . . . . . . 39
Exercise 82: Taxi Fare . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
Exercise 83: Shipping Calculator . . . . . . . . . . . . . . . . . . . . . . . . . . 40
Exercise 84: Median of Three Values . . . . . . . . . . . . . . . . . . . . . . . 40
Exercise 85: Convert an Integer to its Ordinal Number . . . . . . . . . . . 40
Exercise 86: The Twelve Days of Christmas . . . . . . . . . . . . . . . . . . 41
Exercise 87: Center a String in the Terminal . . . . . . . . . . . . . . . . . . 41
Exercise 88: Is it a Valid Triangle? . . . . . . . . . . . . . . . . . . . . . . . . 42
Exercise 89: Capitalize It . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
Exercise 90: Does a String Represent an Integer? . . . . . . . . . . . . . . . 42
Exercise 91: Operator Precedence. . . . . . . . . . . . . . . . . . . . . . . . . . 43
Exercise 92: Is a Number Prime? . . . . . . . . . . . . . . . . . . . . . . . . . . 43
Exercise 93: Next Prime . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
Exercise 94: Random Password . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
Exercise 95: Random License Plate . . . . . . . . . . . . . . . . . . . . . . . . 44
Exercise 96: Check a Password . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
Exercise 97: Random Good Password. . . . . . . . . . . . . . . . . . . . . . . 45
Exercise 98: Hexadecimal and Decimal Digits . . . . . . . . . . . . . . . . . 45
Exercise 99: Arbitrary Base Conversions. . . . . . . . . . . . . . . . . . . . . 45
Exercise 100: Days in a Month . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
Exercise 101: Reduce a Fraction to Lowest Terms . . . . . . . . . . . . . . 46
Exercise 102: Reduce Measures . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
Exercise 103: Magic Dates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
5 List Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
Exercise 104: Sorted Order . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
Exercise 105: Reverse Order . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
Exercise 106: Remove Outliers . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
Exercise 107: Avoiding Duplicates . . . . . . . . . . . . . . . . . . . . . . . . . 50
Exercise 108: Negatives, Zeros and Positives. . . . . . . . . . . . . . . . . . 50
Exercise 109: List of Proper Divisors . . . . . . . . . . . . . . . . . . . . . . . 51
Exercise 110: Perfect Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
Exercise 111: Only the Words . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
Exercise 112: Below and Above Average . . . . . . . . . . . . . . . . . . . . 52
Exercise 113: Formatting a List . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
Exercise 114: Random Lottery Numbers . . . . . . . . . . . . . . . . . . . . . 52
Exercise 115: Pig Latin. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
Exercise 116: Pig Latin Improved . . . . . . . . . . . . . . . . . . . . . . . . . 53
Exercise 117: Line of Best Fit . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
Exercise 118: Shuffling a Deck of Cards. . . . . . . . . . . . . . . . . . . . . 54
Exercise 119: Dealing Hands of Cards . . . . . . . . . . . . . . . . . . . . . . 55
Exercise 120: Is a List already in Sorted Order?. . . . . . . . . . . . . . . . 55
Exercise 121: Count the Elements . . . . . . . . . . . . . . . . . . . . . . . . . 56
Exercise 122: Tokenizing a String . . . . . . . . . . . . . . . . . . . . . . . . . 56
Exercise 123: Infix to Postfix. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
Exercise 124: Evaluate Postfix . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
Exercise 125: Does a List contain a Sublist? . . . . . . . . . . . . . . . . . . 58
Exercise 126: Generate All Sublists of a List . . . . . . . . . . . . . . . . . . 59
Exercise 127: The Sieve of Eratosthenes . . . . . . . . . . . . . . . . . . . . . 59
6 Dictionary Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
Exercise 128: Reverse Lookup . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
Exercise 129: Two Dice Simulation . . . . . . . . . . . . . . . . . . . . . . . . 62
Exercise 130: Text Messaging . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
Exercise 131: Morse Code. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
Exercise 132: Postal Codes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
Exercise 133: Write Out Numbers in English. . . . . . . . . . . . . . . . . . 65
Exercise 134: Unique Characters . . . . . . . . . . . . . . . . . . . . . . . . . . 65
Exercise 135: Anagrams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
Exercise 136: Anagrams Again . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
Exercise 137: Scrabble™ Score . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
Exercise 138: Create a Bingo Card . . . . . . . . . . . . . . . . . . . . . . . . . 66
Exercise 139: Checking for a Winning Card . . . . . . . . . . . . . . . . . . 67
Exercise 140: Play Bingo . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
7 File and Exception Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
Exercise 141: Display the Head of a File . . . . . . . . . . . . . . . . . . . . 69
Exercise 142: Display the Tail of a File . . . . . . . . . . . . . . . . . . . . . 70
Exercise 143: Concatenate Multiple Files . . . . . . . . . . . . . . . . . . . . 70
Exercise 144: Number the Lines in a File . . . . . . . . . . . . . . . . . . . . 70
Exercise 145: Find the Longest Word in a File . . . . . . . . . . . . . . . . 71
Exercise 146: Letter Frequencies . . . . . . . . . . . . . . . . . . . . . . . . . . 71
Exercise 147: Words that Occur Most. . . . . . . . . . . . . . . . . . . . . . . 71
Exercise 148: Sum a List of Numbers. . . . . . . . . . . . . . . . . . . . . . . 72
Exercise 149: Both Letter Grades and Grade Points . . . . . . . . . . . . . 72
Exercise 150: Remove Comments . . . . . . . . . . . . . . . . . . . . . . . . . 72
Exercise 151: Two Word Random Password . . . . . . . . . . . . . . . . . . 73
Exercise 152: What’s that Element Again? . . . . . . . . . . . . . . . . . . . 73
Exercise 153: A Book with No “e” .... . . . . . . . . . . . . . . . . . . . . . . 73
Exercise 154: Names that Reached Number One . . . . . . . . . . . . . . . 74
Exercise 155: Gender Neutral Names . . . . . . . . . . . . . . . . . . . . . . . 74
Exercise 156: Most Births in a given Time Period . . . . . . . . . . . . . . 74
Exercise 157: Distinct Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
Exercise 158: Spell Checker . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
Exercise 159: Repeated Words. . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
Exercise 160: Redacting Text in a File . . . . . . . . . . . . . . . . . . . . . . 76
Exercise 161: Missing Comments. . . . . . . . . . . . . . . . . . . . . . . . . . 76
Exercise 162: Consistent Line Lengths . . . . . . . . . . . . . . . . . . . . . . 77
Exercise 163: Words with Six Vowels in Order . . . . . . . . . . . . . . . . 78
8 Recursion Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
Exercise 164: Total the Values. . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
Exercise 165: Greatest Common Divisor . . . . . . . . . . . . . . . . . . . . . 80
Exercise 166: Recursive Decimal to Binary . . . . . . . . . . . . . . . . . . . 80
Exercise 167: Recursive Palindrome . . . . . . . . . . . . . . . . . . . . . . . . 80
Exercise 168: Recursive Square Root . . . . . . . . . . . . . . . . . . . . . . . 81
Exercise 169: String Edit Distance . . . . . . . . . . . . . . . . . . . . . . . . . 81
Exercise 170: Possible Change. . . . . . . . . . . . . . . . . . . . . . . . . . . . 82
Exercise 171: Spelling with Element Symbols . . . . . . . . . . . . . . . . . 82
Exercise 172: Element Sequences. . . . . . . . . . . . . . . . . . . . . . . . . . 83
Exercise 173: Run-Length Decoding. . . . . . . . . . . . . . . . . . . . . . . . 83
Exercise 174: Run-Length Encoding . . . . . . . . . . . . . . . . . . . . . . . . 84
Part II Solutions
9 Introduction to Programming Solutions . . . . . . . . . . . . . . . . . . . . 87
Solution to Exercise 1: Mailing Address . . . . . . . . . . . . . . . . . . . . . 87
Solution to Exercise 3: Area of a Room . . . . . . . . . . . . . . . . . . . . . 87
Solution to Exercise 4: Area of a Field . . . . . . . . . . . . . . . . . . . . . . 88
Solution to Exercise 5: Bottle Deposits . . . . . . . . . . . . . . . . . . . . . . 88
Solution to Exercise 6: Tax and Tip . . . . . . . . . . . . . . . . . . . . . . . . 89
Solution to Exercise 7: Sum of the First n Positive Integers. . . . . . . . 89
Solution to Exercise 10: Arithmetic . . . . . . . . . . . . . . . . . . . . . . . . 90
Solution to Exercise 13: Making Change. . . . . . . . . . . . . . . . . . . . . 90
Solution to Exercise 14: Height Units . . . . . . . . . . . . . . . . . . . . . . . 91
Solution to Exercise 17: Heat Capacity . . . . . . . . . . . . . . . . . . . . . . 92
Solution to Exercise 19: Free Fall . . . . . . . . . . . . . . . . . . . . . . . . . 92
Solution to Exercise 23: Area of a Regular Polygon . . . . . . . . . . . . . 93
Solution to Exercise 25: Units of Time (Again) . . . . . . . . . . . . . . . . 93
Solution to Exercise 28: Wind Chill . . . . . . . . . . . . . . . . . . . . . . . . 94
Solution to Exercise 32: Sort 3 Integers . . . . . . . . . . . . . . . . . . . . . 94
Solution to Exercise 33: Day Old Bread . . . . . . . . . . . . . . . . . . . . . 95
10 If Statement Solutions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97
Solution to Exercise 34: Even or Odd? . . . . . . . . . . . . . . . . . . . . . . 97
Solution to Exercise 36: Vowel or Consonant . . . . . . . . . . . . . . . . . 97
Solution to Exercise 37: Name that Shape . . . . . . . . . . . . . . . . . . . . 98
Solution to Exercise 38: Month Name to Number of Days . . . . . . . . 98
Solution to Exercise 40: Name that Triangle . . . . . . . . . . . . . . . . . . 99
Solution to Exercise 41: Note to Frequency . . . . . . . . . . . . . . . . . . . 99
Solution to Exercise 42: Frequency to Note . . . . . . . . . . . . . . . . . . . 100
Solution to Exercise 46: Season from Month and Day . . . . . . . . . . . 101
Solution to Exercise 48: Chinese Zodiac . . . . . . . . . . . . . . . . . . . . . 102
Solution to Exercise 51: Letter Grade to Grade Points. . . . . . . . . . . . 103
Solution to Exercise 53: Assessing Employees . . . . . . . . . . . . . . . . . 104
Solution to Exercise 57: Is it a Leap Year? . . . . . . . . . . . . . . . . . . . 105
Solution to Exercise 59: Is a License Plate Valid? . . . . . . . . . . . . . . 105
Solution to Exercise 60: Roulette Payouts . . . . . . . . . . . . . . . . . . . . 106
11 Loop Solutions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107
Solution to Exercise 64: No more Pennies. . . . . . . . . . . . . . . . . . . . 107
Solution to Exercise 65: Computer the Perimeter of a Polygon. . . . . . 108
Solution to Exercise 67: Admission Price . . . . . . . . . . . . . . . . . . . . 109
Solution to Exercise 68: Parity Bits . . . . . . . . . . . . . . . . . . . . . . . . 109
Solution to Exercise 70: Caesar Cipher . . . . . . . . . . . . . . . . . . . . . . 110
Solution to Exercise 72: Is a String a Palindrome? . . . . . . . . . . . . . . 111
Solution to Exercise 74: Multiplication Table. . . . . . . . . . . . . . . . . . 111
Solution to Exercise 75: Greatest Common Divisor . . . . . . . . . . . . . 112
Solution to Exercise 78: Decimal to Binary . . . . . . . . . . . . . . . . . . . 112
Solution to Exercise 79: Maximum Integer . . . . . . . . . . . . . . . . . . . 113
12 Function Solutions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115
Solution to Exercise 84: Median of Three Values. . . . . . . . . . . . . . . 115
Solution to Exercise 86: The Twelve days of Christmas . . . . . . . . . . 116
Solution to Exercise 87: Center a String in the Terminal . . . . . . . . . . 117
Solution to Exercise 89: Capitalize it . . . . . . . . . . . . . . . . . . . . . . . 118
Solution to Exercise 90: Does a String Represent an Integer? . . . . . . 119
Solution to Exercise 92: Is a Number Prime?. . . . . . . . . . . . . . . . . . 119
Solution to Exercise 94: Random Password . . . . . . . . . . . . . . . . . . . 120
Solution to Exercise 96: Check a Password . . . . . . . . . . . . . . . . . . . 121
Solution to Exercise 99: Arbitrary Base Conversions . . . . . . . . . . . . 121
Solution to Exercise 101: Reduce a Fraction to Lowest Terms . . . . . . 123
Solution to Exercise 102: Reduce Measures. . . . . . . . . . . . . . . . . . . 124
Solution to Exercise 103: Magic Dates . . . . . . . . . . . . . . . . . . . . . . 126
13 List Solutions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127
Solution to Exercise 104: Sorted Order . . . . . . . . . . . . . . . . . . . . . . 127
Solution to Exercise 106: Remove Outliers . . . . . . . . . . . . . . . . . . . 128
Solution to Exercise 107: Avoiding Duplicates. . . . . . . . . . . . . . . . . 129
Solution to Exercise 108: Negatives, Zeros and Positives . . . . . . . . . 129
Solution to Exercise 110: Perfect Numbers . . . . . . . . . . . . . . . . . . . 130
Solution to Exercise 113: Formatting a List . . . . . . . . . . . . . . . . . . . 131
Solution to Exercise 114: Random Lottery Numbers. . . . . . . . . . . . . 132
Solution to Exercise 118: Shuffling a Deck of Cards . . . . . . . . . . . . 132
Solution to Exercise 121: Count the Elements . . . . . . . . . . . . . . . . . 133
Solution to Exercise 122: Tokenizing a String . . . . . . . . . . . . . . . . . 134
Solution to Exercise 126: Generate All Sublists of a List. . . . . . . . . . 136
Solution to Exercise 127: The Sieve of Eratosthenes. . . . . . . . . . . . . 136
14 Dictionary Solutions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139
Solution to Exercise 128: Reverse Lookup . . . . . . . . . . . . . . . . . . . 139
Solution to Exercise 129: Two Dice Simulation . . . . . . . . . . . . . . . . 140
Solution to Exercise 134: Unique Characters . . . . . . . . . . . . . . . . . . 141
Solution to Exercise 135: Anagrams . . . . . . . . . . . . . . . . . . . . . . . . 141
Solution to Exercise 137: Scrabble™ Score . . . . . . . . . . . . . . . . . . . 142
Solution to Exercise 138: Create a Bingo Card . . . . . . . . . . . . . . . . 143
15 File and Exception Solutions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145
Solution to Exercise 141: Display the Head of a File . . . . . . . . . . . . 145
Solution to Exercise 142: Display the Tail of a File . . . . . . . . . . . . . 146
Solution to Exercise 143: Concatenate Multiple Files . . . . . . . . . . . . 146
Solution to Exercise 148: Sum a List of Numbers . . . . . . . . . . . . . . 147
Solution to Exercise 150: Remove Comments . . . . . . . . . . . . . . . . . 148
Solution to Exercise 151: Two Word Random Password . . . . . . . . . . 149
Solution to Exercise 153: A Book with No “e” ... . . . . . . . . . . . . . . 149
Solution to Exercise 154: Names that Reached Number One . . . . . . . 151
Solution to Exercise 158: Spell Checker . . . . . . . . . . . . . . . . . . . . . 152
Solution to Exercise 160: Redacting Text in a File . . . . . . . . . . . . . . 153
Solution to Exercise 161: Missing Comments . . . . . . . . . . . . . . . . . 154
16 Recursion Solutions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157
Solution to Exercise 164: Total the Values . . . . . . . . . . . . . . . . . . . 157
Solution to Exercise 167: Recursive Palindrome. . . . . . . . . . . . . . . . 157
Solution to Exercise 169: String Edit Distance . . . . . . . . . . . . . . . . . 158
Solution to Exercise 172: Element Sequences . . . . . . . . . . . . . . . . . 159
Solution to Exercise 174: Run-Length Encoding . . . . . . . . . . . . . . . 161
Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
- Log in to post comments