11module
2+ import Std
23
3- def hex_key : Unit :=
4- ()
4+ open Std
5+
6+ def primeList : List Char :=
7+ ['2' , '3' , '5' , '7' , 'B' , 'D' ]
8+
9+ def primes : HashSet Char :=
10+ .ofList primeList
11+
12+ def hexKey (s : String) : Nat :=
13+ s.chars.filter (· ∈ primes) |>.length
14+
15+ example : hexKey "AB" = 1 := by native_decide
16+ example : hexKey "1077E" = 2 := by native_decide
17+ example : hexKey "ABED1A33" = 4 := by native_decide
18+ example : hexKey "2020" = 2 := by native_decide
19+ example : hexKey "123456789ABCDEF0" = 6 := by native_decide
20+ example : hexKey "112233445566778899AABBCCDDEEFF00" = 12 := by native_decide
21+
22+ theorem hexKey_eq {s : String} : hexKey s = (s.toList.filter (· ∈ primeList)).length := by
23+ rw [hexKey, ← Iter.length_toList_eq_length, primes]
24+ simp
525
626/-!
727## Prompt
828
929```python3
1030
1131def hex_key(num):
12- """You have been tasked to write a function that receives
13- a hexadecimal number as a string and counts the number of hexadecimal
14- digits that are primes (prime number, or a prime, is a natural number
32+ """You have been tasked to write a function that receives
33+ a hexadecimal number as a string and counts the number of hexadecimal
34+ digits that are primes (prime number, or a prime, is a natural number
1535 greater than 1 that is not a product of two smaller natural numbers).
1636 Hexadecimal digits are 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F.
1737 Prime numbers are 2, 3, 5, 7, 11, 13, 17,...
18- So you have to determine a number of the following digits: 2, 3, 5, 7,
38+ So you have to determine a number of the following digits: 2, 3, 5, 7,
1939 B (=decimal 11), D (=decimal 13).
20- Note: you may assume the input is always correct or empty string,
40+ Note: you may assume the input is always correct or empty string,
2141 and symbols A,B,C,D,E,F are always uppercase.
2242 Examples:
2343 For num = "AB" the output should be 1.
@@ -45,16 +65,16 @@ def hex_key(num):
4565def check(candidate):
4666
4767 # Check some simple cases
48- assert candidate("AB") == 1, "First test error: " + str(candidate("AB"))
49- assert candidate("1077E") == 2, "Second test error: " + str(candidate("1077E"))
50- assert candidate("ABED1A33") == 4, "Third test error: " + str(candidate("ABED1A33"))
51- assert candidate("2020") == 2, "Fourth test error: " + str(candidate("2020"))
52- assert candidate("123456789ABCDEF0") == 6, "Fifth test error: " + str(candidate("123456789ABCDEF0"))
53- assert candidate("112233445566778899AABBCCDDEEFF00") == 12, "Sixth test error: " + str(candidate("112233445566778899AABBCCDDEEFF00"))
68+ assert candidate("AB") == 1, "First test error: " + str(candidate("AB"))
69+ assert candidate("1077E") == 2, "Second test error: " + str(candidate("1077E"))
70+ assert candidate("ABED1A33") == 4, "Third test error: " + str(candidate("ABED1A33"))
71+ assert candidate("2020") == 2, "Fourth test error: " + str(candidate("2020"))
72+ assert candidate("123456789ABCDEF0") == 6, "Fifth test error: " + str(candidate("123456789ABCDEF0"))
73+ assert candidate("112233445566778899AABBCCDDEEFF00") == 12, "Sixth test error: " + str(candidate("112233445566778899AABBCCDDEEFF00"))
5474
5575
5676 # Check some edge cases that are easy to work out by hand.
5777 assert candidate([]) == 0
5878
5979```
60- -/
80+ -/
0 commit comments