Skip to content

Instantly share code, notes, and snippets.

@wei2912
Last active November 11, 2018 10:06
Show Gist options
  • Save wei2912/7ad9d2a08168401a3d5424d6adbdd0f5 to your computer and use it in GitHub Desktop.
Save wei2912/7ad9d2a08168401a3d5424d6adbdd0f5 to your computer and use it in GitHub Desktop.
import unittest
def zigzag(cs, num_rows):
"""
Authored by Dhruv - TA will be Dhruv.
In order to encode text messages in a format that is unreadable to other,
Alice decides to create a cipher and wants a program to test it out on her
messages. The cipher (which is essentially an algorithm) rewrites a text
message, taken in as a string of contiguous characters, with the use of a
secret “key”, a constant num_rows.
If the string taken in is "PAYPALISHIRING" and num_rows = 3, the string will
be rewritten as
P P I I N
A A S R G
Y L H I
and the program should thus output “PPIINAASRGYLJI”.
If num_rows = 4, the string will instead be written as
P A H N
A L I G
Y I R
P S I
and the program should thus output “PAHNALIGYIRPSI”.
Write a function zigzag that takes as input a string and an integer
representing num_rows.
"""
# TODO: Fill up your code here, and set output to the final answer.
output = None
# This returns the final output string.
return output
class TestZigZag(unittest.TestCase):
def test_zigzag(self):
f = zigzag
self.assertEqual(f('PAYPALISHIRING', 3), 'PPIINAASRGYLHI')
self.assertEqual(f('PAYPALISHIRING', 4), 'PAHNALIGYIRPSI')
self.assertEqual(f('SCRAMBLESCRABBLE', 4), 'SMSBCBCBRLRLAEAE')
if __name__ == '__main__':
unittest.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment