Python Challenge - Level 1

Problem

everybody thinks twice before solving this.

The image has a keyboard with the letters shifted by 2. The clue is a ROT-2 (Caesar) cipher applied to the text in the page source:

g fmnc wms bgblr rpylqjyrc gr zw fylb. rfyrq ufyr amknsrcpq ypc dmp.
bmgle grgl zw fylb gq glcddgagclr ylb rfyr'q ufw rfgq rcvr gq qm jmle.
sqgle qrpgle.kyicrpylq() gq pcamkkclbcb. lmu ynnjw ml rfc spj.

Each letter is shifted forward by 2 positions (a→c, b→d, … z→b).

Solution

Python

Python's str.maketrans() + str.translate() is the idiomatic one-liner:

raw = "g fmnc wms bgblr rpylqjyrc gr zw fylb. rfyrq ufyr amknsrcpq ypc dmp. bmgle grgl zw fylb gq glcddgagclr ylb rfyr'q ufw rfgq rcvr gq qm jmle. sqgle qrpgle.kyicrpylq() gq pcamkkclbcb. lmu ynnjw ml rfc spj."

table = str.maketrans(
    "abcdefghijklmnopqrstuvwxyz",
    "cdefghijklmnopqrstuvwxyzab"
)
print(raw.translate(table))

Result:

i hope you didnt translate it by hand. thats what computers are for. doing it in by hand is inefficient and that's why this text is so long. using string.maketrans() is recommended. now apply on the url.

Apply the same shift to the URL path map → ocr, which is the next level.

Go

Go has no built-in translation table, so we shift each rune manually:

package main

import (
    "fmt"
    "strings"
)

func caesarShift(s string, shift int) string {
    return strings.Map(func(r rune) rune {
        if r >= 'a' && r <= 'z' {
            return 'a' + (r-'a'+rune(shift))%26
        }
        return r
    }, s)
}

func main() {
    raw := "g fmnc wms bgblr rpylqjyrc gr zw fylb."
    fmt.Println(caesarShift(raw, 2))
    fmt.Println(caesarShift("map", 2)) // → "ocr"
}

Rust

Rust uses iterator chaining with a closure over char:

fn caesar_shift(s: &str, shift: u8) -> String {
    s.chars().map(|c| {
        if c.is_ascii_lowercase() {
            (b'a' + (c as u8 - b'a' + shift) % 26) as char
        } else {
            c
        }
    }).collect()
}

fn main() {
    let raw = "g fmnc wms bgblr rpylqjyrc gr zw fylb.";
    println!("{}", caesar_shift(raw, 2));
    println!("{}", caesar_shift("map", 2)); // → "ocr"
}

Language Comparison

Feature Python Go Rust
Character translation str.maketrans() + str.translate() — O(n), single pass with a lookup table strings.Map(func(rune) rune) — manual modular arithmetic .chars().map().collect() — zero-copy iterator chain
Unicode awareness Full; str.translate() works on full Unicode code points strings.Map works on rune (Unicode code points) .chars() iterates Unicode scalar values; safe by design
In-place mutation No — Python strings are immutable No — builds a new string No — collects into a new String
Standard library str.maketrans built-in strings stdlib No built-in; compose from iterators
Boilerplate Minimal — 2 lines Moderate — explicit function needed Moderate — explicit function needed

Key insight: Python's str.maketrans is a specialized O(1)-build lookup table that makes substitution ciphers trivially fast. Go and Rust achieve the same result through first-class function composition (Map/map) with no loss of clarity.

Next Level

Apply the shift to map → ocr:

http://www.pythonchallenge.com/pc/def/ocr.html

Loading...