-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Rotational Cipher: Exercise Added #460
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 6 commits
e3b3198
1622650
d1b2144
3e78e94
abba798
86b35f6
9b5407e
a0e8c02
8d2c1fb
e38cccb
94e36a2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
def rotational_cipher(): | ||
pass |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
def rotational_cipher(message, key): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The convention is to use nouns for classes and verbs for functions. So in this case I would prefer |
||
import string | ||
alpha_lower = list(string.ascii_lowercase) | ||
alpha_upper = list(string.ascii_uppercase) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of creating the alpha variables, you could rename them inside the import statement like this: from string import ascii_lowercase as alpha_lower
from string import ascii_uppercase as alpha_upper
`` |
||
coded_message = "" | ||
for char in list(message): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The python strings already support the sequence type methods, so you don't need to convert them to lists to use |
||
if char in alpha_lower: | ||
char = alpha_lower[(alpha_lower.index(char) + key) % 26] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You could create a constant for the length of alphabet |
||
elif char in alpha_upper: | ||
char = alpha_upper[(alpha_upper.index(char) + key) % 26] | ||
coded_message = coded_message + char | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This could be shortened with |
||
return coded_message |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is already a description in the markdown file, so I wouldn't add any doc string here. We tend to keep the stub files very limited, so we don't force any specific way of implementing an exercise or naming parameters onto the users. This should be enough: def rotate():
pass |
||
|
||
def rotational_cipher(message, key): | ||
"""Preform a rotation cipher on a message. | ||
|
||
Arguments: | ||
message -- Message to be incoded in the form of a string | ||
key -- The number of letters the charactors are to be shifted | ||
|
||
Outputs: | ||
coded_message -- A string containing the transposed message | ||
""" | ||
coded_message = "" | ||
return coded_message |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import unittest | ||
|
||
import rotational_cipher | ||
|
||
|
||
class RotationalCipher(unittest.TestCase): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We try to stick to the canonical test data provided in the If you are missing a test case that would make sense for all tracks, than it would be best to also add it there. |
||
def test_hello(self): | ||
self.assertEqual(rotational_cipher.rotational_cipher('hello', 0), | ||
'hello') | ||
|
||
def test_space(self): | ||
self.assertEqual( | ||
rotational_cipher.rotational_cipher( | ||
'from the other side', | ||
0), | ||
'from the other side') | ||
|
||
def test_ciphered(self): | ||
self.assertEqual( | ||
rotational_cipher.rotational_cipher( | ||
'i must have called', | ||
13), | ||
'v zhfg unir pnyyrq') | ||
|
||
def test_mixed(self): | ||
self.assertEqual( | ||
rotational_cipher.rotational_cipher( | ||
'... a 1000 TIMES', | ||
13), | ||
'... n 1000 GVZRF') | ||
|
||
def test_emptystring(self): | ||
self.assertEqual(rotational_cipher.rotational_cipher('', 13), '') | ||
|
||
def test_numbers(self): | ||
self.assertEqual(rotational_cipher.rotational_cipher('07041776!!%$$', 13), | ||
'07041776!!%$$') | ||
|
||
def test_mixed(self): | ||
self.assertEqual( | ||
rotational_cipher.rotational_cipher( | ||
"Doesn't tear you apart;", | ||
27), | ||
"Epfto'u ufbs zpv bqbsu;") | ||
|
||
def test_mixed(self): | ||
self.assertEqual( | ||
rotational_cipher.rotational_cipher( | ||
"Anymoreeee :'(", | ||
-26), | ||
"Anymoreeee :'(") | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I assume this file was exceedingly created (at least it's not needed) and should be removed.