Exam Rank 02 Lego Recipes - kevshouse/exam_quest GitHub Wiki

Core Lego Analogy Framework

  • ๐ŸŸจ 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

L0: Foundational Bricks

ft_print_numbers.c

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.

### fizzbuzz.c

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++;  
  }  
}  

L1: String Operations

ft_strlen.c

int ft_strlen(char *str) {  
  int len = 0;                 // ๐ŸŸจ Baseplate  
  while (str[len] != '\0') {   // ๐Ÿ”Œ Connector peg  
    len++;                     // ๐ŸŸฉ Brick counter  
  }  
  return len;  
}  

rot_13.c

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.

L2: Advanced Logic

ft_atoi_base.c

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;  
} 

is_power_of_2.c

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  
} 

L3: Memory & Algorithms

ft_range.c

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;  
}

pgcd.c (Euclidean Algorithm)

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;  
}

L4: Data Structures

ft_split.c

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;  
}

sort_list.c (Bubble Sort)

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);  
}

L5: Complex Systems

flood_fill.c (DFS)

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);  
}

brainfuck.c

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--;  
      }  
    }  
  }  
}

Key Exam Strategies

Modular Bricks:

Break functions into sub-steps (e.g., ft_split's word counter + splitter) 515.

Visual Debugging:

Add write(1, "DEBUG", 5) at critical points to trace execution (remove later).

Norm Compliance:

  • 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.

โš ๏ธ **GitHub.com Fallback** โš ๏ธ