Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Challenges

Comments on Caesar shift cipher

Parent

Caesar shift cipher

+4
−0

Introduction

What is the Caesar shift cipher (ROT$n$)? It's basically a cipher sequence that changes a letter's value from the number chosen. If we use ROT1 on "games", we get "hbnft". The basic interpretation of the cipher is iterating the value of a letter by 1 $n$ times. If you still don't understand, try using the ROT13 website.

Challenge

Make a program that takes input of a number from 1 to 25, call it $n$, and takes input of a string. It could be any. Then, use $n$ to convert the string into ROT$n$ and output the result.

Examples of inputs and outputs:

If $n = 4$ and string is "Hello, world.", we get Lipps, asvph.
If $n = 15$ and string is "trololol", we get igdadada.
If $n = 7$ and string is "gxoxk zhggt zbox rhn ni", we get never gonna give you up.
If $n = 24$ and string is "I want breakfast!", we get G uylr zpcyidyqr!.
If $n = 13$ and string is "guvf grkg vf fhf", we get this text is sus.

Shortest program wins.

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.
Why should this post be closed?

0 comment threads

Post
+1
−0

Python 3, 86 85 bytes

The reasoning behind why this works is basically that 1. we can directly compare the ordinal values of characters (apparently) and 2. we only need that if a character is alphanumeric, then

'A' < '`' < 'a'
which is very interesting to think about.

Also, if we have an expression of the form a+(b)<<c or a+b<<c, apparently Python interprets that as (a+b)<<c, which is cool, but so weird.


Solution:

lambda m,k:''.join([c,chr((o:=2+('`'<c)<<5)+(ord(c)+k-o)%26)][c.isalpha()]for c in m)

Try it online! I set the link to Never expire, but please notify me if the link doesn't work.

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

1 comment thread

Incorrect outputs from this function (2 comments)
Incorrect outputs from this function
lapiscarrot‭ wrote 5 days ago

This gives the wrong answer, '`', for ROT('m',13), since ASCII uses offsets 1-26 for letters while modular arithmetic yields 0-25. A solution I found is to add 1 to the definition for o as follows:

90 bytes: lambda m,k:''.join([c,chr((o:=(2+('`'<c)<<5)+1)+(ord(c)+k-o)%26)][c.isalpha()] for c in m)

Also, this code affects letters with accents; if you want to avoid this, you could add another condition:

98 bytes: lambda m,k:''.join([c,chr((o:=(2+('`'<c)<<5)+1)+(ord(c)+k-o)%26)][c.isalpha()&(c<'{')] for c in m)

CrSb0001‭ wrote 1 day ago

I can try to fix this while still preserving the bytecount.