|
| 1 | +# Thief |
| 2 | +In this code challenge we write a program that displays every possible combination of a 4 digit PIN. |
| 3 | + |
| 4 | +## Problem |
| 5 | +A thief has managed to find out the four digits for an online PIN code, but doesn't know the correct sequence needed to hack into the account.<br> |
| 6 | + |
| 7 | +Design and write a program that displays all the possible combinations for any four numerical digits entered by the user. The program should avoid displaying the same combination more than once. |
| 8 | + |
| 9 | +## Solution |
| 10 | +In the solution (found in `solution.py`) we are going to try and produce every possible combination including duplicates. |
| 11 | +I find this solution will be easier for a beginner to follow as it focuses on looping, and less on recursion and generators. |
| 12 | + |
| 13 | +```python |
| 14 | +# Function gets every pattern made from a string's characters. |
| 15 | +def permutations(string, idx=0): |
| 16 | + # We check if the length-1 is the same as the index. |
| 17 | + if idx == len(string) - 1: |
| 18 | + # This indicates a cycle has been completed and a new pattern has been created. |
| 19 | + # We output the new pattern to the user. |
| 20 | + print("".join(string)) |
| 21 | + |
| 22 | + # Here we simply loop through every character of the string |
| 23 | + # NOTE: idx does not change from 0 in this scope only in the recursive scopes |
| 24 | + for j in range(idx, len(string)): |
| 25 | + letters = list(string) |
| 26 | + # Python magic, we are swapping the elements at letters[idx] and letters[j] |
| 27 | + letters[idx], letters[j] = letters[j], letters[idx] |
| 28 | + # We make a recursive call and increase idx |
| 29 | + # Remember once idx is the same as length-1 we will exit back into the original scope. |
| 30 | + # Once in the original scope, idx is still 0, and j will increment. |
| 31 | + permutations(letters, idx + 1) |
| 32 | +``` |
| 33 | +To see why this solution works, it's best to unravel the loops and function calls. |
| 34 | +This allows us to see exactly what's happening without all the hidden parts. |
| 35 | +Since unravelling everything can make our code very long, we will swap out a 4 digit PIN with a set of two letters. |
| 36 | +```python |
| 37 | +# Original scope of the function |
| 38 | +permutations(string, idx=0): |
| 39 | + string = "ab" |
| 40 | + idx = 0 |
| 41 | + if idx == len(string) - 1: # This is false |
| 42 | + print("".join(string)) |
| 43 | + |
| 44 | + # First cycle of for loop |
| 45 | + j = 0 |
| 46 | + letters = list(string) |
| 47 | + letters[idx], letters[j] = letters[j], letters[idx] # swap "a" with "a" (does nothing) |
| 48 | + |
| 49 | + # Recursive function call (increment idx by 1) |
| 50 | + permutations(letters, idx + 1): |
| 51 | + idx = 1 |
| 52 | + if idx == len(letters) - 1: # This is true |
| 53 | + print("".join(letters)) # print "ab" |
| 54 | + j = 1 |
| 55 | + letters[idx], letters[j] = letters[j], letters[idx] # swap "b" with "b" (does nothing) |
| 56 | + |
| 57 | + # Recursive function call (increment idx by 1) |
| 58 | + permutations(letters, idx + 1): |
| 59 | + idx = 2 |
| 60 | + if idx == len(string) - 1: # This is false |
| 61 | + print("".join(string)) |
| 62 | + j = 2 |
| 63 | + # Loop does not occur as j and idx are the same. |
| 64 | + |
| 65 | + # Loop finishes because j is equal to len(string). |
| 66 | + |
| 67 | + # Next cycle of the for loop |
| 68 | + j = 1 |
| 69 | + letters[idx], letters[j] = letters[j], letters[idx] # swap "a" with "b" (letters is now "ba") |
| 70 | + |
| 71 | + # Recursive function call (increment idx by 1) |
| 72 | + permutations(letters, idx + 1): |
| 73 | + idx = 1 |
| 74 | + if idx == len(letters) - 1: # This is true |
| 75 | + print("".join(letters)) # print "ab" |
| 76 | + j = 1 |
| 77 | + letters[idx], letters[j] = letters[j], letters[idx] # swap "b" with "b" (does nothing) |
| 78 | + |
| 79 | + # Recursive function call (increment idx by 1) |
| 80 | + permutations(letters, idx + 1): |
| 81 | + idx = 2 |
| 82 | + if idx == len(string) - 1: # This is false |
| 83 | + print("".join(string)) |
| 84 | + j = 2 |
| 85 | + # Loop does not occur as j and idx are the same. |
| 86 | + |
| 87 | + # Loop finishes because j is equal to len(string). |
| 88 | + |
| 89 | + # Loop finishes |
| 90 | + # Function exits |
| 91 | + # We have printed both "ab" and "ba", all possible combinations. |
| 92 | +``` |
| 93 | + |
| 94 | +## Extension |
| 95 | +In the extension, our goal is to ensure none of the combinations are duplicates. |
| 96 | + |
| 97 | +### Extension Solution |
| 98 | +In our extension solution, we make use of Python's generators, and recursion to make a generator that calls itself and yields back along calls until the user of the generator receives the calls. |
| 99 | +The solution below can be found in `solution_advanced.py`. |
| 100 | +```python |
| 101 | +# Our new function is a generator. |
| 102 | +# This means it yields values every cycle of a loop. |
| 103 | +def permutations(string): |
| 104 | + if len(string) <= 1: |
| 105 | + yield string |
| 106 | + else: |
| 107 | + # The recursive function calls yield values backwards until they reach the |
| 108 | + # original function scope, in which they are yielded back to the user. |
| 109 | + for i in range(len(string)): |
| 110 | + for p in permutations(string[:i] + string[i + 1:]): |
| 111 | + yield string[i] + p |
| 112 | +``` |
| 113 | +We would then draw yielded values from the generator using our own loop. |
| 114 | +```python |
| 115 | +for pattern in permutations("ABC"): |
| 116 | + print(pattern) |
| 117 | +``` |
| 118 | +This will print every possible combination from the string "ABC". |
| 119 | +However, we must still filter out the duplicates. |
| 120 | +To do this, we would create another list and append all values from patterns that is not already in our list. |
| 121 | +This means when we first come across a value we append it, if we find a value that is the same we will not append it as it is already in the list. |
| 122 | +```python |
| 123 | +# Here we loop through the generator's yields to get every pattern possible. |
| 124 | +patterns = [pattern for pattern in permutations(PIN)] |
| 125 | + |
| 126 | +# Here we filter out repeated values |
| 127 | +unique_patterns = [] |
| 128 | +for pattern in patterns: |
| 129 | + # If a pattern is already in unique_patterns then it is a duplicate. |
| 130 | + # We do not print or append the duplicate pattern. |
| 131 | + if pattern not in unique_patterns: |
| 132 | + unique_patterns.append(pattern) |
| 133 | + print(pattern) |
| 134 | +``` |
| 135 | + |
| 136 | +### Extension Solution 2 (Real world solution) |
| 137 | +Python is a language with "batteries included" a phrase meaning the language has lots of built-in ways of handling tasks we may want to do. |
| 138 | +In some programming languages, writing our own `permutation` function is required as the language does not have a built-in way of getting the permutations of a string or list. |
| 139 | +However, in Python, there is a library called `itertools` already available to us. |
| 140 | +`itertools` has a function called `permutations` that allows us to get every possible combination of a string or list very quickly. |
| 141 | + |
| 142 | +An example of `itertools.permutations` can be found in `solution_advanced2.py`. |
| 143 | +```python |
| 144 | +# itertools.permutations(PIN) is a generator that yields every pattern. |
| 145 | +patterns = [pattern for pattern in itertools.permutations(PIN)] |
| 146 | +``` |
| 147 | +The `itertools` library is part of Python's standard library. |
| 148 | +This means, whenever you are using Python, you will always have access to `itertools` and `itertools` is written in the C programming language. |
| 149 | +This makes `itertools` much faster than our Python versions. |
| 150 | + |
0 commit comments