From 5402cee2dad5c4861a6f8e8e01f57011a86d2a43 Mon Sep 17 00:00:00 2001 From: Xevion Date: Wed, 24 Jul 2019 14:37:17 -0500 Subject: [PATCH] spiral matrix and prime factors exercise --- README.md | 9 -- python/prime-factors/.exercism/metadata.json | 1 + python/prime-factors/README.md | 79 ++++++++++ python/prime-factors/prime_factors.py | 8 + python/prime-factors/prime_factors_test.py | 32 ++++ python/simple-cipher/.exercism/metadata.json | 1 + python/simple-cipher/README.md | 146 +++++++++++++++++++ python/simple-cipher/simple_cipher.py | 26 ++++ python/simple-cipher/simple_cipher_test.py | 78 ++++++++++ python/spiral-matrix/.exercism/metadata.json | 1 + python/spiral-matrix/README.md | 73 ++++++++++ python/spiral-matrix/spiral_matrix.py | 53 +++++++ python/spiral-matrix/spiral_matrix_test.py | 51 +++++++ 13 files changed, 549 insertions(+), 9 deletions(-) delete mode 100644 README.md create mode 100644 python/prime-factors/.exercism/metadata.json create mode 100644 python/prime-factors/README.md create mode 100644 python/prime-factors/prime_factors.py create mode 100644 python/prime-factors/prime_factors_test.py create mode 100644 python/simple-cipher/.exercism/metadata.json create mode 100644 python/simple-cipher/README.md create mode 100644 python/simple-cipher/simple_cipher.py create mode 100644 python/simple-cipher/simple_cipher_test.py create mode 100644 python/spiral-matrix/.exercism/metadata.json create mode 100644 python/spiral-matrix/README.md create mode 100644 python/spiral-matrix/spiral_matrix.py create mode 100644 python/spiral-matrix/spiral_matrix_test.py diff --git a/README.md b/README.md deleted file mode 100644 index f3bd647..0000000 --- a/README.md +++ /dev/null @@ -1,9 +0,0 @@ -# Exercism - -## Purpose - -This is just a repo for storing all of my Exercism exercises somewhere off-site, as well as to build up my git experience and usage. - -## Sections - -* **[Python](./python/README.md)** diff --git a/python/prime-factors/.exercism/metadata.json b/python/prime-factors/.exercism/metadata.json new file mode 100644 index 0000000..6f1dd1a --- /dev/null +++ b/python/prime-factors/.exercism/metadata.json @@ -0,0 +1 @@ +{"track":"python","exercise":"prime-factors","id":"8594f50d455a42c2bcaaf99a23da57ec","url":"https://exercism.io/my/solutions/8594f50d455a42c2bcaaf99a23da57ec","handle":"Xevion","is_requester":true,"auto_approve":false} \ No newline at end of file diff --git a/python/prime-factors/README.md b/python/prime-factors/README.md new file mode 100644 index 0000000..c43f219 --- /dev/null +++ b/python/prime-factors/README.md @@ -0,0 +1,79 @@ +# Prime Factors + +Compute the prime factors of a given natural number. + +A prime number is only evenly divisible by itself and 1. + +Note that 1 is not a prime number. + +## Example + +What are the prime factors of 60? + +- Our first divisor is 2. 2 goes into 60, leaving 30. +- 2 goes into 30, leaving 15. + - 2 doesn't go cleanly into 15. So let's move on to our next divisor, 3. +- 3 goes cleanly into 15, leaving 5. + - 3 does not go cleanly into 5. The next possible factor is 4. + - 4 does not go cleanly into 5. The next possible factor is 5. +- 5 does go cleanly into 5. +- We're left only with 1, so now, we're done. + +Our successful divisors in that computation represent the list of prime +factors of 60: 2, 2, 3, and 5. + +You can check this yourself: + +- 2 * 2 * 3 * 5 +- = 4 * 15 +- = 60 +- Success! + +## Exception messages + +Sometimes it is necessary to raise an exception. When you do this, you should include a meaningful error message to +indicate what the source of the error is. This makes your code more readable and helps significantly with debugging. Not +every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include +a message. + +To raise a message with an exception, just write it as an argument to the exception type. For example, instead of +`raise Exception`, you should write: + +```python +raise Exception("Meaningful message indicating the source of the error") +``` + +## Running the tests + +To run the tests, run the appropriate command below ([why they are different](https://github.com/pytest-dev/pytest/issues/1629#issue-161422224)): + +- Python 2.7: `py.test prime_factors_test.py` +- Python 3.4+: `pytest prime_factors_test.py` + +Alternatively, you can tell Python to run the pytest module (allowing the same command to be used regardless of Python version): +`python -m pytest prime_factors_test.py` + +### Common `pytest` options + +- `-v` : enable verbose output +- `-x` : stop running tests on first failure +- `--ff` : run failures from previous test before running other test cases + +For other options, see `python -m pytest -h` + +## Submitting Exercises + +Note that, when trying to submit an exercise, make sure the solution is in the `$EXERCISM_WORKSPACE/python/prime-factors` directory. + +You can find your Exercism workspace by running `exercism debug` and looking for the line that starts with `Workspace`. + +For more detailed information about running tests, code style and linting, +please see [Running the Tests](http://exercism.io/tracks/python/tests). + +## Source + +The Prime Factors Kata by Uncle Bob [http://butunclebob.com/ArticleS.UncleBob.ThePrimeFactorsKata](http://butunclebob.com/ArticleS.UncleBob.ThePrimeFactorsKata) + +## Submitting Incomplete Solutions + +It's possible to submit an incomplete solution so you can see how others have completed the exercise. diff --git a/python/prime-factors/prime_factors.py b/python/prime-factors/prime_factors.py new file mode 100644 index 0000000..cfb9c1b --- /dev/null +++ b/python/prime-factors/prime_factors.py @@ -0,0 +1,8 @@ +def factors(value): + factors, n = [], 2 + while value > 1: + while value % n == 0: + factors.append(n) + value /= n + n += 1 + return factors \ No newline at end of file diff --git a/python/prime-factors/prime_factors_test.py b/python/prime-factors/prime_factors_test.py new file mode 100644 index 0000000..35d1982 --- /dev/null +++ b/python/prime-factors/prime_factors_test.py @@ -0,0 +1,32 @@ +import unittest + +from prime_factors import factors + + +# Tests adapted from `problem-specifications//canonical-data.json` @ v1.1.0 + +class PrimeFactorsTest(unittest.TestCase): + def test_no_factors(self): + self.assertEqual(factors(1), []) + + def test_prime_number(self): + self.assertEqual(factors(2), [2]) + + def test_square_of_a_prime(self): + self.assertEqual(factors(9), [3, 3]) + + def test_cube_of_a_prime(self): + self.assertEqual(factors(8), [2, 2, 2]) + + def test_product_of_primes_and_non_primes(self): + self.assertEqual(factors(12), [2, 2, 3]) + + def test_product_of_primes(self): + self.assertEqual(factors(901255), [5, 17, 23, 461]) + + def test_factors_include_a_large_prime(self): + self.assertEqual(factors(93819012551), [11, 9539, 894119]) + + +if __name__ == '__main__': + unittest.main() diff --git a/python/simple-cipher/.exercism/metadata.json b/python/simple-cipher/.exercism/metadata.json new file mode 100644 index 0000000..885aee7 --- /dev/null +++ b/python/simple-cipher/.exercism/metadata.json @@ -0,0 +1 @@ +{"track":"python","exercise":"simple-cipher","id":"6d5485599e744039a9d97ab829528b7f","url":"https://exercism.io/my/solutions/6d5485599e744039a9d97ab829528b7f","handle":"Xevion","is_requester":true,"auto_approve":false} \ No newline at end of file diff --git a/python/simple-cipher/README.md b/python/simple-cipher/README.md new file mode 100644 index 0000000..4c34bfa --- /dev/null +++ b/python/simple-cipher/README.md @@ -0,0 +1,146 @@ +# Simple Cipher + +Implement a simple shift cipher like Caesar and a more secure substitution cipher. + +## Step 1 + +"If he had anything confidential to say, he wrote it in cipher, that is, +by so changing the order of the letters of the alphabet, that not a word +could be made out. If anyone wishes to decipher these, and get at their +meaning, he must substitute the fourth letter of the alphabet, namely D, +for A, and so with the others." +—Suetonius, Life of Julius Caesar + +Ciphers are very straight-forward algorithms that allow us to render +text less readable while still allowing easy deciphering. They are +vulnerable to many forms of cryptoanalysis, but we are lucky that +generally our little sisters are not cryptoanalysts. + +The Caesar Cipher was used for some messages from Julius Caesar that +were sent afield. Now Caesar knew that the cipher wasn't very good, but +he had one ally in that respect: almost nobody could read well. So even +being a couple letters off was sufficient so that people couldn't +recognize the few words that they did know. + +Your task is to create a simple shift cipher like the Caesar Cipher. +This image is a great example of the Caesar Cipher: + +![Caesar Cipher][1] + +For example: + +Giving "iamapandabear" as input to the encode function returns the cipher "ldpdsdqgdehdu". Obscure enough to keep our message secret in transit. + +When "ldpdsdqgdehdu" is put into the decode function it would return +the original "iamapandabear" letting your friend read your original +message. + +## Step 2 + +Shift ciphers are no fun though when your kid sister figures it out. Try +amending the code to allow us to specify a key and use that for the +shift distance. This is called a substitution cipher. + +Here's an example: + +Given the key "aaaaaaaaaaaaaaaaaa", encoding the string "iamapandabear" +would return the original "iamapandabear". + +Given the key "ddddddddddddddddd", encoding our string "iamapandabear" +would return the obscured "ldpdsdqgdehdu" + +In the example above, we've set a = 0 for the key value. So when the +plaintext is added to the key, we end up with the same message coming +out. So "aaaa" is not an ideal key. But if we set the key to "dddd", we +would get the same thing as the Caesar Cipher. + +## Step 3 + +The weakest link in any cipher is the human being. Let's make your +substitution cipher a little more fault tolerant by providing a source +of randomness and ensuring that the key contains only lowercase letters. + +If someone doesn't submit a key at all, generate a truly random key of +at least 100 characters in length. + +## Extensions + +Shift ciphers work by making the text slightly odd, but are vulnerable +to frequency analysis. Substitution ciphers help that, but are still +very vulnerable when the key is short or if spaces are preserved. Later +on you'll see one solution to this problem in the exercise +"crypto-square". + +If you want to go farther in this field, the questions begin to be about +how we can exchange keys in a secure way. Take a look at [Diffie-Hellman +on Wikipedia][dh] for one of the first implementations of this scheme. + +[1]: https://upload.wikimedia.org/wikipedia/commons/thumb/4/4a/Caesar_cipher_left_shift_of_3.svg/320px-Caesar_cipher_left_shift_of_3.svg.png +[dh]: http://en.wikipedia.org/wiki/Diffie%E2%80%93Hellman_key_exchange + +## Should I use random or secrets? + +Python, as of version 3.6, includes two different random modules. + +The module called `random` is pseudo-random, meaning it does not generate +true randomness, but follows an algorithm that simulates randomness. +Since random numbers are generated through a known algorithm, they are not truly random. + +The `random` module is not correctly suited for cryptography and should not be used, +precisely because it is pseudo-random. + +For this reason, in version 3.6, Python introduced the `secrets` module, which generates +cryptographically strong random numbers that provide the greater security required for cryptography. + +Since this is only an exercise, `random` is fine to use, but note that **it would be +very insecure if actually used for cryptography.** + + +## Exception messages + +Sometimes it is necessary to raise an exception. When you do this, you should include a meaningful error message to +indicate what the source of the error is. This makes your code more readable and helps significantly with debugging. Not +every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include +a message. + +To raise a message with an exception, just write it as an argument to the exception type. For example, instead of +`raise Exception`, you should write: + +```python +raise Exception("Meaningful message indicating the source of the error") +``` + +## Running the tests + +To run the tests, run the appropriate command below ([why they are different](https://github.com/pytest-dev/pytest/issues/1629#issue-161422224)): + +- Python 2.7: `py.test simple_cipher_test.py` +- Python 3.4+: `pytest simple_cipher_test.py` + +Alternatively, you can tell Python to run the pytest module (allowing the same command to be used regardless of Python version): +`python -m pytest simple_cipher_test.py` + +### Common `pytest` options + +- `-v` : enable verbose output +- `-x` : stop running tests on first failure +- `--ff` : run failures from previous test before running other test cases + +For other options, see `python -m pytest -h` + +## Submitting Exercises + +Note that, when trying to submit an exercise, make sure the solution is in the `$EXERCISM_WORKSPACE/python/simple-cipher` directory. + +You can find your Exercism workspace by running `exercism debug` and looking for the line that starts with `Workspace`. + +For more detailed information about running tests, code style and linting, +please see [Running the Tests](http://exercism.io/tracks/python/tests). + +## Source + +Substitution Cipher at Wikipedia [http://en.wikipedia.org/wiki/Substitution_cipher](http://en.wikipedia.org/wiki/Substitution_cipher) + +## Submitting Incomplete Solutions + +It's possible to submit an incomplete solution so you can see how others have completed the exercise. diff --git a/python/simple-cipher/simple_cipher.py b/python/simple-cipher/simple_cipher.py new file mode 100644 index 0000000..15bdf2e --- /dev/null +++ b/python/simple-cipher/simple_cipher.py @@ -0,0 +1,26 @@ +from string import ascii_lowercase as low + +class Cipher(object): + def __init__(self, key=None): + self.key = key + self.shift = 27 - (sum([low.index(char) for char in self.key]) % 25) + shifted = low[self.shift:] + low[:self.shift] + print(low) + print(shifted) + self.encode_ = str.maketrans(low, shifted) + self.decode_ = str.maketrans(shifted, low) + + def encode(self, text): + return text.translate(self.encode_) + + def decode(self, text): + return text.translate(self.decode_) + +x = list(zip('iamapandabear', 'ldpdsdqgdehdu')) +x = sorted(dict.fromkeys(x)) +x = +from pprint import PrettyPrinter +print = PrettyPrinter().pprint + +# c = Cipher('d' * 18) +# print(c.encode('iamapandabear')) \ No newline at end of file diff --git a/python/simple-cipher/simple_cipher_test.py b/python/simple-cipher/simple_cipher_test.py new file mode 100644 index 0000000..6e8839c --- /dev/null +++ b/python/simple-cipher/simple_cipher_test.py @@ -0,0 +1,78 @@ +import unittest +import re + +from simple_cipher import Cipher + + +# Tests adapted from `problem-specifications//canonical-data.json` @ v2.0.0 + +class SimpleCipherTest(unittest.TestCase): + # Utility functions + def setUp(self): + try: + self.assertRaisesRegex + except AttributeError: + self.assertRaisesRegex = self.assertRaisesRegexp + + def assertRaisesWithMessage(self, exception): + return self.assertRaisesRegex(exception, r".+") + + +class RandomKeyCipherTest(SimpleCipherTest): + def test_can_encode(self): + cipher = Cipher() + plaintext = 'aaaaaaaaaa' + self.assertEqual(cipher.encode(plaintext), cipher.key[:len(plaintext)]) + + def test_can_decode(self): + cipher = Cipher() + plaintext = 'aaaaaaaaaa' + self.assertEqual(cipher.decode(cipher.key[:len(plaintext)]), plaintext) + + def test_is_reversible(self): + cipher = Cipher() + plaintext = 'abcdefghij' + self.assertEqual(cipher.decode(cipher.encode(plaintext)), plaintext) + + def test_key_is_only_made_of_lowercase_letters(self): + self.assertIsNotNone(re.match('^[a-z]+$', Cipher().key)) + + +class SubstitutionCipherTest(SimpleCipherTest): + def test_can_encode(self): + cipher = Cipher('abcdefghij') + self.assertEqual(cipher.encode('aaaaaaaaaa'), cipher.key) + + def test_can_decode(self): + cipher = Cipher('abcdefghij') + self.assertEqual(cipher.decode(cipher.key), 'aaaaaaaaaa') + + def test_is_reversible(self): + cipher = Cipher('abcdefghij') + plaintext = 'abcdefghij' + self.assertEqual(cipher.decode(cipher.encode(plaintext)), plaintext) + + def test_can_double_shift_encode(self): + plaintext = 'iamapandabear' + cipher = Cipher(plaintext) + self.assertEqual(cipher.encode(plaintext), 'qayaeaagaciai') + + def test_can_wrap_on_encode(self): + cipher = Cipher('abcdefghij') + self.assertEqual(cipher.encode('zzzzzzzzzz'), 'zabcdefghi') + + def test_can_wrap_on_decode(self): + cipher = Cipher('abcdefghij') + self.assertEqual(cipher.decode('zabcdefghi'), 'zzzzzzzzzz') + + def test_can_encode_messages_longer_than_key(self): + cipher = Cipher('abc') + self.assertEqual(cipher.encode('iamapandabear'), 'iboaqcnecbfcr') + + def test_can_decode_messages_longer_than_key(self): + cipher = Cipher('abc') + self.assertEqual(cipher.decode('iboaqcnecbfcr'), 'iamapandabear') + + +if __name__ == '__main__': + unittest.main() diff --git a/python/spiral-matrix/.exercism/metadata.json b/python/spiral-matrix/.exercism/metadata.json new file mode 100644 index 0000000..64b08fb --- /dev/null +++ b/python/spiral-matrix/.exercism/metadata.json @@ -0,0 +1 @@ +{"track":"python","exercise":"spiral-matrix","id":"e351192377fc41329076c9d6636ef233","url":"https://exercism.io/my/solutions/e351192377fc41329076c9d6636ef233","handle":"Xevion","is_requester":true,"auto_approve":false} \ No newline at end of file diff --git a/python/spiral-matrix/README.md b/python/spiral-matrix/README.md new file mode 100644 index 0000000..19667d3 --- /dev/null +++ b/python/spiral-matrix/README.md @@ -0,0 +1,73 @@ +# Spiral Matrix + +Given the size, return a square matrix of numbers in spiral order. + +The matrix should be filled with natural numbers, starting from 1 +in the top-left corner, increasing in an inward, clockwise spiral order, +like these examples: + +###### Spiral matrix of size 3 + +```text +1 2 3 +8 9 4 +7 6 5 +``` + +###### Spiral matrix of size 4 + +```text + 1 2 3 4 +12 13 14 5 +11 16 15 6 +10 9 8 7 +``` + +## Exception messages + +Sometimes it is necessary to raise an exception. When you do this, you should include a meaningful error message to +indicate what the source of the error is. This makes your code more readable and helps significantly with debugging. Not +every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include +a message. + +To raise a message with an exception, just write it as an argument to the exception type. For example, instead of +`raise Exception`, you should write: + +```python +raise Exception("Meaningful message indicating the source of the error") +``` + +## Running the tests + +To run the tests, run the appropriate command below ([why they are different](https://github.com/pytest-dev/pytest/issues/1629#issue-161422224)): + +- Python 2.7: `py.test spiral_matrix_test.py` +- Python 3.4+: `pytest spiral_matrix_test.py` + +Alternatively, you can tell Python to run the pytest module (allowing the same command to be used regardless of Python version): +`python -m pytest spiral_matrix_test.py` + +### Common `pytest` options + +- `-v` : enable verbose output +- `-x` : stop running tests on first failure +- `--ff` : run failures from previous test before running other test cases + +For other options, see `python -m pytest -h` + +## Submitting Exercises + +Note that, when trying to submit an exercise, make sure the solution is in the `$EXERCISM_WORKSPACE/python/spiral-matrix` directory. + +You can find your Exercism workspace by running `exercism debug` and looking for the line that starts with `Workspace`. + +For more detailed information about running tests, code style and linting, +please see [Running the Tests](http://exercism.io/tracks/python/tests). + +## Source + +Reddit r/dailyprogrammer challenge #320 [Easy] Spiral Ascension. [https://www.reddit.com/r/dailyprogrammer/comments/6i60lr/20170619_challenge_320_easy_spiral_ascension/](https://www.reddit.com/r/dailyprogrammer/comments/6i60lr/20170619_challenge_320_easy_spiral_ascension/) + +## Submitting Incomplete Solutions + +It's possible to submit an incomplete solution so you can see how others have completed the exercise. diff --git a/python/spiral-matrix/spiral_matrix.py b/python/spiral-matrix/spiral_matrix.py new file mode 100644 index 0000000..a5f0e47 --- /dev/null +++ b/python/spiral-matrix/spiral_matrix.py @@ -0,0 +1,53 @@ +# Two lines, dude. ez. +def spiral_matrix(size): + return [] if size < 1 else Matrix(size).matrix + +# Class for a pathfinding based spiral generation +class Matrix: + def __init__(self, size): + self.size = size + self.matrix = [[None for y in range(size)] for x in range(size)] + self.i = 1 + self.cur = (0, 0) + self.cardinals = [(0, 1), (1, 0), (-1, 0), (0, -1)] + self.dir_index = 0 + self.loop() + + # Loop that builds the spiral matrix + def loop(self): + # While the current number is less than the maximum number + while self.i < (self.size ** 2): + # If the next position is not valid, turn + if not self.valid(self.nextpos): + self.dir_index = (self.dir_index + 1) % 4 + else: + self.access() + self.cur = self.nextpos + self.access() + + # Access a position and increment the counter + def access(self): + self.matrix[self.cur[0]][self.cur[1]] = self.i + self.i += 1 + + # Just the current direction (as an offset) + @property + def direction(self): + return self.cardinals[self.dir_index] + + # Next position for access based on the current direction + @property + def nextpos(self): + return (self.cur[0] + self.direction[0], self.cur[1] + self.direction[1]) + + # Determine whether a position is valid to be approached + def valid(self, pos): + return self.validxy(pos[0], pos[1]) and not self.matrix[pos[0]][pos[1]] + + # Determine whether a position is + def validxy(self, x, y): + return x >= 0 and x < self.size and y >= 0 and y < self.size + + # Printable Matrix with proper character space justification + def __repr__(self): + return '\n'.join([' '.join(map(lambda item : str(item or '?').rjust(len(str(self.size ** 2))), sub)) for sub in self.matrix]) \ No newline at end of file diff --git a/python/spiral-matrix/spiral_matrix_test.py b/python/spiral-matrix/spiral_matrix_test.py new file mode 100644 index 0000000..a88b14c --- /dev/null +++ b/python/spiral-matrix/spiral_matrix_test.py @@ -0,0 +1,51 @@ +import unittest + +from spiral_matrix import spiral_matrix + + +# Tests adapted from `problem-specifications//canonical-data.json` @ v1.1.0 + + +class SpiralMatrixTest(unittest.TestCase): + def test_empty_spiral(self): + self.assertEqual(spiral_matrix(0), [ + ]) + + def test_trivial_spiral(self): + self.assertEqual(spiral_matrix(1), [ + [1] + ]) + + def test_spiral_of_size_2(self): + self.assertEqual(spiral_matrix(2), [ + [1, 2], + [4, 3] + ]) + + def test_spiral_of_size_3(self): + self.assertEqual(spiral_matrix(3), [ + [1, 2, 3], + [8, 9, 4], + [7, 6, 5] + ]) + + def test_spiral_of_size_4(self): + self.assertEqual(spiral_matrix(4), [ + [1, 2, 3, 4], + [12, 13, 14, 5], + [11, 16, 15, 6], + [10, 9, 8, 7] + ]) + + def test_spiral_of_size_5(self): + self.assertEqual(spiral_matrix(5), [ + [1, 2, 3, 4, 5], + [16, 17, 18, 19, 6], + [15, 24, 25, 20, 7], + [14, 23, 22, 21, 8], + [13, 12, 11, 10, 9] + ]) + + +if __name__ == '__main__': + unittest.main()