11module
22
3- def solve : Unit :=
4- ()
3+ def solve (n : Nat) : String :=
4+ String.ofList (Nat.toDigits 2 (n.repr.chars.map (fun c => c.toNat - '0' .toNat) |>.fold (init := 0 ) (· + ·)))
5+
6+ example : solve 1000 = "1" := by native_decide
7+ example : solve 150 = "110" := by native_decide
8+ example : solve 147 = "1100" := by native_decide
9+ example : solve 333 = "1001" := by native_decide
10+ example : solve 963 = "10010" := by native_decide
11+
12+ theorem ofDigitChars_solve {n : Nat} :
13+ Nat.ofDigitChars 2 (solve n).toList 0 = (n.repr.toList.map (fun c => c.toNat - '0' .toNat)).sum := by
14+ rw [solve, String.toList_ofList, Nat.ofDigitChars_toDigits (by simp) (by simp),
15+ ← Std.Iter.foldl_toList, Std.Iter.toList_map]
16+ simp [List.sum_eq_foldl]
517
618/-!
719## Prompt
@@ -10,12 +22,12 @@ def solve : Unit :=
1022
1123def solve(N):
1224 """Given a positive integer N, return the total sum of its digits in binary.
13-
25+
1426 Example
1527 For N = 1000, the sum of digits will be 1 the output should be "1".
1628 For N = 150, the sum of digits will be 6 the output should be "110".
1729 For N = 147, the sum of digits will be 12 the output should be "1100".
18-
30+
1931 Variables:
2032 @N integer
2133 Constraints: 0 ≤ N ≤ 10000.
@@ -47,4 +59,4 @@ def check(candidate):
4759 assert candidate(963) == "10010", "Error"
4860
4961```
50- -/
62+ -/
0 commit comments