Commit 5f8c528
authored
Simplify bin_to_hexadecimal function
Rewrote bin_to_hexadecimal to use Python's built-in int(s, 2) and hex() instead of a manual lookup table and bit-grouping loop.
The old approach had a padding bug: inputs whose length was already a multiple of 4 got an extra 4 zeros prepended, so '1010' came out as '0x0a' instead of '0xa'. The new approach skips padding entirely and lets the stdlib handle it correctly.
Also added a guard for "-" as input, which previously slipped past the empty-string check and raised an unhelpful ValueError about non-binary values.
Removes BITS_TO_HEX, the divmod padding expression, and the grouping loop. Doctests updated to reflect the corrected output.1 parent 791deb4 commit 5f8c528
1 file changed
+7
-36
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
2 | | - | |
3 | | - | |
4 | | - | |
5 | | - | |
6 | | - | |
7 | | - | |
8 | | - | |
9 | | - | |
10 | | - | |
11 | | - | |
12 | | - | |
13 | | - | |
14 | | - | |
15 | | - | |
16 | | - | |
17 | | - | |
18 | | - | |
19 | | - | |
20 | | - | |
21 | 1 | | |
22 | 2 | | |
23 | | - | |
| 3 | + | |
24 | 4 | | |
25 | 5 | | |
26 | 6 | | |
27 | 7 | | |
28 | | - | |
| 8 | + | |
29 | 9 | | |
30 | 10 | | |
31 | 11 | | |
| |||
37 | 17 | | |
38 | 18 | | |
39 | 19 | | |
40 | | - | |
41 | 20 | | |
42 | 21 | | |
43 | | - | |
44 | 22 | | |
45 | 23 | | |
| 24 | + | |
46 | 25 | | |
47 | 26 | | |
48 | | - | |
49 | | - | |
50 | 27 | | |
51 | | - | |
52 | | - | |
53 | | - | |
54 | | - | |
55 | | - | |
56 | | - | |
57 | | - | |
58 | | - | |
| 28 | + | |
| 29 | + | |
59 | 30 | | |
60 | | - | |
| 31 | + | |
| 32 | + | |
61 | 33 | | |
62 | 34 | | |
63 | 35 | | |
64 | 36 | | |
65 | | - | |
66 | 37 | | |
0 commit comments