PYTHON CODE HELP PLEASE restrictions: May not import other modules or use contai
ID: 3715808 • Letter: P
Question
PYTHON CODE HELP PLEASE
restrictions: May not import other modules or use contains(), index(), or try-except.
caesar_decode(shift, message): This function accepts a shift amount (positive or negative) and an encoded message and returns the decoded message. Note: shift is how much the message was shifted, not how much to shift it back.
Test cases:
def test_caesar_decode 01(self): shift 1 message "abcde" encoded "bcdef" result- caesar_decode(shift, encoded) self.assertEqual(message, result) def test_caesar_decode_02 (self): shift 1 message"I've got a lovely bunch of coconuts!" encoded "J (wf!hpu!b !mpwfmz!cvodi!pg!dpdpovut" result-caesar_decode (shift, encoded) self.assertEqual(message, result) def test_caesar_decode_03 (self): shift-1 messageabc+++" encodednabk" result- caesar_decode(shift, encoded) self.assertEqual(message, result) def test_caesar_decode_04 (self): shift 5 message "I've got a lovely bunch of coconuts!" result- caesar_decode(shift, encoded) self.assertEqual(message, result) def test_caesar_decode_05 (self): shift 32 message "I've got a lovely bunch of coconuts!" encoded- "¡G76@ (05@"@-076_ : @#6/$)@0"@$0$0/654A" result- caesar_decode(shift, encoded) self.assertEqual(message, result) def test_caesar_decode_06(self): shift-5 message"Once upon a time, long ago and far away, when mice were mice...'" result-caesar_decode (shift, encoded) self.assertEqual(message, result)Explanation / Answer
def caesar_decode(shift, message): result = '' for ch in message: val = ord(ch) - shift if val < 32: val = 126 - (31 - val) elif val > 126: val = val - 127 + 32 result += chr(val) return result print(caesar_decode(1, 'bcdef')) print(caesar_decode(1, "J(wf!hpu!b!mpwfmz!cvodi!pg!dpdpovut""))Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.