├── level01 ├── t_point.h ├── subjects │ ├── count_alpha.txt │ ├── height_tree.txt │ ├── stack.txt │ ├── queue.txt │ └── flood_fill.txt ├── flood_fill.c ├── height_tree.c ├── count_alpha.c ├── stack.c └── queue.c ├── level00 ├── subjects │ ├── count_of_2.txt │ ├── is_anagram.txt │ ├── equation.txt │ ├── find_pivot.txt │ └── print_doublon.txt ├── is_anagram.c ├── print_doublon.c ├── find_pivot.c ├── count_of_2.c └── equation.c ├── level04 └── subjects │ ├── range_comb.txt │ ├── clone_list.txt │ ├── intersection.txt │ ├── longest_subarray.txt │ └── volume_histogram.txt ├── level05 └── subjects │ ├── infin_add.txt │ ├── infin_mult.txt │ ├── g_diam.txt │ └── count_island.txt ├── README.md ├── .editorconfig ├── level02 ├── subjects │ ├── str_maxlenoc.txt │ ├── reverse_tree.txt │ ├── ord_alphlong.txt │ ├── longest_sequence.txt │ └── is_looping.txt └── reverse_tree.c ├── level03 └── subjects │ ├── convert_bst.txt │ ├── can_split.txt │ ├── gold_gain.txt │ ├── width_tree.txt │ └── perimeter.c └── .gitignore /level01/t_point.h: -------------------------------------------------------------------------------- 1 | #ifndef T_POINT_FLOOD_FILL 2 | # define T_POINT_FLOOD_FILL 3 | 4 | typedef struct s_point { 5 | int x; // x : Width | x-axis 6 | int y; // y : Height | y-axis 7 | } t_point; 8 | 9 | #endif 10 | 11 | -------------------------------------------------------------------------------- /level00/subjects/count_of_2.txt: -------------------------------------------------------------------------------- 1 | Assignment name : count_of_2 2 | Expected files : count_of_2.c 3 | Allowed functions: None 4 | -------------------------------------------------------------------------------- 5 | Implement a function which counts, for a given integer n, the number of 2s 6 | that appear in all the numbers between 0 and n (inclusive). 7 | Your function must be declared as follows: 8 | int count_of_2(int n); 9 | If n <= 1, the function returns 0; 10 | Examples: 11 | input = 25 12 | output = 9 13 | because there are 9 2s in (2, 12, 20, 21, 22, 23, 24 and 25) 14 | Note: 15 | the number 22 counts as 2 because it has two 2s, number 202 count as 2, number 22022 count as 4 etc 16 | -------------------------------------------------------------------------------- /level04/subjects/range_comb.txt: -------------------------------------------------------------------------------- 1 | Assignment name : range_comb 2 | Expected files : range_comb.c 3 | Allowed functions: malloc free memcpy 4 | -------------------------------------------------------------------------------- 5 | Implement a function which computes, for a given integer n, all 6 | the possible permutations of the numbers in the range from 0 to 7 | (n - 1) inclusive. 8 | The function returns a null-terminated array. 9 | Your function must be declared as follows: 10 | int **range_comb(int n); 11 | If n <= 0, the function returns -1; 12 | Examples: 13 | input = 2 14 | output = {{0, 1}, {1, 0}} 15 | input = 3 16 | output = {{0, 1, 2}, {0, 2, 1}, {1, 0, 2}, {1, 2, 0}, {2, 0, 1}, {2, 1, 0}} 17 | -------------------------------------------------------------------------------- /level04/subjects/clone_list.txt: -------------------------------------------------------------------------------- 1 | Assignment name : clone_list 2 | Expected files : clone_list.c 3 | Allowed functions: malloc 4 | -------------------------------------------------------------------------------- 5 | Given a linked list with the following structure : 6 | struct s_node { 7 | int data; 8 | struct s_node *next; 9 | struct s_node *other; 10 | }; 11 | where : 12 | - next pointer points to the next node. 13 | - other pointer points to any node in the list (another node, itself or NULL). 14 | Write a function to create a copy of this list (allocate new memory). 15 | You cannot modify the structure, you cannot modify the list you are given. 16 | The function must be declared as follows: 17 | struct s_node *clone_list(struct s_node *node); 18 | -------------------------------------------------------------------------------- /level01/subjects/count_alpha.txt: -------------------------------------------------------------------------------- 1 | Assignment name : count_alpha 2 | Expected files : count_alpha.c 3 | Allowed functions: write, printf 4 | -------------------------------------------------------------------------------- 5 | 6 | Write a program called count_alpha that takes a string and displays the number 7 | of occurences of its alphabetical characters. Other characters are not counted. 8 | The order is the order of occurence in the string. The display must be ended by 9 | a newline. 10 | 11 | The format is in the examples. 12 | 13 | If the number of arguments is not 1, display only a newline. 14 | 15 | Examples : 16 | $> ./count_alpha abbcc 17 | 1a, 2b, 2c 18 | $> ./count_alpha "abbcc" 19 | 1a, 2b, 2c 20 | $> ./count_alpha "abbcc" "dddeef" | cat -e 21 | $ 22 | $> ./count_alpha "My Hyze 47y 7." | cat -e 23 | 1m, 3y, 1h, 1z, 1e$ 24 | $> ./count_alpha "" | cat -e 25 | $ 26 | -------------------------------------------------------------------------------- /level05/subjects/infin_add.txt: -------------------------------------------------------------------------------- 1 | Assignment name : infin_add 2 | Expected files : *.c, *.h 3 | Allowed functions: write, malloc, free 4 | -------------------------------------------------------------------------------- 5 | Write a program that takes as a parameter two strings that represent two 6 | numbers potentially infinit, and displays on stdout the result of the addition 7 | of these two numbers, followed by a '\n'. 8 | A negative number will always be prefixed by one and only one -. The only 9 | characters that can be part of the strings are digits and the sign -. 10 | Both parameters will always be well formated, and you will always have 11 | exactly two parameters, no tricks. 12 | Example: 13 | $> ./infin_add "879879087" "67548976597" | cat -e 14 | 68428855684$ 15 | $> ./infin_add "-876435" "987143265" | cat -e 16 | 986266830$ 17 | $> ./infin_add "-807965" "-34532" 18 | -842497 19 | $> 20 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # 42_Exam-C-Intermediate 2 | 3 | ## Exam 4 | The exam goes by levels, starting from level 0. 5 | By doing each problem correctly, one level up, and so the difficulty of the 6 | problems. If one fails a problem, you can retry another problem in the same level, 7 | but without getting the full marks. 8 | 9 | ## TODO 10 | The following solutions are pending: 11 | 12 | ### Level 02 13 | - is_looping 14 | - longest_sequence 15 | - ord_alphlong 16 | - str_maxlenoc 17 | 18 | ---- 19 | 20 | ### Level 03 21 | - can_split 22 | - convert_bst 23 | - gold_gain 24 | - width_tree 25 | 26 | ---- 27 | 28 | ### Level 04 29 | - clone_list 30 | - intersection 31 | - longest_subarray 32 | - range_comb 33 | - volume_histogram 34 | 35 | ---- 36 | 37 | ### Level 05 38 | - count_island 39 | - g_diam 40 | - infin_add 41 | - infin_mult 42 | 43 | https://github.com/Manmeet2018/42_Exam-C-Intermediate 44 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | # top-most EditorConfig file 2 | root = true 3 | 4 | # Unix-style newlines with a newline ending every file 5 | [*] 6 | end_of_line = lf 7 | insert_final_newline = true 8 | 9 | # Matches multiple files with brace expansion notation 10 | # Set default charset 11 | [*.{c,h,js,py}] 12 | charset = utf-8 13 | indent_style = space 14 | indent_size = 4 15 | trim_trailing_whitespace = true 16 | 17 | # 4 space indentation 18 | [*.py] 19 | indent_style = space 20 | indent_size = 4 21 | 22 | # Indentation override for all JS under lib directory 23 | [lib/**.js] 24 | indent_style = space 25 | indent_size = 2 26 | 27 | # Matches the exact files either package.json or .travis.yml 28 | [{package.json,.travis.yml}] 29 | indent_style = space 30 | indent_size = 2 31 | 32 | # Tab indentation (no size specified) 33 | [Makefile] 34 | charset = utf-8 35 | indent_style = space 36 | indent_size = 4 37 | trim_trailing_whitespace = true 38 | -------------------------------------------------------------------------------- /level04/subjects/intersection.txt: -------------------------------------------------------------------------------- 1 | Assignment name : intersection 2 | Expected files : intersection.c 3 | Allowed functions: 4 | -------------------------------------------------------------------------------- 5 | Given two singly linked lists, determine if the two lists intersect. 6 | Return the intersecting node. 7 | The intersection is defined based on reference, not value. 8 | That is, if the kth node of the first linked list is the exact same node 9 | (by reference) as the jth node of the second linked list, then they are 10 | intersecting. 11 | The linked lists will use the following structure : 12 | struct s_node { 13 | void *content; 14 | struct s_node *next; 15 | }; 16 | The function must be declared as follows: 17 | void *intersection(struct s_node *lst1, struct s_node *lst2); 18 | If the two linked lists are not intersecting, the function returns NULL. 19 | The function does not modify the two linked list. 20 | -------------------------------------------------------------------------------- /level02/subjects/str_maxlenoc.txt: -------------------------------------------------------------------------------- 1 | Assignment name : str_maxlenoc 2 | Expected files : str_maxlenoc.c 3 | Allowed functions: write, malloc, free 4 | -------------------------------------------------------------------------------- 5 | 6 | Write a program that takes one or more strings and displays, followed by a 7 | newline, the longest string that appears in every parameter. If more that one 8 | string qualifies, it will display the one that appears first in the first 9 | parameter. Note that the empty string technically appears in any string. 10 | 11 | If there are no parameters, the program displays \n. 12 | 13 | Examples: 14 | 15 | $>./str_maxlenoc ab bac abacabccabcb 16 | a 17 | $>./str_maxlenoc bonjour salut bonjour bonjour 18 | u 19 | $>./str_maxlenoc xoxAoxo xoxAox oxAox oxo A ooxAoxx oxooxo Axo | cat -e 20 | $ 21 | $>./str_maxlenoc bosdsdfnjodur atehhellosd afkuonjosurafg headfgllosf fghellosag afdfbosnjourafg 22 | os 23 | $>./str_maxlenoc | cat -e 24 | $ 25 | -------------------------------------------------------------------------------- /level05/subjects/infin_mult.txt: -------------------------------------------------------------------------------- 1 | Assignment name : infin_mult 2 | Expected files : *.c, *.h 3 | Allowed functions: write, malloc, free 4 | -------------------------------------------------------------------------------- 5 | Write a program that takes as a parameter two strings that represent two 6 | numbers potentially infinit, and displays on stdout the result of the 7 | multiplication of these two numbers, followed by a '\n'. 8 | A negative number will always be prefixed by one and only one -. The only 9 | characters that can be part of the strings are digits and the sign -. 10 | Both parameters will always be well formated, and you will always have exactly 11 | two parameters, no tricks. 12 | Example: 13 | $> ./infin_mult "879879087" "67548976597" | cat -e 14 | 59434931855952726939$ 15 | $> ./infin_mult "-876435" "987143265" | cat -e 16 | -865166907460275$ 17 | $> ./infin_mult "-807965" "-34532" | cat -e 18 | 27900647380$ 19 | $> ./infin_mult "-807965" "0" 20 | 0 21 | $> 22 | -------------------------------------------------------------------------------- /level01/subjects/height_tree.txt: -------------------------------------------------------------------------------- 1 | Assignment name : height_tree 2 | Expected files : height_tree.c 3 | Allowed functions: 4 | -------------------------------------------------------------------------------- 5 | Implement a function to calculate the height of an n-ary tree. 6 | Your should use the following node structure in your code for the n-ary tree: 7 | struct s_node { 8 | int value; 9 | struct s_node **nodes; 10 | }; 11 | In this struct nodes is a null-terminated array. 12 | Note that the height of a tree is the number of EDGES on the longest path from 13 | the root node to a leaf. A tree with a single node will have a height of 0. 14 | An empty tree should have height -1; 15 | The function must be declared as follows: 16 | int height_tree(struct s_node *root); 17 | EXAMPLE: 18 | The Input Tree : 19 | 94 20 | / \ 21 | / \ 22 | 34 52 23 | / \ \ 24 | / \ \ 25 | 1 99 11 26 | / 27 | / 28 | 13 29 | Output : 3 30 | -------------------------------------------------------------------------------- /level05/subjects/g_diam.txt: -------------------------------------------------------------------------------- 1 | Assignment name : g_diam 2 | Expected files : *.c, *.h 3 | Allowed functions: write, malloc, free 4 | -------------------------------------------------------------------------------- 5 | 6 | Write a programe that takes a string. This string represents a graph and is 7 | composed of series of links between this graph's nodes. Links are separated by 8 | a single space. Nodes are represented by numbers, and links by two nodes 9 | separated by a '-'. For exemple, if there is a link between nodes 2 10 | and 3, it could be written as "2-3" or "3-2". 11 | 12 | The program will display the number of nodes comprised in the longest chain, 13 | followed by a '\n', given it's impossible to pass through a node more than once. 14 | 15 | If the number of parameters is different from 1, the program displays a '\n'. 16 | 17 | Examples: 18 | 19 | $>./g_diam "17-5 5-8 8-2 2-8 2-8 17-21 21-2 5-2 2-6 6-14 6-12 12-19 19-14 14-42" | cat -e 20 | 10$ 21 | $>./g_diam "1-2 2-3 4-5 5-6 6-7 7-8 9-13 13-10 10-2 10-11 11-12 12-8 16-4 16-11 21-8 21-12 18-10 18-13 21-18" | cat -e 22 | 15$ 23 | -------------------------------------------------------------------------------- /level04/subjects/longest_subarray.txt: -------------------------------------------------------------------------------- 1 | Assignment name : longest_subarray 2 | Expected files : longest_subarray.c 3 | Allowed functions: malloc free strlen 4 | -------------------------------------------------------------------------------- 5 | Given a null-terminated array of digit characters, implement a function which 6 | finds the longest subarray with an equal number of even and odd digits. 7 | The function returns a null-terminated array. 8 | Your function must be declared as follows: 9 | char *longest_subarray(char *arr); 10 | Example 1: 11 | input = 134 12 | output = 34 13 | In the above example, 1 and 3 is odd and 4 is even, 14 | the longest subarray with an equal number of odd and even digits is 34. 15 | Example 2: 16 | input = 454 17 | output = 45 18 | Example 3: 19 | input = 1357913579024680213579 20 | output = 79135790246802 21 | Example 4: 22 | input = 2010102 23 | output = 0101 24 | Note: 25 | - As a reminder, 0, 2, 4, 6, 8 are even digits and 1, 3, 5, 7, 9 are odd. 26 | - In case of more than one subarray with the same length, 27 | the expected output is the first one. 28 | -------------------------------------------------------------------------------- /level03/subjects/convert_bst.txt: -------------------------------------------------------------------------------- 1 | Assignment name : convert_bst 2 | Expected files : convert_bst.c 3 | Allowed functions: 4 | -------------------------------------------------------------------------------- 5 | A binary search tree (BST) is a binary tree in which every node fits 6 | a specific ordering property : 7 | all left descendants <= n < all right descendants 8 | This must be true for each node n. 9 | Implement a function to convert a binary search tree to a sorted, circular, 10 | doubly-linked list. 11 | This conversion must be in-place (using the tree nodes as the new list nodes). 12 | The binary search tree uses the following node structure : 13 | struct s_node { 14 | int value; 15 | struct s_node *right; 16 | struct s_node *left; 17 | }; 18 | The function must be declared as follows: 19 | struct s_node *convert_bst(struct s_node *bst); 20 | The function must return a pointer to the smallest element of the sorted, 21 | circular, doubly-linked list. 22 | For each node of the linked list, the right pointer points to the next node 23 | and the left pointer points to the previous node. 24 | The sort is in increasing order. 25 | -------------------------------------------------------------------------------- /level01/flood_fill.c: -------------------------------------------------------------------------------- 1 | #include "t_point.h" 2 | 3 | void flood_fill(char **tab, t_point size, t_point begin) 4 | { 5 | char point = tab[begin.y][begin.x]; 6 | 7 | tab[begin.y][begin.x] = 'F'; 8 | begin.y += 1; 9 | if (begin.y < size.y && tab[begin.y][begin.x] == point) 10 | flood_fill(tab, size, begin); 11 | begin.y -= 2; 12 | if (begin.y >= 0 && tab[begin.y][begin.x] == point) 13 | flood_fill(tab, size, begin); 14 | begin.y += 1; 15 | begin.x += 1; 16 | if (begin.x < size.x && tab[begin.y][begin.x] == point) 17 | flood_fill(tab, size, begin); 18 | begin.x -= 2; 19 | if (begin.x >= 0 && tab[begin.y][begin.x] == point) 20 | flood_fill(tab, size, begin); 21 | return ; 22 | } 23 | 24 | /* 25 | 26 | #include "test_functions.h" 27 | #include "flood_fill.h" 28 | 29 | int main(void) 30 | { 31 | char **area; 32 | t_point size = {8, 5}; 33 | t_point begin = {2, 2}; 34 | char *zone[] = { 35 | "1 1 1 1 1 1 1 1", 36 | "1 0 0 0 1 0 0 1", 37 | "1 0 0 1 0 0 0 1", 38 | "1 0 1 1 0 0 0 1", 39 | "1 1 1 0 0 0 0 1", 40 | } 41 | 42 | area = make_area(zone); 43 | print_tab(area); 44 | flood_fill(area, size, begin); 45 | putc('\n'); 46 | print_tab(area); 47 | return (0); 48 | } 49 | 50 | */ -------------------------------------------------------------------------------- /level02/subjects/reverse_tree.txt: -------------------------------------------------------------------------------- 1 | Assignment name : reverse_tree 2 | Expected files : reverse_tree.c 3 | Allowed functions: 4 | -------------------------------------------------------------------------------- 5 | Implement a function to reverse a binary tree (i.e., flip it from right to left). 6 | You must declare the following node structure for the binary tree in your code: 7 | struct s_node { 8 | int value; 9 | struct s_node *right; 10 | struct s_node *left; 11 | }; 12 | The function must be declared as follows: 13 | void reverse_tree(struct s_node *root); 14 | You must include the struct in your file. 15 | EXAMPLE : 16 | The following tree : 17 | 94 18 | / \ 19 | / \ 20 | 34 52 21 | / \ 22 | / \ 23 | 1 99 24 | / / \ 25 | 20 / \ 26 | / \ 27 | 83 39 28 | \ / \ 29 | 61 / \ 30 | 37 67 31 | would become : 32 | 94 33 | / \ 34 | / \ 35 | 52 34 36 | / \ 37 | / \ 38 | 99 1 39 | / \ \ 40 | / \ 20 41 | / \ 42 | 39 83 43 | / \ / 44 | / \ 61 45 | 67 37 46 | -------------------------------------------------------------------------------- /level02/subjects/ord_alphlong.txt: -------------------------------------------------------------------------------- 1 | Assignment name : ord_alphlong 2 | Expected files : *.c, *.h 3 | Allowed functions: write, malloc, free 4 | -------------------------------------------------------------------------------- 5 | Write a program that takes a string as a parameter and prints its words sorted 6 | in order of their length first and then in alphabetical order on each line: 7 | when we say alphabetical order we ignore the case of letters. 8 | For example aA and Aa are the same but the original order must remain 9 | (lower and upper case are the same in alphabetical order). If there are 10 | duplicates, they must remain. 11 | If number of parameters is different from 1, the program prints 12 | \n. 13 | There will be only spaces, tabs and alphanumeric caracters in strings. 14 | You'll print only one space between each word. Nothing before the first and 15 | after the last word of each line. 16 | Examples: 17 | $>./ord_alphlong 18 | $ 19 | $>./ord_alphlong "De son baton il frappa la pierre et l eau jaillit" | cat -e 20 | l$ 21 | De et il la$ 22 | eau son$ 23 | baton$ 24 | frappa pierre$ 25 | jaillit$ 26 | $>./ord_alphlong "A a b B cc ca cd" | cat -e 27 | A a b B$ 28 | ca cc cd$ 29 | $>./ord_alphlong "Pour l Imperium de l humanite" | cat -e 30 | l l$ 31 | de$ 32 | Pour$ 33 | humanite Imperium$ 34 | $> 35 | -------------------------------------------------------------------------------- /level04/subjects/volume_histogram.txt: -------------------------------------------------------------------------------- 1 | Assignment name : volume_histogram 2 | Expected files : volume_histogram.c 3 | Allowed functions: None 4 | -------------------------------------------------------------------------------- 5 | Implement a function which computes, for a given histogram (bar graph) 6 | of positive integers, the volume of water it could hold if someone poured 7 | water across the top. 8 | Each histogram bar has width 1. 9 | Your function must be declared as follows: 10 | int volume_histogram(int *histogram, int size); 11 | Example 1: 12 | With the following input: 13 | int histogram[] = {1, 0, 2, 0, 2}; 14 | int size = 5; 15 | ('#' bars are the histogram. '.' is water) 16 | # . # 17 | # . # . # 18 | --------- 19 | 1 0 2 0 2 20 | The function here returns 3 (because there is 3 emplacement of water). 21 | Example 2: 22 | With the following input: 23 | int histogram[] = {0, 0, 4, 0, 0, 6, 0, 0, 3, 0, 5, 0, 1, 0, 0, 0}; 24 | int size = 16; 25 | ('#' bars are the histogram. '.' is water) 26 | # 27 | # . . . . # 28 | # . . # . . . . # 29 | # . . # . . # . # 30 | # . . # . . # . # 31 | # . . # . . # . # . # 32 | ------------------------------- 33 | 0 0 4 0 0 6 0 0 3 0 5 0 1 0 0 0 34 | The function returns 26. 35 | -------------------------------------------------------------------------------- /level01/subjects/stack.txt: -------------------------------------------------------------------------------- 1 | Assignment name : stack 2 | Expected files : stack.c 3 | Allowed functions: malloc free 4 | -------------------------------------------------------------------------------- 5 | Implement a stack data structure in C, using the following structures in your 6 | code: 7 | struct s_node { 8 | void *content; 9 | struct s_node *next; 10 | }; 11 | struct s_stack { 12 | struct s_node *top; 13 | }; 14 | A stack uses LIFO (last-in fist-out) ordering : 15 | the most recent item added to the stack is the first item to be removed. 16 | Implement 5 functions for the following stack operations : 17 | - init() : Initialize the stack. 18 | The top pointer is set to NULL. 19 | - pop(stack) : Remove the top item from the stack and return it. 20 | If the stack is empty, the function returns NULL. 21 | - push(stack, item) : Add an item to the top of the stack. 22 | - peek(stack) : Return the top of the stack. 23 | If the stack is empty, the function returns NULL. 24 | - isEmpty(stack) : Return 1 if the stack is empty, 0 otherwise. 25 | These functions must be declared as follows: 26 | struct s_stack *init(void); 27 | void *pop(struct s_stack *stack); 28 | void push(struct s_stack *stack, void *content); 29 | void *peek(struct s_stack *stack); 30 | int isEmpty(struct s_stack *stack); 31 | -------------------------------------------------------------------------------- /level00/subjects/is_anagram.txt: -------------------------------------------------------------------------------- 1 | Assignment name : is_anagram 2 | Expected files : is_anagram.c 3 | Allowed functions: 4 | -------------------------------------------------------------------------------- 5 | ALERT: OPTIMIZED SOLUTION REQUIRED. 6 | An anagram is a sequence of characters formed by rearranging the letters of 7 | another sequence, such as 'cinema', formed from 'iceman'. 8 | Given two strings as parameters, create a function able to tell whether or 9 | not the first string is an anagram of the second. 10 | The function must be declared as follows: 11 | int is_anagram(char *a, char *b); 12 | Considerations: 13 | - Be careful: the naive solution won't work on our big input, you have to 14 | find an optimized solution which will run in O(sa + sb) time (where sa is 15 | the length of a and sb length of b). 16 | - Our tested string will always be a sequence of ascii characters between 32 17 | and 126 inclusive. 18 | - The bigger test we will do is on 2 sequences of 1.000.000 characters each. 19 | It should run in less than 2 seconds. 20 | Example 1: 21 | a='abcdef' 22 | b='fabcde' 23 | In this case, these two strings are anagrams, your function should return 1. 24 | Example 2: 25 | a='.123?.' 26 | b='?321..' 27 | In this case, these two strings are anagrams, your function should return 1. 28 | Example 3: 29 | a='abca' 30 | b='bcab' 31 | In this case, these two strings are not anagrams, your function should return 0. 32 | -------------------------------------------------------------------------------- /level01/subjects/queue.txt: -------------------------------------------------------------------------------- 1 | Assignment name : queue 2 | Expected files : queue.c 3 | Allowed functions: malloc free 4 | -------------------------------------------------------------------------------- 5 | Implement a queue data structure in C, using the following structures in your 6 | code: 7 | struct s_node { 8 | void *content; 9 | struct s_node *next; 10 | }; 11 | struct s_queue { 12 | struct s_node *first; 13 | struct s_node *last; 14 | }; 15 | A queue uses FIFO (first-in fist-out) ordering : 16 | items are removed from the data structure in the same order that they are added. 17 | Implement 5 functions for the following queue operations : 18 | - init() : Initialize the queue. 19 | The first and last pointers are set to NULL. 20 | - enqueue(queue, item) : Add an item to the end of the queue. 21 | - dequeue(queue) : Remove the first item from the queue and return it. 22 | If the queue is empty, the function returns NULL. 23 | - peek(queue) : Return the first item of the queue. 24 | If the queue is empty, the function returns NULL. 25 | - isEmpty(queue) : Return 1 if the queue is empty, 0 otherwise. 26 | These functions must be declared as follows: 27 | struct s_queue *init(void); 28 | void enqueue(struct s_queue *queue, void *content); 29 | void *dequeue(struct s_queue *queue); 30 | void *peek(struct s_queue *queue); 31 | int isEmpty(struct s_queue *queue); 32 | -------------------------------------------------------------------------------- /level02/subjects/longest_sequence.txt: -------------------------------------------------------------------------------- 1 | Assignment name : longest_sequence 2 | Expected files : longest_sequence.c 3 | Allowed functions: 4 | -------------------------------------------------------------------------------- 5 | 6 | Given the root node of a binary tree, create a function that return the length of the longest path which comprises of nodes with consecutive values in increasing order. 7 | Every node is considered as a path of length 1. 8 | 9 | The binary tree uses the following node structure : 10 | 11 | struct s_node 12 | { 13 | int value; 14 | struct s_node *left; 15 | struct s_node *right; 16 | }; 17 | 18 | The function must be declared as follows: 19 | 20 | int longest_sequence(struct s_node *node); 21 | 22 | 23 | Example 1: 24 | 25 | 10 26 | / 27 | 5 28 | / \ 29 | / \ 30 | 6 9 31 | / \ 32 | / \ 33 | 7 13 34 | 35 | In this case, it should return 3 (because the longest consecutive sequence is: 5 -> 6 -> 7). 36 | 37 | Example 2: 38 | 39 | 5 40 | / \ 41 | / \ 42 | 6 4 43 | / \ 44 | 9 3 45 | / \ \ 46 | 3 2 2 47 | 48 | In this case, your function return 2 (because the longest consecutive sequence is: 5 -> 6 ). 49 | 50 | Example 3: 51 | 52 | 30 53 | / \ 54 | / \ 55 | 15 41 56 | / / 57 | 61 80 58 | 59 | In this case, it should return 1. 60 | 61 | Example 4: 62 | 63 | NULL 64 | 65 | In this case, as the root node is null, your function should return 0. 66 | -------------------------------------------------------------------------------- /level00/subjects/equation.txt: -------------------------------------------------------------------------------- 1 | Assignment name : equation 2 | Expected files : equation.c 3 | Allowed functions: printf 4 | ------------------------------------------------------------------------------- 5 | The goal of this exercise is to find all possible answers to the following 6 | equation : 7 | 8 | AB + CA = n 9 | 10 | where A, B, and C are individual digits [0-9] and n is an integer. 11 | Note that here AB is not the product of A and B. It is merely a two digit number 12 | with A being the first digit (tens place) and B the second digit (ones place). 13 | 14 | Implement a function that, given an integer n, prints on the standard 15 | output all the possible values of A, B, C for which the equation is true. 16 | 17 | Your function must be declared as follows: 18 | 19 | void equation(int n); 20 | 21 | If a solution could not be found, nothing is printed. 22 | Examples: 23 | For the value n = 42, the output would be : 24 | $> ./equation 42 25 | A = 0, B = 2, C = 4 26 | A = 1, B = 1, C = 3 27 | A = 2, B = 0, C = 2 28 | A = 3, B = 9, C = 0 29 | $> 30 | For the value n = 111, the output would be : 31 | $> ./equation 111 32 | A = 2, B = 9, C = 8 33 | A = 3, B = 8, C = 7 34 | A = 4, B = 7, C = 6 35 | A = 5, B = 6, C = 5 36 | A = 6, B = 5, C = 4 37 | A = 7, B = 4, C = 3 38 | A = 8, B = 3, C = 2 39 | A = 9, B = 2, C = 1 40 | $> 41 | For the value n = 0, the output would be : 42 | $> ./equation 0 | cat -e 43 | A = 0, B = 0, C = 0$ 44 | $> 45 | 46 | Note: 47 | - The displayed output will always be sorted in ascending order beginning with A, then B and then C. 48 | (as shown in the examples above) 49 | -------------------------------------------------------------------------------- /level00/is_anagram.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* is_anagram.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jaleman +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2018/02/06 20:42:51 by jaleman #+# #+# */ 9 | /* Updated: 2018/02/06 20:42:52 by jaleman ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | /* 14 | ** Returns whether the first string is an anagram of the second or not. 15 | */ 16 | 17 | int is_anagram(char *a, char *b) 18 | { 19 | int tab[127] = {0}; 20 | int count = 0; 21 | 22 | for (int i = 0; a[i] != '\0'; i += 1) 23 | tab[(int)a[i]] += 1; 24 | for (int i = 0; b[i] != '\0'; i += 1) 25 | tab[(int)b[i]] += 1; 26 | while (i < 126) 27 | if (tab[i++] != 0) 28 | break ; 29 | return (i < 126 ? 0 : 1); 30 | } 31 | 32 | /* 33 | ** Main function. Uncomment to test this file! 34 | */ 35 | 36 | /* 37 | #include 38 | 39 | int main(void) 40 | { 41 | char *a = "cinema"; 42 | char *b = "iceman"; 43 | 44 | printf("%s\n", (is_anagram(a, b) ? "Anagram" : "Nada")); 45 | return (0); 46 | } 47 | */ 48 | -------------------------------------------------------------------------------- /level03/subjects/can_split.txt: -------------------------------------------------------------------------------- 1 | Assignment name : can_split 2 | Expected files : can_split.c 3 | Allowed functions: 4 | -------------------------------------------------------------------------------- 5 | ALERT: OPTIMIZED SOLUTION REQUIRED. 6 | Given the root node of a binary tree, create a function that returns 1 if, by 7 | removing one edge of the tree, it can be splitted in two trees with the same 8 | number of nodes (same 'size'). 9 | The binary tree uses the following node structure : 10 | struct s_node 11 | { 12 | int value; 13 | struct s_node *left; 14 | struct s_node *right; 15 | }; 16 | The function must be declared as follows: 17 | int can_split(struct s_node *n); 18 | Consideration: 19 | - Be careful: the naive solution won't work on our big input, you have to find 20 | an optimized solution which will run in O(N) time (where N is the number of nodes). 21 | - The bigger test we will do is about 250 000 nodes. It should run in less 22 | than 2 seconds. 23 | Example 1: 24 | 28 25 | / 26 | 51 27 | / \ 28 | / \ 29 | 26 48 30 | / \ 31 | / \ 32 | 76 13 33 | In this case, it should return 1 (remove the edge between 51 and 26 will split the tree 34 | in 2 trees with each a size of 3). 35 | Example 2: 36 | 30 37 | / \ 38 | / \ 39 | 15 41 40 | / / 41 | 61 80 42 | In this case, it should return 0 (you can't split the tree and make 2 trees with the 43 | same size). 44 | Example 3: 45 | 10 46 | \ 47 | 12 48 | In this case, your function return 1. 49 | Example 4: 50 | 5 51 | / \ 52 | / \ 53 | 1 6 54 | / \ 55 | 7 4 56 | / \ \ 57 | 3 2 8 58 | In this case, your function should return 0. 59 | -------------------------------------------------------------------------------- /level02/subjects/is_looping.txt: -------------------------------------------------------------------------------- 1 | Assignment name : is_looping 2 | Expected files : is_looping.c 3 | Allowed functions: 4 | -------------------------------------------------------------------------------- 5 | ALERT: OPTIMIZED SOLUTION REQUIRED. 6 | Given the first node of a linked list as parameter, create a function which 7 | returns 1 if the linked list is looping, otherwise 0. 8 | The linked list uses the following structure: 9 | struct s_node { 10 | int value; 11 | struct s_node *next; 12 | }; 13 | The function must be declared as follows: 14 | int is_looping(struct s_node *node); 15 | Considerations: 16 | - Be careful: the naive solution won't work on our big input, you have to 17 | find a solution with better complexity than O(N^2) time (where N is the 18 | number of nodes). 19 | - The values of each node does not matter. 20 | - The bigger test we will do is on a linked list of 500.000 nodes, with the 21 | beginning of the loop at the middle. It should run in less than 2 seconds. 22 | Example 1: 23 | 1 -> 2 -> 3 -> 4 -> 5 24 | ^ | 25 | | v 26 | | 6 27 | \ | 28 | \______/ 29 | In this case, it should return 1 (at the node 3 begins the loop). 30 | Example 2: 31 | 12 -> 150 -> 30 -> 50 -> 345 -> 120 32 | ^ | 33 | | v 34 | | 200 35 | \ / 36 | \____________________/ 37 | In this case, it should return 1 (the loop begins at node 150). 38 | Example 3: 39 | 12 -> 150 -> 30 -> 50 -> 345 -> 120 40 | In this case, it should return 0 (no loop begins). 41 | Example 4: 42 | 12 -> 19 -> 14 43 | ^ \ 44 | | | 45 | \/ 46 | In this case, it should return 1 (the loop begins at node 14). 47 | -------------------------------------------------------------------------------- /level01/subjects/flood_fill.txt: -------------------------------------------------------------------------------- 1 | Assignment name : flood_fill 2 | Expected files : *.c, *.h 3 | Allowed functions: - 4 | -------------------------------------------------------------------------------- 5 | 6 | Write a function that takes a char ** as a 2-dimensional array of char, a 7 | t_point as the dimensions of this array and a t_point as the starting point. 8 | 9 | Starting from the given 'begin' t_point, this function fills an entire zone 10 | by replacing characters inside with the character 'F'. A zone is an group of 11 | the same character delimitated horizontally and vertically by other characters 12 | or the array boundry. 13 | 14 | The flood_fill function won't fill diagonally. 15 | 16 | The flood_fill function will be prototyped like this: 17 | void flood_fill(char **tab, t_point size, t_point begin); 18 | 19 | The t_point structure is prototyped like this: 20 | 21 | typedef struct s_point 22 | { 23 | int x; 24 | int y; 25 | } t_point; 26 | 27 | Example: 28 | 29 | $> cat test_main.c 30 | #include "test_functions.h" 31 | #include "flood_fill.h" 32 | 33 | int main(void) 34 | { 35 | char **area; 36 | t_point size = {8, 5}; 37 | t_point begin = {2, 2}; 38 | char *zone[] = { 39 | "1 1 1 1 1 1 1 1", 40 | "1 0 0 0 1 0 0 1", 41 | "1 0 0 1 0 0 0 1", 42 | "1 0 1 1 0 0 0 1", 43 | "1 1 1 0 0 0 0 1", 44 | } 45 | 46 | area = make_area(zone); 47 | print_tab(area); 48 | flood_fill(area, size, begin); 49 | putc('\n'); 50 | print_tab(area); 51 | return (0); 52 | } 53 | 54 | $> gcc flood_fill.c test_main.c test_functions.c -o flood_fill; ./flood_fill 55 | 1 1 1 1 1 1 1 1 56 | 1 0 0 0 1 0 0 1 57 | 1 0 0 1 0 0 0 1 58 | 1 0 1 0 0 0 0 1 59 | 1 1 0 0 0 0 0 0 60 | 61 | 1 1 1 1 1 1 1 1 62 | 1 F F F 1 0 0 1 63 | 1 F F 1 0 0 0 1 64 | 1 F 1 0 0 0 0 1 65 | 1 1 0 0 0 0 0 0 66 | $> -------------------------------------------------------------------------------- /level00/subjects/find_pivot.txt: -------------------------------------------------------------------------------- 1 | Assignment name : find_pivot 2 | Expected files : find_pivot.c 3 | Allowed functions: 4 | -------------------------------------------------------------------------------- 5 | ALERT: OPTIMIZED SOLUTION REQUIRED. 6 | Given an array of integers and its size passed as parameters, 7 | create a function able to return the pivot index of this array. 8 | The pivot index is the index where the sum of the numbers on the left 9 | is equal to the sum of the numbers on the right. 10 | The function must be declared as follows: 11 | int find_pivot(int *arr, int n); 12 | If there is no pivot present, return -1. 13 | Considerations: 14 | - Be careful: the naive solution won't work on our big input, you have to 15 | find an optimized solution which will run in O(n) time (where n is the 16 | length of the array). 17 | - The array will always have a length bigger than 1. 18 | - You don't have to take care of overflow or underflow of sums, 19 | it will stay in an range of an int. 20 | - The bigger test we will do is on an array of 1.000.000 elements. 21 | It should run in less than 2 seconds. 22 | 23 | Example 1: 24 | arr = [ 1, 2, 3, 4, 0, 6 ] , n = 6 25 | In this case, your function should return 3. 26 | Because at index 3, the sum of the elements on the left is equals 27 | to the sum of the elements on the right: 28 | = 6 = 6 29 | _______ ____ 30 | [ 1, 2, 3, 4, 0, 6 ] 31 | ^ 32 | | 33 | with pivot = 3 34 | Example 2: 35 | arr = [ -5, 10, 2, 5 ] , n = 4 36 | In this case, your function should return 2. 37 | Example 3: 38 | arr = [ 1, 100, 0, 0, 1 ] , n = 5 39 | In this case, your function should return 1. 40 | Example 4: 41 | arr = [ 7, 9, 8 ] , n = 3 42 | In this case, your function should return -1. 43 | Example 5: 44 | arr = [ 1 , 2 ] , n = 2 45 | In this case, your function should return -1. 46 | -------------------------------------------------------------------------------- /level00/subjects/print_doublon.txt: -------------------------------------------------------------------------------- 1 | Assignment name : print_doublon 2 | Expected files : print_doublon.c 3 | Allowed functions: printf 4 | -------------------------------------------------------------------------------- 5 | ALERT: OPTIMIZED SOLUTION REQUIRED. 6 | Given two sorted arrays passed as parameters, 7 | create a function able to print all the number that are present 8 | in one array and in the other, separated by a space. 9 | Then print a new line. 10 | The function must be declared as follows (with 'na' the length of the array 'a', 11 | and 'nb' the length of the array 'b'): 12 | void print_doublon(int *a, int na, int *b, int nb); 13 | Considerations: 14 | - Be careful: the naive solution won't work on our big input, you have to 15 | find an optimized solution which will run in O(n) time (where n is 16 | the length of the longest array between a and b). 17 | - All elements of an array are unique. The same number will not repeat in an array. 18 | - If there is no doublon, just print a new line '\n'. 19 | - The bigger test we will run will be on 2 arrays of 500 000 elements each, 20 | it should run in less than 2 seconds. 21 | Example 1: 22 | a = [ 1, 2, 10, 15 ] , na = 4 23 | b = [ 2, 20, 40, 70 ] , nb = 4 24 | In this case, by using a main file that use your function with the above input, 25 | the output should be (using cat -e): 26 | $> ./example1 | cat -e 27 | 2$ 28 | Example 2: 29 | a = [ -5, 2, 10, 15, 50, 70, 100, 200, 300, 1200, 5000 ] , na = 11 30 | b = [ 2, 4, 5, 6, 7, 10, 40, 70 ] , nb = 8 31 | In this case, by using a main file that use your function with the above input, 32 | the output should be (using cat -e): 33 | $> ./example2 | cat -e 34 | 2 10 70$ 35 | Example 3: 36 | a = [ 100, 200, 300 ] , na = 3 37 | b = [ 1, 2, 3, 4 ] , nb = 4 38 | In this case, by using a main file that use your function with the above input, 39 | the output should be (using cat -e): 40 | $> ./example3 | cat -e 41 | $ 42 | -------------------------------------------------------------------------------- /level00/print_doublon.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* print_doubleon.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jaleman +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2018/10/22 21:44:52 by jaleman #+# #+# */ 9 | /* Updated: 2018/10/22 21:44:53 by jaleman ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include 14 | 15 | /* 16 | ** Print to stdout all the numbers that are present in both arrays that are 17 | ** passed as parameter. 18 | ** The numbers are separated by a space, and a new line at the end. 19 | */ 20 | 21 | void print_doublon(int *a, int na, int *b, int nb) 22 | { 23 | int i = 0; 24 | int j = 0; 25 | int total = 0; 26 | 27 | while (na > i && nb > j) 28 | { 29 | if (a[i] == b[j]) 30 | { 31 | if (total > 0) 32 | printf(" "); 33 | printf("%i", a[i]); 34 | i += 1; 35 | j += 1; 36 | total += 1; 37 | } 38 | else if (a[i] > b[j]) 39 | j += 1; 40 | else 41 | i += 1; 42 | } 43 | printf("\n"); 44 | return ; 45 | } 46 | 47 | /* 48 | ** Main function. Uncomment to test this file! 49 | */ 50 | 51 | /* 52 | int main(void) 53 | { 54 | int a[] = { -5, 2, 10, 15, 50, 70, 100, 200, 300, 1200, 5000 }; 55 | int b[] = { 2, 4, 5, 6, 7, 10, 40, 70 }; 56 | int na = 11; 57 | int nb = 8; 58 | 59 | print_doublon(a, na, b, nb); 60 | return (0); 61 | } 62 | */ 63 | -------------------------------------------------------------------------------- /level03/subjects/gold_gain.txt: -------------------------------------------------------------------------------- 1 | Assignment name : gold_gain 2 | Expected files : gold_gain.c 3 | Allowed functions: malloc 4 | -------------------------------------------------------------------------------- 5 | ALERT: OPTIMIZED SOLUTION REQUIRED. 6 | Given a matrix which represent a gold mine of n*n dimensions. 7 | Each field in this mine contains a positive integer which is the 8 | amount of gold in tons. 9 | Initially the miner is at first column but can be at any row. 10 | He can move only (right->,right up /,right down\) that is from a given cell, 11 | the miner can move to the cell diagonally up towards the right or right or 12 | diagonally down towards the right. 13 | implement an algorithm able to return the maximum amount of gold he can collect. 14 | The function must be declared as follows: 15 | int gold_gain(int **mine, int n); 16 | Considerations: 17 | - Be careful: the brute force solution won't work on our big input, you have to 18 | find an optimized solution which use dynamic programming. 19 | - You don't have to handle error case, the matrix will be always squared. 20 | - The bigger test we will do is on matrix of 1 000 * 1 000, 21 | it should run in less than 2 seconds. 22 | Example 1: 23 | mine = [ 24 | [ 1, 0, 0 ], 25 | [ 0, 3, 4 ], 26 | [ 0, 0, 0 ] 27 | ] 28 | n = 3 29 | In this example, your function should return 8, 30 | because taking the following path gain 8: 31 | (0,0) -> (1,1) -> (1,2) 32 | 1 -> 3 -> 4 33 | Example 2: 34 | mine = [ 35 | [ 1, 2, 3 ], 36 | [ 3, 4, 8 ], 37 | [ 9, 6, 7 ] 38 | ] 39 | n = 3 40 | In this example, your function should return 23, 41 | because taking the following path gain 23: 42 | (2,0) -> (2,1) -> (1,3) 43 | Example 3: 44 | mine = [ 45 | [ 1, 3, 1, 5 ], 46 | [ 2, 2, 4, 1 ], 47 | [ 5, 0, 2, 3 ], 48 | [ 0, 6, 1, 2 ] 49 | ] 50 | n = 4 51 | In this example, your function should return 16, 52 | because there is 2 path which give this gain: 53 | (2,0) -> (1,1) -> (1,2) -> (0,3) 54 | or 55 | (2,0) -> (3,1) -> (2,2) -> (2,3) 56 | -------------------------------------------------------------------------------- /level00/find_pivot.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* find_pivot.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jaleman +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2018/02/06 20:41:29 by jaleman #+# #+# */ 9 | /* Updated: 2018/02/06 20:41:31 by jaleman ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | /* 14 | ** Get the total sum of all the values in an int array. 15 | */ 16 | 17 | static int get_sum(int *arr, int n) 18 | { 19 | int sum = 0; 20 | 21 | for (int i = 0; i < n; i += 1) 22 | sum += arr[i]; 23 | return (sum); 24 | } 25 | 26 | /* 27 | ** Return the pivot index of an array, where the sum of the values on the 28 | ** left is equal to the sum on the right. 29 | */ 30 | 31 | int find_pivot(int *arr, int n) 32 | { 33 | int count = 0; 34 | int pivot = -1; 35 | int sum = get_sum(arr, n); 36 | 37 | for (int i = 0; (pivot == -1 && i < n); i += 1) 38 | { 39 | sum -= arr[i]; 40 | pivot = (count == sum) ? i : -1; 41 | count += arr[i]; 42 | } 43 | return (pivot); 44 | } 45 | 46 | /* 47 | ** Main function. Uncomment to test this file! 48 | */ 49 | 50 | /* 51 | #include 52 | #include 53 | 54 | int main(int argc, const char *argv[]) 55 | { 56 | int *arr = malloc(sizeof(int) * (argc - 1)); 57 | 58 | for (int i = 0; (argc - 1) > i; i += 1) 59 | arr[i] = atoi(argv[i + 1]); 60 | return (printf("Pivot: %d\n", find_pivot(arr, argc - 1))); 61 | } 62 | */ 63 | -------------------------------------------------------------------------------- /level00/count_of_2.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* count_of_2.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jaleman +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2018/10/22 23:36:29 by jaleman #+# #+# */ 9 | /* Updated: 2018/10/22 23:36:29 by jaleman ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | /* 14 | ** Count the numbers of two (2) that appears in the number n passed as argument. 15 | ** Second argument keeps track of the current count state. 16 | ** If a number has 2 or more digits, we have to split it into smaller digits. 17 | ** We do this by using recursion. 18 | ** If it's a single digit, we can check really quick because 2 == n. ;) 19 | */ 20 | 21 | static void count_number(int n, int *count) 22 | { 23 | if (n >= 10) 24 | { 25 | count_number(n / 10, count); 26 | count_number(n % 10, count); 27 | } 28 | if (n == 2) 29 | *count += 1; 30 | return ; 31 | } 32 | 33 | /* 34 | ** Counts, for a given integer n, the number of two (2) that appear in all 35 | ** the numbers between 0 and n (inclusive). 36 | */ 37 | 38 | int count_of_2(int n) 39 | { 40 | int i = 1; 41 | int count = 0; 42 | 43 | while (++i <= n) 44 | count_number(i, &count); 45 | return (count); 46 | } 47 | 48 | /* 49 | ** Main function. Uncomment to test this file! 50 | */ 51 | 52 | /* 53 | #include 54 | #include 55 | 56 | int main(int argc, char *argv[]) 57 | { 58 | if (argc == 2) 59 | printf("%i\n", count_of_2(atoi(argv[1]))); 60 | return (0); 61 | } 62 | */ 63 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # **************************************************************************** # 2 | # # 3 | # ::: :::::::: # 4 | # .gitignore :+: :+: :+: # 5 | # +:+ +:+ +:+ # 6 | # By: jaleman +#+ +:+ +#+ # 7 | # +#+#+#+#+#+ +#+ # 8 | # Created: 2017/08/11 21:25:48 by jaleman #+# #+# # 9 | # Updated: 2017/08/11 21:25:49 by jaleman ### ########.fr # 10 | # # 11 | # **************************************************************************** # 12 | 13 | # Prerequisites 14 | *.d 15 | 16 | # Object files 17 | *.o 18 | *.ko 19 | *.obj 20 | *.elf 21 | *.pyc 22 | 23 | # Linker output 24 | *.ilk 25 | *.map 26 | *.exp 27 | 28 | # Precompiled Headers 29 | *.gch 30 | *.pch 31 | 32 | # Libraries 33 | *.lib 34 | *.a 35 | *.la 36 | *.lo 37 | 38 | # Shared objects (inc. Windows DLLs) 39 | *.dll 40 | *.so 41 | *.so.* 42 | *.dylib 43 | 44 | # Executables 45 | *.exe 46 | *.out 47 | *.app 48 | *.i*86 49 | *.x86_64 50 | *.hex 51 | 52 | # Debug files 53 | *.dSYM/ 54 | *.su 55 | *.idb 56 | *.pdb 57 | 58 | # Kernel Module Compile Results 59 | *.mod* 60 | *.cmd 61 | .tmp_versions/ 62 | modules.order 63 | Module.symvers 64 | Mkfile.old 65 | dkms.conf 66 | 67 | # Junk files 68 | *.DS_Store 69 | .AppleDouble 70 | .LSOverride 71 | .nfs* 72 | 73 | # Icon must end with two \r 74 | Icon 75 | 76 | # Thumbnails 77 | ._* 78 | 79 | # Files that might appear in the root of a volume 80 | .DocumentRevisions-V100 81 | .fseventsd 82 | .Spotlight-V100 83 | .TemporaryItems 84 | .Trashes 85 | .VolumeIcon.icns 86 | .com.apple.timemachine.donotpresent 87 | 88 | # Directories potentially created on remote AFP share 89 | .AppleDB 90 | .AppleDesktop 91 | Network Trash Folder 92 | Temporary Items 93 | .apdisk 94 | -------------------------------------------------------------------------------- /level03/subjects/width_tree.txt: -------------------------------------------------------------------------------- 1 | Assignment name : width_tree 2 | Expected files : width_tree.c 3 | Allowed functions: 4 | -------------------------------------------------------------------------------- 5 | 6 | ALERT: OPTIMIZED SOLUTION REQUIRED. 7 | 8 | Given the root node of a binary tree, create a function that returns the 9 | 'width' of the tree, which is the number of node present on the longest 10 | path between two leaves in the tree. 11 | 12 | The binary tree uses the following node structure : 13 | 14 | struct s_node 15 | { 16 | int value; 17 | struct s_node *left; 18 | struct s_node *right; 19 | }; 20 | 21 | The function must be declared as follows: 22 | 23 | int width_tree(struct s_node *n); 24 | 25 | Consideration: 26 | 27 | - Be careful: the naive solution won't work on our big input, you have to find 28 | an optimized solution which will run in O(N) time (where N is the number of nodes). 29 | - The values of each node does not matter. 30 | - The bigger test we will do is about 250 000 nodes. It should run in less 31 | than 2 seconds. 32 | 33 | Example 1: 34 | 35 | 1 36 | / \ 37 | 2 \ 38 | / \ \ 39 | 3 4 5 40 | / / \ 41 | 6 7 8 42 | 43 | In this case, your function should return 6, 44 | 45 | because the longest path between two leaves is 6->4->2->1->5->7 (or finish by 8), 46 | which contains 6 nodes, so the tree have a 'width' of 6. 47 | 48 | Example 2: 49 | 50 | 1 51 | / \ 52 | 2 \ 53 | / \ 3 54 | 4 \ 55 | / \ \ 56 | 5 6 7 57 | \ / \ 58 | 8 9 10 59 | / \ \ 60 | 11 12 13 61 | 62 | In this case, your function should return 7, 63 | 64 | because the longest path between two leaves is 8->5->4->2->7->9->11 . 65 | 66 | Example 3: 67 | 68 | 10 69 | \ 70 | 12 71 | 72 | In this case, your function should return 2. 73 | 74 | Example 4: 75 | 76 | 25 77 | / 78 | 33 79 | / \ 80 | 12 9 81 | / 82 | 3 83 | In this case, your function should return 4. 84 | 85 | Example 5: 86 | 87 | 10 88 | (here is a root with no children) 89 | In this case, your function should return 1. 90 | -------------------------------------------------------------------------------- /level00/equation.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* equation.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jaleman +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2018/02/08 20:03:00 by jaleman #+# #+# */ 9 | /* Updated: 2018/02/08 20:03:01 by jaleman ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include 14 | 15 | /* 16 | ** Prints the values of the digits to the stdout. 17 | */ 18 | 19 | static void print_digits(int a, int b, int c) 20 | { 21 | printf("A = %d, B = %d, C = %d\n", a, b, c); 22 | return ; 23 | } 24 | 25 | /* 26 | ** Return the result of the following equation: AB + CA = n 27 | */ 28 | 29 | static int calculate_digits(int a, int b, int c) 30 | { 31 | return (((a * 10) + b) + ((c * 10) + a)); 32 | } 33 | 34 | /* 35 | ** Check if the equation is balanced, and return true if it does. 36 | */ 37 | 38 | static int check_digits(int n, int a, int b, int c) 39 | { 40 | return ((calculate_digits(a, b, c) == n) ? 1 : 0); 41 | } 42 | 43 | /* 44 | ** If the equation is balanced, zero (0), prints to stdout all the possible 45 | ** values of A, B, C 46 | */ 47 | 48 | void equation(int n) 49 | { 50 | for (int i = 0; i < 10; i += 1) 51 | for (int j = 0; j < 10; j += 1) 52 | for (int k = 0; k < 10; k += 1) 53 | if (check_digits(n, i, j, k)) 54 | print_digits(i, j, k); 55 | return ; 56 | } 57 | 58 | /* 59 | ** Main function. Uncomment to test this file! 60 | */ 61 | 62 | /* 63 | #include 64 | 65 | int main(int argc, const char *argv[]) 66 | { 67 | if (argc == 2) 68 | equation(atoi(argv[1])); 69 | return (0); 70 | } 71 | */ 72 | -------------------------------------------------------------------------------- /level01/height_tree.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* height_tree.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jaleman +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2018/09/05 20:58:59 by jaleman #+# #+# */ 9 | /* Updated: 2019/06/28 00:42:02 by jaleman ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | struct s_node 14 | { 15 | int value; 16 | struct s_node **nodes; 17 | }; 18 | 19 | int height_tree(struct s_node *root) 20 | { 21 | int current = 0; 22 | int height = 0; 23 | 24 | if (!root) 25 | return (-1); 26 | for (int i = 0; root->nodes[i]; i += 1) 27 | if ((current = height_tree(root->nodes[i]) + 1) > height) 28 | height = current; 29 | return (height); 30 | } 31 | 32 | /* 33 | #include 34 | #include 35 | 36 | int main(void) 37 | { 38 | struct s_node *root; 39 | 40 | root = malloc(sizeof(struct s_node)); 41 | root->value = 1; 42 | root->nodes = malloc(sizeof(struct s_node)); 43 | 44 | root->nodes[0] = malloc(sizeof(struct s_node)); 45 | root->nodes[0]->value = 2; 46 | root->nodes[0]->nodes = malloc(sizeof(struct s_node)); 47 | 48 | root->nodes[0]->nodes[0] = malloc(sizeof(struct s_node)); 49 | root->nodes[0]->nodes[0]->value = 3; 50 | root->nodes[0]->nodes[0]->nodes = malloc(sizeof(struct s_node)); 51 | 52 | root->nodes[0]->nodes[0]->nodes[0] = malloc(sizeof(struct s_node)); 53 | root->nodes[0]->nodes[0]->nodes[0]->value = 4; 54 | root->nodes[0]->nodes[0]->nodes[0]->nodes = malloc(sizeof(struct s_node)); 55 | 56 | root->nodes[0]->nodes[0]->nodes[0]->nodes[0] = NULL; 57 | 58 | printf("%d\n", height_tree(root)); 59 | return (0); 60 | } 61 | */ 62 | -------------------------------------------------------------------------------- /level01/count_alpha.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* count_alpha.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jaleman +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2018/10/23 00:09:27 by jaleman #+# #+# */ 9 | /* Updated: 2018/10/23 00:09:28 by jaleman ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include 14 | 15 | /* 16 | ** Returns true if a character is alphabetical. 17 | */ 18 | 19 | static int is_alpha(int c) 20 | { 21 | return (c >= 'a' && c <= 'z'); 22 | } 23 | 24 | /* 25 | ** Lowcase a string passed as parameter. 26 | */ 27 | 28 | static char *lowcase(char *str) 29 | { 30 | for (int i = 0; str[i]; i += 1) 31 | str[i] += (str[i] >= 'A' && str[i] <= 'Z') ? 32 : 0; 32 | return (str); 33 | } 34 | 35 | /* 36 | ** Takes a string and displays the number of occurences of its alpha characters. 37 | ** Other characters are not counted. The printed characters must follow the 38 | ** order of occurence in the string. The display must end by a newline. 39 | */ 40 | 41 | static int count_alpha(char *s) 42 | { 43 | int tab[26] = {0}; 44 | char *str = lowcase(s); 45 | int total = 0; 46 | int i = -1; 47 | 48 | while (str[++i]) 49 | tab[str[i] - 'a'] += (is_alpha(str[i]) ? 1 : 0); 50 | i = -1; 51 | while (str[++i]) 52 | { 53 | if (tab[str[i] - 'a'] > 0) 54 | { 55 | if (total > 0) 56 | printf(", "); 57 | printf("%d%c", tab[str[i] - 'a'], str[i]); 58 | tab[str[i] - 'a'] = 0; 59 | total += 1; 60 | } 61 | } 62 | return (0); 63 | } 64 | 65 | /* 66 | ** Main function. 67 | ** This is a program, so this one is needed. 68 | ** DO NOT COMMENT :) 69 | */ 70 | 71 | int main(int argc, char *argv[]) 72 | { 73 | if (argc == 2) 74 | count_alpha(argv[1]); 75 | printf("\n"); 76 | return (0); 77 | } 78 | -------------------------------------------------------------------------------- /level01/stack.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* stack.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jaleman +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2018/10/23 00:53:44 by jaleman #+# #+# */ 9 | /* Updated: 2018/10/23 00:53:44 by jaleman ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include 14 | 15 | struct s_node 16 | { 17 | void *content; 18 | struct s_node *next; 19 | }; 20 | struct s_stack 21 | { 22 | struct s_node *top; 23 | }; 24 | 25 | struct s_stack *init(void) 26 | { 27 | struct s_stack *stack = malloc(sizeof(stack)); 28 | 29 | stack->top = NULL; 30 | return (stack); 31 | } 32 | 33 | void *pop(struct s_stack *stack) 34 | { 35 | void *content = NULL; 36 | struct s_node *node; 37 | 38 | if (stack->top) 39 | { 40 | node = stack->top; 41 | content = stack->top->content; 42 | stack->top = stack->top->next; 43 | free(node); 44 | node = NULL; 45 | } 46 | return (content); 47 | } 48 | 49 | void push(struct s_stack *stack, void *content) 50 | { 51 | struct s_node *node = malloc(sizeof(node)); 52 | node->content = content; 53 | node->next = stack->top; 54 | stack->top = node; 55 | return ; 56 | } 57 | 58 | void *peek(struct s_stack *stack) 59 | { 60 | return (stack->top ? stack->top->content : NULL); 61 | } 62 | 63 | int isEmpty(struct s_stack *stack) 64 | { 65 | return (!stack->top ? 1 : 0); 66 | } 67 | 68 | /* 69 | 70 | #include 71 | 72 | int main(void) 73 | { 74 | struct s_stack *stack = init(); 75 | char *content[][1] = { 76 | "Uno", 77 | "Dos", 78 | "Tres", 79 | "Cuatro", 80 | "Cinco" 81 | }; 82 | 83 | for (int i = 0; i < 5; i += 1) 84 | { 85 | push(stack, *content[i]); 86 | printf("Content : %s\n", peek(stack)); 87 | printf("Empty : %s\n", (isEmpty(stack) ? "yes" : "no")); 88 | } 89 | for (int i = 5; i > 0; i -= 1) 90 | { 91 | pop(stack); 92 | printf("Content : %s\n", peek(stack)); 93 | printf("Empty : %s\n", (isEmpty(stack) ? "yes" : "no")); 94 | } 95 | return (0); 96 | } 97 | 98 | */ 99 | -------------------------------------------------------------------------------- /level01/queue.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* queue.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jaleman +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2018/10/23 02:26:07 by jaleman #+# #+# */ 9 | /* Updated: 2018/10/23 02:26:08 by jaleman ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include 14 | 15 | struct s_node 16 | { 17 | void *content; 18 | struct s_node *next; 19 | }; 20 | struct s_queue 21 | { 22 | struct s_node *first; 23 | struct s_node *last; 24 | }; 25 | 26 | struct s_queue *init(void) 27 | { 28 | struct s_queue *q = malloc(sizeof(struct s_queue)); 29 | 30 | queue->first = NULL; 31 | queue->last = NULL; 32 | return (q); 33 | } 34 | void enqueue(struct s_queue *q, void *c) 35 | { 36 | struct s_node *n = malloc(sizeof(struct s_node)); 37 | 38 | n->content = c; 39 | n->next = NULL; 40 | if (!queue->first) 41 | queue->first = n; 42 | if (queue->last) 43 | queue->last->next = n; 44 | queue->last = n; 45 | return ; 46 | } 47 | void *dequeue(struct s_queue *q) 48 | { 49 | void *c = NULL; 50 | struct s_node *n = queue->first; 51 | 52 | if (queue->first) 53 | { 54 | c = n->content; 55 | queue->first = n->next; 56 | queue->last = (queue->first ? queue->last : NULL); 57 | free(n); 58 | } 59 | return (c); 60 | } 61 | void *peek(struct s_queue *q) 62 | { 63 | return (queue->first ? queue->first->content : NULL); 64 | } 65 | int isEmpty(struct s_queue *q) 66 | { 67 | return (!queue->first ? 1 : 0); 68 | } 69 | 70 | /* 71 | 72 | #include 73 | 74 | int main(void) 75 | { 76 | struct s_queue *queue = init(); 77 | char *content[][1] = { 78 | "Eins", 79 | "Zwei", 80 | "Drei", 81 | "Vier", 82 | "Funf" 83 | }; 84 | 85 | for (int i = 0; i < 5; i += 1) 86 | { 87 | enqueue(queue, *content[i]); 88 | printf("Content : %s\n", peek(queue)); 89 | printf("Empty : %s\n", (isEmpty(queue) ? "yes" : "no")); 90 | } 91 | for (int i = 5; i > 0; i -= 1) 92 | { 93 | dequeue(queue); 94 | printf("Content : %s\n", peek(queue)); 95 | printf("Empty : %s\n", (isEmpty(queue) ? "yes" : "no")); 96 | } 97 | return (0); 98 | } 99 | 100 | */ 101 | -------------------------------------------------------------------------------- /level02/reverse_tree.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* reverse_tree.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: exam +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2019/07/16 10:24:21 by exam #+# #+# */ 9 | /* Updated: 2019/07/16 10:53:36 by exam ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | struct s_node { 14 | int value; 15 | struct s_node *right; 16 | struct s_node *left; 17 | }; 18 | 19 | void reverse_tree(struct s_node *root) 20 | { 21 | struct s_node *tmp; 22 | 23 | if (root) { 24 | tmp = root->right; 25 | root->right = root->left; 26 | root->left = tmp; 27 | reverse_tree(root->right); 28 | reverse_tree(root->left); 29 | } 30 | return ; 31 | } 32 | 33 | /* 34 | #include 35 | #include 36 | 37 | int main(void) 38 | { 39 | struct s_node *root = malloc(sizeof(struct s_node)); 40 | root->value = 94; 41 | 42 | root->left = malloc(sizeof(struct s_node)); 43 | root->right = malloc(sizeof(struct s_node)); 44 | root->left->value = 34; 45 | root->right->value = 52; 46 | root->right->left = NULL; 47 | root->right->right = NULL; 48 | 49 | root->left->left = malloc(sizeof(struct s_node)); 50 | root->left->right = malloc(sizeof(struct s_node)); 51 | root->left->left->value = 1; 52 | root->left->right->value = 99; 53 | 54 | root->left->left->left = malloc(sizeof(struct s_node)); 55 | root->left->left->left->value = 20; 56 | root->left->left->left->left = NULL; 57 | root->left->left->left->right = NULL; 58 | 59 | root->left->right->left = malloc(sizeof(struct s_node)); 60 | root->left->right->right = malloc(sizeof(struct s_node)); 61 | root->left->right->left->value = 83; 62 | root->left->right->right->value = 39; 63 | root->left->right->left->left = NULL; 64 | root->left->right->left->right = NULL; 65 | root->left->right->right->left = NULL; 66 | root->left->right->right->right = NULL; 67 | 68 | printf("[r: %d, rl: %d, rr: %d, rll: %d, rlr: %d, rlll: %d, rlrl: %d, rlrr: %d]\n", 69 | root->value, 70 | root->left->value, 71 | root->right->value, 72 | root->left->left->value, 73 | root->left->right->value, 74 | root->left->left->left->value, 75 | root->left->right->left->value, 76 | root->left->right->right->value 77 | ); 78 | 79 | reverse_tree(root); 80 | printf("[r: %d, rl: %d, rr: %d, rrl: %d, rrr: %d, rrll: %d, rrlr: %d, rrrr: %d]\n", 81 | root->value, 82 | root->left->value, 83 | root->right->value, 84 | root->right->left->value, 85 | root->right->right->value, 86 | root->right->left->left->value, 87 | root->right->left->right->value, 88 | root->right->right->right->value 89 | ); 90 | 91 | return (0); 92 | } 93 | */ 94 | -------------------------------------------------------------------------------- /level05/subjects/count_island.txt: -------------------------------------------------------------------------------- 1 | Assignment name : count_island 2 | Expected files : *.c, *.h 3 | Allowed functions: open, close, read, write, malloc, free 4 | -------------------------------------------------------------------------------- 5 | Write a program that takes a file that contains lines of equal length. Those 6 | lines contain characters that are either '.' or 'X'. All those lines put 7 | together form rectangles of '.' containing "islands" of 'X'. 8 | The maximum size of a line is 1024 characters, including the terminating 9 | newline. 10 | A column if formed of the set of characters in the file that are separated from 11 | the start of their respective lines by the same number of characters. 12 | Two characters are said to be touching if they are contiguous and on the same 13 | line, or on contiguous lines and on the same column. 14 | An "island" of 'X' means a set of 'X' touching each other. 15 | The program must walk though the file and display it after replacing all the 16 | 'X' by a number corresponding to the position their island appears in the file, 17 | starting at the beginning of the file. 18 | There can be only one result. 19 | If the file is empty, or there is an error (Incoherent input, for example), or 20 | no parameters are passed, the program must display a newline. 21 | The file contains at most 10 islands. 22 | You will find examples in the subject directory. 23 | Examples: 24 | $>cat toto 25 | .................XXXXXXXX.......................................... 26 | ....................XXXXXXXXX.......XXXXXXXX....................... 27 | .................XXXXXXXX..............XXX...XXXXX................. 28 | .....................XXXXXX.....X...XXXXXXXXXXX.................... 29 | ................................X.................................. 30 | ......XXXXXXXXXXXXX.............X.................................. 31 | ..................X.............XXXXXXXXX.......................... 32 | ..................X.........XXXXXXXXXXXX........................... 33 | ..................X................................................ 34 | XX.............................................................XXXX 35 | XX..................XXXXXXXXXXXXX.................................X 36 | ................................................................... 37 | .................................................................X. 38 | .....................XXXXX.......................................XX 39 | $> 40 | $>./count_island toto 41 | .................00000000.......................................... 42 | ....................000000000.......11111111....................... 43 | .................00000000..............111...11111................. 44 | .....................000000.....2...11111111111.................... 45 | ................................2.................................. 46 | ......3333333333333.............2.................................. 47 | ..................3.............222222222.......................... 48 | ..................3.........222222222222........................... 49 | ..................3................................................ 50 | 44.............................................................5555 51 | 44..................6666666666666.................................5 52 | ................................................................... 53 | .................................................................7. 54 | .....................88888.......................................77 55 | $> 56 | $>cat qui_est_la 57 | ................................................................... 58 | ...X........X.....XXXXX......XXXXXXX...XXXXXXXXXX..XXXXXXXXXX...... 59 | ...XX......XX....XX...XX....XX.....XX.....XXXX.....XXXXXXXXXX...... 60 | ...XXXX..XXXX...XX.....XX...XX.....XX......XX......XX.............. 61 | ...XX.XXXX.XX...XX.....XX...XX.....XX......XX......XX.............. 62 | ...XX...X..XX...XX.....XX...XXXXXXXX.......XX......XXXXX........... 63 | ...XX......XX...XXXXXXXXX...XXXX...........XX......XXXXX........... 64 | ...XX......XX..XX.......XX..XX.XX..........XX......XX.............. 65 | ...XX......XX..XX.......XX..XX...X.........XX......XX.............. 66 | ...XX......XX..XX.......XX..XX....X......XXXXXX....XXXXXXXXXX...... 67 | ...XX......XX.XX.........XX.XX.....XX..XXXXXXXXXX..XXXXXXXXXX..X... 68 | ................................................................... 69 | $> 70 | $>./count_island qui_est_la 71 | ................................................................... 72 | ...0........0.....11111......2222222...3333333333..4444444444...... 73 | ...00......00....11...11....22.....22.....3333.....4444444444...... 74 | ...0000..0000...11.....11...22.....22......33......44.............. 75 | ...00.0000.00...11.....11...22.....22......33......44.............. 76 | ...00...0..00...11.....11...22222222.......33......44444........... 77 | ...00......00...111111111...2222...........33......44444........... 78 | ...00......00..11.......11..22.22..........33......44.............. 79 | ...00......00..11.......11..22...5.........33......44.............. 80 | ...00......00..11.......11..22....6......333333....4444444444...... 81 | ...00......00.11.........11.22.....77..3333333333..4444444444..8... 82 | ................................................................... 83 | $> 84 | $>cat -e rien 85 | $>./count_island rien | cat -e 86 | $ 87 | $> 88 | -------------------------------------------------------------------------------- /level03/subjects/perimeter.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* perimeter.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: exam +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2019/07/16 10:55:35 by exam #+# #+# */ 9 | /* Updated: 2019/07/16 11:47:29 by exam ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include 14 | #include 15 | 16 | struct s_node { 17 | int value; 18 | struct s_node *right; 19 | struct s_node *left; 20 | }; 21 | 22 | void perimeter(struct s_node *root) 23 | { 24 | (void)root; 25 | return ; 26 | } 27 | 28 | int main(void) 29 | { 30 | struct s_node *root = malloc(sizeof(struct s_node)); 31 | root->value = 92; 32 | 33 | root->left = malloc(sizeof(struct s_node)); 34 | root->left->value = 85; 35 | root->left->right = NULL; 36 | 37 | root->left->left = malloc(sizeof(struct s_node)); 38 | root->left->left->value = 79; 39 | root->left->left->left = NULL; 40 | 41 | root->left->left->right = malloc(sizeof(struct s_node)); 42 | root->left->left->right->value = 10; 43 | root->left->left->right->right = NULL; 44 | 45 | root->left->left->right->left = malloc(sizeof(struct s_node)); 46 | root->left->left->right->left->value = 39; 47 | root->left->left->right->left->right = NULL; 48 | 49 | root->left->left->right->left->left = malloc(sizeof(struct s_node)); 50 | root->left->left->right->left->left->value = 35; 51 | root->left->left->right->left->left->right = NULL; 52 | 53 | root->left->left->right->left->left->left = malloc(sizeof(struct s_node)); 54 | root->left->left->right->left->left->left->value = 96; 55 | root->left->left->right->left->left->left->left = NULL; 56 | root->left->left->right->left->left->left->right = NULL; 57 | 58 | root->right = malloc(sizeof(struct s_node)); 59 | root->right->value = 26; 60 | root->right->left = NULL; 61 | 62 | root->right->right = malloc(sizeof(struct s_node)); 63 | root->right->right->value = 64; 64 | 65 | root->right->right->left = malloc(sizeof(struct s_node)); 66 | root->right->right->left->value = 40; 67 | 68 | root->right->right->left->left = malloc(sizeof(struct s_node)); 69 | root->right->right->left->left->value = 88; 70 | 71 | root->right->right->left->left->left = malloc(sizeof(struct s_node)); 72 | root->right->right->left->left->left->value = 12; 73 | root->right->right->left->left->left->right = NULL; 74 | 75 | root->right->right->left->left->left->left = malloc(sizeof(struct s_node)); 76 | root->right->right->left->left->left->left->value = 58; 77 | root->right->right->left->left->left->left->left = NULL; 78 | root->right->right->left->left->left->left->right = NULL; 79 | 80 | root->right->right->left->left->right = malloc(sizeof(struct s_node)); 81 | root->right->right->left->left->right->value = 55; 82 | 83 | root->right->right->left->left->right->left = malloc(sizeof(struct s_node)); 84 | root->right->right->left->left->right->left->value = 58; 85 | root->right->right->left->left->right->left->left = NULL; 86 | root->right->right->left->left->right->left->right = NULL; 87 | 88 | root->right->right->left->left->right->right = malloc(sizeof(struct s_node)); 89 | root->right->right->left->left->right->right->value = 41; 90 | root->right->right->left->left->right->right->left = NULL; 91 | root->right->right->left->left->right->right->right = NULL; 92 | 93 | root->right->right->left->right = malloc(sizeof(struct s_node)); 94 | root->right->right->left->right->value = 10; 95 | 96 | root->right->right->left->right->left = malloc(sizeof(struct s_node)); 97 | root->right->right->left->right->left->value = 52; 98 | 99 | root->right->right->left->right->left->left = malloc(sizeof(struct s_node)); 100 | root->right->right->left->right->left->left->value = 22; 101 | root->right->right->left->right->left->left->left = NULL; 102 | root->right->right->left->right->left->left->right = NULL; 103 | 104 | root->right->right->left->right->left->right = malloc(sizeof(struct s_node)); 105 | root->right->right->left->right->left->right->value = 35; 106 | root->right->right->left->right->left->right->left = NULL; 107 | root->right->right->left->right->left->right->right = NULL; 108 | 109 | root->right->right->left->right->right = malloc(sizeof(struct s_node)); 110 | root->right->right->left->right->right->value = 87; 111 | root->right->right->left->right->right->left = NULL; 112 | 113 | root->right->right->left->right->right->right = malloc(sizeof(struct s_node)); 114 | root->right->right->left->right->right->right->value = 31; 115 | root->right->right->left->right->right->right->left = NULL; 116 | root->right->right->left->right->right->right->right = NULL; 117 | 118 | root->right->right->right = malloc(sizeof(struct s_node)); 119 | root->right->right->right->value = 78; 120 | 121 | root->right->right->right->left = malloc(sizeof(struct s_node)); 122 | root->right->right->right->left->value = 2; 123 | 124 | root->right->right->right->left->left = malloc(sizeof(struct s_node)); 125 | root->right->right->right->left->left->value = 33; 126 | root->right->right->right->left->left->left = NULL; 127 | 128 | root->right->right->right->left->left->right = malloc(sizeof(struct s_node)); 129 | root->right->right->right->left->left->right->value = 55; 130 | root->right->right->right->left->left->right->left = NULL; 131 | root->right->right->right->left->left->right->right = NULL; 132 | 133 | root->right->right->right->left->right = malloc(sizeof(struct s_node)); 134 | root->right->right->right->left->right->value = 11; 135 | root->right->right->right->left->right->right = NULL; 136 | 137 | root->right->right->right->left->right->left = malloc(sizeof(struct s_node)); 138 | root->right->right->right->left->right->left->value = 99; 139 | root->right->right->right->left->right->left->left = NULL; 140 | root->right->right->right->left->right->left->right = NULL; 141 | 142 | root->right->right->right->right = malloc(sizeof(struct s_node)); 143 | root->right->right->right->right->value = 85; 144 | root->right->right->right->left = NULL; 145 | 146 | root->right->right->right->right->right = malloc(sizeof(struct s_node)); 147 | root->right->right->right->right->right->value = 51; 148 | root->right->right->right->right->right->left = NULL; 149 | root->right->right->right->right->right->right = NULL; 150 | return (0); 151 | } 152 | --------------------------------------------------------------------------------