Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
Caesar Cipher Encoding Function
00:00
5 left

Caesar Cipher Encoding Function

EasyPython

Problem

Next Insurance uses lightweight text transformations in internal policy-processing tools. Implement a Caesar cipher encoder that shifts every English alphabetic character by a specified amount while preserving the original letter case.

Formal Specification

Implement encrypt_message(text, shift):

  • text is a string containing ASCII letters, digits, spaces, punctuation, and possibly other characters.
  • shift is an integer and may be positive, zero, or negative.
  • Shift uppercase letters within A-Z and lowercase letters within a-z, wrapping around at the end of each alphabet.
  • Leave digits, whitespace, punctuation, and all non-ASCII-letter characters unchanged.
  • Return the encoded string.

The shift may be larger than 26, so reduce it modulo 26. The input string must not be modified in place.

Constraints

  • 0 <= len(text) <= 10^5
  • -10^9 <= shift <= 10^9
  • Letters in the input are restricted to ASCII A-Z and a-z
  • Digits, whitespace, punctuation, and other characters must be preserved

Function Signature

def encrypt_message(text, shift):
Interviewer

Your question is Caesar Cipher Encoding Function. Start with the requirements in the Question tab.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.
CodePython 3
You need to log in / sign up to run or submit.Ln 2
Run your code to see test output here.