Rank 02 tougher - kevshouse/exam_quest GitHub Wiki
Based on your focus areas, here's a technical breakdown of core patterns and abstracted flows using Mermaid diagrams. These techniques address 90% of 42 Exam 02 challenges:
1. String/Number Conversion Pattern (ft_atoi, ft_itoa, fprime)
graph TD
A[Input] --> B{Type}
B -->|String| C[ft_atoi/ft_atoi_base]
B -->|Integer| D[ft_itoa/fprime]
C --> C1[Skip whitespace]
C1 --> C2[Detect sign]
C2 --> C3[Convert char to digit]
C3 --> C4[result = result * base + digit]
D --> D1[Handle negative]
D1 --> D2[Count digits]
D2 --> D3[Allocate memory]
D3 --> D4[Backward fill digits]
Key Techniques:
- Atoi Base:
digit = position_in_base("0123456789ABCDEF", char)
- Itoa:
buffer[i--] = (abs(num % 10) + '0'; num /= 10
- Fprime:
while (n % factor == 0) n /= factor; factor++
2. String Transformation Pattern (rot_13, expand_str, epur_str)
graph TD
A[Input String] --> B[Initialize Output]
B --> C{Char Check}
C -->|Alphabet| D["rot_13 rotone char = (char - 'a' + offset) % 26 + 'a'"]
C -->|Space/Tab| E[expand_str/epur_str]
D --> F[Write Char]
E --> G[State Machine]
G -->|Word Start| H[Write Space?]
G -->|In Word| I[Write Char]
H --> F
I --> F
F --> C
Key Techniques:
- ROT Cipher:
char += 13; if (char > 'z') char -= 26
- Epur_Str: State machine with flags
(in_word, leading_space)
- Expand_Str:
if (!isspace(c)) write(char + ' ') else write(" ")
3. Memory Management Pattern (ft_split, ft_list_foreach)
graph TD
A[Input] --> B[Count Elements]
B --> C[Allocate Array]
C --> D[Tokenize]
D --> E[Copy Substring]
E --> F[Store Pointer]
F --> G{More Tokens?}
G -->|Yes| D
G -->|No| H[NULL Terminate]
I[ft_list_foreach] --> J[Traverse List]
J --> K[Apply Function]
K --> L[Next Node]
Key Techniques:
- Split:
while (*str) { while (*str == delim) str++; start = str; while (*str && *str != delim) str++; if (start != str) arr[i++] = strndup(start, str - start); }
- List Foreach:
while (list) { f(list->data); list = list->next; }
4. Matrix/Array Algorithms (flood_fill, sort_int_tab)
graph TD
A[Input Data] --> B{Type}
B -->|2D Matrix| C[flood_fill]
B -->|1D Array| D[sort_int_tab]
C --> C1[Check Bounds]
C1 --> C2[Match Original Color?]
C2 -->|Yes| C3[Set New Color]
C3 --> C4[Recurse: Up/Down/Left/Right]
D --> D1[Bubble Sort]
D1 --> D2[Compare Adjacent]
D2 -->|Swap Needed| D3[ft_swap]
Key Techniques:
- Flood Fill:
void fill(char **tab, t_point size, t_point cur, char orig) { if (cur.y < 0 || cur.y >= size.y || cur.x < 0 || cur.x >= size.x || tab[cur.y][cur.x] != orig) return; tab[cur.y][cur.x] = 'F'; fill(tab, size, (t_point){cur.x+1, cur.y}, orig); fill(tab, size, (t_point){cur.x-1, cur.y}, orig); // ... up/down }
- Sort Tab:
for (i=0; i<size-1; i++) for (j=0; j<size-i-1; j++) if (tab[j] > tab[j+1]) swap(&tab[j], &tab[j+1]);
5. Math Operations (lcm, pgcd)
graph TD
A[Input a,b] --> B[Absolute Values]
B --> C[PGCD: Euclidean Algorithm]
C --> D{while b != 0}
D -->|Yes| E[temp = b, b = a % b, a = temp]
D -->|No| F[PGCD = a]
F --> G["LCM = |a*b| / PGCD"]
Key Techniques:
- PGCD:
while (b) { temp = b; b = a % b; a = temp; } return a;
- LCM:
return (a * b) / pgcd(a, b)
(handle overflow withlong
)
Universal Strategies:
-
Edge Case Handling:
- Null inputs
- Empty strings
- INT_MIN/Max
- Single-element arrays
if (!str || !*str) return (0); // Standard guard clause
-
Memory Safety:
- Always NULL-terminate strings
- Use
valgrind
test cases:
valgrind ./a.out "test case" | grep "ERROR SUMMARY"
-
ASCII Math Shortcuts:
c >= 'a' && c <= 'z'
digit = c - '0'
upper = lower - 32
-
Pointer Arithmetic:
while (*s && !delim(*s)) s++; // Instead of index counters
Would you like deep-dive implementations for specific functions or optimization techniques for recursion-heavy tasks like flood_fill?