42 Exam Rank 03 Today

| Exercise | Difficulty | Data Structure | Recursion Required? | |----------|------------|----------------|----------------------| | ft_list_size | Easy | Singly linked list | No (iterative) | | ft_list_push_back | Easy | Singly linked list | No | | ft_list_last | Easy | Singly linked list | No | | ft_list_foreach | Easy | Singly linked list | No | | ft_list_remove_if | Medium | Singly linked list | No | | ft_list_reverse | Medium | Singly linked list | No | | ft_list_sort | Medium-Hard | Singly linked list | Optional | | ft_list_merge | Medium | Singly linked list | No | | ft_itoa_base | Medium | String (no struct) | No (but tricky) | | ft_atoi_base | Medium | String (no struct) | No | | ft_split_whitespaces | Easy | String → array | No | | ft_range | Easy | Array | No | | ft_ultimate_range | Easy | Array | No | | ft_strjoin | Easy | String | No | | ft_btree_create_node | Easy | Binary tree | No | | ft_btree_apply_prefix | Medium | Binary tree | Yes | | ft_btree_apply_infix | Medium | Binary tree | Yes | | ft_btree_apply_suffix | Medium | Binary tree | Yes | | ft_btree_insert_data | Hard | Binary tree | Yes | | ft_btree_search_item | Medium | Binary tree | Yes | | ft_btree_level_count | Medium | Binary tree | Yes | | ft_btree_apply_by_level | Hard | Binary tree | Yes (with queue) | When you start Rank 03:

Good luck, and may your pointers never be dangling!

> 3 ex00? y Submitted. Moulinette: OK (2/2)

// Code in /rendu/ex00/ft_list_size.c // Test locally with a main (not submitted) // Submit 42 Exam Rank 03

typedef struct s_queue

int max(int a, int b) return (a > b ? a : b); int ft_btree_level_count(t_btree *root)

> 2 Available exercises: ex00: ft_list_size (2 pts) ex01: ft_btree_insert_data (4 pts) ex02: ft_itoa_base (4 pts) | Exercise | Difficulty | Data Structure |

You need a pointer to pointer begin_list because the head may change. 3. ft_itoa_base (Classic tricky one) char *ft_itoa_base(int value, int base)

t_list *current = *begin_list; t_list *previous = NULL; while (current)

struct s_list *next; void *data; t_list; typedef struct s_btree y Submitted

> 2 ex01: ft_btree_insert_data // Write recursive insert... // Submit Moulinette: KO (segfault) // Debug: forgot to malloc node // Resubmit Moulinette: OK (4/4)

if (!*root) *root = ft_btree_create_node(item); return; if (cmpf(item, (*root)->item) < 0) ft_btree_insert_data(&(*root)->left, item, cmpf); else ft_btree_insert_data(&(*root)->right, item, cmpf);

if (!node) return (ft_btree_create_node(item)); if (cmp(item, node->item) < 0) node->left = insert(node->left, item, cmp); else node->right = insert(node->right, item, cmp); return (node);

| Mistake | Consequence | Prevention | |---------|------------|------------| | Forgetting to include #include <stdlib.h> | Implicit function declaration → Moulinette fails | Write includes at top | | Memory leak in list remove_if | Fails strict test | Always free removed node | | Not handling NULL input | Segmentation fault in tests | Check if (!list) return | | Using recursion for deep lists | Stack overflow (not tested but bad style) | Use iteration for long lists | | Modifying original pointer without pointer-to-pointer | Head lost | Use t_list ** when head can change | | ft_itoa_base INT_MIN bug | Wrong output for -2147483648 | Special case: convert to unsigned | | Not checking base bounds | Undefined behavior → fails | if (base < 2 \|\| base > 16) return (NULL); | Recursion Cheat Sheet for Rank 03 Pattern 1: Traversal (no return value) void traverse(t_btree *node)

Introduction: What is Exam Rank 03? Exam Rank 03 is the third of five progressively difficult exams in the 42 curriculum (following Rank 00 and Rank 01, preceding Rank 04 and Rank 05). Unlike a school exam where you memorize facts, the 42 exam is a practical coding test in a restricted environment.