11module
22
3- def encrypt : Unit :=
4- ()
3+ def Char.shiftByFour (c : Char) : Char :=
4+ Char.ofNat ((c.toNat - 'a' .toNat + 4 ) % 26 + 'a' .toNat)
5+
6+ def encrypt (s : String) : String :=
7+ s.map Char.shiftByFour
8+
9+ example : encrypt "hi" = "lm" := by native_decide
10+ example : encrypt "asdfghjkl" = "ewhjklnop" := by native_decide
11+ example : encrypt "gf" = "kj" := by native_decide
12+ example : encrypt "et" = "ix" := by native_decide
13+ example : encrypt "a" = "e" := by native_decide
14+ example : encrypt "faewfawefaewg" = "jeiajeaijeiak" := by native_decide
15+ example : encrypt "hellomyfriend" = "lippsqcjvmirh" := by native_decide
16+ example : encrypt "dxzdlmnilfuhmilufhlihufnmlimnufhlimnufhfucufh" = "hbdhpqrmpjylqmpyjlpmlyjrqpmqryjlpmqryjljygyjl" := by native_decide
17+
18+ def Char.unshiftByFour (c : Char) : Char :=
19+ Char.ofNat ((c.toNat - 'a' .toNat + 22 ) % 26 + 'a' .toNat)
20+
21+ def decrypt (s : String) : String :=
22+ s.map Char.unshiftByFour
23+
24+ theorem Char.toNat_ofNat_of_isValidChar {n : Nat} (h : n.isValidChar) : (Char.ofNat n).toNat = n := by
25+ simp [ofNat, h, ofNatAux]
26+
27+ @[simp]
28+ theorem Char.unshiftByFour_shiftByFour {c : Char} (hc : c.isLower) : c.shiftByFour.unshiftByFour = c := by
29+ simp [Char.isLower, Char.shiftByFour, Char.unshiftByFour, UInt32.le_iff_toNat_le,
30+ ← Char.toNat_inj] at *
31+ grind [toNat_ofNat_of_isValidChar]
32+
33+ theorem List.map_eq_self {f : α → α} {l : List α} (hf : ∀ a ∈ l, f a = a) : l.map f = l := by
34+ induction l <;> grind
35+
36+ theorem decryptShift_encryptShift (s : String) (hs : ∀ c ∈ s.toList, c.isLower) :
37+ decrypt (encrypt s) = s := by
38+ simpa [← String.toList_inj, decrypt, encrypt] using
39+ List.map_eq_self (by grind [Char.unshiftByFour_shiftByFour])
540
641/-!
742## Prompt
@@ -10,8 +45,8 @@ def encrypt : Unit :=
1045
1146def encrypt(s):
1247 """Create a function encrypt that takes a string as an argument and
13- returns a string encrypted with the alphabet being rotated.
14- The alphabet should be rotated in a manner such that the letters
48+ returns a string encrypted with the alphabet being rotated.
49+ The alphabet should be rotated in a manner such that the letters
1550 shift down by two multiplied to two places.
1651 For example:
1752 encrypt('hi') returns 'lm'
@@ -53,4 +88,4 @@ def check(candidate):
5388 assert candidate('a')=='e', "This prints if this assert fails 2 (also good for debugging!)"
5489
5590```
56- -/
91+ -/
0 commit comments