Question: Python3: Encode('attackatdawn',3)..... Returns the text encoded by shifting each letter by n positions. import introcs def encode(text,n): Returns the text encoded by shifting each
Python3: Encode('attackatdawn',3)..... Returns the text encoded by shifting each letter by n positions.
import introcs
def encode(text,n): """ Returns the text encoded by shifting each letter by n positions. Letters at the end of the alphabet wrap back around. So if n is 3, 'x' becomes 'a'. Examples: encode('attackatdawn',3) returns 'dwwdfndwgdzq' encode('dwwdfndwgdzq',23) returns 'attackatdawn' encode('attackatdawn',13) returns 'nggnpxngqnja' encode('',13) returns '' Parameter text: the text to encode Precondition: text is a string Parameter n: the number of positions to shift Precondition: n is an int between 0 and 25 (inclusive) """ # Hint: Look at the Python functions ord and chr # Lower case letters have ord values 97 to 122
-----------------------------------------------------------------------------------
Here are the Test Cases below.
import func
def test_encode(): """ Tests procedure for function encode(). """ print('Testing encode()') # Start with the original Caesar example result = func.encode('attackatdawn',3) introcs.assert_equals('dwwdfndwgdzq',result) # Decode with 26-3 result = func.encode(result,23) introcs.assert_equals('attackatdawn',result) # Rot 13 result = func.encode('attackatdawn',13) introcs.assert_equals('nggnpxngqnja',result) result = func.encode(result,13) introcs.assert_equals('attackatdawn',result) result = func.encode('ordertheretreat',13) introcs.assert_equals('beqregurergerng',result) result = func.encode(result,13) introcs.assert_equals('ordertheretreat',result) # Once more test result = func.encode('letsmeetforlunch',10) introcs.assert_equals('vodcwoodpybvexmr',result) result = func.encode(result,16) introcs.assert_equals('letsmeetforlunch',result) # Empty string result = func.encode('',25) introcs.assert_equals('',result)
if __name__ == '__main__': test_encode() print('Module func passed all tests.')
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
