Exam Rank 02 Lego Recipes - kevshouse/exam_quest GitHub Wiki
-
๐จ Basic Brick: Input sanitization (whitespace/signs)
-
๐ฅ Logic Brick: Core computation (math/conditionals)
-
๐ฉ Assembly Brick: Output construction
-
๐ก๏ธ Safety Brick: Edge-case handling
-
๐ Connector Peg: Pointers/array traversal
void ft_print_numbers(void) {
char c = '0'; // ๐จ Starter brick
while (c <= '9') { // ๐ฉ Conveyor belt
write(1, &c, 1); // ๐ฅ Printer arm
c++;
}
}
Norm Tip: Replace printf with write 110.
void fizzbuzz(void) {
int i = 1; // ๐จ Starter brick
while (i <= 100) { // ๐ฉ Conveyor belt
if (i % 15 == 0) write(1, "fizzbuzz", 8); // ๐ก๏ธ Safety gate (divisible by 3 & 5)
else if (i % 3 == 0) write(1, "fizz", 4); // ๐ Logic peg
else if (i % 5 == 0) write(1, "buzz", 4);
else {
char c = '0' + i / 10; // ๐ฅ Tens digit assembler
if (i > 9) write(1, &c, 1);
c = '0' + i % 10; // ๐ฅ Units digit assembler
write(1, &c, 1);
}
write(1, "\n", 1);
i++;
}
}
int ft_strlen(char *str) {
int len = 0; // ๐จ Baseplate
while (str[len] != '\0') { // ๐ Connector peg
len++; // ๐ฉ Brick counter
}
return len;
}
void rotone(char *str) {
int i = 0;
while (str[i]) {
if (('a' <= str[i] && str[i] <= 'm') || ('A' <= str[i] && str[i] <= 'M'))
str[i] += 13; // ๐ Forward cipher wheel
else if (('n' <= str[i] && str[i] <= 'z') || ('N' <= str[i] && str[i] <= 'Z'))
str[i] -= 13; // ๐ Backward cipher wheel
write(1, &str[i++], 1); // ๐ฉ Output conveyor
}
}
Debug Hook: Visualize cipher as LEGO gear rotation 15.
int ft_atoi_base(char *str, char *base) {
int i = 0, sign = 1, res = 0;
// ๐จ Skip whitespace
while (str[i] == ' ') i++;
// ๐ฅ Sign brick
if (str[i] == '-' || str[i] == '+') sign = 1 - 2 * (str[i++] == '-');
// ๐ก๏ธ Base validation
int base_len = 0;
while (base[base_len]) base_len++;
// ๐ฉ Digit assembly
while (str[i]) {
int digit = -1, j = 0;
while (base[j])
if (str[i] == base[j]) digit = j; // ๐ Digit scanner
if (digit == -1) break;
res = res * base_len + digit; // ๐ง Assembly multiplier
i++;
}
return res * sign;
}
int is_power_of_2(int n) {
if (n <= 0) return 0; // ๐ก๏ธ Safety gate
while (n % 2 == 0) n /= 2; // ๐ช Division saw
return n == 1; // ๐ Logic peg
}
int *ft_range(int start, int end) {
int len = (end > start) ? end - start : start - end;
int *arr = malloc((len + 1) * sizeof(int)); // ๐งฑ Memory allocator brick
if (!arr) return NULL;
int i = 0;
while (i <= len) {
arr[i] = (start < end) ? start + i : start - i; // ๐ Directional conveyor
i++;
}
return arr;
}
int pgcd(int a, int b) {
while (b != 0) { // ๐ Loop until remainder 0
int temp = b; // ๐ง Gear swap
b = a % b; // โ๏ธ Modulo reducer
a = temp;
}
return a;
}
char **ft_split(char *str, char delim) {
int words = 0, i = 0;
// ๐งฎ Word counter brick
while (str[i])
if (str[i++] != delim && (str[i] == delim || !str[i])) words++;
char **res = malloc((words + 1) * sizeof(char *));
// โ๏ธ String splitter
int start = 0, idx = 0;
i = 0;
while (str[i]) {
if (str[i] == delim || !str[i + 1]) {
int len = i - start + (str[i + 1] ? 0 : 1);
res[idx] = malloc(len + 1);
int j = 0;
while (start < i) res[idx][j++] = str[start++];
res[idx++][j] = '\0';
start = i + 1;
}
i++;
}
res[idx] = NULL; // ๐ Termination cap
return res;
}
void sort_list(t_list **head, int (*cmp)(int, int)) {
int swapped;
t_list *ptr;
do {
swapped = 0;
ptr = *head;
while (ptr->next) {
if (cmp(ptr->data, ptr->next->data)) {
int temp = ptr->data; // ๐ Data swapper
ptr->data = ptr->next->data;
ptr->next->data = temp;
swapped = 1; // ๐ฅ Swap flag
}
ptr = ptr->next; // ๐ Traversal peg
}
} while (swapped);
}
void flood_fill(char **tab, t_point size, t_point start) {
char target = tab[start.y][start.x];
if (target == 'F') return;
// ๐ฆ Stack initialization
t_point *stack = malloc(size.x * size.y * sizeof(t_point));
int top = 0;
stack[top] = start;
while (top >= 0) {
t_point p = stack[top--];
if (p.x < 0 || p.x >= size.x || p.y < 0 || p.y >= size.y || tab[p.y][p.x] != target)
continue;
tab[p.y][p.x] = 'F'; // ๐จ Paint brick
// ๐ Neighbor stacker
stack[++top] = (t_point){p.x + 1, p.y};
stack[++top] = (t_point){p.x - 1, p.y};
stack[++top] = (t_point){p.x, p.y + 1};
stack[++top] = (t_point){p.x, p.y - 1};
}
free(stack);
}
void brainfuck(char *code) {
char tape[4096] = {0}; // ๐งฑ Memory tape
char *ptr = tape;
int loop_level = 0;
// ๐ Code parser
for (int i = 0; code[i]; i++) {
if (code[i] == '>') ptr++;
else if (code[i] == '<') ptr--;
else if (code[i] == '+') (*ptr)++;
else if (code[i] == '-') (*ptr)--;
else if (code[i] == '.') write(1, ptr, 1);
else if (code[i] == '[' && !*ptr) {
loop_level = 1;
while (loop_level) { // ๐ Loop skimmer
i++;
if (code[i] == '[') loop_level++;
else if (code[i] == ']') loop_level--;
}
}
}
}
Break functions into sub-steps (e.g., ft_split's word counter + splitter) 515.
Add write(1, "DEBUG", 5) at critical points to trace execution (remove later).
- Replace for with while
- Use write instead of printf
- Avoid ternary operators in complex logic
- Memory Safety: Always check malloc returns and free temp arrays (e.g., flood_fill's stack) 1.
โTruth can only be found in one place: the code.โ โ Robert C. Martin 1.
Test these models against 42's testers (e.g., war-machine, Moulinette) and refine the Lego analogies as you practice. For full projects (e.g., push_swap), extend the conveyor belt/assembly line metaphor 513.