Skip to content

Commit ef0bb0e

Browse files
author
Brian Elinsky
committed
Log convert_base attempt (solved with hints, optimal) and add flashcards
1 parent f0024c2 commit ef0bb0e

16 files changed

+126
-1
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Key Insight
2+
3+
**Q:** What's the key insight for converting between arbitrary bases?
4+
5+
**A:** Use decimal (Python's integer type) as an intermediate representation. Convert b1 → int → b2.
6+
7+
```
8+
"615" (base 7) → 306 (decimal) → "1A7" (base 13)
9+
```
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Two Phases of Conversion
2+
3+
**Q:** What are the two main phases of base conversion?
4+
5+
**A:**
6+
1. **String → Int:** Parse the string in base b1 to get decimal integer
7+
2. **Int → String:** Convert the integer to a string in base b2
8+
9+
Note: There's no separate "number in base N" - the string IS the base-N representation. Parsing directly produces the int; formatting directly produces the string.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# String to Int Formula
2+
3+
**Q:** What's the formula to convert a string "d2 d1 d0" in base b to an integer?
4+
5+
**A:** `result = d2 * b^2 + d1 * b^1 + d0 * b^0`
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Character to Digit Value
2+
3+
**Q:** How do you convert a single character ('0'-'9', 'A'-'F') to its digit value?
4+
5+
**A:** `string.hexdigits.index(c.lower())` — returns 0-15
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Digit Position Meaning
2+
3+
**Q:** In base b, what does each digit position represent (right to left)?
4+
5+
**A:** Powers of b: rightmost is b^0, then b^1, b^2, ...
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Base 7 Example
2+
3+
**Q:** What does "61" in base 7 mean in decimal?
4+
5+
**A:** `6 * 7^1 + 1 * 7^0 = 42 + 1 = 43`
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Hex Example
2+
3+
**Q:** What does "1A" in base 16 mean in decimal?
4+
5+
**A:** `1 * 16^1 + 10 * 16^0 = 16 + 10 = 26`
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Accumulation Pattern
2+
3+
**Q:** What's the accumulation pattern for building an integer from digits (left to right)?
4+
5+
**A:**
6+
1. Shift left: `result = result * base`
7+
2. Add digit: `result = result + digit`
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# String to Int Algorithm
2+
3+
**Q:** Describe the algorithm to convert a string in base b to a base 10 int.
4+
5+
**A:**
6+
1. Start with result = 0
7+
2. For each character left to right:
8+
- Convert character (base b) to digit value (base 10) using hexdigits lookup
9+
- Shift result left (multiply by base)
10+
- Add digit to result
11+
3. Return result
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Extract Digits Operations
2+
3+
**Q:** What two operations extract digits when converting a base 10 int to base b?
4+
5+
**A:**
6+
- `num % base` → gives the least significant digit (rightmost)
7+
- `num // base` → removes the least significant digit

0 commit comments

Comments
 (0)