├── bitwise ├── README.md ├── is_nth_bit_set │ ├── main.cpp │ └── README.md ├── is_power_of_four │ └── README.md ├── reverse_bits │ └── README.md ├── is_power_of_two │ └── README.md ├── number_of_common_letters │ └── README.md ├── int_to_hex │ └── README.md ├── repeated_dna_sequences │ └── README.md ├── bitwise_AND_of_numbers_range │ └── README.md ├── find_the_single_integer │ └── README.md ├── match_maker │ ├── main.cpp │ └── README.md ├── number_complement │ ├── README.md │ └── number_complement.cpp ├── longest_palindrome │ └── README.md ├── spy_hawk │ └── README.md ├── align_number_on_power_of_two │ └── README.md ├── binary_gap │ └── README.md ├── symetric_bits │ └── README.md ├── is_valid_utf8_array │ └── README.md └── least_operators_to_express_number │ └── README.md ├── hashmap ├── group_anagram │ ├── group_anagrams.png │ └── README.md └── target_sum │ └── README.md ├── greedy ├── README.md └── change_problem │ └── README.md ├── math ├── is_power_of_three │ ├── new-meltdown-spectre-attacks.png │ ├── benchmark.cpp │ └── README.md ├── binary_search │ ├── main.cpp │ └── README.md ├── fibonacci_series │ └── README.md └── the_dancer │ └── README.md ├── .gitignore ├── misc ├── triangle │ └── README.md ├── largest_perimeter_triangle.cpp │ └── README.md ├── first_missing_one_in_series │ └── README.md ├── matching_socks │ └── README.md ├── closest_point_to_origin │ └── README.md ├── sum_of_k │ ├── README.md │ └── benchmark.cpp ├── sum_of_three │ └── README.md └── sum_of_two │ └── README.md ├── array ├── contains_duplicate │ └── README.md ├── array_rotation │ └── README.md ├── move_zeroes_back │ └── README.md └── remove_duplicates │ └── README.md ├── parsing ├── reverse_integer │ └── README.md └── string_to_lowercase │ ├── README.md │ ├── benchmark.cpp │ └── benchmark.s ├── list ├── merge_two_sorted_lists │ └── README.md └── merge_k_sorted_lists │ └── README.md ├── stack └── brackets │ └── README.md └── README.md /bitwise/README.md: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /hashmap/group_anagram/group_anagrams.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/agavrel/Nailing-the-Coding-Interview/HEAD/hashmap/group_anagram/group_anagrams.png -------------------------------------------------------------------------------- /greedy/README.md: -------------------------------------------------------------------------------- 1 | A Greedy Algorithm always makes a choice that looks the best at the moment. You should be careful that it also handles special cases. 2 | -------------------------------------------------------------------------------- /math/is_power_of_three/new-meltdown-spectre-attacks.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/agavrel/Nailing-the-Coding-Interview/HEAD/math/is_power_of_three/new-meltdown-spectre-attacks.png -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Prerequisites 2 | *.d 3 | 4 | # Compiled Object files 5 | *.slo 6 | *.lo 7 | *.o 8 | *.obj 9 | 10 | # Precompiled Headers 11 | *.gch 12 | *.pch 13 | 14 | # Compiled Dynamic libraries 15 | *.so 16 | *.dylib 17 | *.dll 18 | 19 | # Fortran module files 20 | *.mod 21 | *.smod 22 | 23 | # Compiled Static libraries 24 | *.lai 25 | *.la 26 | *.a 27 | *.lib 28 | 29 | # Executables 30 | *.exe 31 | *.out 32 | *.app 33 | -------------------------------------------------------------------------------- /bitwise/is_nth_bit_set/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | bool is_nth_bit_set(int value, int n) { 6 | return (value >> (n - 1)) & 0b1; 7 | } 8 | 9 | bool is_nth_bit_set2(int value, int n) { 10 | return value & (0b1 << (n - 1)); 11 | } 12 | 13 | int main(int ac, char **av) 14 | { 15 | for (int i = 1; i < ac; i++) 16 | { 17 | cout << is_nth_bit_set(atoi(av[i]), 4) << endl; 18 | cout << is_nth_bit_set2(atoi(av[i]), 4) << endl; 19 | } 20 | return 0; 21 | } 22 | 23 | // g++ main.cpp && ./a.out 15 16 23 24 24 | -------------------------------------------------------------------------------- /math/binary_search/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int binary_search(int *a, int len, int x) 4 | { 5 | int begin = 0; 6 | int last = len - 1; 7 | int i = 0; 8 | 9 | while (begin <= last) 10 | { 11 | i = (begin + last) >> 1; 12 | if (a[i] < x) 13 | begin = i + 1; 14 | else if (a[i] > x) 15 | last = i - 1; 16 | else 17 | return i; 18 | } 19 | 20 | return -1; 21 | } 22 | 23 | int main(int ac, char **av) 24 | { 25 | int a[24] = {0, 3, 5, 6, 7, 9, 13, 14, 15, 16, 18, 19, \ 26 | 21, 24, 26, 29, 31, 35, 45, 76, 87, 91, 94, 97}; 27 | 28 | std::cout << binary_search(a, sizeof(a) / sizeof(a[0]), 9) << std::endl; 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /misc/triangle/README.md: -------------------------------------------------------------------------------- 1 | ### Problem 2 | 3 | *Write a function that, given an unsigned integer n, display the triangle.* 4 | 5 | ##### Prototype 6 | ```c++ 7 | void triangle(unsigned int n) { ;} 8 | ``` 9 | 10 | ##### For example 11 | * Consider the following result for n = 5 12 | ``` 13 | # 14 | ## 15 | ### 16 | #### 17 | ##### 18 | ``` 19 | 20 |
21 | 
22 | 
23 | 
24 | 
25 | 
26 | 
27 | 
28 | 
29 | 
30 | 
31 | 
32 | 
33 | 
34 | 
35 | 
36 | 
37 | 
38 | 
39 | 
40 | 
41 | 
42 | 
43 | 
44 | 
45 | 
46 | 
47 | 
48 | 
49 | 
50 | 51 | ### solution 52 | 53 | ```cpp 54 | #include 55 | 56 | using namespace std; 57 | 58 | 59 | void triangle(unsigned int n) { 60 | string s; 61 | 62 | for (unsigned int i = 0; i < n; i++) 63 | { 64 | s.append(n - i - 1, ' '); 65 | s.append(i + 1, '#'); 66 | s.append("\n"); 67 | } 68 | cout << s; 69 | } 70 | 71 | int main() 72 | { 73 | int n; 74 | cin >> n; 75 | 76 | staircase(n); 77 | 78 | return 0; 79 | } 80 | ``` 81 | -------------------------------------------------------------------------------- /math/fibonacci_series/README.md: -------------------------------------------------------------------------------- 1 | ### Problem 2 | 3 | *Write a function that, given an integer n, returns its Fibonacci value.* 4 | 5 | ##### Prototype 6 | ```c++ 7 | int fibonacci(int n) { ;} 8 | ``` 9 | 10 | ##### For example 11 | * Consider n = 6, the function should return 8. 12 | 13 | ##### Assumptions 14 | * 0 <= n <= 46 15 | 16 |
17 | 
18 | 
19 | 
20 | 
21 | 
22 | 
23 | 
24 | 
25 | 
26 | 
27 | 
28 | 
29 | 
30 | 
31 | 
32 | 
33 | 
34 | 
35 | 
36 | 
37 | 
38 | 
39 | 
40 | 
41 | 
42 | 
43 | 
44 | 
45 | 
46 | 47 | 48 | ### Solution 49 | ```c++ 50 | int fibonacci(int n) { 51 | const int fibonacci_series[47] = {0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368, 75025, 121393, 196418, 317811, 514229, 832040, 1346269, 2178309, 3524578, 5702887, 9227465, 14930352, 24157817, 39088169, 63245986, 102334155, 165580141, 267914296, 433494437, 701408733, 1134903170, 1836311903}; 52 | 53 | return fibonacci_series[n]; 54 | } 55 | ``` 56 | 57 | *We pre-compute the series until buffer overflow. Btw no protection added (if n > 46 return -1), to optimize for speed.* 58 | -------------------------------------------------------------------------------- /array/contains_duplicate/README.md: -------------------------------------------------------------------------------- 1 | ### Out of Blue 2 | 3 | ##### 이루마 - 샤콘느 4 | 5 | youtube link 8 | 9 | --- 10 | 11 | ### Problem 12 | *Write a function that, given an array of integers, returns true if the array contains duplicate, false otherwise.* 13 | 14 | ##### Prototype 15 | ```c++ 16 | bool contains_duplicate(vector& v) {; } 17 | ``` 18 | 19 | ##### Assumptions 20 | * The array may be empty 21 | 22 | ##### For example 23 | * "{0, 0, 3, 7, 7, 8, 9, 9, 9}" -> returns true 24 | 25 |
26 | 
27 | 
28 | 
29 | 
30 | 
31 | 
32 | 
33 | 
34 | 
35 | 
36 | 
37 | 
38 | 
39 | 
40 | 
41 | 
42 | 
43 | 
44 | 
45 | 
46 | 
47 | 
48 | 
49 | 
50 | 
51 | 
52 | 
53 | 
54 | 
55 | 56 | ### C++ Solution 57 | 58 | ```c++ 59 | bool contains_duplicate(vector& v) { 60 | unordered_sets; 61 | 62 | for (const int& n : v) 63 | { 64 | if (s.count(n)) 65 | return true; 66 | s.insert(n); 67 | } 68 | return false; 69 | } 70 | ``` 71 | -------------------------------------------------------------------------------- /bitwise/is_power_of_four/README.md: -------------------------------------------------------------------------------- 1 | ### Out of Blue 2 | 3 | ##### Tatsuro Yamashita 山下達郎 - あまく危険な香り 4 | 5 | Tatsuro Yamashita on youtube 8 | 9 | ### Problem 10 | 11 | *Write a function that, given an integer, returns true if it is a power of four, false other wise.* 12 | 13 | ##### Prototype 14 | ```c++ 15 | bool is_power_of_four(int n) 16 | ``` 17 | 18 | ##### For example 19 | * n = 16, returns true 20 | * n = 42, returns false 21 | 22 |
23 | 
24 | 
25 | 
26 | 
27 | 
28 | 
29 | 
30 | 
31 | 
32 | 
33 | 
34 | 
35 | 
36 | 
37 | 
38 | 
39 | 
40 | 
41 | 
42 | 
43 | 
44 | 
45 | 
46 | 
47 | 
48 | 
49 | 
50 | 
51 | 
52 | 53 | 54 | ### Solution 55 | 56 | ```c++ 57 | bool isPowerOfFour(int n) { 58 | return (__builtin_popcount(n) == 1) && (!(__builtin_ctz(n) & 1)); 59 | } 60 | ``` 61 | 62 | Popcnt checks that it is a power of two. 63 | 64 | Then ctz(n) check the the index of the bit. 65 | 66 | Finally ```!(& 1)``` ensure that this index is an even number like all powers of four as 0b1 (index 0), 0b100 (4, index 2), 0b10000 (16, index 4) 67 | 68 | *time 0ms 69 | memory 6Mb* -------------------------------------------------------------------------------- /bitwise/reverse_bits/README.md: -------------------------------------------------------------------------------- 1 | ### Problem 2 | 3 | *Reverse bits of a given 32 bits unsigned integer.* 4 | 5 | ##### Example 1: 6 | ``` 7 | Input: 00000010100101000001111010011100 8 | Output: 00111001011110000010100101000000 9 | Explanation: The input binary string 00000010100101000001111010011100 represents the unsigned integer 43261596, so return 964176192 which its binary representation is 00111001011110000010100101000000. 10 | ``` 11 | 12 | ##### Example 2: 13 | ``` 14 | Input: 11111111111111111111111111111101 15 | Output: 10111111111111111111111111111111 16 | Explanation: The input binary string 11111111111111111111111111111101 represents the unsigned integer 4294967293, so return 3221225471 which its binary representation is 10111111111111111111111111111111. 17 | ``` 18 | 19 | ##### Prototype 20 | ```c++ 21 | uint32_t reverse_bits(uint32_t n) 22 | ``` 23 | 24 | 25 |
26 | 
27 | 
28 | 
29 | 
30 | 
31 | 
32 | 
33 | 
34 | 
35 | 
36 | 
37 | 
38 | 
39 | 
40 | 
41 | 
42 | 
43 | 
44 | 
45 | 
46 | 
47 | 
48 | 
49 | 
50 | 
51 | 
52 | 
53 | 
54 | 
55 | 56 | 57 | ### Solution 58 | 59 | ```c++ 60 | uint32_t reverse_bits(uint32_t n) { 61 | __asm__ (R"( 62 | .intel_syntax 63 | xor eax, eax 64 | inc eax 65 | myloop: 66 | shr esi, 1 67 | adc eax, eax 68 | jnc short myloop 69 | ;)"); 70 | 71 | return n; 72 | } 73 | ``` 74 | -------------------------------------------------------------------------------- /bitwise/is_power_of_two/README.md: -------------------------------------------------------------------------------- 1 | ### Problem 2 | 3 | *Write a function that, given an unsigned integer n, returns 1 if n is a power of 2 and 0 if it is not.* 4 | 5 | ##### Prototype 6 | ```c++ 7 | bool is_power_of_two(uint32_t n) { ;} 8 | ``` 9 | 10 | ##### For example 11 | * Consider n = 25, n = 0 or n = 34, the function should return 0. 12 | * If n = 32 the function should return 1. 13 | 14 | 15 |
16 | 
17 | 
18 | 
19 | 
20 | 
21 | 
22 | 
23 | 
24 | 
25 | 
26 | 
27 | 
28 | 
29 | 
30 | 
31 | 
32 | 
33 | 
34 | 
35 | 
36 | 
37 | 
38 | 
39 | 
40 | 
41 | 
42 | 
43 | 
44 | 
45 | 46 | 47 | ### Solution 48 | ```c++ 49 | bool is_power_of_two(uint32_t n) 50 | { 51 | return (n > 0 && !(n & (n - 1))); 52 | } 53 | ``` 54 | 55 | *We use the fact that ```n & ( n - 1)``` returns 0 if n is a multiple of 2 as the mask will not catch the following bits. 56 | For example with n = 8 it will be ```1000 & 0111``` which is equal to 0.* 57 | 58 | *We use the exclamation dot (not instruction) to reverse 0 to 1 and (x != 0) to 1.* 59 | 60 | *lso n > 0 is not necessary in this case as if n = 0 it will underflow resulting in 0 & unsigned int* 61 | 62 | ### Solution using builtin instructions 63 | ```c++ 64 | bool is_power_of_two(uint32_t n) 65 | { 66 | return n > 0 && (__builtin_clz(n) + __builtin_ctz(n)) == 31; 67 | } 68 | ``` 69 | 70 | *__builtin_clz will return the count of leading zeroes (from left until 1)* 71 | *__builtin_ctz will return the count of trailing zeroes (from right until right)* 72 | -------------------------------------------------------------------------------- /parsing/reverse_integer/README.md: -------------------------------------------------------------------------------- 1 | ### Problem 2 | 3 | *Write a function that, given an integer, reverses it. 4 | If the reversed integer overflows then the function returns 0* 5 | 6 | ##### Prototype 7 | ```c++ 8 | int reverse_integer(int x) { ;} 9 | ``` 10 | 11 | ##### For example 12 | * Consider a = 12345, the function should return 54321. 13 | * If x = -678, the function should return -876. 14 | * If x = 5678098243 the function should return 0 because the reversed value overflows (go beyond INT_MAX) 15 | 16 | ##### Assumption 17 | * INT_MIN < x < INT_MAX 18 | * You are dealing with a 32 bits environment (use of 'long' forbidden) 19 | 20 |
21 | 
22 | 
23 | 
24 | 
25 | 
26 | 
27 | 
28 | 
29 | 
30 | 
31 | 
32 | 
33 | 
34 | 
35 | 
36 | 
37 | 
38 | 
39 | 
40 | 
41 | 
42 | 
43 | 
44 | 
45 | 
46 | 
47 | 
48 | 
49 | 
50 | 51 | 52 | ### Solution 53 | ```c++ 54 | int reverse_integer(int x) { 55 | if (x == INT_MIN) // handle extreme min value 56 | return 0; 57 | 58 | int sign = 1; // handle negative values 59 | if (x < 0) 60 | { 61 | sign = -1; 62 | x = abs(x); 63 | } 64 | 65 | int rev = 0; 66 | while (x) 67 | { 68 | if (rev > INT_MAX / 10) 69 | return 0; // overflow protection 70 | rev *= 10; 71 | if (rev == INT_MAX - INT_MAX % 10 && x % 10 > INT_MAX % 10) 72 | return 0; // overflow protection 73 | rev += x % 10; 74 | x /= 10; 75 | } 76 | 77 | return rev * sign; 78 | } 79 | ``` 80 | -------------------------------------------------------------------------------- /math/binary_search/README.md: -------------------------------------------------------------------------------- 1 | ### Problem 2 | 3 | *Write a function that, given a sorted integer array (ascending), its length len, and an integer x, returns its index.* 4 | *If the array does not contain x the function should return -1.* 5 | 6 | ##### Prototype 7 | ```c++ 8 | int binary_search(int *a, int len, int x) { ;} 9 | ``` 10 | 11 | ##### For example 12 | * Consider a = {0, 3, 5, 6, 7, 9, 13, 14, 15, 16, 18, 19, 21, 24, 26, 29, 31, 35, 45, 76, 87, 91, 94, 97}, len = 24, and x = 9 the function should return 5, the index of value 9 contained in the array a. 13 | * For the same array if x = 2, the function should return -1. 14 | 15 | ##### Assumption 16 | * 0 < len < 10000 17 | * len is valid (corresponds to the array size) 18 | * the array contains no duplicates 19 | 20 |
21 | 
22 | 
23 | 
24 | 
25 | 
26 | 
27 | 
28 | 
29 | 
30 | 
31 | 
32 | 
33 | 
34 | 
35 | 
36 | 
37 | 
38 | 
39 | 
40 | 
41 | 
42 | 
43 | 
44 | 
45 | 
46 | 
47 | 
48 | 
49 | 
50 | 51 | 52 | ### Solution 53 | ```c++ 54 | #include 55 | 56 | int binary_search(int *a, int len, int x) 57 | { 58 | int begin = 0; 59 | int last = len - 1; 60 | int i = 0; 61 | 62 | while (begin <= last) 63 | { 64 | i = (begin + last) >> 1; 65 | if (a[i] < x) 66 | begin = i + 1; 67 | else if (a[i] > x) 68 | last = i - 1; 69 | else 70 | return i; 71 | } 72 | 73 | return -1; 74 | } 75 | ``` 76 | 77 | *This search will perform in O(log N) in the worst case because at each jump you remove half of the possibilities* 78 | -------------------------------------------------------------------------------- /list/merge_two_sorted_lists/README.md: -------------------------------------------------------------------------------- 1 | ### Out of Blue 2 | 3 | ##### River flows in you : Yiruma : 이루마 : 리버 플로우즈 인 유 4 | 5 | youtube video 8 | 9 | --- 10 | 11 | ### Problem 12 | 13 | *Write a function that, given two sorted lists merges them into one sorted list and returns it* 14 | 15 | ##### Prototype 16 | ```c++ 17 | struct Node { 18 | int value; 19 | Node *next; 20 | Node(int x) : value(x), next(NULL) {} 21 | }; 22 | 23 | Node* merge_sorted_lists(Node* l1, Node* l2) { ;} 24 | ``` 25 | 26 | ##### For example 27 | * Consider a list l1 containing {-1,0,1,2,2,4} and another one containing {-2,1,3}, the function should return a list containing {-2,-1,0,1,1,2,2,3,4} 28 | 29 |
30 | 
31 | 
32 | 
33 | 
34 | 
35 | 
36 | 
37 | 
38 | 
39 | 
40 | 
41 | 
42 | 
43 | 
44 | 
45 | 
46 | 
47 | 
48 | 
49 | 
50 | 
51 | 
52 | 
53 | 
54 | 
55 | 
56 | 
57 | 
58 | 
59 | 60 | 61 | 62 | ### Elegant Recursive C++ Solution 63 | ```c++ 64 | struct Node { 65 | int value; 66 | Node *next; 67 | Node(int x) : value(x), next(NULL) {} 68 | }; 69 | 70 | Node* merge_sorted_lists(Node* l1, Node* l2) { 71 | if (l1 == NULL) 72 | return l2; 73 | if (l2 == NULL) 74 | return l1; 75 | 76 | if (l1->value < l2->value) { 77 | l1->next = merge_sorted_lists(l1->next, l2); 78 | return l1; 79 | } 80 | l2->next = merge_sorted_lists(l1, l2->next); 81 | return l2; 82 | } 83 | ``` 84 | -------------------------------------------------------------------------------- /bitwise/number_of_common_letters/README.md: -------------------------------------------------------------------------------- 1 | ### Out of Blue 2 | 3 | ##### Pyrrhic Victory 4 | 5 | [A Pyrrhic victory](https://en.wikipedia.org/wiki/Pyrrhic_victory) is a victory that inflicts such a devastating toll on the victor that it is tantamount to defeat. Someone who wins a Pyrrhic victory has also taken a heavy toll that negates any true sense of achievement. 6 | 7 | --- 8 | 9 | 10 | ### Problem 11 | 12 | *You're given strings S1 and S2. S1 has distinct letters. Count the occurrences of S1 letters in S2.* 13 | 14 | ##### Prototype 15 | ```c++ 16 | int common_letters(string S1, string S2) { ;} 17 | ``` 18 | 19 | ##### For example 20 | * Consider S1 = "aAb" and S2 = "ggorgkgeAAb", it will return 3 as S2 contains 2x 'A' and 1x 'b'. 21 | 22 |
23 | 
24 | 
25 | 
26 | 
27 | 
28 | 
29 | 
30 | 
31 | 
32 | 
33 | 
34 | 
35 | 
36 | 
37 | 
38 | 
39 | 
40 | 
41 | 
42 | 
43 | 
44 | 
45 | 
46 | 
47 | 
48 | 
49 | 
50 | 
51 | 
52 | 53 | 54 | 55 | ### C++ Solutions 56 | ```c++ 57 | int common_letters(string S1, string S2) { 58 | unordered_set myset; 59 | int count = 0; 60 | 61 | for (const char & c : S1) 62 | myset.insert(c); 63 | 64 | for (const char & c : S2) 65 | if (myset.find(c) != myset.end()) 66 | count++; 67 | 68 | return count; 69 | } 70 | ``` 71 | 72 | ```c++ 73 | int common_letters(string S1, string S2) { 74 | uint64_t a = 0; 75 | int count = 0; 76 | 77 | for (const char & c : S1) 78 | a |= (uint64_t)1 << (c - 'A'); 79 | 80 | for (const char & c : S2) 81 | count += !!((a & ((uint64_t)1 << (c - 'A')))); 82 | 83 | return count; 84 | } 85 | ``` 86 | -------------------------------------------------------------------------------- /bitwise/int_to_hex/README.md: -------------------------------------------------------------------------------- 1 | ### Out of Blue 2 | 3 | ##### Blinded by Light (Boston Live 2014) 4 | 5 | Blinded by Light Concert on youtube 8 | 9 | --- 10 | ### Problem 11 | 12 | *Given an integer, write an algorithm to convert it to hexadecimal. For negative integer, two’s complement method is used.* 13 | 14 | ##### Note: 15 | ``` 16 | All letters in hexadecimal (a-f) must be in lowercase. 17 | The hexadecimal string must not contain extra leading 0s. If the number is zero, it is represented by a single zero character '0'; otherwise, the first character in the hexadecimal string will not be the zero character. 18 | The given number is guaranteed to fit within the range of a 32-bit signed integer. 19 | You must not use any method provided by the library which converts/formats the number to hex directly. 20 | ``` 21 | 22 | ##### Example 1: 23 | ``` 24 | Input: 25 | 26 26 | Output: 27 | "1a" 28 | ``` 29 | 30 | ##### Example 2: 31 | ``` 32 | Input: 33 | -1 34 | Output: 35 | "ffffffff" 36 | ``` 37 | 38 | ##### Prototype 39 | ```c++ 40 | string int_to_hex(int num) 41 | ``` 42 | 43 |
44 | 
45 | 
46 | 
47 | 
48 | 
49 | 
50 | 
51 | 
52 | 
53 | 
54 | 
55 | 
56 | 
57 | 
58 | 
59 | 
60 | 
61 | 
62 | 
63 | 
64 | 
65 | 
66 | 
67 | 
68 | 
69 | 
70 | 
71 | 
72 | 
73 | 74 | --- 75 | ### Solution 76 | ```c++ 77 | string int_to_hex(int num) { 78 | if (num == 0) 79 | return "0"; 80 | 81 | const string x = "0123456789abcdef"; 82 | string s; 83 | int count = 8; 84 | do { 85 | s = x[num & 0xf] + s; 86 | num >>= 4; 87 | } while (num && --count); 88 | 89 | return s; 90 | } 91 | ``` 92 | -------------------------------------------------------------------------------- /list/merge_k_sorted_lists/README.md: -------------------------------------------------------------------------------- 1 | ### Out of Blue 2 | 3 | ##### Murder On The Dance Floor - Sophie Ellis Bextor 4 | 5 | youtube video 8 | 9 | --- 10 | 11 | ### Problem 12 | 13 | *Write a function that, given k sorted lists merges them into one sorted list* 14 | 15 | ##### Prototype 16 | ```c++ 17 | struct Node { 18 | int value; 19 | Node *next; 20 | Node(int x) : value(x), next(NULL) {} 21 | }; 22 | 23 | Node* merge_k_sorted_lists(vector& sorted_lists) { ;} 24 | ``` 25 | 26 | ##### For example 27 | * Consider vector containing a list l1 containing {-1,0,1,2,2,4} and another one containing {-2,1,3}, the function should return a list containing {-2,-1,0,1,1,2,2,3,4} 28 | 29 |
30 | 
31 | 
32 | 
33 | 
34 | 
35 | 
36 | 
37 | 
38 | 
39 | 
40 | 
41 | 
42 | 
43 | 
44 | 
45 | 
46 | 
47 | 
48 | 
49 | 
50 | 
51 | 
52 | 
53 | 
54 | 
55 | 
56 | 
57 | 
58 | 
59 | 60 | 61 | 62 | ### Elegant Recursive C++ Solution 63 | ```c++ 64 | struct Node { 65 | int value; 66 | Node *next; 67 | Node(int x) : value(x), next(NULL) {} 68 | }; 69 | 70 | Node* merge_k_sorted_lists(vector& sorted_lists) { 71 | map mymap; 72 | 73 | for (Node * l : sorted_lists) 74 | { 75 | while (l) 76 | { 77 | mymap[l->val]++; 78 | l = l->next; 79 | } 80 | 81 | } 82 | 83 | Node *lst = new Node(0);; 84 | Node *head = lst; 85 | 86 | for (const auto & key_value : mymap ) 87 | { 88 | int n = key_value.second; 89 | while (n--) 90 | { 91 | lst->next = new Node(key_value.first); 92 | lst = lst->next; 93 | } 94 | } 95 | 96 | return head->next; 97 | } 98 | ``` 99 | -------------------------------------------------------------------------------- /bitwise/repeated_dna_sequences/README.md: -------------------------------------------------------------------------------- 1 | ### Problem 2 | 3 | *All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACGAATTCCG". When studying DNA, it is sometimes useful to identify repeated sequences within the DNA.* 4 | 5 | *Write a function to find all the 10-letter-long sequences (substrings) that occur more than once in a DNA molecule.* 6 | 7 | ##### Prototype 8 | ```c++ 9 | vector findRepeatedDnaSequences(string s); 10 | ``` 11 | 12 | ##### For example 13 | ``` 14 | Input: s = "AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT" 15 | Output: ["AAAAACCCCC", "CCCCCAAAAA"] 16 | ``` 17 | 18 | 19 |
20 | 
21 | 
22 | 
23 | 
24 | 
25 | 
26 | 
27 | 
28 | 
29 | 
30 | 
31 | 
32 | 
33 | 
34 | 
35 | 
36 | 
37 | 
38 | 
39 | 
40 | 
41 | 
42 | 
43 | 
44 | 
45 | 
46 | 
47 | 
48 | 
49 | 50 | 51 | ### Solution 52 | ```c++ 53 | class Solution { 54 | private: 55 | string bits_to_dna(uint32_t b) { 56 | string s = "AAAAAAAAAA"; 57 | static const constexpr unsigned char x[4] = {'A', 'C', 'T', 'G'}; 58 | int i = 0; 59 | for (;b; b >>= 2) 60 | s[i++] = x[b & 0b11]; 61 | 62 | return s; 63 | } 64 | 65 | public: 66 | vector findRepeatedDnaSequences(string s) { 67 | if (s.size() <= 10) 68 | return {}; 69 | 70 | uint32_t b = 0; 71 | for (int i = 0; i < 10; i++) 72 | b = (b >> 2) | ((s[i] & 0b110) << 17); 73 | 74 | unordered_map m ={}; 75 | ++m[b]; 76 | 77 | vector r; 78 | for (int i = 10; i < s.size(); i++) { 79 | b = (b >> 2) | ((s[i] & 0b110) << 17); 80 | if (m[b]++ == 1) 81 | r.push_back(bits_to_dna(b)); 82 | } 83 | return r; 84 | } 85 | }; 86 | ``` 87 | 88 | *We use the fact that ```s[i] & 0b111``` will produce either 0, 1, 2 or 3 with the letters ATGC. 89 | 90 | *Runtime: 56 ms* 91 | *Memory: 14.7 Mb* 92 | -------------------------------------------------------------------------------- /misc/largest_perimeter_triangle.cpp/README.md: -------------------------------------------------------------------------------- 1 | ### Out of Blue 2 | 3 | ##### Bob Steagall “Fast Conversion From UTF-8 with C++, DFAs, and SSE Intrinsics 4 | 5 | C++ conference on youtube 8 | 9 | --- 10 | 11 | ### Problem 12 | 13 | *Write a function that, given an array of unsigned integers, return the largest perimeter triangle you can build with 3 of its elements. If it cannot build any triangle it should return 0.* 14 | 15 | ##### Tips 16 | 17 | *You cannot build a triangle if the longest side is longer than the two other sides !* 18 | 19 | ##### Prototype 20 | ```c++ 21 | uint32_t largest_perimeter_triangle(vector& A) {;} 22 | ``` 23 | 24 | ##### For example 25 | * If vector& A contains {3, 5, 3, 4} it should return 0 26 | * If vector& A contains {3, 7, 3, 4} it should return (7 + 3 + 3) = 13 27 | 28 |
29 | 
30 | 
31 | 
32 | 
33 | 
34 | 
35 | 
36 | 
37 | 
38 | 
39 | 
40 | 
41 | 
42 | 
43 | 
44 | 
45 | 
46 | 
47 | 
48 | 
49 | 
50 | 
51 | 
52 | 
53 | 
54 | 
55 | 
56 | 
57 | 
58 | 59 | ### C++ Solution 60 | ```c++ 61 | uint32_t largest_perimeter_triangle(vector& A) { 62 | sort(A.begin(), A.end(), std::greater<>()); // sort values given by descending order thank to std::greater<>() 63 | vector B; 64 | 65 | for (auto& n : A) 66 | { 67 | B.push_back(n); // add the next biggest side to B 68 | if (B.size() == 3) // we could store the first three values in B to avoid this condition. 69 | { 70 | if (B[0] < B[1] + B[2]) // if the biggest side is strictly inferior to the other two sides then we can build a triangle 71 | return B[0] + B[1] + B[2]; 72 | B.erase(B.begin()); // remove the biggest side 73 | } 74 | } 75 | 76 | return 0; 77 | } 78 | ``` 79 | -------------------------------------------------------------------------------- /misc/first_missing_one_in_series/README.md: -------------------------------------------------------------------------------- 1 | ### Problem 2 | 3 | *Write a function that, given an array A of N integers, returns the smallest positive integer (greater than 0) that does not occur in A.* 4 | 5 | ##### Prototype 6 | ```cs 7 | class Solution { public int solution(int[] A); } 8 | ``` 9 | 10 | ##### For example 11 | * Given A = [1, 3, 6, 4, 1, 2], the function should return 5. 12 | * Given A = [1, 2, 3], the function should return 4. 13 | * Given A = [−1, −3], the function should return 1. 14 | 15 | ##### Constraints 16 | * 1 <= N <= 100,000 17 | * Each element of array A is an integer within the range [−1,000,000..1,000,000]. 18 | 19 |
20 | 
21 | 
22 | 
23 | 
24 | 
25 | 
26 | 
27 | 
28 | 
29 | 
30 | 
31 | 
32 | 
33 | 
34 | 
35 | 
36 | 
37 | 
38 | 
39 | 
40 | 
41 | 
42 | 
43 | 
44 | 
45 | 
46 | 
47 | 
48 | 
49 | 50 | ### C# Solution 51 | ```cs 52 | using System; 53 | ​ 54 | class Solution { 55 | public int solution(int[] A) { 56 | const int MAX = 1000000; 57 | bool[] check = new bool[MAX + 1]; 58 | ​ 59 | foreach (int a in A) 60 | if (a > 0) // each time we encouter a positive number we set it to true 61 | check[a] = true; 62 | ​ 63 | if (check.Equals(new bool[MAX + 1])) // if array is empty 64 | return 1; 65 | for (int i = 1; i < MAX + 1; i++) 66 | if (check[i] == false) 67 | return i; 68 | return MAX + 1; 69 | } 70 | } 71 | ``` 72 | 73 | ### C++ Solution 74 | ```cs 75 | class Solution { 76 | public: 77 | int firstMissingPositive(vector& nums) { 78 | const int MAX = 2000; // double size to store negative values 79 | bool check[MAX + 1] = { false }; 80 | 81 | for (const auto& a : nums) 82 | check[a + 1000] = true; 83 | for (int i = 1001; i < MAX + 1; i++) 84 | if (check[i] == false) 85 | return i - 1000; 86 | 87 | return MAX - 1000 + 1; 88 | } 89 | }; 90 | ``` 91 | -------------------------------------------------------------------------------- /misc/matching_socks/README.md: -------------------------------------------------------------------------------- 1 | ### Problem 2 | 3 | *Write a function that, given an integer n (the number of socks that the washing machine can clean), two arrays CleanSocks and DirtySocks (containing the color representations of N clean and M dirty socks respectively), returns the maximum number of pairs of socks that Bob can take on the trip.* 4 | 5 | ##### Prototype 6 | ```cs 7 | public int solution(int n, int[] CleanSocks, int[] DirtySocks) { ;} 8 | ``` 9 | 10 | ##### For example 11 | * Consider CleanSocks = {4, 7, 4, 4}, DirtySocks contains {4, 8, 3, 7, 8} and n = 2. 12 | * The function should return 3 with one '4' and one '7' cleaned which makes 3 pairs of clean ones. 13 | 14 | 15 |
16 | 
17 | 
18 | 
19 | 
20 | 
21 | 
22 | 
23 | 
24 | 
25 | 
26 | 
27 | 
28 | 
29 | 
30 | 
31 | 
32 | 
33 | 
34 | 
35 | 
36 | 
37 | 
38 | 
39 | 
40 | 
41 | 
42 | 
43 | 
44 | 
45 | 46 | ### Solution 47 | 48 | ```c++ 49 | using System; 50 | ​ 51 | public int solution(int n, int[] CleanSocks, int[] DirtySocks) { 52 | int ret = 0; 53 | ​ 54 | int[] clean = new int[51]; 55 | for (int i = 0; i < CleanSocks.Length; i++) 56 | clean[CleanSocks[i]]++; 57 | ​ 58 | int[] dirty = new int[51]; 59 | for (int i = 0; i < DirtySocks.Length; i++) 60 | dirty[DirtySocks[i]]++; 61 | ​ 62 | // get the clean ones 63 | for (int i = 0; i <= 50; i++) 64 | { 65 | if ((clean[i] % 2 == 1) && (dirty[i] > 0) && n > 0) // if we have an odd number of clean one and at least one dirty one available and enough room (n) then we have one more pair washed. 66 | { 67 | ++clean[i]; 68 | --dirty[i]; 69 | --n; 70 | } 71 | ret += clean[i] / 2; // By dividing by 2 the clean ones we get the number of pairs available. 72 | } 73 | ​ 74 | // Finally we use the remaining n to wash by pair the dirty socks 75 | for (int i = 0; i <= 50 && n > 1; i++) 76 | while ((dirty[i] -= 2) >= 0 && (n -=2) >= 0) 77 | ++ret; 78 | ​ 79 | return ret; 80 | } 81 | 82 | ``` 83 | -------------------------------------------------------------------------------- /bitwise/is_nth_bit_set/README.md: -------------------------------------------------------------------------------- 1 | ### Out of Blue 2 | 3 | ##### Multithreading is the Answer, what is the Question 4 | 5 | C++ conference on youtube 8 | 9 | ### Problem 10 | 11 | *Write a function that, given an integer 'value', returns true or false if the value of the 'n'th bit is set.* 12 | 13 | ##### Prototype 14 | ```c++ 15 | bool is_nth_bit_set(int value, int n) { ; } 16 | ``` 17 | 18 | ##### For example 19 | * For value = 16 (0b10000) and n = 5, should return true 20 | * For value = 15 (0b01111) and n = 5, should return false 21 | 22 | ##### Write the most efficient algorithm for the following assumptions: 23 | * 1 <= n <= 32 (we consider that 1 is index 0) 24 | * value is a signed integer 25 | 26 |
27 | 
28 | 
29 | 
30 | 
31 | 
32 | 
33 | 
34 | 
35 | 
36 | 
37 | 
38 | 
39 | 
40 | 
41 | 
42 | 
43 | 
44 | 
45 | 
46 | 
47 | 
48 | 
49 | 
50 | 
51 | 
52 | 
53 | 
54 | 
55 | 
56 | 57 | 58 | ### C/C++ Solution 59 | *We can simply right shift value by (n - 1) bits and check if the bit is set.* 60 | 61 | ```c++ 62 | #include 63 | 64 | bool is_nth_bit_set(int value, int n) { 65 | return (value >> (n - 1)) & 0b1; 66 | } 67 | ``` 68 | 69 | *Alternatively we can write the function the following way:* 70 | 71 | ```c++ 72 | #include 73 | 74 | bool is_nth_bit_set(int value, int n) { 75 | return value & (0b1 << (n - 1)); 76 | } 77 | ``` 78 | *We left shift 1 by (n - 1) to have the suitable mask that will check if the nth bit is set on 'value'. Since it is a bool function there is no need to use not instruction (!) twice which would help to return 1 for any value different from 0:* 79 | ```c++ 80 | return !!(value & (0b1 << (n - 1))); 81 | ``` 82 | *It would be required if the prototype function was returning an integer to avoid returning the mask 0b1 << (n - 1)* 83 | ```c++ 84 | int is_nth_bit_set(int value, int n) {;} 85 | ``` 86 | -------------------------------------------------------------------------------- /array/array_rotation/README.md: -------------------------------------------------------------------------------- 1 | ### Out of Blue 2 | 3 | ##### Rich Dad Poor Dad: What The Rich Teach Their Kids About Money - Robert T.Kiyosaki 4 | > Being a product of two strong dads allowed me the luxury of observing the effects different thoughts have on one's life. 5 | 6 | > I noticed that people really do shape their life through their thoughts. For example, my poor dad always said, “I'll never be rich.” And that prophesy became reality. 7 | 8 | > My rich dad, on the other hand, always referred to himself as rich. He would say things like, “I'm a rich man, and rich people don't do this.” Even when he was flat broke after a major financial setback, he continued to refer to himself as a rich man. He would cover himself by saying, “There is a difference between being poor and being broke. -Broke is temporary, and poor is eternal.” 9 | 10 | ### Problem 11 | 12 | *Write a function that, given an array of integers, return the array with values rotated by n.* 13 | 14 | ##### Assumptions 15 | 16 | * If n is negative then it will rotate through the left, if n is positive it will rotate through the right side. 17 | 18 | ##### Prototype 19 | ```c++ 20 | void rotate_array(vector& a, int n) {;} 21 | ``` 22 | 23 | ##### For example 24 | *If the vector contains {3, 5, 9, 4} and n = -7 (left rotation) then it will become a vector containing {4, 3, 5, 9}* 25 | 26 |
27 | 
28 | 
29 | 
30 | 
31 | 
32 | 
33 | 
34 | 
35 | 
36 | 
37 | 
38 | 
39 | 
40 | 
41 | 
42 | 
43 | 
44 | 
45 | 
46 | 
47 | 
48 | 
49 | 
50 | 
51 | 
52 | 
53 | 
54 | 
55 | 
56 | 57 | ### C++ Solution 58 | ```c++ 59 | void rotate_array(vector& a, int n) { 60 | if (n == 0 || a.size() == 0) 61 | return; 62 | if (n > 0) // right side rotation 63 | { 64 | n %= a.size(); 65 | reverse(a.begin(),a.end()); 66 | reverse(a.begin(),a.begin() + n); 67 | reverse(a.begin() + n,a.end()); 68 | } 69 | else // left side rotation 70 | std::rotate(a.begin(), a.begin() - n, a.end()); 71 | } 72 | ``` 73 | 74 | *Runs in O (1)* 75 | -------------------------------------------------------------------------------- /greedy/change_problem/README.md: -------------------------------------------------------------------------------- 1 | ### Problem 2 | 3 | *Write a function that, given an amount s and a structure Change c, returns true or false depending if a solution was possible as well as the optimal change of coins and bills.* 4 | 5 | ```c++ 6 | typedef struct { 7 | long coin2; 8 | long bill5; 9 | long bill10; 10 | } Change; 11 | ``` 12 | 13 | ##### Prototype 14 | ```c++ 15 | static bool optimalChange(long s, Change &c) { ;} 16 | ``` 17 | 18 | ##### For example 19 | * With s = 9, the function should return True and Change c should contain 1 x bill5 and 2x coin2 20 | 21 | 22 |
 23 | 
 24 | 
 25 | 
 26 | 
 27 | 
 28 | 
 29 | 
 30 | 
 31 | 
 32 | 
 33 | 
 34 | 
 35 | 
 36 | 
 37 | 
 38 | 
 39 | 
 40 | 
 41 | 
 42 | 
 43 | 
 44 | 
 45 | 
 46 | 
 47 | 
 48 | 
 49 | 
 50 | 
 51 | 
52 | 53 | ​ 54 | ​ 55 | ​ 56 | ### Solution 57 | 58 | ```c++ 59 | #include 60 | #include 61 | #include 62 | ​ 63 | using namespace std; 64 | ​ 65 | typedef struct { 66 | long coin2; 67 | long bill5; 68 | long bill10; 69 | } Change; 70 | ​ 71 | class Answer { 72 | public: 73 | static bool optimalChange(long s, Change &c) { 74 | memset(&c, 0, sizeof(Change)); 75 | ​ 76 | if (s <= 0 || s == 1) 77 | return false; 78 | if (s == 3) 79 | c.coin2 = 1; 80 | else if (s % 2 == 1) 81 | { 82 | c.bill5 = 1; 83 | s -= 5; 84 | } 85 | c.bill10 = s / 10; 86 | s %= 10; 87 | c.coin2 = s / 2; 88 | s %= 2; 89 | return (!s); 90 | } 91 | }; 92 | ``` 93 | 94 | ##### Test file 95 | 96 | ```c++ 97 | int main() 98 | { 99 | Change c; 100 | ​ 101 | for (long s = 0; s < 99; s++) 102 | { 103 | bool found = Answer::optimalChange(s, c); 104 | cout << c.coin2 << endl; 105 | cout << c.bill5 << endl; 106 | cout << c.bill10 << endl; 107 | ​ 108 | long result = c.coin2 * 2 + c.bill5 * 5 + c.bill10 * 10; 109 | ​ 110 | cout << endl << "s = "<< s << " change given = " << result << endl; 111 | cout << endl << "possible ? " << found << endl; 112 | }; 113 | } 114 | ``` 115 | -------------------------------------------------------------------------------- /bitwise/bitwise_AND_of_numbers_range/README.md: -------------------------------------------------------------------------------- 1 | ### Out of Blue 2 | 3 | ##### Chrono Trigger Beyond Time Crashed Epoch Ending (BEST) 4 | 5 | Chrono Trigger Best Ending on youtube 8 | 9 | 10 | --- 11 | ### Problem 12 | 13 | *Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers in this range, inclusive.* 14 | 15 | ##### Example 1: 16 | ``` 17 | Input: [5,7] 18 | Output: 4 19 | ``` 20 | 21 | ##### Example 2: 22 | ``` 23 | Input: [0,1] 24 | Output: 0 25 | ``` 26 | 27 | ##### Note: 28 | 29 | * The given integer is guaranteed to fit within the range of a 32-bit signed integer. 30 | * You could assume no leading zero bit in the integer’s binary representation. 31 | 32 | ##### Prototype 33 | ```c++ 34 | int rangeBitwiseAnd(int m, int n) 35 | ``` 36 | 37 | 38 |
 39 | 
 40 | 
 41 | 
 42 | 
 43 | 
 44 | 
 45 | 
 46 | 
 47 | 
 48 | 
 49 | 
 50 | 
 51 | 
 52 | 
 53 | 
 54 | 
 55 | 
 56 | 
 57 | 
 58 | 
 59 | 
 60 | 
 61 | 
 62 | 
 63 | 
 64 | 
 65 | 
 66 | 
 67 | 
68 | 69 | --- 70 | ### Solution 71 | 72 | See .cpp file for explanations 73 | ```c++ 74 | class Solution { 75 | public: 76 | int rangeBitwiseAnd(int m, int n) { 77 | __asm__ (R"(.intel_syntax 78 | xor ecx, ecx 79 | mov eax, esi 80 | xor esi, edx 81 | jz .end 82 | .myloop: 83 | inc ecx 84 | shr eax 85 | shr edx 86 | cmp edx, eax 87 | jne .myloop 88 | .end: 89 | shl eax, cl 90 | leave 91 | ret 92 | )"); 93 | return 42; 94 | } 95 | }; 96 | ``` 97 | 98 | ### C++ One liners 99 | 100 | ```c++ 101 | int rangeBitwiseAnd(int m, int n) { 102 | return ((n ^ m) == 0 || n==1) ? n : (n & (~( 0xFFFFFFFF >> __builtin_clz(n ^ m) ))); 103 | } 104 | ``` 105 | 106 | ```c++ 107 | int rangeBitwiseAnd(int m, int n) { 108 | return (n > m) ? (rangeBitwiseAnd(m/2, n/2) << 1) : m; 109 | } 110 | ``` 111 | -------------------------------------------------------------------------------- /math/is_power_of_three/benchmark.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | using namespace std; 14 | 15 | auto timeFuncInvocation = 16 | [](auto&& func, auto&&... params) { 17 | // get time before function invocation 18 | const auto& start = chrono::high_resolution_clock::now(); 19 | // function invocation using perfect forwarding 20 | std::forward(func)(std::forward(params)...); 21 | // get time after function invocation 22 | const auto& stop = chrono::high_resolution_clock::now(); 23 | return (stop - start)/100000/*largeNumber*/; 24 | }; 25 | 26 | bool is_power_of_three(uint32_t n) { 27 | return n > 0 && !(1162261467 % n); // only word with prime numbers 28 | } 29 | 30 | bool is_power_of_three2(uint32_t n) { 31 | const set set = {1, 3, 9, 27, 81, 243, 729, 2187, 6561, 19683, 59049, 177147, 531441, 1594323, 4782969, 14348907, 43046721, 129140163, 387420489, 1162261467}; 32 | return set.count(n); 33 | } 34 | 35 | 36 | int number_of_power_of_three(vector v, int func_ptr_index) 37 | { 38 | static bool (*pf[])(uint32_t) = {is_power_of_three, is_power_of_three2}; 39 | 40 | int count = 0; 41 | for (const int& n : v) 42 | count += pf[func_ptr_index](n); 43 | return count; 44 | } 45 | 46 | int main(void) 47 | { 48 | // First create an instance of an engine. 49 | random_device rnd_device; 50 | // Specify the engine and distribution. 51 | mt19937 mersenne_engine {rnd_device()}; // Generates random integers 52 | uniform_int_distribution dist {0, INT_MAX}; // between 0 and INT_MAX 53 | 54 | auto gen = [&dist, &mersenne_engine](){ 55 | return dist(mersenne_engine); 56 | }; 57 | 58 | vector v(100000); 59 | generate(begin(v), end(v), gen); 60 | std::cout << timeFuncInvocation(number_of_power_of_three, v, 0).count() << std::endl; 61 | 62 | vector v2(100000); 63 | generate(begin(v2), end(v2), gen); 64 | std::cout << timeFuncInvocation(number_of_power_of_three, v2, 1).count() << std::endl; 65 | 66 | } 67 | -------------------------------------------------------------------------------- /bitwise/find_the_single_integer/README.md: -------------------------------------------------------------------------------- 1 | ### Out of Blue 2 | 3 | ##### Nir Friedman “Understanding Optimizers: Helping the Compiler Help You” 4 | 5 | Nir Friedman on youtube 8 | 9 | ### Problem 10 | 11 | *Write a function that, given an array of integer containing integers that can be paired with another of the same value except one with a single value, returns the value of the one that can't be paired.* 12 | 13 | ##### Prototype 14 | ```c++ 15 | int find_the_single_integer(const vector V) { ;} 16 | ``` 17 | 18 | ##### For example 19 | * Consider an array V such that A[0] = 6 A[1] = 34 A[2] = 6 A[3] = 7 A[4] = 42 A[5] = 7 A[6] = 34 20 | * The function should return 42 which is the single one integer. 21 | 22 | ##### Write the most efficient algorithm for the following assumptions: 23 | * Array/Vector length is an odd integer within the range [1..1,000,000]; 24 | * Each element of array V is an integer that can have any value; 25 | * All but one of the values in V occur an even number of times. 26 | 27 |
28 | 
29 | 
30 | 
31 | 
32 | 
33 | 
34 | 
35 | 
36 | 
37 | 
38 | 
39 | 
40 | 
41 | 
42 | 
43 | 
44 | 
45 | 
46 | 
47 | 
48 | 
49 | 
50 | 
51 | 
52 | 
53 | 
54 | 
55 | 
56 | 
57 | 58 | 59 | ### Solution 60 | We can simply go through every single number and use the XOR operator '^' using the fact that two numbered xored will cancel each other. 61 | 62 | By xoring n we will be able to set bit when one or the other is set and unset bit when both bits are set . All duplicates will cancel each other and we will keep the remaining single one integer. 63 | 64 | ##### Example 65 | * with V = {1, 3, 1} we have to return 3, 66 | * n ^= 1 will equal 1 67 | * then 0b01 ^ 0b11 will equal to 0b10 68 | * and finally 0b10 ^ 0b01 will give us 0b11 (3) 69 | 70 | ##### C++ Solution 71 | ```c++ 72 | #include 73 | 74 | int find_the_single_integer(const vector V) { 75 | int n = 0; 76 | 77 | for (const int ITEM : V) // for each item of the vector V 78 | n ^= ITEM; 79 | 80 | return n; 81 | } 82 | ``` 83 | 84 | ##### C# Solution 85 | 86 | ```cs 87 | using System; 88 | ​ 89 | 90 | public int find_single_integer(const int[] V) { 91 | int n = 0; 92 | ​ 93 | for (int i = 0; i < A.Length; ++i) 94 | n ^= V[i]; 95 | return n; 96 | } 97 | 98 | ``` 99 | -------------------------------------------------------------------------------- /bitwise/match_maker/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include // for high_resolution_clock 5 | 6 | using namespace std; 7 | 8 | // 0x55555555 is equivalent to 0b 0101 0101 0101 0101 0101 0101 0101 0101 9 | // 0x33333333 is equivalent to 0b 0011 0011 0011 0011 0011 0011 0011 0011 10 | // 0x0F0F0F0F is equivalent to 0b 0000 1111 0000 1111 0000 1111 0000 1111 11 | // 0x01010101 is equivalent to 0b 0000 0001 0000 0001 0000 0001 0000 0001 12 | // credit : 'parallel' or 'variable-precision SWAR algorithm' 13 | uint32_t number_of_set_bits(uint32_t i) 14 | { 15 | i = i - ((i >> 1) & 0x55555555); 16 | i = (i & 0x33333333) + ((i >> 2) & 0x33333333); 17 | return (((i + (i >> 4)) & 0x0F0F0F0F) * 0x01010101) >> 24; 18 | } 19 | 20 | int match_maker(vector v1, vector v2) { 21 | int count = 0; 22 | 23 | unordered_map hashmap1[33]; 24 | unordered_map hashmap2[33]; 25 | 26 | for (const uint32_t & n : v1) 27 | { 28 | ++hashmap1[number_of_set_bits(n)][n]; 29 | cout << number_of_set_bits(n) << endl; 30 | } 31 | for (const uint32_t & n : v2) 32 | { 33 | ++hashmap2[number_of_set_bits(n)][n]; 34 | cout << number_of_set_bits(n) << endl; 35 | } 36 | 37 | count += min(hashmap1[0][0], hashmap2[0][0]); // adding 0s 38 | for (int i = 0; i < 32; i++) 39 | { 40 | uint32_t tmp = min(hashmap1[1][0b1 << i],hashmap2[1][0b1 << i]); 41 | hashmap1[1][0b1 << i] -= tmp; 42 | hashmap2[2][0b1 << i] -= tmp; 43 | count += tmp; 44 | //if (tmp) 45 | // cout << "hey" << i + 1 << endl; 46 | } 47 | /* 48 | for (int i = 1; i < 33; i++) 49 | { 50 | for( const auto& n : hashmap1[i] ) 51 | { 52 | if (n & ) 53 | } 54 | 55 | } 56 | for (const uint32_t & n : v2) 57 | { 58 | 59 | cout << numberOfSetBits(n) << endl; 60 | }*/ 61 | 62 | return count; 63 | } 64 | 65 | int main(void) 66 | { 67 | // Record start time 68 | auto start = std::chrono::high_resolution_clock::now(); 69 | vector v1 = {1, 3, 4, 16}; 70 | vector v2 = {3, 6, 17, 4}; 71 | 72 | cout << "result :" << match_maker(v1, v2) << endl; 73 | auto finish = std::chrono::high_resolution_clock::now(); 74 | std::chrono::duration elapsed = finish - start; 75 | 76 | std::cout << "Elapsed time: " << elapsed.count() * 1000 << " ms\n"; 77 | __asm__( 78 | ".intel_syntax;" 79 | "push rbp;" 80 | "mov rbp, rsp;" 81 | "mov rax, 1;" 82 | "leave"); 83 | 84 | return 0; 85 | } 86 | -------------------------------------------------------------------------------- /bitwise/number_complement/README.md: -------------------------------------------------------------------------------- 1 | ### Out of Blue 2 | 3 | ##### My life for Aiur... mh... C language ! 4 | 5 | *On Wed, 5 Sep 2007, Dmitry Kakurin (about to be flamed) wrote:* 6 | 7 | >> When I first looked at Git source code two things struck me as odd: 8 | >> 1. Pure C as opposed to C++. No idea why. Please don't talk about portability, 9 | 10 | *Linus Torvald's reply* 11 | > it's BS. *YOU* are full of bullshit. 12 | C++ is a horrible language. It's made more horrible by the fact that a lot 13 | of substandard programmers use it, to the point where it's much much 14 | easier to generate total and utter crap with it. Quite frankly, even if 15 | the choice of C were to do *nothing* but keep the C++ programmers out, 16 | that in itself would be a huge reason to use C. 17 | In other words: the choice of C is the only sane choice. I know Miles 18 | Bader jokingly said "to piss you off", but it's actually true. I've come 19 | to the conclusion that any programmer that would prefer the project to be 20 | in C++ over C is likely a programmer that I really *would* prefer to piss 21 | off, so that he doesn't come and screw up any project I'm involved with. 22 | 23 | ### Problem 24 | 25 | *Given a positive integer, output its complement number. The complement strategy is to flip the bits of its binary representation.* 26 | 27 | ##### Example 1: 28 | ``` 29 | Input: 5 30 | Output: 2 31 | Explanation: The binary representation of 5 is 101 (no leading zero bits), and its complement is 010. So you need to output 2. 32 | ``` 33 | 34 | ##### Example 2: 35 | ``` 36 | Input: 1 37 | Output: 0 38 | Explanation: The binary representation of 1 is 1 (no leading zero bits), and its complement is 0. So you need to output 0. 39 | ``` 40 | 41 | ##### Note: 42 | 43 | * The given integer is guaranteed to fit within the range of a 32-bit signed integer. 44 | * You could assume no leading zero bit in the integer’s binary representation. 45 | 46 | ##### Prototype 47 | ```c++ 48 | int find_complement(int num) 49 | ``` 50 | 51 | 52 |
 53 | 
 54 | 
 55 | 
 56 | 
 57 | 
 58 | 
 59 | 
 60 | 
 61 | 
 62 | 
 63 | 
 64 | 
 65 | 
 66 | 
 67 | 
 68 | 
 69 | 
 70 | 
 71 | 
 72 | 
 73 | 
 74 | 
 75 | 
 76 | 
 77 | 
 78 | 
 79 | 
 80 | 
 81 | 
82 | 83 | 84 | ### Solution 85 | 86 | See .cpp file for explanations 87 | ```c++ 88 | int findComplement(int num) { 89 | __asm__ (R"(.intel_syntax 90 | bsr ecx, esi 91 | 92 | xor eax, eax 93 | dec eax 94 | shl eax, cl 95 | or eax, esi 96 | not eax 97 | ;)"); 98 | return num; 99 | } 100 | ``` 101 | 102 | **agavrel to Linus** 103 | > *In other words: the choice of Assembly is the only sane choice* 104 | -------------------------------------------------------------------------------- /bitwise/longest_palindrome/README.md: -------------------------------------------------------------------------------- 1 | ### Out of Blue 2 | 3 | ##### Tonari No Totoro - Azumi Inoue / My Neighbor Totoro 4 | 5 | Tonari No Totoro on youtube 8 | 9 | ### Problem 10 | 11 | *Write a function that, given a string containing only characters (of value between 0 and 127) returns the longest palindrome that we can build.* 12 | 13 | ##### Prototype 14 | ```c++ 15 | int longest_palindrome(string s) { ;} 16 | ``` 17 | 18 | ##### For example 19 | * Consider a string "abaabc" we can build "ababa" or "abcba" so the longest palindrome length is 5. 20 | 21 | ##### Write the most efficient algorithm for the following assumptions: 22 | * Characters are between 0 and 127; 23 | * 'a' is not equivalent to 'A' 24 | 25 |
26 | 
27 | 
28 | 
29 | 
30 | 
31 | 
32 | 
33 | 
34 | 
35 | 
36 | 
37 | 
38 | 
39 | 
40 | 
41 | 
42 | 
43 | 
44 | 
45 | 
46 | 
47 | 
48 | 
49 | 
50 | 
51 | 
52 | 
53 | 
54 | 
55 | 56 | 57 | ### Solution 58 | We can approach this problem by thinking this way: Every letter that pair will add 2, and if we have any extra unpaired letter it will add one to the solution. 59 | 60 | ##### C++ Solution 61 | ```c++ 62 | static int lambda_0 = []() { std::ios::sync_with_stdio(false); cin.tie(NULL); return 0; }(); // small optimization to get faster runtime 63 | 64 | int longest_palindrome(string s) { 65 | __int128 letters = 0; // instead of using a vector or array of char we will store each letter on each bit of a 128 bits integer. 66 | int result = 0; // the return of our function 67 | int unpaired_letter = 0; // the "left overs" xD 68 | 69 | for (const char c : s) 70 | { 71 | bool letter = letters & ((__int128)1 << c); // used to check if the character had already appeared and was unpaired. 72 | letters ^= ((__int128)1 << c); // this will switch the bit corresponding to the character 73 | result += letter; // we add 2 to the result, but instead of operating a left shift during the loop we perform it during the return. 74 | unpaired_letter += !letter; // if there was no character previously it will add one 75 | unpaired_letter -= letter; // if there was already one, we remove one since the character is now paired with a former one. 76 | } 77 | return (result << 1) | !!unpaired_letter; // if any character was left without another one that matches it will add one. 78 | } 79 | ``` 80 | 81 | This solution, thanks to memory optimization and branching avoidance, will gives better speed and better memory than 100% of other solutions on https://leetcode.com/problems/longest-palindrome/ 82 | -------------------------------------------------------------------------------- /bitwise/spy_hawk/README.md: -------------------------------------------------------------------------------- 1 | ### Out of Blue 2 | 3 | ##### Money as Debt 4 | *Central banks, perhaps the biggest curse of our century?* 5 | 6 | C++ conference on youtube 9 | 10 | ### Problem 11 | 12 | A spy wants to send a message to his headquarters in order to warn for an upcoming danger. 13 | The message is bamboozled and sent through hawks, each hawk having word position in the sentence (starting at 1) to the HQ. As a safeguard he decides to send many times the same words just in case. 14 | 15 | You are given an array A consisting of X integers representing the number of hawks sent. A[i] represents the word carried by the hawk and i is the time they arrive at the HQ. 16 | 17 | The goal is to find the earliest time when the full sentence is received by the HQ. It is done when each word from 1 to N have been received. 18 | 19 | *Write a function that, given a non-empty array A consisting of X integers and integer N, returns the earliest time when HQ receives the full sentence. If the sentence is not fully transmitted to the HQ, the function should return −1.* 20 | 21 | ##### Prototype 22 | ```cs 23 | class Solution { public int solution(int N, int[] A);} 24 | ``` 25 | 26 | ##### For example 27 | * Given integer N = 5 and array A such that A[0] = 1 A[1] = 3 A[2] = 1 A[3] = 4 A[4] = 2 A[5] = 3 A[6] = 5 A[7] = 4 28 | At index 6, the hawk carrying the 5th word arrives. This is the earliest time when HQ receives the full message. The function should hence return 6. 29 | 30 | #####Assumptions 31 | * 1 <= N and A length <= 100,000 32 | * each element of array A is an integer within the range [1..N]. 33 | 34 | ​ 35 | 36 | 37 | ##### Write the most efficient algorithm for the following assumptions: 38 | * Array/Vector length is an odd integer within the range [1..1,000,000]; 39 | * Each element of array V is an integer that can have any value; 40 | * All but one of the values in V occur an even number of times. 41 | 42 |
43 | 
44 | 
45 | 
46 | 
47 | 
48 | 
49 | 
50 | 
51 | 
52 | 
53 | 
54 | 
55 | 
56 | 
57 | 
58 | 
59 | 
60 | 
61 | 
62 | 
63 | 
64 | 
65 | 
66 | 
67 | 
68 | 
69 | 
70 | 
71 | 
72 | 73 | ##### C# Solution 74 | 75 | ```cs 76 | Solution 77 | using System; 78 | using System.Collections; 79 | ​ 80 | class Solution { 81 | public int solution(int N, int[] A) { 82 | BitArray B = new BitArray(N + 1); 83 | ​ 84 | for (int i = 0; i < A.Length; ++i) 85 | { 86 | if (B.Get(A[i]) == false) 87 | { 88 | --N; 89 | if (N == 0) 90 | return i; 91 | B.Set(A[i], true); 92 | } 93 | } 94 | return -1; 95 | } 96 | } 97 | ``` 98 | -------------------------------------------------------------------------------- /array/move_zeroes_back/README.md: -------------------------------------------------------------------------------- 1 | ### Out of Blue 2 | 3 | ##### [The Inside Story of the CryptoKitties Congestion Crisis](https://media.consensys.net/the-inside-story-of-the-cryptokitties-congestion-crisis-499b35d119cc) 4 | 5 | *When Vancouver-based venture studio Axiom Zen launched CryptoKitties in October of 2017, it was intended to be a playful experiment of early stage blockchain technology. CryptoKitties users breed and trade unique digital kittens, upvote and engage with the community, and revel in the still-novel joy of a fully-functional application built on a distributed network. Conceptually somewhere between Neopets and futures trading, the disarmingly simple CryptoKitties game captured imaginations with the emergent blockchain community and beyond. Before long, the CryptoKitties craze had spiraled beyond anyone’s expectations— for better and worse.* 6 | 7 | *By early December, CryptoKitties had turned viral sensation, sending prices for some kittens well into the six figures, earning slightly bemused mentions from across mainstream news media, and clogging up the Ethereum network with so many transactions — a sixfold increase in total network requests in the first week of December alone — that many feared the entire Ethereum blockchain would grind to a halt before it ever even had a chance to achieve its promise.* 8 | 9 | *In response to what was unfolding to be an industry-wide dilemma, an impromptu taskforce of Ethereum developers from projects like MetaMask, Infura, and Grid+ came together and joined the CryptoKitties team in formulating short-term optimizations and longer-term scaling solutions that not only alleviated the immediate danger, but laid out roadmaps towards a more functional future. Perhaps even more importantly, the saga highlights the remarkably collaborative and cooperative nature of the blockchain space and developer community.* 10 | 11 | --- 12 | 13 | ### Problem 14 | 15 | *Write a function that, given an array with unsorted integers, returns the same array with all 0s moved at the back.* 16 | 17 | ##### Prototype 18 | ```c++ 19 | void move_zeroes_back(vector& v) { ;} 20 | ``` 21 | 22 | ##### For example 23 | * Consider an array containing {-1,0,1,0,-1,-4}, the function should return {-1,1,-1,-4, 0, 0}; 24 | 25 |
26 | 
27 | 
28 | 
29 | 
30 | 
31 | 
32 | 
33 | 
34 | 
35 | 
36 | 
37 | 
38 | 
39 | 
40 | 
41 | 
42 | 
43 | 
44 | 
45 | 
46 | 
47 | 
48 | 
49 | 
50 | 
51 | 
52 | 
53 | 
54 | 
55 | 56 | 57 | ### Solution 58 | ```c++ 59 | void move_zeroes_back(vector& v) { 60 | int cnt = 0; 61 | for (vector::iterator it = v.begin(); it != v.end() - cnt; ) 62 | { 63 | if (*it == 0){ 64 | std::rotate(it, it + 1, v.end()); 65 | cnt++; 66 | } 67 | else 68 | it++; 69 | } 70 | } 71 | ``` 72 | 73 | ### Explanation 74 | 75 | *We use the std rotate function that will move the second parameter to the position of the first one which will be move at the position of the third parameter, here v.end(), the end of the array.* 76 | -------------------------------------------------------------------------------- /parsing/string_to_lowercase/README.md: -------------------------------------------------------------------------------- 1 | ### Out of Blue 2 | 3 | ##### My Inspirational Quotes Collections 4 | ###### Elon Musk 5 | > "When something is important enough, you do it even if the odds are not in your favor." 6 | 7 | ###### Bjarne Stroustrup 8 | > “There are only two kinds of languages: the ones people complain about and the ones nobody uses.” 9 | > “I have always wished for my computer to be as easy to use as my telephone; my wish has come true because I can no longer figure out how to use my telephone.” 10 | 11 | ###### Oscar Wilde 12 | > “The only difference between the saint and the sinner is that every saint has a past, and every sinner has a future." 13 | > “The only way to get rid of temptation is to yield to it.” 14 | > “Nowadays people know the price of everything and the value of nothing.” 15 | > “Never marry at all, Dorian. Men marry because they are tired, women, because they are curious: both are disappointed.” 16 | 17 | ###### Fyodor Dostoevsky 18 | > “While nothing is easier than to denounce the evildoer, nothing is more difficult than to understand him.” 19 | 20 | --- 21 | 22 | ### Problem 23 | *Write a function that, given a string, returns the string with all uppercase letters transformed into lowercase letters.* 24 | 25 | ##### Prototype 26 | ```c++ 27 | string str_to_lowercase(string & str) {; } 28 | ``` 29 | 30 | ##### Assumptions 31 | * The string contains only ASCII characters 32 | 33 | ##### For example 34 | * "A beautiful Evening !" -> "a beautiful evening !" 35 | 36 |
 37 | 
 38 | 
 39 | 
 40 | 
 41 | 
 42 | 
 43 | 
 44 | 
 45 | 
 46 | 
 47 | 
 48 | 
 49 | 
 50 | 
 51 | 
 52 | 
 53 | 
 54 | 
 55 | 
 56 | 
 57 | 
 58 | 
 59 | 
 60 | 
 61 | 
 62 | 
 63 | 
 64 | 
 65 | 
66 | 67 | ### C++ Solution 68 | ```c++ 69 | constexpr auto lowercase_mask() { 70 | std::array v{0}; 71 | for(uint8_t i = 0x41; i < 0x5B; ++i) { 72 | v[i] = 0x20; 73 | } 74 | return v; // generate an array with mask for uppercase letters 75 | } 76 | 77 | string str_to_lowercase(string & str) { 78 | constexpr auto v = lowercase_mask(); 79 | 80 | for (char& c : str) 81 | c = c | v[c]; 82 | 83 | return str; 84 | } 85 | ``` 86 | 87 | *This solution is 3x faster than libc toupper function (see benchmark.cpp)* 88 | 89 | ```c++ 90 | string str_to_lowercase2(string& str) { 91 | for (char& c : str) 92 | c = tolower(c); 93 | 94 | return str; 95 | } 96 | ``` 97 | 98 | *Also Equivalent to:* 99 | 100 | ```c++ 101 | string str_to_lowercase4(string& str) { 102 | for (char& c : str) 103 | c |= (!!isalpha(c)) << 5; // as I suspected, it gives the same result as c = toupper(c); 104 | 105 | return str; 106 | } 107 | ``` 108 | 109 | *Proof that we want to avoid the branch predictor, this function is even 2.5x slower than libc toupper:* 110 | ```c++ 111 | string str_to_lowercase3(string& str) { 112 | for (char& c : str) 113 | if (isalpha(c)) 114 | c |= 32; 115 | 116 | return str; 117 | } 118 | ``` 119 | -------------------------------------------------------------------------------- /bitwise/align_number_on_power_of_two/README.md: -------------------------------------------------------------------------------- 1 | ### Out of Blue 2 | 3 | ##### Best Programming Blogs 4 | 5 | * http://cliffc.org/blog 6 | * https://lemire.me/blog 7 | * http://www.agner.org/optimize/ 8 | 9 | ### Problem 10 | 11 | *Write a function that, given a size_t n, align it on 32 bits.* 12 | 13 | ##### Prototype 14 | ```c++ 15 | #include 16 | size_t align_on_32_bits(size_t n) 17 | ``` 18 | 19 | ##### For example 20 | * Consider n = 25, the function should return 32 (upper bound of 32 multiples) 21 | * For n = 0, the function should return 0 22 | * For n = 34, the function should return 64 (upper bound of 32 multiples) 23 | * For n = 32 the function should return 32. 24 | 25 |
 26 | 
 27 | 
 28 | 
 29 | 
 30 | 
 31 | 
 32 | 
 33 | 
 34 | 
 35 | 
 36 | 
 37 | 
 38 | 
 39 | 
 40 | 
 41 | 
 42 | 
 43 | 
 44 | 
 45 | 
 46 | 
 47 | 
 48 | 
 49 | 
 50 | 
 51 | 
 52 | 
 53 | 
 54 | 
55 | 56 | 57 | ### Solution 58 | 59 | ##### Solution A 60 | We can use a combination of left and right shifts using the exponent of the power of two we want to align with. 61 | 62 | * For exponent = 5 then it means that we will align on 2^5 = 32 bytes 63 | 64 | ```c++ 65 | #include 66 | #include 67 | ​ 68 | size_t ft_align_power_of_two(size_t size, size_t exponent) 69 | { 70 | return (((size + (1 << exponent) - 1) >> exponent) << exponent); 71 | } 72 | ​ 73 | unsigned long long align_on_32_bits(unsigned long long n) 74 | { 75 | return ft_align_power_of_two(n, 5); // as 2^5 = 32 76 | } 77 | ``` 78 | *NB : exponent should be inferior to 32 to avoid overflow* 79 | 80 | ##### Solution B 81 | Alternatively we can use a mask equal to any power of 2 minus 1: 82 | 83 | * if size is 37 and we add the mask (31 or 0b11111) we get 64. 84 | * Hence we only keep the bits above the 5th since '~mask' toggle bits will becomes 0b1...10000 85 | 86 | 87 | ```c++ 88 | #include 89 | #include 90 | ​ 91 | size_t ft_align_mask(size_t size, size_t mask) 92 | { 93 | return ((size + mask) & ~mask); 94 | } 95 | ​ 96 | unsigned long long align_on_32_bits(unsigned long long n) 97 | { 98 | return ft_align_mask(n, 0x1f)); // 0x1f + 1 = 0x20 = 32 99 | } 100 | ``` 101 | *NB: mask must be set to a power of 2 minus 1* 102 | 103 | ##### main.c test file 104 | ```c++ 105 | int main(void) 106 | { 107 | int n = 37; 108 | printf("%lu\n", ft_align_power_of_two(n, 5)); // 2^5 = 32 109 | printf("%lu\n", ft_align_mask(n, 0x1f)); // 0x1f + 1 = 0x20 = 32 110 | return (0); 111 | } 112 | ``` 113 | 114 | ##### Bottom Line 115 | We can observe that the function using the mask is more efficient by looking at the assembly code: 116 | 117 | ```asm 118 | ft_align_power_of_two(unsigned long long, unsigned long long): 119 | mov rcx, rsi 120 | mov eax, 1 121 | sal eax, cl 122 | cdqe 123 | lea rax, [rdi-1+rax] 124 | shr rax, cl 125 | sal rax, cl 126 | ret 127 | ``` 128 | 129 | ```asm 130 | ft_align_mask(unsigned long long, unsigned long long): 131 | lea rax, [rdi+rsi] 132 | not rsi 133 | and rax, rsi 134 | ret 135 | ``` 136 | -------------------------------------------------------------------------------- /misc/closest_point_to_origin/README.md: -------------------------------------------------------------------------------- 1 | ### Out of Blue 2 | 3 | ##### Fondation "The Psychohistorians" Summary - Isaac Asimov 4 | > This part is original to the 1951 book version and takes place in 0 F.E. ("Foundation Era"). The story begins on Trantor, the capital of the 12,000-year-old Galactic Empire, a powerful but slowly decaying empire. Hari Seldon, a mathematician and psychologist, has developed psychohistory, a new field of science and psychology that equates all possibilities in large societies to mathematics, allowing for the prediction of future events. 5 | 6 | > By means of psychohistory, Seldon has discovered the declining nature of the Empire, angering the aristocratic members of the Committee of Public Safety, the de facto rulers of the Empire. The Committee considers Seldon's views and statements treasonous, and he is arrested along with young mathematician Gaal Dornick, who has arrived on Trantor to meet Seldon. Seldon is tried by the Committee and defends his beliefs, explaining his theories and predictions, including his belief that the Empire will collapse in 500 years and enter a 30,000-year dark age, to the Committee's members. He informs the Committee that an alternative to this future is attainable, and explains to them that creating a compendium of all human knowledge, the Encyclopedia Galactica, would not avert the inevitable fall of the Empire but would reduce the dark age to one millennium. 7 | 8 | > The skeptical Committee, not wanting to make Seldon a martyr, offers him exile to a remote world, Terminus, with others who could help him create the Encyclopedia. He accepts their offer, prepares for the departure of the "Encyclopedists" and receives an imperial decree officially acknowledging his actions. 9 | 10 | ### Problem 11 | 12 | *Write a function that, given an array of points, return the K points whose value is closest to the origin.* 13 | 14 | ##### Prototype 15 | ```c++ 16 | vector> closest_point_to_origin(vector>& points, int K) {;} 17 | ``` 18 | 19 | ##### For example 20 | * If points = {[3,-1],[2,2]} and K = 1 21 | Output: {[2,2]} as sqrt(2*2 + 2*2) < sqrt(3*3 + -1*-1) 22 | 23 |
24 | 
25 | 
26 | 
27 | 
28 | 
29 | 
30 | 
31 | 
32 | 
33 | 
34 | 
35 | 
36 | 
37 | 
38 | 
39 | 
40 | 
41 | 
42 | 
43 | 
44 | 
45 | 
46 | 
47 | 
48 | 
49 | 
50 | 
51 | 
52 | 
53 | 54 | ### C++ Solution 55 | ```c++ 56 | vector> closest_point_to_origin(vector>& points, int K) { 57 | map > map; 58 | vector> sol; 59 | 60 | for (vector& point : points) 61 | { 62 | int dist = point[0] * point[0] + point[1] * point[1]; // origin is {0, 0} so no need to substract and take absolute value !! 63 | map[dist] = point; // we hence don't need to compute the square root, it saves a lot of time !! 64 | } 65 | for (auto &i : map) 66 | sol.push_back(i.second); 67 | 68 | sol.resize(K); // could be make simpler by pushing K elements but we will have to check that the map contains enough elements 69 | return sol; 70 | } 71 | ``` 72 | 73 | *PS: Probably not the most optimized (but still working) algo, send me an email / pull request if you know a better one* 74 | -------------------------------------------------------------------------------- /misc/sum_of_k/README.md: -------------------------------------------------------------------------------- 1 | ### Out of Blue 2 | 3 | ##### 杏里 Anri - Bi・Ki・Ni (1983) [FULL ALBUM] 4 | 5 | youtube video 8 | 9 | --- 10 | 11 | ### Problem 12 | 13 | *Write a function that, given an array with unsorted integers, returns all unique k values in the array whose sum equals to the targeted value.* 14 | 15 | ##### Prototype 16 | ```c++ 17 | vector> k(vector& array, int target, int k) { ;} 18 | ``` 19 | 20 | ##### For example 21 | * Consider an array containing {-1,0,1,2,-1,-4}, the function should return a vector containing 2 vectors: {-1,-1,2} and {-1,0,1} if k = 3. 22 | 23 | ##### Assumption 24 | * The solution set must not contain duplicate arrays. 25 | 26 |
 27 | 
 28 | 
 29 | 
 30 | 
 31 | 
 32 | 
 33 | 
 34 | 
 35 | 
 36 | 
 37 | 
 38 | 
 39 | 
 40 | 
 41 | 
 42 | 
 43 | 
 44 | 
 45 | 
 46 | 
 47 | 
 48 | 
 49 | 
 50 | 
 51 | 
 52 | 
 53 | 
 54 | 
 55 | 
56 | 57 | 58 | 59 | ### Elegant Recursive C++ Solution 60 | *It is critical to sort the array to achieve [O (k - 1) time complexity](https://en.wikipedia.org/wiki/Big_O_notation).* 61 | 62 | ```c++ 63 | // DFS 64 | vector> sum_of_k(vector& array, int target, int k, int j) { 65 | vector> res; 66 | 67 | if (k == 2) // end of the recursive call when k = 2, we look for the two last integers 68 | { 69 | vector tmp; 70 | int k = array.size() - 1; 71 | 72 | while (j < k) 73 | { 74 | int total = array[j] + array[k]; 75 | if (total < target) 76 | j++; 77 | else if (total > target) 78 | k--; 79 | else 80 | { 81 | tmp.clear(); // essential step 82 | tmp.push_back(array[j]); 83 | tmp.push_back(array[k]); 84 | res.push_back(tmp); // array[j] is the penultimate value and array[k] the ultimate value we are looking for 85 | while (array[j] == array[j++ + 1] && j < array.size() - 1) ; 86 | while (array[k] == array[k-- - 1] && k > j) ; 87 | } 88 | 89 | } 90 | return res; 91 | } 92 | 93 | else 94 | { 95 | for (int i = j; i < array.size() - k + 1; i++) 96 | { 97 | vector> t; 98 | int tmp = array[i]; 99 | 100 | if (i > j && array[i - 1] == tmp) // Skip duplicates 101 | continue; 102 | 103 | t = sum_of_k(array, target - tmp, k - 1, i + 1); // core recursive call 104 | 105 | for (int j = 0; j < t.size(); j++) 106 | { 107 | t[j].push_back(tmp); 108 | res.push_back(t[j]); 109 | } 110 | } 111 | return res; 112 | } 113 | } 114 | 115 | vector> sum_of_k_init(vector& array, int target, int k) { 116 | if (array.size() < k) 117 | return vector> {0}; // protection 118 | 119 | sort(array.begin(), array.end()); 120 | 121 | return sum_of_k(array, target, k, 0); 122 | } 123 | ``` 124 | 125 | Credit: Adapted from [MingLiangYang's solution](https://github.com/MingLiangYang) 126 | -------------------------------------------------------------------------------- /bitwise/binary_gap/README.md: -------------------------------------------------------------------------------- 1 | ### Problem 2 | 3 | *Write a function that, given an integer n, returns the longest gap of unset bits surrounded by set bits* 4 | 5 | ##### Prototype 6 | ```c++ 7 | int binary_gap(int n) {;} 8 | ``` 9 | 10 | ##### For example 11 | * Consider n = 0b100 the function should return 0 (same for any power of two). 12 | * For n = 0b11 the function should also return 0. 13 | * For n = 0b100101 the function should return 2. We want the longest streak 14 | 15 | 16 |
 17 | 
 18 | 
 19 | 
 20 | 
 21 | 
 22 | 
 23 | 
 24 | 
 25 | 
 26 | 
 27 | 
 28 | 
 29 | 
 30 | 
 31 | 
 32 | 
 33 | 
 34 | 
 35 | 
 36 | 
 37 | 
 38 | 
 39 | 
 40 | 
 41 | 
 42 | 
 43 | 
 44 | 
 45 | 
46 | 47 | ### How to Start 48 | 1) So what doss it mean ? It means that we have to first go through the number and check each bit and see if it is set or not. 49 | 50 | ```c++ 51 | int binary_gap(int n) { 52 | int max = 0; 53 | 54 | while (n) 55 | {Do stuff} 56 | 57 | return max; 58 | } 59 | ``` 60 | 61 | 2) How do we go through a number to check its power of 2? It is simple we divide it by 2 and check the LSB (least significant bit, the bit on the extreme right) Dividing by 2 is done through right shifting '>> 1' (it is equivalent to '/ 2' since the compiler will optimize and use the right shift instruction in both cases). 62 | 63 | ```c++ 64 | int binary_gap(int n) { 65 | int max = 0; 66 | 67 | while (n) 68 | { 69 | {Do stuff} 70 | n >>=1; // 2) 71 | } 72 | return max; 73 | } 74 | ``` 75 | 76 | 3) The condition to start the counter is to have a bit set, it means n should be odd. We can easily check by using (n & 1) and then start a counter. 77 | 78 | ```c++ 79 | int binary_gap(int n) { 80 | int max = 0; 81 | 82 | while (n) 83 | { 84 | if (n & 1) // 3) 85 | { 86 | {Do stuff} 87 | } 88 | n >>=1; 89 | } 90 | return max; 91 | } 92 | ``` 93 | 94 | 4) As soon as we know that we are facing a 1 we want : 95 | 96 | a) to move to the next bit with '(n >>= 1)' and 97 | 98 | b) to check if it is a 0 with '!(n & 1)' If it is confirmed then we will increase our counter with '++count' equivalent to count = count + 1; 99 | 100 | ```c++ 101 | int binary_gap(int n) { 102 | int max = 0; 103 | int count; 104 | 105 | while (n) 106 | { 107 | if (n & 1) 108 | { 109 | while ((n >>= 1) && !(n & 1)) // a) and b) 110 | ++count; 111 | {Do stuff} 112 | } 113 | n >>=1; 114 | } 115 | return max; 116 | } 117 | ``` 118 | 119 | ### Solution 120 | 5) We keep the counter as our new maximum if it was above the previously calculated maximums with 'if (count > max) max = count;' 121 | 122 | ```c++ 123 | int binary_gap(int n) { 124 | int max = 0; 125 | int count; 126 | 127 | while (n) 128 | { 129 | if (n & 1) 130 | { 131 | while ((n >>= 1) && !(n & 1)) 132 | ++count; 133 | if (count > max) // 5) 134 | max = count; 135 | } 136 | n >>=1; 137 | } 138 | return max; 139 | } 140 | ``` 141 | -------------------------------------------------------------------------------- /array/remove_duplicates/README.md: -------------------------------------------------------------------------------- 1 | ### Out of Blue 2 | 3 | ##### Nujabes Biography 4 | 5 | > Jun Seba (瀬葉 淳 Seba Jun, February 7, 1974 – February 26, 2010), better known by his stage name Nujabes (ヌジャベス Nujabesu), was a Japanese record producer, DJ, composer and arranger who produced atmospheric instrumental mixes sampling from hip hop and jazz and released three solo studio albums: Metaphorical Music (2003), Modal Soul (2005) and Spiritual State (released posthumously in 2011). Seba was founder of the independent label Hydeout Productions and released two collection compilations: Hydeout Productions 1st Collection (2003) and 2nd Collection (2007). 6 | > Additionally, Seba produced the soundtrack for Shinichirō Watanabe's anime series Samurai Champloo (Music Record: Departure and Impression) in 2004. 7 | 8 | ##### Aruarian Dance - Nujabes 9 | 10 | youtube link 13 | 14 | --- 15 | 16 | ### Problem 17 | *Write a function that, given a sorted array of integers, modifies it in order to retain only unique values and returns the number of unique values it contains.* 18 | 19 | ##### Prototype 20 | ```c++ 21 | int remove_duplicates(vector& v) {; } 22 | ``` 23 | 24 | ##### Assumptions 25 | * The array is already sorted by ascending order 26 | 27 | ##### For example 28 | * "{0, 0, 3, 7, 7, 8, 9, 9, 9}" -> "{0, 3, 7, 8, 9}" and returns 5 29 | 30 |
31 | 
32 | 
33 | 
34 | 
35 | 
36 | 
37 | 
38 | 
39 | 
40 | 
41 | 
42 | 
43 | 
44 | 
45 | 
46 | 
47 | 
48 | 
49 | 
50 | 
51 | 
52 | 
53 | 
54 | 
55 | 
56 | 
57 | 
58 | 
59 | 
60 | 61 | ### C++ Solution 62 | ```c++ 63 | #include 64 | #include 65 | 66 | int remove_duplicates(vector& v) { 67 | v.erase(unique(v.begin(), v.end()), v.end()); 68 | return v.size(); 69 | } 70 | ``` 71 | 72 | *This solution is make use of the [unique algorithm](https://en.cppreference.com/w/cpp/algorithm/unique) from the STL* 73 | 74 | > Eliminates all but the first element from every consecutive group of equivalent elements from the range [first, last) and returns a past-the-end iterator for the new logical end of the range. 75 | 76 | > Removing is done by shifting the elements in the range in such a way that elements to be erased are overwritten. Relative order of the elements that remain is preserved and the physical size of the container is unchanged. Iterators pointing to an element between the new logical end and the physical end of the range are still dereferenceable, but the elements themselves have unspecified values. A call to unique is typically followed by a call to a container's erase method, which erases the unspecified values and reduces the physical size of the container to match its new logical size. 77 | 78 | ```c++ 79 | #include 80 | #include 81 | #include 82 | #include 83 | #include 84 | 85 | int main() 86 | { 87 | // remove duplicate elements 88 | std::vector v{1,2,3,1,2,3,3,4,5,4,5,6,7}; 89 | std::sort(v.begin(), v.end()); // 1 1 2 2 3 3 3 4 4 5 5 6 7 90 | auto last = std::unique(v.begin(), v.end()); 91 | // v now holds {1 2 3 4 5 6 7 x x x x x x}, where 'x' is indeterminate 92 | v.erase(last, v.end()); 93 | for (int i : v) 94 | std::cout << i << " "; // 1 2 3 4 5 6 7 95 | std::cout << "\n"; 96 | } 97 | ``` 98 | -------------------------------------------------------------------------------- /bitwise/symetric_bits/README.md: -------------------------------------------------------------------------------- 1 | ### Problem 2 | 3 | *Write a function that takes a byte, reverses it, bit by bit and returns the result.* 4 | 5 | ##### Prototype 6 | ```c++ 7 | unsigned char reverse_bits(unsigned char octet); 8 | ``` 9 | 10 | ##### For example 11 | * 1 byte 0100 0001 ----> 1000 0010 (we mirror the byte given) 12 | 13 | **NB: an octet means 1 byte, equal to 8 bits** 14 | 15 |
 16 | 
 17 | 
 18 | 
 19 | 
 20 | 
 21 | 
 22 | 
 23 | 
 24 | 
 25 | 
 26 | 
 27 | 
 28 | 
 29 | 
 30 | 
 31 | 
 32 | 
 33 | 
 34 | 
 35 | 
 36 | 
 37 | 
 38 | 
 39 | 
 40 | 
 41 | 
 42 | 
 43 | 
 44 | 
45 | 46 | 47 | ### Solution 48 | 49 | 50 | ##### 1st Method, for mortals like us ;) 51 | 1) As a byte is equal to 8 it means we will do it while (byte_len--) 52 | 53 | 2) We first check if b as a bit on the extreme right with (b & 1); if so we light bit 1 on r and move it juste 1 bit on the left by multiplying r by 2 with (r << 1) 54 | 55 | 3) Then we divide our unsigned char b by 2 to erase the bit located on the extreme right of the variable b. For reminder b >>= 1; is equivalent to b /= 2; 56 | 57 | ```c++ 58 | unsigned char reverse_bits(unsigned char b) 59 | { 60 | unsigned char r = 0; 61 | unsigned byte_len = 8; 62 | ​ 63 | while (byte_len--) 64 | { 65 | r = (r << 1) | (b & 1); // equivalent to r = r * 2 + b % 2 66 | b >>= 1; 67 | } 68 | return (r); 69 | } 70 | ``` 71 | 72 | ##### 2nd Method, swap bits 4 by 4, 2 by 2 and then 1 by 1 73 | 74 | 1) The first formula swaps 4 by 4 by applying masks: (b & 0xF0) >> 4 applies a mask to the higher 4 bits (0x0F = 1111 0000) then move them to the right. (b & 0x0F) << 4 applies a mask to the lowest 4 bits (0x0F = 0000 1111) then move them to the left. 75 | 76 | 2) Now swap them 2 by 2: 0xCC = 1100 1100 and 0x33 = 0011 0011 77 | 78 | 3) Finally swap 1 by 1: 0xAA = 1010 1010 and 0x55 = 0101 0101. 79 | 80 | If you had byte b equal to 0111 0100 ('t') it will gradually becomes: 81 | 82 | 0100 0111 ('G') 83 | 84 | 00 01 11 01 (character 29, group separator, not a printable char) 85 | 86 | 0 0 1 0 1 1 1 0 = 0010 1110 ('.') 87 | 88 | ```c++ 89 | unsigned char reverse_bits2(unsigned char b) 90 | { 91 | b = (b & 0xF0) >> 4 | (b & 0x0F) << 4; // 1) 92 | b = (b & 0xCC) >> 2 | (b & 0x33) << 2; // 2) 93 | b = (b & 0xAA) >> 1 | (b & 0x55) << 1; // 3) 94 | return (b); 95 | } 96 | ``` 97 | 98 | ##### 3rd Method, attributed to Rich Schroeppel in the Programming Hacks section​ 99 | 1) The multiply operation (b 0x0202020202ULL) creates five separate copies of the 8-bit byte pattern to fan-out into a 64-bit value. 100 | 101 | 2) The AND operation (& 0x010884422010ULL) selects the bits that are in the correct (reversed) positions, relative to each 10-bit groups of bits. 102 | 103 | 3) Together the multiply and the AND operations copy the bits from the original byte so they each appear in only one of the 10-bit sets. The reversed positions of the bits from the original byte coincide with their relative positions within any 10-bit set. 104 | 105 | 4) The last step (% 0x3ff), which involves modulus division by 2^10 - 1 has the effect of merging together each set of 10 bits (from positions 0-9, 10-19, 20-29, ...) in the 64-bit value. They do not overlap, so the addition steps underlying the modulus division behave like OR operations./ 106 | 107 | ```c++ 108 | unsigned char reverse_bits3(unsigned char b) 109 | { 110 | b = (b * 0x0202020202ULL & 0x010884422010ULL) % 0x3ff; 111 | return (b); 112 | } 113 | ``` 114 | -------------------------------------------------------------------------------- /parsing/string_to_lowercase/benchmark.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | auto timeFuncInvocation = 9 | [](auto&& func, auto&&... params) { 10 | // get time before function invocation 11 | const auto& start = chrono::high_resolution_clock::now(); 12 | // function invocation using perfect forwarding 13 | std::forward(func)(std::forward(params)...); 14 | // get time after function invocation 15 | const auto& stop = chrono::high_resolution_clock::now(); 16 | return (stop - start)/100000/*largeNumber*/; 17 | }; 18 | 19 | string generate_random_string(uint32_t len) 20 | { 21 | mt19937 generator{random_device{}()}; 22 | uniform_int_distribution distribution{'0', 'z'}; 23 | string s(len, '\0'); 24 | 25 | for(auto& c: s) 26 | c = distribution(generator); 27 | 28 | return s; 29 | } 30 | 31 | constexpr auto lowercase_mask() { 32 | std::array v{0}; 33 | for(uint8_t i = 0x41; i < 0x5B; ++i) { 34 | v[i] = 0x20; 35 | } 36 | 37 | return v; // generate an array with mask for uppercase letters 38 | } 39 | 40 | string str_to_lowercase(string& str) { 41 | constexpr auto v = lowercase_mask(); 42 | 43 | for (char& c : str) 44 | c |= v[c]; 45 | 46 | return str; 47 | } 48 | 49 | string str_to_lowercase2(string& str) { 50 | for (char& c : str) 51 | c = tolower(c); 52 | 53 | return str; 54 | } 55 | 56 | string str_to_lowercase3(string& str) { 57 | for (char& c : str) 58 | if (isalpha(c)) 59 | c |= 32; 60 | 61 | return str; 62 | } 63 | 64 | string str_to_lowercase4(string& str) { 65 | for (char& c : str) 66 | c |= (!!isalpha(c)) << 5; // as I suspected, it gives the same result as c = toupper(c); 67 | 68 | return str; 69 | } 70 | 71 | char agavrel_tolower(char& c) { 72 | constexpr auto v = lowercase_mask(); 73 | 74 | return c | v[c]; 75 | } 76 | 77 | #include 78 | string str_to_lowercase5(string& data) { 79 | std::transform(data.begin(), data.end(), data.begin(), ::agavrel_tolower); 80 | 81 | return data; 82 | } 83 | 84 | 85 | int main(void) 86 | { 87 | size_t N = 3000000; 88 | vector s; 89 | s.push_back(generate_random_string(N)); 90 | s.push_back(generate_random_string(N)); 91 | s.push_back(generate_random_string(N)); 92 | s.push_back(generate_random_string(N)); 93 | s.push_back(generate_random_string(N)); 94 | // cout << s[0] << endl; 95 | std::cout << timeFuncInvocation(str_to_lowercase, s[0]).count() << std::endl; 96 | std::cout << timeFuncInvocation(str_to_lowercase2, s[1]).count() << std::endl; 97 | std::cout << timeFuncInvocation(str_to_lowercase3, s[2]).count() << std::endl; 98 | std::cout << timeFuncInvocation(str_to_lowercase4, s[3]).count() << std::endl; 99 | std::cout << timeFuncInvocation(str_to_lowercase5, s[4]).count() << std::endl; 100 | // for (const string& str : s) 101 | // cout << str << endl; 102 | // cout << s[3] << endl; 103 | } 104 | 105 | /* g++ main.cpp -std=c++1z && ./a.out will display 106 | 347 107 | 319 108 | 408 109 | 404 110 | Not that good right ? 111 | 112 | But when compiling with: g++ main.cpp -O2 -std=c++1z && ./a.out 113 | and 114 | 22 115 | 63 116 | 147 117 | 63 118 | 349 119 | */ 120 | -------------------------------------------------------------------------------- /bitwise/is_valid_utf8_array/README.md: -------------------------------------------------------------------------------- 1 | ### Out of Blue 2 | 3 | #### UNIX: Making Computers Easier To Use -- AT&T Archives film from 1982, Bell Laboratories 4 | 5 | UNIX: Making Computers Easier To Use on youtube 8 | 9 | --- 10 | ### Problem 11 | 12 | *Write a function that return true if the array is valid utf-8 array, false if not* 13 | 14 | ##### Prototype 15 | ```c++ 16 | bool validUtf8(vector& data) 17 | ``` 18 | 19 | Refer to wikipedia for utf-8 spec : https://en.wikipedia.org/wiki/UTF-8 20 | 21 |
 22 | 
 23 | 
 24 | 
 25 | 
 26 | 
 27 | 
 28 | 
 29 | 
 30 | 
 31 | 
 32 | 
 33 | 
 34 | 
 35 | 
 36 | 
 37 | 
 38 | 
 39 | 
 40 | 
 41 | 
 42 | 
 43 | 
 44 | 
 45 | 
 46 | 
 47 | 
 48 | 
 49 | 
 50 | 
51 | 52 | 53 | ### Solution 54 | 55 | [One solution using bitmask](https://leetcode.com/problems/utf-8-validation/discuss/528963/C%2B%2B-two-4-ms-solutions-using-bitset-and-bit-mask): 56 | ```c++ 57 | bool validUtf8(vector& data) { 58 | int count = 0; 59 | for(const int b: data) { 60 | if(count) { 61 | if((b&m1) != m1) return false; 62 | --count; 63 | } 64 | else { 65 | if((b&m5) == m5) return false; 66 | if((b&m4) == m4) count = 3; 67 | else if((b&m3) == m3) count = 2; 68 | else if((b&m2) == m2) count = 1; 69 | else if((b&m1) == m1) return false; 70 | } 71 | } 72 | return count == 0; 73 | } 74 | const int m5 = 0b11111000; // invalid 75 | const int m4 = 0b11110000; // 3 continuation bytes 76 | const int m3 = 0b11100000; // 2 77 | const int m2 = 0b11000000; // 1 78 | const int m1 = 0b10000000; // invalid 79 | ``` 80 | 81 | However, although it is good to understand the logic, there is nothing faster than lookup tables when it comes to comparison. 82 | 83 | 84 | ### Solution using lookup table 85 | 86 | First generate a lookup table with the following function: 87 | 88 | ```c 89 | const int m5 = 0b11111000; 90 | const int m4 = 0b11110000; 91 | const int m3 = 0b11100000; 92 | const int m2 = 0b11000000; 93 | const int m1 = 0b10000000; 94 | int n ; 95 | for (int b = 0; b < 256; b++) { 96 | if (!(b & 0b10000000)) n = 1; 97 | else if((b&m5) == m5) n = 0; 98 | else if((b&m4) == m4) n = 4; 99 | else if((b&m3) == m3) n = 3; 100 | else if((b&m2) == m2) n = 2; 101 | else if((b&m1) == m1) n = 0; 102 | printf("%d,", n); 103 | if ((b & 0x1f) == 0x1f) 104 | printf("\n"); 105 | } 106 | ``` 107 | 108 | Then use it: 109 | ```c++ 110 | static inline const int utf8d[256] = { 111 | 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 112 | 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 113 | 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 114 | 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 115 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 116 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 117 | 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, 118 | 3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,4,4,4,4,4,4,4,4,0,0,0,0,0,0,0,0, 119 | }; 120 | 121 | bool validUtf8(vector& data) { 122 | int count = 1; 123 | int *ptr = &data[0]; 124 | int len = data.size(); 125 | while (len--) { 126 | if (!--count) 127 | count = utf8d[*ptr]; 128 | else if (*ptr <= 127) 129 | return false; 130 | ptr++; 131 | } 132 | return count == 1; 133 | } 134 | ``` 135 | -------------------------------------------------------------------------------- /misc/sum_of_k/benchmark.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | 14 | using namespace std; 15 | 16 | auto timeFuncInvocation = 17 | [](auto&& func, auto&&... params) { 18 | // get time before function invocation 19 | const auto& start = chrono::high_resolution_clock::now(); 20 | // function invocation using perfect forwarding 21 | std::forward(func)(std::forward(params)...); 22 | // get time after function invocation 23 | const auto& stop = chrono::high_resolution_clock::now(); 24 | return (stop - start)/100000/*largeNumber*/; 25 | }; 26 | 27 | vector> sum_of_k(vector& array, int target, int k, int j); 28 | 29 | vector> sum_of_k_init(vector& array, int target, int k) { 30 | if (array.size() < k) 31 | return vector> {0}; // protection 32 | 33 | sort(array.begin(), array.end()); 34 | return sum_of_k(array, target, k, 0); 35 | } 36 | 37 | // DFS 38 | vector> sum_of_k(vector& array, int target, int k, int j) { 39 | vector> res; 40 | 41 | if (k == 2) // end of the recursive call when k = 2, we look for the two last integers 42 | { 43 | vector tmp; 44 | int k = array.size() - 1; 45 | 46 | while (j < k) 47 | { 48 | int total = array[j] + array[k]; 49 | if (total < target) 50 | j++; 51 | else if (total > target) 52 | k--; 53 | else 54 | { 55 | tmp.clear(); // essential step 56 | tmp.push_back(array[j]); 57 | tmp.push_back(array[k]); 58 | res.push_back(tmp); // array[j] is the penultimate value and array[k] the ultimate value we are looking for 59 | while (array[j] == array[j++ + 1] && j < array.size() - 1) ; 60 | while (array[k] == array[k-- - 1] && k > j) ; 61 | } 62 | 63 | } 64 | return res; 65 | } 66 | 67 | else 68 | { 69 | for (int i = j; i < array.size() - k + 1; i++) 70 | { 71 | vector> t; 72 | int tmp = array[i]; 73 | 74 | if (i > j && array[i - 1] == tmp) // Skip duplicates 75 | continue; 76 | 77 | t = sum_of_k(array, target - tmp, k - 1, i + 1); // core recursive call 78 | 79 | for (int j = 0; j < t.size(); j++) 80 | { 81 | t[j].push_back(tmp); 82 | res.push_back(t[j]); 83 | } 84 | } 85 | return res; 86 | } 87 | } 88 | 89 | #define TARGET 87 90 | 91 | int main(void) 92 | { 93 | // First create an instance of an engine. 94 | random_device rnd_device; 95 | // Specify the engine and distribution. 96 | mt19937 mersenne_engine {rnd_device()}; // Generates random integers 97 | uniform_int_distribution dist {0, INT_MAX}; // between 0 and INT_MAX 98 | 99 | auto gen = [&dist, &mersenne_engine](){ 100 | return dist(mersenne_engine); 101 | }; 102 | 103 | vector v(1000); 104 | generate(begin(v), end(v), gen); 105 | 106 | std::cout << timeFuncInvocation(sum_of_k_init, v, TARGET, 4).count() << std::endl; 107 | 108 | 109 | /* 110 | vector v2(100000); 111 | generate(begin(v2), end(v2), gen); 112 | std::cout << timeFuncInvocation(number_of_power_of_three, v2, 1).count() << std::endl; 113 | */ 114 | } 115 | -------------------------------------------------------------------------------- /math/is_power_of_three/README.md: -------------------------------------------------------------------------------- 1 | ### Out of Blue 2 | 3 | ##### Spectre and Meltdown attacks 4 | 5 | How do Spectre and Meltdown work? 6 | To understand Spectre, you need to grasp the basics of how modern computer processors work. 7 | 8 | Modern processors accelerate the rate at which they execute instructions by loading data into the processor's on-board cache memory ahead of when it's needed. Data can be retrieved from this on-board cache far more rapidly than from the computer's main memory. 9 | 10 | If a processor is executing a set of instructions that branches depending on the input, then processors will try to guess which branch of instructions is most likely to be executed and load the necessary data into the processor's cache. These processes, called Branch Prediction and Speculative Execution, are what can be exploited by Spectre attacks. The attacker manipulates the processor so it loads a value from protected memory into the cache. They then follow up by attempting to load known data from unprotected memory. If one piece of this known data loads far more rapidly than the others, then they can infer that this data is being retrieved from the cache, and therefore is related to the value stored in protected memory. 11 | 12 | Meltdown works slightly differently, taking advantage of a privilege escalation flaw that allows any user able to execute code on the system to access protected memory. This has the effect of neutralizing security models based on address space isolation and paravirtualized software containers. 13 | 14 | ##### New variantes discovered, credit : Graz University of Technology, imec-DistriNet, KU Leuven, & the College of William and Mary 15 | 16 | ![Alt text](new-meltdown-spectre-attacks.png?raw=true "Optional Title") 17 | 18 | **New Meltdown attacks** 19 | 20 | The new Meltdown attacks that the research team discovered are: 21 | 22 | * Meltdown-BR - exploits an x86 bound instruction on Intel and AMD 23 | * Meltdown-PK - bypasses memory protection keys on Intel CPUs 24 | 25 | They also tried and failed to exploit other Meltdown attacks that targeted the following internal CPU operations: 26 | 27 | * Meltdown-AC - tried to exploit memory alignment check exceptions 28 | * Meltdown-DE - tried to exploit division (by zero) errors 29 | * Meltdown-SM - tried to exploit the supervisor mode access prevention (SMAP) mechanism 30 | * Meltdown-SS - tried to exploit out-of-limit segment accesses 31 | * Meltdown-UD - tried to exploit invalid opcode exception 32 | * Meltdown-XD - tried to exploit non-executable memory 33 | 34 | [...] 35 | -- 36 | --- 37 | 38 | ### Problem 39 | 40 | *Write a function that, given an uint32_t n, returns true if n is a power of 3 and false otherwise.* 41 | 42 | ##### Prototype 43 | ```c++ 44 | bool is_power_of_three(uint32_t n) { ;} 45 | ``` 46 | 47 | ##### For example 48 | * Consider n = 25, n = 0 or n = 34, the function should return false. 49 | * If n = 27 the function should return true. 50 | 51 | 52 |
53 | 
54 | 
55 | 
56 | 
57 | 
58 | 
59 | 
60 | 
61 | 
62 | 
63 | 
64 | 
65 | 
66 | 
67 | 
68 | 
69 | 
70 | 
71 | 
72 | 
73 | 
74 | 
75 | 
76 | 
77 | 
78 | 
79 | 
80 | 
81 | 
82 | 83 | 84 | ### Solution 85 | ```c++ 86 | return n > 0 && !(3486784401 % n); // only word with prime numbers 87 | ``` 88 | 89 | *Explanation : 0 is not a power of 3 and it also prevents to use mod 0 (% 0) to avoid undefined behavior* 90 | > [...] If the second operand of / or % is zero the behavior is undefined; [...] 91 | 92 | *3486784401, equivalent to 3^20 is the maximum value possible for uint32_t (1162261467, 3^19 for int32_t) before overflow (gets bigger than UINT_MAX or INT_MAX if signed integer int32_t).* 93 | 94 | *As 3 is a prime number it means that n has to be a multiple of 3486784401 (you can only reach this number by continuously multiplying by 3), hence if n is a power of three 3486784401 % n must equal 0.* 95 | -------------------------------------------------------------------------------- /math/the_dancer/README.md: -------------------------------------------------------------------------------- 1 | ### Out of Blue 2 | 3 | ##### Fast inverse square root 4 | The following code is the [fast inverse square root](https://en.wikipedia.org/wiki/Fast_inverse_square_root) implementation from Quake III Arena, stripped of C preprocessor directives, but including the exact original comment text: 5 | ```c 6 | float Q_rsqrt( float number ) 7 | { 8 | long i; 9 | float x2, y; 10 | const float threehalfs = 1.5F; 11 | 12 | x2 = number * 0.5F; 13 | y = number; 14 | i = * ( long * ) &y; // evil floating point bit level hacking 15 | i = 0x5f3759df - ( i >> 1 ); // what the fuck? 16 | y = * ( float * ) &i; 17 | y = y * ( threehalfs - ( x2 * y * y ) ); // 1st iteration 18 | // y = y * ( threehalfs - ( x2 * y * y ) ); // 2nd iteration, this can be removed 19 | 20 | return y; 21 | } 22 | ``` 23 | **This algorithm computes ​1⁄√x by performing the following steps:** 24 | 25 | * Alias the argument x to an integer, as a way to compute an approximation of log2(x) 26 | * Use this approximation to compute an approximation of log2(​1⁄√x) 27 | * Alias back to a float, as a way to compute an approximation of the base-2 exponential 28 | * Refine the approximation using a single iteration of the Newton's method. 29 | 30 | ### Problem 31 | 32 | *Write a function that, given an integer representing the time elapsed since the beginning of the dance, returns the final position of the dancer.* 33 | 34 | ##### Prototype 35 | ```c++ 36 | int32_t dancer_position(uint32_t time_elapsed) { ;} 37 | ``` 38 | 39 | ##### For example 40 | This dance follow a precise pattern: 41 | Time 0 : The dancer is at position 0. 42 | Time 1 : Take one step forward (+1 step) 43 | Time 2 : Take two steps backward (-2 steps) 44 | 45 | To follow, the steps as well as the direction you will have to take in your next move will each time be obtained thanks to a specific calculation: the number of steps you took on the previous time minus the number of steps you took on the penultimate time. 46 | 47 | That is, on time 3, a dancer will have to take 3 steps backwards (-2 - 1). 48 | 49 |
 50 | 
 51 | 
 52 | 
 53 | 
 54 | 
 55 | 
 56 | 
 57 | 
 58 | 
 59 | 
 60 | 
 61 | 
 62 | 
 63 | 
 64 | 
 65 | 
 66 | 
 67 | 
 68 | 
 69 | 
 70 | 
 71 | 
 72 | 
 73 | 
 74 | 
 75 | 
 76 | 
 77 | 
 78 | 
79 | 80 | ### Get started 81 | I asked this question to my students specialized in econometric and computer science. They were struggling to design a recursive solution or an iterative one. 82 | 83 | However this problem checks if you are able to leave your keyboard, take a pen and a blank sheet to think about the pattern and notice similarities. 84 | 85 | At time 3, the dancer is at position -4. time(n) = time(n-1) - time(n-2) pos(n) = pos(n-1) + time(n) 86 | 87 | At time 4, the dancer is at position -5. 88 | 89 | 1) We notice the it moves repeatedly the following way: / 1 -2 -3 -1 2 3 repeat 1 -2 -3 -1 2 3 repeat 1 -2 .../ 90 | 91 | 2) It results in the following position having a cycle every 6 steps, see table below : 92 | 93 | | Starting Position | Movement | Final Position 94 | |---|---|---| 95 | |0|1|1 96 | |1|-2|-1 97 | |-1|-3|-4 98 | |-4|-1|-5 99 | |-5|2|-3 100 | |-3|3|0 101 | |0|1|1 102 | 103 | 104 | 105 | *NB: To calculate the current position we take the last position and add the dancer's steps.* 106 | 107 | ### C++ Solution 108 | 109 | Hence we only need to put it to put the 6 repeating positions (0,1,−1,−4.−5,−3,…) in an array and return the given number modulo 6. 110 | 111 | ```c++ 112 | const int a[6] = {0, 1, -1, -4, -5, -3}; 113 | ​ 114 | int32_t dancer_position(uint32_t time_elapsed) { 115 | return a[time_elapsed % 6]; 116 | } 117 | ``` 118 | 119 | None of my students found the most optimized solution and some students complained that the solution was too simple. Yet this "simple solution" was elegant and O (1). 120 | -------------------------------------------------------------------------------- /misc/sum_of_three/README.md: -------------------------------------------------------------------------------- 1 | ### Problem 2 | 3 | *Write a function that, given an array with unsorted integers, returns all unique triplets in the array whose sum equals zero.* 4 | 5 | ##### Prototype 6 | ```c++ 7 | vector> sum_of_three(vector& array) { ;} 8 | ``` 9 | 10 | ##### For example 11 | * Consider an array containing {-1,0,1,2,-1,-4}, the function should return a vector containing 2 vectors: {-1,-1,2} and {-1,0,1}. 12 | 13 | ##### Assumption 14 | * The solution set must not contain duplicate triplets. 15 | 16 |
 17 | 
 18 | 
 19 | 
 20 | 
 21 | 
 22 | 
 23 | 
 24 | 
 25 | 
 26 | 
 27 | 
 28 | 
 29 | 
 30 | 
 31 | 
 32 | 
 33 | 
 34 | 
 35 | 
 36 | 
 37 | 
 38 | 
 39 | 
 40 | 
 41 | 
 42 | 
 43 | 
 44 | 
 45 | 
46 | 47 | 48 | ### Solution 49 | ```c++ 50 | #include 51 | #define TARGET 0 52 | 53 | vector> sum_of_three(vector& array) { 54 | if (array.size() == 0) return {}; // protection against empty array 55 | 56 | vector> ret; // what we will return 57 | int begin, end, sum; 58 | 59 | sort(array.begin(), array.end()); 60 | 61 | for (int i = 0; array[i] <= 0 && i < array.size(); i++) 62 | { 63 | if (i > 0 && array[i] == array[i - 1]) 64 | continue; 65 | begin = i + 1; 66 | end = array.size() - 1; 67 | 68 | while (begin < end) 69 | { 70 | sum = array[begin] + array[end] + array[i]; 71 | if (sum < TARGET) 72 | begin++; 73 | else if (sum > TARGET) 74 | end--; 75 | else 76 | { 77 | ret.push_back({array[i], array[begin], array[end]}); 78 | 79 | while (begin < end && array[begin] == ret.back()[1]) 80 | begin++; 81 | } 82 | } 83 | } 84 | 85 | return ret; 86 | } 87 | ``` 88 | 89 | *This search will perform in O(N^2).* 90 | 91 | *Alternatively we can squeeze out 10% extra performance by extracting from the loop the ```(i > 0)``` added before ```if (array[i] == array[i - 1])``` and check for the very first item in order to avoid performing a check for each array's item.* 92 | 93 | ```c++ 94 | #include 95 | #define TARGET 0 96 | 97 | vector> sum_of_three(vector& array) { 98 | if (array.size() == 0) return {}; 99 | 100 | int begin, end, sum; 101 | vector> ret; 102 | 103 | sort(array.begin(), array.end()); 104 | 105 | if (array[0] <= 0 && 0 < array.size()) 106 | { 107 | begin = 1; 108 | end = array.size() - 1; 109 | 110 | while (begin < end) 111 | { 112 | sum = array[begin] + array[end] + array[0]; 113 | if (sum < TARGET) 114 | begin++; 115 | else if (sum > TARGET) 116 | end--; 117 | else 118 | { 119 | ret.push_back({array[0], array[begin], array[end]}); 120 | 121 | while (begin < end && array[begin] == ret.back()[1]) 122 | begin++; 123 | } 124 | } 125 | } 126 | 127 | for (int i = 1; array[i] <= 0 && i < array.size(); i++) 128 | { 129 | if (array[i] == array[i - 1]) 130 | continue; 131 | begin = i + 1; 132 | end = array.size() - 1; 133 | 134 | while (begin < end) 135 | { 136 | sum = array[begin] + array[end] + array[i]; 137 | if (sum < TARGET) 138 | begin++; 139 | else if (sum > TARGET) 140 | end--; 141 | else 142 | { 143 | ret.push_back({array[i], array[begin], array[end]}); 144 | 145 | while (begin < end && array[begin] == ret.back()[1]) 146 | begin++; 147 | } 148 | } 149 | } 150 | 151 | return ret; 152 | } 153 | ``` 154 | -------------------------------------------------------------------------------- /hashmap/group_anagram/README.md: -------------------------------------------------------------------------------- 1 | ### Out of Blue 2 | 3 | ##### Plasmodium 4 | 5 | *Plasmodium is a genus of unicellular eukaryotes that are obligate parasites of vertebrates and insects. The life cycles of Plasmodium species involve development in a blood-feeding insect host which then injects parasites into a vertebrate host during a blood meal. Parasites grow within a vertebrate body tissue (often the liver) before entering the bloodstream to infect red blood cells. The ensuing destruction of host red blood cells can result in disease, called malaria. During this infection, some parasites are picked up by a blood-feeding insect, continuing the life cycle.* 6 | 7 | *Plasmodium is a member of the phylum Apicomplexa, a large group of parasitic eukaryotes. Within Apicomplexa, Plasmodium is in the order Haemosporida and family Plasmodiidae. Over 200 species of Plasmodium have been described, many of which have been subdivided into 14 subgenera based on parasite morphology and host range. Evolutionary relationships among different Plasmodium species do not always follow taxonomic boundaries; some species that are morphologically similar or infect the same host turn out to be distantly related.* 8 | 9 | *Species of Plasmodium are distributed globally wherever suitable hosts are found. Insect hosts are most frequently mosquitoes of the genera Culex and Anopheles. Vertebrate hosts include reptiles, birds, and mammals. Plasmodium parasites were first identified in the late 19th century by Charles Laveran. Over the course of the 20th century, many other species were discovered in various hosts and classified, including five species that regularly infect humans: P. vivax, P. falciparum, P. malariae, P. ovale, and P. knowlesi. P. falciparum is by far the most lethal in humans, resulting in hundreds of thousands of deaths per year. A number of drugs have been developed to treat Plasmodium infection; however, the parasites have evolved resistance to each drug developed.* 10 | 11 | ### Problem 12 | 13 | *Write a function that, given an array of strings, group the strings that are anagrams of each others and return them as an array of array of string. The order in which group of anagrams are return do not matter. 14 | 15 | ##### Assumptions 16 | 17 | *Words length is reasonable* 18 | 19 | ##### Prototype 20 | ```c++ 21 | vector> group_anagrams(vector& strs){;} 22 | ``` 23 | 24 | ##### For example 25 | *If vector& strs contains {"tea, eat, hello"} then it should return a vector containing {{"tea, eat"}, {"hello"}}* 26 | 27 |
28 | 
29 | 
30 | 
31 | 
32 | 
33 | 
34 | 
35 | 
36 | 
37 | 
38 | 
39 | 
40 | 
41 | 
42 | 
43 | 
44 | 
45 | 
46 | 
47 | 
48 | 
49 | 
50 | 
51 | 
52 | 
53 | 
54 | 
55 | 
56 | 
57 | 58 | ### C++ Solution 59 | ```c++ 60 | static int lambda_0 = []() { std::ios::sync_with_stdio(false); cin.tie(NULL); return 0; }(); // small optimization to get faster runtime 61 | 62 | uint32_t get_hash(const string &word) { 63 | static int primes[26] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101}; // the 26 first integers 64 | 65 | uint32_t hash = 1; 66 | for (char c : word) { 67 | int prime = primes[c - 'a']; // each letter will have a prime number assigned 68 | hash *= prime; // multiplication order does not matter since we are dealing with prime numbers 69 | } 70 | return hash; // it will generate a unique Hash value 71 | } 72 | 73 | vector> group_anagrams(vector& strs) { 74 | unordered_map> anagram_hash_map; 75 | anagram_hash_map.reserve(strs.size() >> 3); // small optimization, play with the value that matches the average number of group of anagrams expected 76 | 77 | for (string& s : strs) 78 | anagram_hash_map[get_hash(s)].push_back(s); // we get the hash for the string and it will become 79 | 80 | vector> res; 81 | 82 | for (auto value : anagram_hash_map) // each group of anagrams has the same key, we return the value to obtain the vector 83 | res.push_back(value.second); 84 | 85 | return res; 86 | } 87 | ``` 88 | 89 | ![Speed](https://raw.githubusercontent.com/agavrel/Nailing-the-Coding-Interview/master/hashmap/group_anagram/group_anagrams.png) 90 | -------------------------------------------------------------------------------- /bitwise/match_maker/README.md: -------------------------------------------------------------------------------- 1 | ### Out of Blue 2 | 3 | ##### Anri Shyness Boy 4 | 5 | Anri Shyness Boy on youtube 8 | 9 | Natsu no party de koe wo 10 | 夏の Partyで声を 11 | *Your voice at a summer party* 12 | 13 | Kakete kita no wa anata no Hou kara yo 14 | かけてきたのは あなたの方からよ 15 | *I called you, because I'd rather listen to you* 16 | 17 | Itsumo denwa no koe wa 18 | いつも電話の声は 19 | *I always hear your voice on the phone* 20 | 21 | Arikitari na koto bakari itteru no 22 | ありきたりな事ばかり 言ってるの 23 | *Are you saying those things already?* 24 | 25 | You are my shyness boy Matteru no yo 26 | You are my shyness boy 待ってるのよ 27 | *You are my shyness boy, I am waiting for* 28 | 29 | Sasoi no kotoba 30 | 誘いの言葉 31 | *Those inviting words* 32 | 33 | Modokashisa nara kyou Kagiri ni Shite hoshii 34 | もどかしさなら今日かぎりにしてほしい 35 | *If things are moving to slow, I want to see the end today* 36 | 37 | I'm falling love・・・! 38 | 39 | Yoru no Doraivu no ato de 40 | 夜のドライヴの後で 41 | *After driving late at night* 42 | 43 | Osoi kara okuru yo nante Anmari ne 44 | 遅いから送るよなんて あんまりね 45 | *I'm sending you a hint because you're too late, aren't you?* 46 | 47 | Kyou wa Asa made zutto 48 | 今日は朝までずっと 49 | *Today until morning* 50 | 51 | Isshoni irareru you ni Shite aru no 52 | 一緒にいられるようにしてあるの 53 | *It seems like we will have been together for so long* 54 | 55 | You are my shyness boy jirettai no 56 | You are my shyness boy じれったいの 57 | *You are my shyness boy, your love* 58 | 59 | Anata no ai wa 60 | あなたの愛は 61 | *Is so vexing* 62 | 63 | Tabako motsu te mo furueteru wa 64 | タバコ持つ手もふるえてるわ 65 | *The hand holding that cigarette is trembling* 66 | 67 | Dame na hito! 68 | ダメな人!! 69 | *You better watch out!!* 70 | 71 | You are my shyness boy Matteru no yo 72 | You are my shyness boy 待ってるのよ 73 | *You are my shyness boy, I am waiting for* 74 | 75 | Kimari-monku wo 76 | きまり文句を 77 | *It happens that your eyes* 78 | 79 | Yasashisugiru anata no hitomi 80 | やさしすぎるあなたの瞳 81 | *Have the right thing to say* 82 | 83 | Soko ga suki!! 84 | そこが好き!! 85 | *I like that!!* 86 | 87 | You are my shyness boy Matteru no yo 88 | You are my shyness boy 待ってるのよ 89 | *You are my shyness boy, your love* 90 | 91 | Sasoi no kotoba 92 | 誘いの言葉 93 | *Is so vexing* 94 | 95 | Modokashisa nara kyou Kagiri ni Shite hoshii 96 | もどかしさなら今日かぎりにしてほしい 97 | *The hand holding that cigarette is trembling* 98 | 99 | You are my shyness boy matteru no yo 100 | You are my shyness boy 待ってるのよ 101 | *You are my shyness boy, I am waiting for* 102 | 103 | Kimari-monku wo 104 | きまり文句を 105 | *It happens that your eyes* 106 | 107 | Yasashisugiru anata no hitomi 108 | やさしすぎるあなたの瞳 109 | *Have the right thing to say* 110 | 111 | Soko ga suki!! 112 | そこが好き!! 113 | *I like that!!* 114 | 115 | ### Problem 116 | 117 | *Write a function that, given two arrays (or vectors), will return the maximum match possible between both. Values from first array can only match another single one the other array when they both have at least 1 common bit set.* 118 | 119 | ##### Prototype 120 | ```c++ 121 | int match_maker(vector v1, vector v2) { ; } 122 | ``` 123 | 124 | ##### For example 125 | * For vector v1 = {1, 3} and vector v2 = {2, 5} the function would return 2 as we can match 1 with 5 (first bit) and 3 with 2 (second bit) 126 | 127 | * For vector v1 = {16, 3, 1} and vector v2 = {3, 6, 17} the function would return 3 as 16 matches 17 (5th bit), 3 matches 6 (second bit), 1 matches 3 (first bit) 128 | 129 | ##### Write the most efficient algorithm for the following assumptions: 130 | * Both array have the same size 131 | 132 |
133 | 
134 | 
135 | 
136 | 
137 | 
138 | 
139 | 
140 | 
141 | 
142 | 
143 | 
144 | 
145 | 
146 | 
147 | 
148 | 
149 | 
150 | 
151 | 
152 | 
153 | 
154 | 
155 | 
156 | 
157 | 
158 | 
159 | 
160 | 
161 | 
162 | 163 | 164 | ### C/C++ Solution 165 | *WIP* 166 | 167 | ```c++ 168 | #include 169 | #include 170 | #include 171 | 172 | using namespace std; 173 | 174 | int main(void) 175 | { 176 | 177 | vector v1 = {1, 3, 4, 16}; 178 | vector v2 = {3, 6, 17, 4}; 179 | 180 | cout << "result :" << match_maker(v1, v2) << endl; // should return 4 181 | 182 | return 0; 183 | } 184 | ``` 185 | -------------------------------------------------------------------------------- /bitwise/number_complement/number_complement.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include // for clock() 4 | #include 5 | #include 6 | #include // for rand() 7 | 8 | // g++ -masm=intel a.cpp && ./a.out 9 | 10 | using namespace std; 11 | 12 | // https://leetcode.com/problems/number-complement/ 13 | // Given a positive integer, output its complement number. 14 | // The complement strategy is to flip the bits of its binary representation. 15 | 16 | int findComplement1(int num) { 17 | unsigned mask = ~0; 18 | while (num & mask) mask <<= 1; 19 | return ~mask & ~num; 20 | } 21 | 22 | // NB: function argument will either be stored either in edi (my cpu) or esi (leetcode) 23 | 24 | int findComplement2(int num) { 25 | __asm__ __volatile__ (R"( 26 | bsr ecx, edi 27 | 28 | neg ecx 29 | add ecx, 31 30 | mov eax, edi 31 | not eax 32 | shl eax, cl 33 | shr eax, cl 34 | ;)"); 35 | } 36 | 37 | int findComplement3(int num) { // NB: return 1 for num=0 38 | __asm__ __volatile__ (R"( 39 | bsr ecx, edi 40 | 41 | mov eax, 2 42 | shl eax, cl 43 | dec eax 44 | xor eax, edi 45 | ;)"); 46 | } 47 | 48 | int findComplement4(int num) { // NB: return 1 for num=0 49 | __asm__ __volatile__ (R"( 50 | bsr ecx, edi 51 | 52 | xor eax, eax 53 | dec eax 54 | shl eax, cl 55 | or eax, edi 56 | not eax 57 | ;)"); 58 | } 59 | 60 | 61 | /* 62 | int findComplement2(int num) { 63 | __asm__ __volatile__ (R"( 64 | bsr ecx, edi // get highest bit index from num and store it to ecx 65 | 66 | neg ecx // ecx = -ecx 67 | add ecx, 31 // ecx += 31 68 | 69 | mov eax, edi // our return value = num 70 | not eax // our return value = ~num 71 | shl eax, cl // rotate eax left then right 72 | shr eax, cl // in order to get the bits above index out 73 | ;)"); 74 | } 75 | 76 | int findComplement3(int num) { 77 | __asm__ (R"(.intel_syntax 78 | bsr ecx, esi // get highest bit index from num and store it to ecx 79 | 80 | inc ecx // ecx++ 81 | xor eax, eax // eax = 0 82 | inc eax // eax++ 83 | shl eax, cl // shift eax left by ecx bits 84 | dec eax // obtain final mask 85 | xor eax, esi // xor with argument to obtain complement 86 | ;)"); 87 | return num; 88 | } 89 | 90 | int findComplement4(int num) { 91 | __asm__ (R"(.intel_syntax 92 | bsr ecx, esi // get highest bit index 93 | 94 | xor eax, eax // eax = 0 95 | dec eax // eax = 0 - 1 = 0bFFFFFFFF (mask) 96 | shl eax, cl // remove cl most significant bits 97 | or eax, esi // combine common bits to find complement 98 | not eax // flip bits to obtain complement 99 | ;)"); 100 | return num; 101 | } 102 | */ 103 | 104 | #define N 1000000 105 | 106 | float benchmark(int (*f)(int)) { 107 | clock_t start = clock(); 108 | //static int test_number = 0; 109 | 110 | for (int i{}; i != N; ++i) 111 | f(rand()); 112 | 113 | clock_t end = clock(); 114 | float elapsed = (end - start) * (float)1000000000 / (CLOCKS_PER_SEC * N); 115 | // printf("Test %d tooks a total of %.02f on average\n", ++test_number, elapsed); 116 | return elapsed; 117 | } 118 | 119 | 120 | int main() { 121 | srand (time (NULL)); 122 | 123 | int (*func_ptr[])(int) = {&findComplement1, &findComplement2, &findComplement3, &findComplement4}; 124 | int len = sizeof(func_ptr) / sizeof(void*); 125 | 126 | /* for (int a = 0; a < 10; a++) { 127 | cout << "Testing with: " << a << endl; 128 | for (int i = 0; i < len; i++) 129 | cout << func_ptr[i](a) << endl; 130 | } 131 | */ 132 | int battle = 100; 133 | float time_elapsed[len]; 134 | int score[len] = {}; 135 | while (battle--) { 136 | for (int i = 0; i < len; i++) { 137 | time_elapsed[i] = benchmark(func_ptr[i]); 138 | 139 | } 140 | int a = 0; 141 | float lowest_latency = __FLT_MAX_EXP__; 142 | for (int i = 0; i < len; i++) { 143 | if (time_elapsed[i] < lowest_latency) { 144 | lowest_latency = time_elapsed[i]; 145 | a = i; 146 | } 147 | 148 | } 149 | score[a]++; 150 | } 151 | 152 | for (int i = 0; i < len; ++i) { 153 | printf("Function %d scored %d\n", i, score[i]); 154 | } 155 | 156 | /* 157 | Function 0 scored 0 158 | Function 1 scored 3 159 | Function 2 scored 43 160 | Function 3 scored 54 161 | */ 162 | 163 | return 0; 164 | } 165 | -------------------------------------------------------------------------------- /hashmap/target_sum/README.md: -------------------------------------------------------------------------------- 1 | ### Out of Blue 2 | 3 | ##### SSE instructions 4 | 5 | *Streaming SIMD Extensions (SSE) is a single instruction, multiple data (SIMD) instruction set extension to the x86 architecture, designed by Intel* 6 | 7 | ```c++ 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | 14 | using namespace std; 15 | 16 | void normal(float* a, uint32_t N) 17 | { 18 | for (uint32_t i = 0; i < N; ++i) 19 | a[i] = sqrt(a[i]); 20 | } 21 | 22 | void sse_original(float* a, uint32_t N) 23 | { 24 | // We assume N % 4 == 0. 25 | int nb_iters = N / 4; 26 | __m128* ptr = (__m128*)a; 27 | 28 | for (uint32_t i = 0; i < nb_iters; ++i, ++ptr, a += 4) 29 | _mm_store_ps(a, _mm_sqrt_ps(*ptr)); 30 | } 31 | 32 | void sse(float* a, uint32_t N) 33 | { 34 | __m128* ptr = (__m128*)a; // use 128 bits register 35 | 36 | do 37 | { 38 | _mm_store_ps(a, _mm_sqrt_ps(*ptr++)); 39 | a += 4; 40 | } 41 | while (N--); 42 | } 43 | 44 | uint32_t ft_align_mask(uint32_t size, uint32_t mask) 45 | { 46 | return ((size + mask) & ~mask); 47 | } 48 | 49 | int main(int argc, char** argv) 50 | { 51 | /*if (argc != 2) 52 | return 1; 53 | uint32_t N = ft_align_mask(atoi(argv[1]), 3); // align N on 4*/ 54 | 55 | float* a; 56 | posix_memalign((void**)&a, 16, N * sizeof(float)); 57 | 58 | for (uint32_t i = 0; i < N; ++i) 59 | a[i] = 3141592.65358; 60 | { 61 | clock_t t = clock(); 62 | normal(a, N); 63 | t = clock() - t; 64 | printf ("No SSE: %lu clocks\n",t); 65 | } 66 | 67 | for (uint32_t i = 0; i < N; ++i) 68 | a[i] = 3141592.65358; 69 | { 70 | clock_t t = clock(); 71 | sse_original(a, N); 72 | t = clock() - t; 73 | printf ("Original SSE: %lu clocks\n",t); 74 | } 75 | 76 | for (uint32_t i = 0; i < N; ++i) 77 | a[i] = 3141592.65358; 78 | { 79 | clock_t t = clock(); 80 | sse(a, N >> 2); 81 | t = clock() - t; 82 | printf ("Optimized SSE: %lu clocks\n",t); 83 | } 84 | } 85 | ``` 86 | 87 | --- 88 | ##### Benchmark result 89 | *g++ sse_benchmark.cpp && ./a.out 64000000* 90 | 91 | * No SSE: 255000 clocks 92 | * Original SSE: 41500 clocks // credit : [Félix Abecassis](https://felix.abecassis.me/2011/09/cpp-getting-started-with-sse/) 93 | * Optimized SSE: 38500 clocks // credit : agavrel, 3000 clocks saved, 8% increased speed 94 | 95 | --- 96 | 97 | ##### Adding -O2 flag will also tremendously increase speed 98 | 99 | *g++ -O2 sse_benchmark.cpp && ./a.out 64000000* 100 | * No SSE: 198000 clocks 101 | * Original SSE: 22000 clocks 102 | * Optimized SSE: 20000 clocks 103 | * Optimized SSE 256 bits registers : 19400 104 | 105 | 106 | ##### Resulting assembly ouput of the sse critical loops: 107 | ```assembly 108 | .ORIGINAL_SSE 109 | addl $1, %edx // this increase time by 8-10% compared to optimized version below 110 | sqrtps (%rax), %xmm0 111 | addq $16, %rax 112 | movaps %xmm0, -16(%rax) 113 | cmpl %r13d, %edx 114 | jb .ORIGINAL_SSE // jump_below 115 | 116 | 117 | .OPTIMIZED_SSE 118 | sqrtps (%rax), %xmm0 119 | addq $16, %rax 120 | movaps %xmm0, -16(%rax) 121 | cmpq %rax, %rdx 122 | jne .OPTIMIZED_SSE // jump_not_equal will be more effective 123 | ``` 124 | 125 | *PS: To know if computer support AVX2 (Advanced Vector Extensions) instructions: grep avx2 /proc/cpuinfo* 126 | 127 | ### Problem 128 | 129 | *Write a function that, given an array of integers, return indices of the two numbers whose sum equals the target.* 130 | 131 | ##### Assumptions 132 | 133 | *Unique solution, and can only use one element of the array once* 134 | 135 | ##### Prototype 136 | ```c++ 137 | vector target_index(vector& arr, int target){;} 138 | ``` 139 | 140 | ##### For example 141 | *If vector& arr contains {3, 5, 9, 4} and the target is 12 then it should return a vector containing {3, 9}* 142 | 143 |
144 | 
145 | 
146 | 
147 | 
148 | 
149 | 
150 | 
151 | 
152 | 
153 | 
154 | 
155 | 
156 | 
157 | 
158 | 
159 | 
160 | 
161 | 
162 | 
163 | 
164 | 
165 | 
166 | 
167 | 
168 | 
169 | 
170 | 
171 | 
172 | 
173 | 174 | ### C++ Solution 175 | ```c++ 176 | vector target_index(vector& arr, int target) { 177 | map map; 178 | 179 | for (int i = 0; i < arr.size(); i++) { 180 | int complement = target - arr[i]; // equivalent to complement + arr[i] = target 181 | if (map.count(complement)) // if complement was previously stored 182 | return vector {map[complement], i}; // then we found the solution, we return the index of the complement and the one of the current number 183 | map[arr[i]] = i; // else we store current index as value for the corresponding key (value of current number) in the hashmap 184 | } 185 | } 186 | ``` 187 | -------------------------------------------------------------------------------- /bitwise/least_operators_to_express_number/README.md: -------------------------------------------------------------------------------- 1 | ### Problem 2 | 3 | *Every number can be described in powers of 2. For example, 29 = 2^0 + 2^2 + 2^3 + 2^4. 4 | Write a function that, given an unsigned integer 0 < n < 10 000 000, returns the minimum number of additions and subtractions of 2^i to get n* 5 | 6 | ##### Prototype 7 | ```c 8 | int32_t minPowersOfTwo(int32_t n) {;} 9 | ``` 10 | 11 | ##### For example 12 | * Consider n = 0b1111 (15) the function should return 2: 0 + 0b10000 - 0b1 = 0b1111. 13 | * For n = 0b100 (4) the function should return 1: 0 + 0b100 14 | * For n = 0 the function should return 0. 15 | 16 | 17 |
 18 | 
 19 | 
 20 | 
 21 | 
 22 | 
 23 | 
 24 | 
 25 | 
 26 | 
 27 | 
 28 | 
 29 | 
 30 | 
 31 | 
 32 | 
 33 | 
 34 | 
 35 | 
 36 | 
 37 | 
 38 | 
 39 | 
 40 | 
 41 | 
 42 | 
 43 | 
 44 | 
 45 | 
 46 | 
47 | 48 | ### How to Start 49 | 1) As it involves getting a minimum we can already design a helper function to compute the minimum between two integers : 50 | 51 | ```c 52 | u_int32_t getMin(u_int32_t a, u_int32_t b) { 53 | return a < b ? a : b; 54 | } 55 | ``` 56 | 57 | 2) We then design a function that will give the complementary number of our integer n, which mean that this function will give the smallest number to make a power of two if we addition it with n 58 | 59 | ```c 60 | u_int32_t getComple(int32_t n) { 61 | _Bool is_power_of_two = !(n & (n - 1)); 62 | u_int32_t leading_zeros = __builtin_clz(n); 63 | return (1 << (32 - leading_zeros - is_power_of_two)) - n; 64 | } 65 | ``` 66 | 67 | ### Solution 68 | 69 | 3) We go through each number and check if the least significant bit is set with '& 1' (a). If it is set then we know that it will require an addition. It is the same for the comple_bit with will require a substraction. Finally we set add as the minimum between its current value and sub + 1 and reversely for sub. 70 | We then take care to eliminate the bit with '>>= 1' 71 | 72 | ``` 73 | int32_t minPowersOfTwo(int32_t n) { 74 | if (n == 0) // we assume no negative integers are used, but it could be 75 | return 0; 76 | u_int32_t comple = getComple(n); 77 | u_int32_t add = 0; 78 | u_int32_t sub = 0; 79 | 80 | while (n) { 81 | _Bool bit = n & 1; // a 82 | add += bit; 83 | _Bool comple_bit = comple & 1; // a 84 | sub += comple_bit; 85 | add = getMin(add, 1 + sub); 86 | sub = getMin(sub, 1 + add); 87 | n >>= 1; 88 | comple >>= 1; 89 | } 90 | 91 | return add; 92 | } 93 | ``` 94 | 95 | ### Another solution 96 | 97 | This solution is 8% faster: 98 | 99 | ```c 100 | int min_num_pow2(int n) { 101 | int ans = 0; 102 | int len = 0; 103 | 104 | for (int i = 0; i < 32; ++i) { 105 | if (n & (1 << i)) { 106 | ++len; 107 | 108 | } 109 | else if (len > 0) { 110 | ++ans; 111 | len = len == 1 ? 0 : 1; 112 | } 113 | } 114 | 115 | if (len > 1) 116 | ++ans; //Check for overflow. 117 | 118 | return ans; 119 | } 120 | ``` 121 | 122 | ### Another solution: Quadratic equation 123 | 124 | I found this interesting solution from another fellow developer (originally written in java): 125 | 126 | As 2 n^2 + n = v 127 | We can then write that 2 n^2 + n - v = 0 128 | We just have to solve the quadratic equation 129 | 130 | ```c 131 | #include 132 | 133 | double getMaxFromDoubleType(double a, double b) { 134 | return a > b ? a : b; 135 | } 136 | 137 | // return the results for √(b2 − 4ac) 138 | double root(double a, double b, double c){ 139 | return sqrt((b * b) - (4 * a * c)); 140 | } 141 | 142 | // *return the solutions of the quadratic equation for x = −b ± √(b2 − 4ac) / 2a. * 143 | void quadratic(double a, double b, double c, double result[2]){ 144 | double sqrt = root(a,b,c); 145 | double t2a = 2 * a; 146 | double x = (-b + sqrt) / t2a; 147 | double y = (-b - sqrt) / t2a; 148 | 149 | result[0] = x; 150 | result[1] = y; 151 | } 152 | 153 | // 2 n^2 + n = v -> 2 n^2 + n - v = 0 154 | void possibleNumbers(int v, double *result) { 155 | const int a = 2; 156 | const int b = 1; 157 | const int c = -1 * v; 158 | quadratic(a,b,c, result); 159 | } 160 | 161 | 162 | int minNum(int v){ 163 | double result[2]; 164 | possibleNumbers(v, &result); 165 | int x = (int)getMaxFromDoubleType(result[0], result[1]); 166 | return x; 167 | } 168 | ``` 169 | 170 | But I noticed that since s y will always be negative it can be discarded. 171 | Hence we do not neet to get the maximum number 172 | Some refactoring later: 173 | 174 | ```c 175 | #include 176 | int minPowerTwo2(int n){ return (-1 + sqrt(1 + (8 * n))) / 2; } 177 | ``` 178 | 179 | with gcc -O2 count_to_pw_of_2.c -lm && ./a.out it is about the same speed as the one with the moving mask on the 32 bits. 180 | 181 | 182 | 183 | ### Challenge 184 | Solve this problem to make it work with negative integers 185 | 186 | ### Credit 187 | This question was part of a Google On-Site Interview question as reported on [leetcode](https://leetcode.com/discuss/interview-question/371592/google-onsite-min-number-of-powers-of-2-to-get-an-integer) 188 | -------------------------------------------------------------------------------- /misc/sum_of_two/README.md: -------------------------------------------------------------------------------- 1 | ### Out of Blue 2 | 3 | ##### SSE instructions 4 | 5 | *Streaming SIMD Extensions (SSE) is a single instruction, multiple data (SIMD) instruction set extension to the x86 architecture, designed by Intel* 6 | 7 | ```c++ 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | 14 | using namespace std; 15 | 16 | void normal(float* a, uint32_t N) 17 | { 18 | for (uint32_t i = 0; i < N; ++i) 19 | a[i] = sqrt(a[i]); 20 | } 21 | 22 | void sse_original(float* a, uint32_t N) 23 | { 24 | // We assume N % 4 == 0. 25 | int nb_iters = N / 4; 26 | __m128* ptr = (__m128*)a; 27 | 28 | for (uint32_t i = 0; i < nb_iters; ++i, ++ptr, a += 4) 29 | _mm_store_ps(a, _mm_sqrt_ps(*ptr)); 30 | } 31 | 32 | void sse(float* a, uint32_t N) 33 | { 34 | __m128* ptr = (__m128*)a; // use 128 bits register 35 | 36 | do 37 | { 38 | _mm_store_ps(a, _mm_sqrt_ps(*ptr++)); 39 | a += 4; 40 | } 41 | while (N--); 42 | } 43 | 44 | uint32_t ft_align_mask(uint32_t size, uint32_t mask) 45 | { 46 | return ((size + mask) & ~mask); 47 | } 48 | 49 | int main(int argc, char** argv) 50 | { 51 | /*if (argc != 2) 52 | return 1; 53 | uint32_t N = ft_align_mask(atoi(argv[1]), 3); // align N on 4*/ 54 | 55 | float* a; 56 | posix_memalign((void**)&a, 16, N * sizeof(float)); 57 | 58 | for (uint32_t i = 0; i < N; ++i) 59 | a[i] = 3141592.65358; 60 | { 61 | clock_t t = clock(); 62 | normal(a, N); 63 | t = clock() - t; 64 | printf ("No SSE: %lu clocks\n",t); 65 | } 66 | 67 | for (uint32_t i = 0; i < N; ++i) 68 | a[i] = 3141592.65358; 69 | { 70 | clock_t t = clock(); 71 | sse_original(a, N); 72 | t = clock() - t; 73 | printf ("Original SSE: %lu clocks\n",t); 74 | } 75 | 76 | for (uint32_t i = 0; i < N; ++i) 77 | a[i] = 3141592.65358; 78 | { 79 | clock_t t = clock(); 80 | sse(a, N >> 2); 81 | t = clock() - t; 82 | printf ("Optimized SSE: %lu clocks\n",t); 83 | } 84 | } 85 | ``` 86 | 87 | --- 88 | ##### Benchmark result 89 | *g++ sse_benchmark.cpp && ./a.out 64000000* 90 | 91 | * No SSE: 255000 clocks 92 | * Original SSE: 41500 clocks // credit : [Félix Abecassis](https://felix.abecassis.me/2011/09/cpp-getting-started-with-sse/) 93 | * Optimized SSE: 38500 clocks // credit : agavrel, 3000 clocks saved, 8% increased speed 94 | 95 | --- 96 | 97 | ##### Adding -O2 flag will also tremendously increase speed 98 | 99 | *g++ -O2 sse_benchmark.cpp && ./a.out 64000000* 100 | * No SSE: 198000 clocks 101 | * Original SSE: 22000 clocks 102 | * Optimized SSE: 20000 clocks 103 | * Optimized SSE 256 bits registers : 19400 104 | 105 | 106 | ##### Resulting assembly ouput of the sse critical loops: 107 | ```assembly 108 | .ORIGINAL_SSE 109 | addl $1, %edx // this increase time by 8-10% compared to optimized version below 110 | sqrtps (%rax), %xmm0 111 | addq $16, %rax 112 | movaps %xmm0, -16(%rax) 113 | cmpl %r13d, %edx 114 | jb .ORIGINAL_SSE // jump_below 115 | 116 | 117 | .OPTIMIZED_SSE 118 | sqrtps (%rax), %xmm0 119 | addq $16, %rax 120 | movaps %xmm0, -16(%rax) 121 | cmpq %rax, %rdx 122 | jne .OPTIMIZED_SSE // jump_not_equal will be more effective 123 | ``` 124 | 125 | *PS: To know if computer support AVX2 (Advanced Vector Extensions) instructions: grep avx2 /proc/cpuinfo* 126 | 127 | ### Problem 128 | 129 | *Write a function that, given an array of integers, return indices of the two numbers whose sum equals the target.* 130 | 131 | ##### Assumptions 132 | 133 | *Unique solution, and can only use one element of the array once* 134 | 135 | ##### Prototype 136 | ```c++ 137 | vector sum_of_two(vector& v, int target) { 138 | ;} 139 | ``` 140 | 141 | ##### For example 142 | *If vector& v contains {3, 5, 9, 4} and the target is 12 then it should return a vector containing {3, 9}* 143 | 144 |
145 | 
146 | 
147 | 
148 | 
149 | 
150 | 
151 | 
152 | 
153 | 
154 | 
155 | 
156 | 
157 | 
158 | 
159 | 
160 | 
161 | 
162 | 
163 | 
164 | 
165 | 
166 | 
167 | 
168 | 
169 | 
170 | 
171 | 
172 | 
173 | 
174 | 175 | ### C++ Solution 176 | ```c++ 177 | static int lambda_0 = []() { std::ios::sync_with_stdio(false); cin.tie(NULL); return 0; }(); 178 | 179 | vector sum_of_two(vector& v, int target) { 180 | unordered_map map; // keys = values and values = indices 181 | 182 | for (int i = 0; i < v.size(); i++) 183 | { 184 | int tmp = target - v[i]; 185 | if (map.find(tmp) != map.end()) 186 | return {i, map[tmp]}; 187 | map[v[i]] = i; // keys = v[i] values and values = i indices 188 | } 189 | return {}; // The array does not contain any or is empty 190 | } 191 | ``` 192 | 193 | [*The first lambda allow to improve execution speed.*](https://stackoverflow.com/questions/31162367/significance-of-ios-basesync-with-stdiofalse-cin-tienull) 194 | ```c++ 195 | static int lambda_0 = []() { std::ios::sync_with_stdio(false); cin.tie(NULL); return 0; }(); 196 | ``` 197 | -------------------------------------------------------------------------------- /stack/brackets/README.md: -------------------------------------------------------------------------------- 1 | ​ 2 | ### Problem 3 | 4 | *The 'Brackets' problem is quite popular and look if candidates are able to check if a string is properly formatted.* 5 | 6 | *Write a function that, given a string S, returns true (1) or false (0) depending on if each opening bracket ('(', '{', '[') matches the corresponding closing bracket.* 7 | 8 | ##### Prototype 9 | ```c++ 10 | bool brackets(string str) { ;} 11 | ``` 12 | 13 | ##### For example 14 | * {}} -> invalid 15 | * ()()[(){}] -> valid 16 | 17 | ##### Write the most efficient algorithm for the following assumptions: 18 | * The string can have a very big Length 19 | 20 | 21 |
 22 | 
 23 | 
 24 | 
 25 | 
 26 | 
 27 | 
 28 | 
 29 | 
 30 | 
 31 | 
 32 | 
 33 | 
 34 | 
 35 | 
 36 | 
 37 | 
 38 | 
 39 | 
 40 | 
 41 | 
 42 | 
 43 | 
 44 | 
 45 | 
 46 | 
 47 | 
 48 | 
 49 | 
 50 | 
51 | 52 | ### How to start 53 | A) We know that is will be useful to check if we are facing an opening or a closing bracket so we will start by coding 2 boolean functions : 54 | 55 | ```c++ 56 | static bool is_opening_bracket(char c) 57 | { 58 | return (c == '{' || c == '(' || c == '['); 59 | } 60 | ​ 61 | static bool is_closing_bracket(char c) 62 | { 63 | return (c == '}' || c == ')' || c == ']'); 64 | } 65 | ​ 66 | bool brackets(string str) { 67 | return {stuff}; 68 | } 69 | ``` 70 | 71 | *B) Now that we have these 2 helper functions let us focus on the main function. C# has implemented the [Stack class](https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.stack-1) with ```using System.Collections.Generic;``` which is very handy for what we need : a LIFO (Last In, First Out) stack. 72 | It is also implemented in c++ using ```#include ```* 73 | 74 | ```c++ 75 | bool brackets(string str) 76 | { 77 | stack mystack; 78 | 79 | for (const char & c : str) 80 | { 81 | if (is_opening_bracket(c) == true) 82 | {do stuff} 83 | else if (is_closing_bracket(c) == true) 84 | {do stuff}; 85 | } 86 | return {stuff}; 87 | } 88 | ``` 89 | 90 | ```cs 91 | using System.Collections.Generic; // Do not forget to add the library 92 | ​ 93 | public int is_correctly_bracketted(string S) { 94 | char[] str = S.ToCharArray(); // split the string into a char array 95 | Stack stack = new Stack(); // b) 96 | ​ 97 | foreach (char c in str) // in order to individually check every character. 98 | { 99 | if (is_opening_bracket(c)) 100 | {do stuff} 101 | else if (is_closing_bracket(c)) 102 | {do stuff} 103 | } 104 | return {stuff}; 105 | } 106 | ``` 107 | 108 | 109 | *C) If we are facing an opening bracket we want to push it. 110 | We can do elegantly it with 'stack.Push(c >> 5)' (divide the character by 32).* 111 | 112 | *Although it hurts a little bit readability, we can notice that '(' and ')' are at equal to 40 and 41 in the ASCII table, '[' and ']' at 91 and 93 and '{' and '}' at 123 and 125.* 113 | 114 | *Dividing by 32 will allow us to store directly 1, 2 and 3 as (40 or 41) / 32 = 1, (91 or 93) / 32 = 2 and (123 or 125) / 32 = 3.* 115 | 116 | ```c++ 117 | bool brackets(string str) 118 | { 119 | stack mystack; 120 | 121 | for (const char & c : str) 122 | { 123 | if (is_opening_bracket(c) == true) 124 | mystack.push(c >> 5); 125 | else if (is_closing_bracket(c) == true) 126 | {do stuff}; 127 | } 128 | return {stuff}; 129 | } 130 | ``` 131 | 132 | ```cs 133 | public static int brackets(string S) { 134 | char[] str = S.ToCharArray(); 135 | Stack stack = new Stack(); 136 | ​ 137 | foreach (char c in str) 138 | { 139 | if (is_opening_bracket(c)) 140 | stack.Push(c >> 5); // 3 141 | else if (is_closing_bracket(c)) 142 | {do stuff}; 143 | } 144 | return {stuff}; 145 | } 146 | ``` 147 | *D) Now when facing a closing bracket we have to check that :* 148 | 149 | *a) The Stack is not empty with ```mystack.empty()``` in c++ and ```stack.Count == 0``` in c#* 150 | 151 | *b) That the last opening bracket stored is related to the new closing one with ```mystack.top() != (c >> 5)``` in c++ and ```stack.Pop() != c >> 5``` in c#.* 152 | 153 | *If we face either case it means that the string is not correctly formatted and we have to return false.* 154 | 155 | *c) In c++ there is a distinction between .top() which access the last element of the stack and .pop() which removes it. So we don't forget to add .pop().* 156 | 157 | ```c++ 158 | bool brackets(string str) 159 | { 160 | stack mystack; 161 | 162 | for (const char & c : str) 163 | { 164 | if (is_opening_bracket(c) == true) 165 | mystack.push(c >> 5); 166 | else if (is_closing_bracket(c) == true) 167 | { 168 | if (mystack.empty() || mystack.top() != (c >> 5)) // a) and b) 169 | return false; 170 | mystack.pop(); // c) 171 | } 172 | 173 | } 174 | return (mystack.empty()); 175 | } 176 | ``` 177 | 178 | ```cs 179 | public static int brackets(string S) { 180 | char[] str = S.ToCharArray(); 181 | Stack stack = new Stack(); 182 | ​ 183 | foreach (char c in str) 184 | { 185 | if (is_opening_bracket(c)) 186 | stack.Push(c >> 5); 187 | else if (is_closing_bracket(c)) 188 | if (stack.Count == 0 || stack.Pop() != c >> 5) // a) and b) 189 | return (0); 190 | } 191 | return {stuff}; 192 | } 193 | ``` 194 | 195 | ### Final C++ Solution 196 | 197 | ```c++ 198 | #include 199 | #include 200 | 201 | static bool is_opening_bracket(char c) 202 | { 203 | return (c == '[' || c == '(' || c == '{'); 204 | } 205 | 206 | static bool is_closing_bracket(char c) 207 | { 208 | return (c == ']' || c == ')' || c == '}'); 209 | } 210 | 211 | bool brackets(string str) 212 | { 213 | stack mystack; 214 | 215 | for (const char & c : str) 216 | { 217 | if (is_opening_bracket(c) == true) 218 | mystack.push(c >> 5); 219 | else if (is_closing_bracket(c) == true) 220 | { 221 | if (mystack.empty() || mystack.top() != (c >> 5)) 222 | return false; 223 | mystack.pop(); 224 | } 225 | 226 | } 227 | return (mystack.empty()); 228 | } 229 | ``` 230 | ### Iterative C# Solution 231 | ```cs 232 | using System; 233 | using System.Collections.Generic; 234 | ​ 235 | 236 | private bool is_opening_bracket(char c) 237 | { 238 | return (c == '{' || c == '(' || c == '['); 239 | } 240 | ​ 241 | private bool is_closing_bracket(char c) 242 | { 243 | return (c == '}' || c == ')' || c == ']'); 244 | } 245 | ​ 246 | public static int brackets(string S) { 247 | if (String.isNullorEmpty(s)) 248 | return 0; 249 | char[] str = S.ToCharArray(); 250 | Stack stack = new Stack(); 251 | ​ 252 | foreach (char c in str) 253 | { 254 | if (is_opening_bracket(c)) 255 | stack.Push(c >> 5); 256 | else if (is_closing_bracket(c)) 257 | if (stack.Count == 0 || stack.Pop() != c >> 5) 258 | return (0); 259 | } 260 | return (stack.Count == 0 ? 1 : 0); // a) 261 | } 262 | ``` 263 | 264 | *a) We finally check with a ternary operation that the Stack is empty with 'return (stack.Count == 0 ? 1 : 0);'* 265 | 266 | ### Recursive C Solution; beware as it will stack overflow 267 | 268 | ```c 269 | #include 270 | ​ 271 | int braclose(char *str, char c, int i, int b) 272 | { 273 | while (b && *(++str) && (i++)) 274 | if (*str == c || *str == c + c % 2 + 1) 275 | *str == c ? ++b : --b; 276 | return (i); 277 | } 278 | ​ 279 | int brackets(char *str, char c) 280 | { 281 | if (*str == c) 282 | return (1); 283 | else if (!*str || *str == ')' || *str == '}' || *str == ']') 284 | return (0); 285 | else if (*str == '(' || *str == '{' || *str == '[') 286 | return (brackets(str + 1, *str + *str % 2 + 1) 287 | * brackets(str + braclose(str, *str, 1, 1), c)); 288 | else 289 | return (brackets(str + 1, c)); 290 | } 291 | ​ 292 | int main(int ac, char **av) 293 | { 294 | int i; 295 | ​ 296 | i = 0; 297 | if (ac > 1) 298 | while (++i < ac) 299 | brackets(av[i], 0) ? write(1, "OK\n", 3) : write(1, "Error\n", 6); 300 | else 301 | write(1, "\n", 1); 302 | return (0); 303 | } 304 | ``` 305 | *Credit: [Anselme Grumbach](https://github.com/grumbach)* 306 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Nailing-the-Coding-Interview 2 | 3 | *Author: [github.com/agavrel](https://github.com/agavrel)* 4 | 5 | ### Learn how to pass C++ assessment tests with the most optimized algorithms 6 | 7 | 8 | --- 9 | ## 0x00 - Purpose of this repository 10 | 11 | **This repository is aimed at a broad range of people:** 12 | * Computer Science and Mathematics students 13 | * Professionals looking to sharpen their skills for interviews 14 | * People who prefer to have fun on week-ends solving interesting problems 15 | * Kids who need to build and enhance their problem-solving logic 16 | * Elders who want to keep their brain active 17 | 18 | 19 | --- 20 | ## 0x01 - Algorithm Definition (quoting wikipedia) 21 | 22 | > In mathematics and computer science, an algorithm is an unambiguous specification of how to solve a class of problems. Algorithms can perform calculation, data processing and automated reasoning tasks. 23 | 24 | > Algorithms have become an important way of optimizing programs in Computer Science. It is rather easy to code a solution to a specific problem. What is more difficult is to have the most efficient algorithm, meaning the algorithm with the [lowest time complexity](https://en.wikipedia.org/wiki/Time_complexity). 25 | 26 | 27 | --- 28 | ## 0x02 - Algorithm General Categories 29 | 30 | While they are all interesting, they serve different purposes. Depending on which kind of career you are looking for, you might want to focus more specifically in one area or another 31 | 32 | Category | Purpose 33 | ---|---| 34 | [Well-Known Algorithms](https://github.com/agavrel/Nailing-the-Coding-Interview/tree/master/misc) | Starting Point for Interviews 35 | [Bitwise](https://github.com/agavrel/Nailing-the-Coding-Interview/tree/master/bitwise)| Best for those looking for careers in Embedded Systems 36 | [Mathematics and Logical Sequences](https://github.com/agavrel/Nailing-the-Coding-Interview/tree/master/math) | Mathematics is ideal for those interested in working on Graphic Motors 37 | [Linked List]() | To understand better Recursion, structure and pointers 38 | Prime Numbers | Excellent for Cryptography as Prime Numbers allow to compute unique Hashes 39 | [Greedy](https://github.com/agavrel/Nailing-the-Coding-Interview/tree/master/greedy) | Interesting for those who contemplate AI 40 | [Parsing](https://github.com/agavrel/Nailing-the-Coding-Interview/tree/master/parsing) | Best for those interested into Hacking, segfaulting the parser is often the best way to exploit a program 41 | [Binary Search Algorithms](https://github.com/agavrel/Nailing-the-Coding-Interview/tree/master/math/binary_search) | Logarithmic search of a special number in a sorted array, good to understand map/dictionary implementations 42 | 43 | 44 | --- 45 | ### 0x03 - Bitwise Category 46 | 47 | Name | How Interesting | Notes 48 | ---|---|--- 49 | [Is Power of Two](https://github.com/agavrel/Nailing-the-Coding-Interview/tree/master/bitwise/is_power_of_two) | :star::star::star::star::star: | Understanding Binary Representation 50 | [Find the Single Integer](https://github.com/agavrel/Nailing-the-Coding-Interview/tree/master/bitwise/find_the_single_integer) | :star: | Understanding Binary Representation 51 | [Is Nth Bit Set](https://github.com/agavrel/Nailing-the-Coding-Interview/tree/master/bitwise/is_nth_bit_set) | :star::star: | Binary Representation 52 | [Spy Hawk](https://github.com/agavrel/Nailing-the-Coding-Interview/tree/master/bitwise/spy_hawk) | :star::star::star::star::star: | Bitset 53 | [Reverse 32 bits](https://github.com/agavrel/Nailing-the-Coding-Interview/tree/master/bitwise/reverse_bits) | :star::star::star::star::star: | Lookup Table, Shifts and Masks, Use of Carry Flag (assembly) 54 | [Reverse 8 bits](https://github.com/agavrel/Nailing-the-Coding-Interview/tree/master/bitwise/symetric_bits) | :star::star::star::star::star: | Lookup Table, Shifts and Masks, Use of Carry Flag (assembly) 55 | [INT to HEX](https://github.com/agavrel/Nailing-the-Coding-Interview/tree/master/bitwise/int_to_hex) | :star::star::star: | printf %x or print_base_16 implementation, easier in C++ thanks to std::string 56 | [Number of Common Letters](https://github.com/agavrel/Nailing-the-Coding-Interview/tree/master/bitwise/number_of_common_letters) | :star::star::star: | Bitwise Approach 57 | [Least Operators to Express a Number](https://github.com/agavrel/Nailing-the-Coding-Interview/tree/master/bitwise/least_operators_to_express_number) | :star::star::star: | Binary Representation and Mathematics 58 | [Match Maker](https://github.com/agavrel/Nailing-the-Coding-Interview/tree/master/bitwise/match_maker) | :star::star::star::star: | Algorithm that I coined myself, no solution published 59 | [Binary Gap](https://github.com/agavrel/Nailing-the-Coding-Interview/tree/master/bitwise/binary_gap) | :star::star::star::star: | Shifts and Masks 60 | [Align Number on Power of Two](https://github.com/agavrel/Nailing-the-Coding-Interview/tree/master/bitwise/align_number_on_power_of_two) | :star::star::star::star::star: | Shifts and Masks 61 | [Longest Palindrome](https://github.com/agavrel/Nailing-the-Coding-Interview/tree/master/bitwise/longest_palindrome) | :star: | Shifts and Masks 62 | [Number Complement](https://github.com/agavrel/Nailing-the-Coding-Interview/tree/master/bitwise/number_complement) | :two_hearts: | Excellent in-depth Algorithm 63 | [Bitwise AND of Number Range](https://github.com/agavrel/Nailing-the-Coding-Interview/tree/master/bitwise/bitwise_AND_of_numbers_range) | :two_hearts: | Excellent in-depth Algorithm to apply Mathematics to bitwise 64 | [Repeated DNA Sequences](https://github.com/agavrel/Nailing-the-Coding-Interview/tree/master/bitwise/repeated_dna_sequences) | :star::star::star::star: | Bitwise Approach 65 | 66 | 67 | 68 | --- 69 | ### 0x04 - Mathematics and Logical Sequences 70 | 71 | Name | How Interesting | Notes 72 | ---|---|--- 73 | [The Dancer](https://github.com/agavrel/Nailing-the-Coding-Interview/tree/master/math/the_dancer) | :star::star::star::star: | Do you see a Pattern ? 74 | [Fibonacci Series](https://github.com/agavrel/Nailing-the-Coding-Interview/tree/master/math/fibonacci_series) | :star: | Top 10 interview questions in Finance 75 | [Binary Search](https://github.com/agavrel/Nailing-the-Coding-Interview/tree/master/math/binary_search) | :star: | Also Top 10 interview questions in Finance 76 | [Is Power of Three](https://github.com/agavrel/Nailing-the-Coding-Interview/tree/master/math/is_power_of_three) | :star::star: | Return result in O(1) 77 | 78 | 79 | --- 80 | ### 0x05 - Linked List 81 | Name | How Interesting | Notes 82 | ---|---|--- 83 | [Merge Two Sorted Lists](https://github.com/agavrel/Nailing-the-Coding-Interview/tree/master/list/merge_two_sorted_lists) | :star::star::star: | Sort Algorithms 84 | [Merge k Sorted Lists](https://github.com/agavrel/Nailing-the-Coding-Interview/tree/master/list/merge_k_sorted_lists) | :star::star::star::star::star: | Sort Algorithms 85 | 86 | 87 | --- 88 | ### 0x06 - Hashmap 89 | Name | How Interesting | Notes 90 | ---|---|--- 91 | [Group Anagram](https://github.com/agavrel/Nailing-the-Coding-Interview/tree/master/hashmap/group_anagram) | :star::star::star: | How about implementing the Hash function yourself ? 92 | [Target Sum](https://github.com/agavrel/Nailing-the-Coding-Interview/tree/master/hashmap/target_sum) | :star::star::star::star: | Beware of Integer overflow 93 | 94 | 95 | --- 96 | ### 0x07 - Greedy 97 | 98 | Name | How Interesting | Notes 99 | ---|---|--- 100 | [Change Problem](https://github.com/agavrel/Nailing-the-Coding-Interview/tree/master/greedy/change_problem) | :star: | If you ever have to code the program of an ATM 101 | 102 | 103 | --- 104 | ### 0x08 - Parsing 105 | 106 | Name | How Interesting | Notes 107 | ---|---|--- 108 | [Reverse Integer Digits](https://github.com/agavrel/Nailing-the-Coding-Interview/tree/master/parsing/reverse_integer) | :star: | Not the most exciting one 109 | [https://github.com/agavrel/Nailing-the-Coding-Interview/tree/master/parsing/string_to_lowercase](https://github.com/agavrel/Nailing-the-Coding-Interview/tree/master/parsing/string_to_lowercase) | :star::star::star: | [Best Way to get Flamed on StackOverflow](https://stackoverflow.com/questions/61148630/calculate-the-number-of-times-each-letter-appears-in-a-string/61149709#61149709) 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | --- 120 | ## 0xFE - Credit 121 | My past interview assessments with different financial companies (BNP Paribas, Societe Generale, Murex etc.) were what got me started into algorithms and competitive programming. 122 | 123 | I want to thank my Friends of 42 School in Paris, especially Iwan Burel (iburel) and Steven Toupin (stoupin), for their precious insights and outstanding problem solving skills. 124 | 125 | The various internet sites like Codingame, Leetcode, Hackerrank and Codility were helpful to read about new algorithm problems. 126 | 127 | Last, I want to thank the numerous quidam on github and stackoverflow who raised my knowledge of algorithm resolution using assembly language 128 | 129 | --- 130 | ## 0xFF - Purpose of this repository 131 | 132 | All rights reserved by [Antonin GAVREL](https://github.com/agavrel). Contact me if you want to redistribute the code. No commercial use. 133 | **** 134 | -------------------------------------------------------------------------------- /parsing/string_to_lowercase/benchmark.s: -------------------------------------------------------------------------------- 1 | .file "benchmark.cpp" 2 | .text 3 | .align 2 4 | .p2align 4,,15 5 | .type _ZNKUlOT_DpOT0_E_clIRFNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERSB_EJSC_EEEDaS0_S3_.isra.26, @function 6 | _ZNKUlOT_DpOT0_E_clIRFNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERSB_EJSC_EEEDaS0_S3_.isra.26: 7 | .LFB4308: 8 | .cfi_startproc 9 | pushq %r13 10 | .cfi_def_cfa_offset 16 11 | .cfi_offset 13, -16 12 | pushq %r12 13 | .cfi_def_cfa_offset 24 14 | .cfi_offset 12, -24 15 | movq %rsi, %r13 16 | pushq %rbp 17 | .cfi_def_cfa_offset 32 18 | .cfi_offset 6, -32 19 | pushq %rbx 20 | .cfi_def_cfa_offset 40 21 | .cfi_offset 3, -40 22 | movq %rdi, %r12 23 | subq $56, %rsp 24 | .cfi_def_cfa_offset 96 25 | movq %rsp, %rbx 26 | movq %fs:40, %rax 27 | movq %rax, 40(%rsp) 28 | xorl %eax, %eax 29 | call _ZNSt6chrono3_V212system_clock3nowEv@PLT 30 | movq %rbx, %rdi 31 | movq %rax, %rbp 32 | movq %r13, %rsi 33 | call *%r12 34 | movq (%rsp), %rdi 35 | addq $16, %rbx 36 | cmpq %rbx, %rdi 37 | je .L2 38 | call _ZdlPv@PLT 39 | .L2: 40 | call _ZNSt6chrono3_V212system_clock3nowEv@PLT 41 | movabsq $3022314549036572937, %rdx 42 | subq %rbp, %rax 43 | movq %rax, %rcx 44 | imulq %rdx 45 | sarq $63, %rcx 46 | sarq $14, %rdx 47 | movq %rdx, %rax 48 | subq %rcx, %rax 49 | movq 40(%rsp), %rsi 50 | xorq %fs:40, %rsi 51 | jne .L6 52 | addq $56, %rsp 53 | .cfi_remember_state 54 | .cfi_def_cfa_offset 40 55 | popq %rbx 56 | .cfi_def_cfa_offset 32 57 | popq %rbp 58 | .cfi_def_cfa_offset 24 59 | popq %r12 60 | .cfi_def_cfa_offset 16 61 | popq %r13 62 | .cfi_def_cfa_offset 8 63 | ret 64 | .L6: 65 | .cfi_restore_state 66 | call __stack_chk_fail@PLT 67 | .cfi_endproc 68 | .LFE4308: 69 | .size _ZNKUlOT_DpOT0_E_clIRFNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERSB_EJSC_EEEDaS0_S3_.isra.26, .-_ZNKUlOT_DpOT0_E_clIRFNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERSB_EJSC_EEEDaS0_S3_.isra.26 70 | .section .rodata.str1.1,"aMS",@progbits,1 71 | .LC0: 72 | .string "basic_string::_M_create" 73 | .text 74 | .align 2 75 | .p2align 4,,15 76 | .type _ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEE9_M_createERmm.isra.38.constprop.51, @function 77 | _ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEE9_M_createERmm.isra.38.constprop.51: 78 | .LFB4333: 79 | .cfi_startproc 80 | movq (%rdi), %rdi 81 | testq %rdi, %rdi 82 | js .L12 83 | addq $1, %rdi 84 | jmp _Znwm@PLT 85 | .L12: 86 | leaq .LC0(%rip), %rdi 87 | subq $8, %rsp 88 | .cfi_def_cfa_offset 16 89 | call _ZSt20__throw_length_errorPKc@PLT 90 | .cfi_endproc 91 | .LFE4333: 92 | .size _ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEE9_M_createERmm.isra.38.constprop.51, .-_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEE9_M_createERmm.isra.38.constprop.51 93 | .section .rodata.str1.8,"aMS",@progbits,1 94 | .align 8 95 | .LC1: 96 | .string "basic_string::_M_construct null not valid" 97 | .text 98 | .align 2 99 | .p2align 4,,15 100 | .type _ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEE12_M_constructIPKcEEvT_S8_St20forward_iterator_tag.isra.39, @function 101 | _ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEE12_M_constructIPKcEEvT_S8_St20forward_iterator_tag.isra.39: 102 | .LFB4321: 103 | .cfi_startproc 104 | pushq %rbp 105 | .cfi_def_cfa_offset 16 106 | .cfi_offset 6, -16 107 | pushq %rbx 108 | .cfi_def_cfa_offset 24 109 | .cfi_offset 3, -24 110 | movq %rdi, %rbp 111 | subq $40, %rsp 112 | .cfi_def_cfa_offset 64 113 | movq %fs:40, %rax 114 | movq %rax, 24(%rsp) 115 | xorl %eax, %eax 116 | testq %rsi, %rsi 117 | jne .L14 118 | testq %rdx, %rdx 119 | jne .L30 120 | .L14: 121 | movq %rdx, %rbx 122 | subq %rsi, %rbx 123 | cmpq $15, %rbx 124 | movq %rbx, 16(%rsp) 125 | ja .L31 126 | movq 0(%rbp), %rdx 127 | cmpq $1, %rbx 128 | movq %rdx, %rax 129 | je .L32 130 | testq %rbx, %rbx 131 | jne .L16 132 | .L18: 133 | movq 16(%rsp), %rax 134 | movq %rax, 8(%rbp) 135 | movb $0, (%rdx,%rax) 136 | movq 24(%rsp), %rax 137 | xorq %fs:40, %rax 138 | jne .L33 139 | addq $40, %rsp 140 | .cfi_remember_state 141 | .cfi_def_cfa_offset 24 142 | popq %rbx 143 | .cfi_def_cfa_offset 16 144 | popq %rbp 145 | .cfi_def_cfa_offset 8 146 | ret 147 | .p2align 4,,10 148 | .p2align 3 149 | .L32: 150 | .cfi_restore_state 151 | movzbl (%rsi), %eax 152 | movb %al, (%rdx) 153 | movq 0(%rbp), %rdx 154 | jmp .L18 155 | .p2align 4,,10 156 | .p2align 3 157 | .L31: 158 | leaq 16(%rsp), %rdi 159 | movq %rsi, 8(%rsp) 160 | call _ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEE9_M_createERmm.isra.38.constprop.51 161 | movq 16(%rsp), %rdx 162 | movq 8(%rsp), %rsi 163 | movq %rax, 0(%rbp) 164 | movq %rdx, 16(%rbp) 165 | .L16: 166 | movq %rbx, %rdx 167 | movq %rax, %rdi 168 | call memcpy@PLT 169 | movq 0(%rbp), %rdx 170 | jmp .L18 171 | .L30: 172 | leaq .LC1(%rip), %rdi 173 | call _ZSt19__throw_logic_errorPKc@PLT 174 | .L33: 175 | call __stack_chk_fail@PLT 176 | .cfi_endproc 177 | .LFE4321: 178 | .size _ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEE12_M_constructIPKcEEvT_S8_St20forward_iterator_tag.isra.39, .-_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEE12_M_constructIPKcEEvT_S8_St20forward_iterator_tag.isra.39 179 | .set _ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEE12_M_constructIPcEEvT_S7_St20forward_iterator_tag.isra.41,_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEE12_M_constructIPKcEEvT_S8_St20forward_iterator_tag.isra.39 180 | .p2align 4,,15 181 | .globl _Z16str_to_lowercaseRNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE 182 | .type _Z16str_to_lowercaseRNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE, @function 183 | _Z16str_to_lowercaseRNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE: 184 | .LFB3443: 185 | .cfi_startproc 186 | pushq %rbx 187 | .cfi_def_cfa_offset 16 188 | .cfi_offset 3, -16 189 | movl $32, %ecx 190 | movq %rdi, %rbx 191 | movq %rsi, %r8 192 | subq $272, %rsp 193 | .cfi_def_cfa_offset 288 194 | movq %fs:40, %rax 195 | movq %rax, 264(%rsp) 196 | xorl %eax, %eax 197 | movq %rsp, %rdi 198 | rep stosq 199 | movabsq $2314885530818453536, %rax 200 | movq 8(%rsi), %rdi 201 | movq %rax, 65(%rsp) 202 | movq %rax, 73(%rsp) 203 | movq %rax, 81(%rsp) 204 | movl $8224, %eax 205 | movw %ax, 89(%rsp) 206 | movq (%rsi), %rax 207 | addq %rax, %rdi 208 | cmpq %rax, %rdi 209 | je .L35 210 | .p2align 4,,10 211 | .p2align 3 212 | .L36: 213 | movsbq (%rax), %rcx 214 | addq $1, %rax 215 | movq %rcx, %rdx 216 | orb (%rsp,%rcx), %dl 217 | movb %dl, -1(%rax) 218 | cmpq %rax, %rdi 219 | jne .L36 220 | .L35: 221 | leaq 16(%rbx), %rax 222 | movq %rbx, %rdi 223 | movq %rax, (%rbx) 224 | movq (%r8), %rsi 225 | movq 8(%r8), %rdx 226 | addq %rsi, %rdx 227 | call _ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEE12_M_constructIPcEEvT_S7_St20forward_iterator_tag.isra.41 228 | movq 264(%rsp), %rsi 229 | xorq %fs:40, %rsi 230 | movq %rbx, %rax 231 | jne .L40 232 | addq $272, %rsp 233 | .cfi_remember_state 234 | .cfi_def_cfa_offset 16 235 | popq %rbx 236 | .cfi_def_cfa_offset 8 237 | ret 238 | .L40: 239 | .cfi_restore_state 240 | call __stack_chk_fail@PLT 241 | .cfi_endproc 242 | .LFE3443: 243 | .size _Z16str_to_lowercaseRNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE, .-_Z16str_to_lowercaseRNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE 244 | .p2align 4,,15 245 | .globl _Z17str_to_lowercase2RNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE 246 | .type _Z17str_to_lowercase2RNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE, @function 247 | _Z17str_to_lowercase2RNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE: 248 | .LFB3444: 249 | .cfi_startproc 250 | pushq %r13 251 | .cfi_def_cfa_offset 16 252 | .cfi_offset 13, -16 253 | pushq %r12 254 | .cfi_def_cfa_offset 24 255 | .cfi_offset 12, -24 256 | movq %rsi, %r13 257 | pushq %rbp 258 | .cfi_def_cfa_offset 32 259 | .cfi_offset 6, -32 260 | pushq %rbx 261 | .cfi_def_cfa_offset 40 262 | .cfi_offset 3, -40 263 | movq %rdi, %r12 264 | subq $8, %rsp 265 | .cfi_def_cfa_offset 48 266 | movq (%rsi), %rbx 267 | movq 8(%rsi), %rbp 268 | addq %rbx, %rbp 269 | cmpq %rbx, %rbp 270 | je .L42 271 | .p2align 4,,10 272 | .p2align 3 273 | .L43: 274 | movsbl (%rbx), %edi 275 | addq $1, %rbx 276 | call toupper@PLT 277 | movb %al, -1(%rbx) 278 | cmpq %rbx, %rbp 279 | jne .L43 280 | .L42: 281 | leaq 16(%r12), %rax 282 | movq %r12, %rdi 283 | movq %rax, (%r12) 284 | movq 0(%r13), %rsi 285 | movq 8(%r13), %rdx 286 | addq %rsi, %rdx 287 | call _ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEE12_M_constructIPcEEvT_S7_St20forward_iterator_tag.isra.41 288 | addq $8, %rsp 289 | .cfi_def_cfa_offset 40 290 | movq %r12, %rax 291 | popq %rbx 292 | .cfi_def_cfa_offset 32 293 | popq %rbp 294 | .cfi_def_cfa_offset 24 295 | popq %r12 296 | .cfi_def_cfa_offset 16 297 | popq %r13 298 | .cfi_def_cfa_offset 8 299 | ret 300 | .cfi_endproc 301 | .LFE3444: 302 | .size _Z17str_to_lowercase2RNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE, .-_Z17str_to_lowercase2RNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE 303 | .p2align 4,,15 304 | .globl _Z17str_to_lowercase3RNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE 305 | .type _Z17str_to_lowercase3RNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE, @function 306 | _Z17str_to_lowercase3RNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE: 307 | .LFB3445: 308 | .cfi_startproc 309 | pushq %r14 310 | .cfi_def_cfa_offset 16 311 | .cfi_offset 14, -16 312 | pushq %r13 313 | .cfi_def_cfa_offset 24 314 | .cfi_offset 13, -24 315 | movq %rsi, %r14 316 | pushq %r12 317 | .cfi_def_cfa_offset 32 318 | .cfi_offset 12, -32 319 | pushq %rbp 320 | .cfi_def_cfa_offset 40 321 | .cfi_offset 6, -40 322 | movq %rdi, %r13 323 | pushq %rbx 324 | .cfi_def_cfa_offset 48 325 | .cfi_offset 3, -48 326 | movq (%rsi), %rbx 327 | movq 8(%rsi), %r12 328 | addq %rbx, %r12 329 | cmpq %rbx, %r12 330 | je .L47 331 | .p2align 4,,10 332 | .p2align 3 333 | .L49: 334 | movsbl (%rbx), %edi 335 | movl %edi, %ebp 336 | call isalpha@PLT 337 | testl %eax, %eax 338 | je .L48 339 | orl $32, %ebp 340 | movb %bpl, (%rbx) 341 | .L48: 342 | addq $1, %rbx 343 | cmpq %r12, %rbx 344 | jne .L49 345 | .L47: 346 | leaq 16(%r13), %rax 347 | movq %r13, %rdi 348 | movq %rax, 0(%r13) 349 | movq (%r14), %rsi 350 | movq 8(%r14), %rdx 351 | addq %rsi, %rdx 352 | call _ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEE12_M_constructIPcEEvT_S7_St20forward_iterator_tag.isra.41 353 | popq %rbx 354 | .cfi_def_cfa_offset 40 355 | movq %r13, %rax 356 | popq %rbp 357 | .cfi_def_cfa_offset 32 358 | popq %r12 359 | .cfi_def_cfa_offset 24 360 | popq %r13 361 | .cfi_def_cfa_offset 16 362 | popq %r14 363 | .cfi_def_cfa_offset 8 364 | ret 365 | .cfi_endproc 366 | .LFE3445: 367 | .size _Z17str_to_lowercase3RNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE, .-_Z17str_to_lowercase3RNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE 368 | .p2align 4,,15 369 | .globl _Z17str_to_lowercase4RNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE 370 | .type _Z17str_to_lowercase4RNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE, @function 371 | _Z17str_to_lowercase4RNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE: 372 | .LFB3446: 373 | .cfi_startproc 374 | pushq %r14 375 | .cfi_def_cfa_offset 16 376 | .cfi_offset 14, -16 377 | pushq %r13 378 | .cfi_def_cfa_offset 24 379 | .cfi_offset 13, -24 380 | movq %rsi, %r14 381 | pushq %r12 382 | .cfi_def_cfa_offset 32 383 | .cfi_offset 12, -32 384 | pushq %rbp 385 | .cfi_def_cfa_offset 40 386 | .cfi_offset 6, -40 387 | movq %rdi, %r13 388 | pushq %rbx 389 | .cfi_def_cfa_offset 48 390 | .cfi_offset 3, -48 391 | movq (%rsi), %rbp 392 | movq 8(%rsi), %r12 393 | addq %rbp, %r12 394 | cmpq %rbp, %r12 395 | je .L56 396 | .p2align 4,,10 397 | .p2align 3 398 | .L58: 399 | movsbl 0(%rbp), %edi 400 | movl %edi, %ebx 401 | call isalpha@PLT 402 | movl %ebx, %edx 403 | orl $32, %edx 404 | testl %eax, %eax 405 | cmovne %edx, %ebx 406 | addq $1, %rbp 407 | movb %bl, -1(%rbp) 408 | cmpq %rbp, %r12 409 | jne .L58 410 | .L56: 411 | leaq 16(%r13), %rax 412 | movq %r13, %rdi 413 | movq %rax, 0(%r13) 414 | movq (%r14), %rsi 415 | movq 8(%r14), %rdx 416 | addq %rsi, %rdx 417 | call _ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEE12_M_constructIPcEEvT_S7_St20forward_iterator_tag.isra.41 418 | popq %rbx 419 | .cfi_def_cfa_offset 40 420 | movq %r13, %rax 421 | popq %rbp 422 | .cfi_def_cfa_offset 32 423 | popq %r12 424 | .cfi_def_cfa_offset 24 425 | popq %r13 426 | .cfi_def_cfa_offset 16 427 | popq %r14 428 | .cfi_def_cfa_offset 8 429 | ret 430 | .cfi_endproc 431 | .LFE3446: 432 | .size _Z17str_to_lowercase4RNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE, .-_Z17str_to_lowercase4RNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE 433 | .section .text._ZNSt6vectorINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESaIS5_EED2Ev,"axG",@progbits,_ZNSt6vectorINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESaIS5_EED5Ev,comdat 434 | .align 2 435 | .p2align 4,,15 436 | .weak _ZNSt6vectorINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESaIS5_EED2Ev 437 | .type _ZNSt6vectorINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESaIS5_EED2Ev, @function 438 | _ZNSt6vectorINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESaIS5_EED2Ev: 439 | .LFB3797: 440 | .cfi_startproc 441 | pushq %r12 442 | .cfi_def_cfa_offset 16 443 | .cfi_offset 12, -16 444 | pushq %rbp 445 | .cfi_def_cfa_offset 24 446 | .cfi_offset 6, -24 447 | pushq %rbx 448 | .cfi_def_cfa_offset 32 449 | .cfi_offset 3, -32 450 | movq 8(%rdi), %rbp 451 | movq (%rdi), %rbx 452 | cmpq %rbx, %rbp 453 | je .L64 454 | movq %rdi, %r12 455 | .p2align 4,,10 456 | .p2align 3 457 | .L66: 458 | movq (%rbx), %rdi 459 | leaq 16(%rbx), %rax 460 | cmpq %rax, %rdi 461 | je .L65 462 | call _ZdlPv@PLT 463 | .L65: 464 | addq $32, %rbx 465 | cmpq %rbx, %rbp 466 | jne .L66 467 | movq (%r12), %rbx 468 | .L64: 469 | testq %rbx, %rbx 470 | je .L63 471 | movq %rbx, %rdi 472 | popq %rbx 473 | .cfi_remember_state 474 | .cfi_def_cfa_offset 24 475 | popq %rbp 476 | .cfi_def_cfa_offset 16 477 | popq %r12 478 | .cfi_def_cfa_offset 8 479 | jmp _ZdlPv@PLT 480 | .p2align 4,,10 481 | .p2align 3 482 | .L63: 483 | .cfi_restore_state 484 | popq %rbx 485 | .cfi_def_cfa_offset 24 486 | popq %rbp 487 | .cfi_def_cfa_offset 16 488 | popq %r12 489 | .cfi_def_cfa_offset 8 490 | ret 491 | .cfi_endproc 492 | .LFE3797: 493 | .size _ZNSt6vectorINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESaIS5_EED2Ev, .-_ZNSt6vectorINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESaIS5_EED2Ev 494 | .weak _ZNSt6vectorINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESaIS5_EED1Ev 495 | .set _ZNSt6vectorINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESaIS5_EED1Ev,_ZNSt6vectorINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESaIS5_EED2Ev 496 | .section .text._ZNSt23mersenne_twister_engineImLm32ELm624ELm397ELm31ELm2567483615ELm11ELm4294967295ELm7ELm2636928640ELm15ELm4022730752ELm18ELm1812433253EEclEv,"axG",@progbits,_ZNSt23mersenne_twister_engineImLm32ELm624ELm397ELm31ELm2567483615ELm11ELm4294967295ELm7ELm2636928640ELm15ELm4022730752ELm18ELm1812433253EEclEv,comdat 497 | .align 2 498 | .p2align 4,,15 499 | .weak _ZNSt23mersenne_twister_engineImLm32ELm624ELm397ELm31ELm2567483615ELm11ELm4294967295ELm7ELm2636928640ELm15ELm4022730752ELm18ELm1812433253EEclEv 500 | .type _ZNSt23mersenne_twister_engineImLm32ELm624ELm397ELm31ELm2567483615ELm11ELm4294967295ELm7ELm2636928640ELm15ELm4022730752ELm18ELm1812433253EEclEv, @function 501 | _ZNSt23mersenne_twister_engineImLm32ELm624ELm397ELm31ELm2567483615ELm11ELm4294967295ELm7ELm2636928640ELm15ELm4022730752ELm18ELm1812433253EEclEv: 502 | .LFB4052: 503 | .cfi_startproc 504 | movq 4992(%rdi), %rax 505 | cmpq $623, %rax 506 | leaq 1(%rax), %rdx 507 | ja .L89 508 | .L72: 509 | movq (%rdi,%rax,8), %rax 510 | movq %rdx, 4992(%rdi) 511 | movq %rax, %rcx 512 | shrq $11, %rcx 513 | movl %ecx, %edx 514 | xorq %rax, %rdx 515 | movq %rdx, %rax 516 | salq $7, %rax 517 | andl $2636928640, %eax 518 | xorq %rax, %rdx 519 | movq %rdx, %rax 520 | salq $15, %rax 521 | andl $4022730752, %eax 522 | xorq %rdx, %rax 523 | movq %rax, %rdx 524 | shrq $18, %rdx 525 | xorq %rdx, %rax 526 | ret 527 | .p2align 4,,10 528 | .p2align 3 529 | .L89: 530 | leaq 1816(%rdi), %r8 531 | movq %rdi, %rax 532 | movq %rdi, %rdx 533 | movl $2567483615, %r9d 534 | .p2align 4,,10 535 | .p2align 3 536 | .L74: 537 | movq (%rdx), %rcx 538 | movq 8(%rdx), %rsi 539 | andq $-2147483648, %rcx 540 | andl $2147483647, %esi 541 | orq %rsi, %rcx 542 | movq %rcx, %rsi 543 | shrq %rsi 544 | xorq 3176(%rdx), %rsi 545 | andl $1, %ecx 546 | je .L73 547 | xorq %r9, %rsi 548 | .L73: 549 | movq %rsi, (%rdx) 550 | addq $8, %rdx 551 | cmpq %rdx, %r8 552 | jne .L74 553 | leaq 3168(%rdi), %rsi 554 | movl $2567483615, %r8d 555 | .p2align 4,,10 556 | .p2align 3 557 | .L76: 558 | movq 1816(%rax), %rdx 559 | movq 1824(%rax), %rcx 560 | andq $-2147483648, %rdx 561 | andl $2147483647, %ecx 562 | orq %rcx, %rdx 563 | movq %rdx, %rcx 564 | shrq %rcx 565 | xorq (%rax), %rcx 566 | andl $1, %edx 567 | je .L75 568 | xorq %r8, %rcx 569 | .L75: 570 | movq %rcx, 1816(%rax) 571 | addq $8, %rax 572 | cmpq %rax, %rsi 573 | jne .L76 574 | movq 4984(%rdi), %rax 575 | movq (%rdi), %rdx 576 | andq $-2147483648, %rax 577 | andl $2147483647, %edx 578 | orq %rdx, %rax 579 | movq %rax, %rdx 580 | shrq %rdx 581 | xorq 3168(%rdi), %rdx 582 | testb $1, %al 583 | je .L77 584 | movl $2567483615, %eax 585 | xorq %rax, %rdx 586 | .L77: 587 | movq %rdx, 4984(%rdi) 588 | xorl %eax, %eax 589 | movl $1, %edx 590 | jmp .L72 591 | .cfi_endproc 592 | .LFE4052: 593 | .size _ZNSt23mersenne_twister_engineImLm32ELm624ELm397ELm31ELm2567483615ELm11ELm4294967295ELm7ELm2636928640ELm15ELm4022730752ELm18ELm1812433253EEclEv, .-_ZNSt23mersenne_twister_engineImLm32ELm624ELm397ELm31ELm2567483615ELm11ELm4294967295ELm7ELm2636928640ELm15ELm4022730752ELm18ELm1812433253EEclEv 594 | .section .text._ZNSt24uniform_int_distributionIiEclISt23mersenne_twister_engineImLm32ELm624ELm397ELm31ELm2567483615ELm11ELm4294967295ELm7ELm2636928640ELm15ELm4022730752ELm18ELm1812433253EEEEiRT_RKNS0_10param_typeE,"axG",@progbits,_ZNSt24uniform_int_distributionIiEclISt23mersenne_twister_engineImLm32ELm624ELm397ELm31ELm2567483615ELm11ELm4294967295ELm7ELm2636928640ELm15ELm4022730752ELm18ELm1812433253EEEEiRT_RKNS0_10param_typeE,comdat 595 | .align 2 596 | .p2align 4,,15 597 | .weak _ZNSt24uniform_int_distributionIiEclISt23mersenne_twister_engineImLm32ELm624ELm397ELm31ELm2567483615ELm11ELm4294967295ELm7ELm2636928640ELm15ELm4022730752ELm18ELm1812433253EEEEiRT_RKNS0_10param_typeE 598 | .type _ZNSt24uniform_int_distributionIiEclISt23mersenne_twister_engineImLm32ELm624ELm397ELm31ELm2567483615ELm11ELm4294967295ELm7ELm2636928640ELm15ELm4022730752ELm18ELm1812433253EEEEiRT_RKNS0_10param_typeE, @function 599 | _ZNSt24uniform_int_distributionIiEclISt23mersenne_twister_engineImLm32ELm624ELm397ELm31ELm2567483615ELm11ELm4294967295ELm7ELm2636928640ELm15ELm4022730752ELm18ELm1812433253EEEEiRT_RKNS0_10param_typeE: 600 | .LFB3940: 601 | .cfi_startproc 602 | pushq %r15 603 | .cfi_def_cfa_offset 16 604 | .cfi_offset 15, -16 605 | pushq %r14 606 | .cfi_def_cfa_offset 24 607 | .cfi_offset 14, -24 608 | pushq %r13 609 | .cfi_def_cfa_offset 32 610 | .cfi_offset 13, -32 611 | pushq %r12 612 | .cfi_def_cfa_offset 40 613 | .cfi_offset 12, -40 614 | pushq %rbp 615 | .cfi_def_cfa_offset 48 616 | .cfi_offset 6, -48 617 | pushq %rbx 618 | .cfi_def_cfa_offset 56 619 | .cfi_offset 3, -56 620 | movq %rdx, %rbp 621 | movq %rsi, %rbx 622 | subq $40, %rsp 623 | .cfi_def_cfa_offset 96 624 | movslq 4(%rdx), %r15 625 | movq %fs:40, %rax 626 | movq %rax, 24(%rsp) 627 | xorl %eax, %eax 628 | movslq (%rdx), %rax 629 | subq %rax, %r15 630 | movl $4294967294, %eax 631 | cmpq %rax, %r15 632 | ja .L91 633 | leaq 1(%r15), %r12 634 | addq $1, %rax 635 | xorl %edx, %edx 636 | divq %r12 637 | imulq %rax, %r12 638 | movq %rax, %r13 639 | .p2align 4,,10 640 | .p2align 3 641 | .L92: 642 | movq %rbx, %rdi 643 | call _ZNSt23mersenne_twister_engineImLm32ELm624ELm397ELm31ELm2567483615ELm11ELm4294967295ELm7ELm2636928640ELm15ELm4022730752ELm18ELm1812433253EEclEv 644 | cmpq %rax, %r12 645 | jbe .L92 646 | xorl %edx, %edx 647 | divq %r13 648 | .L93: 649 | addl 0(%rbp), %eax 650 | movq 24(%rsp), %rcx 651 | xorq %fs:40, %rcx 652 | jne .L106 653 | addq $40, %rsp 654 | .cfi_remember_state 655 | .cfi_def_cfa_offset 56 656 | popq %rbx 657 | .cfi_def_cfa_offset 48 658 | popq %rbp 659 | .cfi_def_cfa_offset 40 660 | popq %r12 661 | .cfi_def_cfa_offset 32 662 | popq %r13 663 | .cfi_def_cfa_offset 24 664 | popq %r14 665 | .cfi_def_cfa_offset 16 666 | popq %r15 667 | .cfi_def_cfa_offset 8 668 | ret 669 | .p2align 4,,10 670 | .p2align 3 671 | .L91: 672 | .cfi_restore_state 673 | movl $4294967295, %eax 674 | cmpq %rax, %r15 675 | jne .L107 676 | movq %rsi, %rdi 677 | call _ZNSt23mersenne_twister_engineImLm32ELm624ELm397ELm31ELm2567483615ELm11ELm4294967295ELm7ELm2636928640ELm15ELm4022730752ELm18ELm1812433253EEclEv 678 | jmp .L93 679 | .p2align 4,,10 680 | .p2align 3 681 | .L107: 682 | leaq 16(%rsp), %rax 683 | movq %rdi, %r13 684 | movabsq $-4294967296, %r12 685 | movq %rax, 8(%rsp) 686 | .L98: 687 | movq 8(%rsp), %rdx 688 | movq %rbx, %rsi 689 | movq %r13, %rdi 690 | movq %r12, 16(%rsp) 691 | call _ZNSt24uniform_int_distributionIiEclISt23mersenne_twister_engineImLm32ELm624ELm397ELm31ELm2567483615ELm11ELm4294967295ELm7ELm2636928640ELm15ELm4022730752ELm18ELm1812433253EEEEiRT_RKNS0_10param_typeE 692 | movq %rbx, %rdi 693 | movl %eax, %r14d 694 | call _ZNSt23mersenne_twister_engineImLm32ELm624ELm397ELm31ELm2567483615ELm11ELm4294967295ELm7ELm2636928640ELm15ELm4022730752ELm18ELm1812433253EEclEv 695 | movq %r14, %rdx 696 | salq $32, %rdx 697 | addq %rdx, %rax 698 | setc %dl 699 | cmpq %rax, %r15 700 | movzbl %dl, %edx 701 | jb .L98 702 | testq %rdx, %rdx 703 | jne .L98 704 | jmp .L93 705 | .L106: 706 | call __stack_chk_fail@PLT 707 | .cfi_endproc 708 | .LFE3940: 709 | .size _ZNSt24uniform_int_distributionIiEclISt23mersenne_twister_engineImLm32ELm624ELm397ELm31ELm2567483615ELm11ELm4294967295ELm7ELm2636928640ELm15ELm4022730752ELm18ELm1812433253EEEEiRT_RKNS0_10param_typeE, .-_ZNSt24uniform_int_distributionIiEclISt23mersenne_twister_engineImLm32ELm624ELm397ELm31ELm2567483615ELm11ELm4294967295ELm7ELm2636928640ELm15ELm4022730752ELm18ELm1812433253EEEEiRT_RKNS0_10param_typeE 710 | .section .rodata.str1.1 711 | .LC2: 712 | .string "default" 713 | .text 714 | .p2align 4,,15 715 | .globl _Z22generate_random_stringB5cxx11j 716 | .type _Z22generate_random_stringB5cxx11j, @function 717 | _Z22generate_random_stringB5cxx11j: 718 | .LFB3439: 719 | .cfi_startproc 720 | .cfi_personality 0x9b,DW.ref.__gxx_personality_v0 721 | .cfi_lsda 0x1b,.LLSDA3439 722 | pushq %r14 723 | .cfi_def_cfa_offset 16 724 | .cfi_offset 14, -16 725 | pushq %r13 726 | .cfi_def_cfa_offset 24 727 | .cfi_offset 13, -24 728 | leaq 7+.LC2(%rip), %rdx 729 | pushq %r12 730 | .cfi_def_cfa_offset 32 731 | .cfi_offset 12, -32 732 | pushq %rbp 733 | .cfi_def_cfa_offset 40 734 | .cfi_offset 6, -40 735 | movq %rdi, %rbp 736 | pushq %rbx 737 | .cfi_def_cfa_offset 48 738 | .cfi_offset 3, -48 739 | movl %esi, %ebx 740 | leaq -7(%rdx), %rsi 741 | subq $10064, %rsp 742 | .cfi_def_cfa_offset 10112 743 | leaq 16(%rsp), %r12 744 | leaq 5056(%rsp), %r13 745 | movq %fs:40, %rax 746 | movq %rax, 10056(%rsp) 747 | xorl %eax, %eax 748 | leaq 16(%r12), %rax 749 | movq %r12, %rdi 750 | movq %rax, 16(%rsp) 751 | .LEHB0: 752 | call _ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEE12_M_constructIPKcEEvT_S8_St20forward_iterator_tag.isra.39 753 | .LEHE0: 754 | movq %r12, %rsi 755 | movq %r13, %rdi 756 | .LEHB1: 757 | call _ZNSt13random_device7_M_initERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE@PLT 758 | .LEHE1: 759 | movq %r13, %rdi 760 | .LEHB2: 761 | call _ZNSt13random_device9_M_getvalEv@PLT 762 | .LEHE2: 763 | movl %eax, %ecx 764 | movl $1, %edx 765 | leaq 48(%rsp), %r14 766 | movq %rcx, 48(%rsp) 767 | jmp .L110 768 | .p2align 4,,10 769 | .p2align 3 770 | .L128: 771 | movq -8(%r14,%rdx,8), %rcx 772 | .L110: 773 | movq %rcx, %rax 774 | shrq $30, %rax 775 | xorq %rcx, %rax 776 | imulq $1812433253, %rax, %rax 777 | addl %edx, %eax 778 | movq %rax, (%r14,%rdx,8) 779 | addq $1, %rdx 780 | cmpq $624, %rdx 781 | jne .L128 782 | movq %r13, %rdi 783 | movq $624, 5040(%rsp) 784 | addq $16, %r12 785 | call _ZNSt13random_device7_M_finiEv@PLT 786 | movq 16(%rsp), %rdi 787 | cmpq %r12, %rdi 788 | je .L111 789 | call _ZdlPv@PLT 790 | .L111: 791 | movabsq $523986010160, %rax 792 | movl %ebx, %edx 793 | movq %rax, 8(%rsp) 794 | leaq 16(%rbp), %rax 795 | cmpq $15, %rdx 796 | movq %rdx, (%rsp) 797 | movq %rax, 0(%rbp) 798 | ja .L129 799 | .L112: 800 | testq %rdx, %rdx 801 | je .L113 802 | cmpq $1, %rdx 803 | je .L130 804 | xorl %esi, %esi 805 | movq %rax, %rdi 806 | call memset@PLT 807 | movq (%rsp), %rdx 808 | movq 0(%rbp), %rax 809 | .L113: 810 | movq %rdx, 8(%rbp) 811 | movb $0, (%rax,%rdx) 812 | movq 0(%rbp), %rbx 813 | movq 8(%rbp), %r13 814 | addq %rbx, %r13 815 | cmpq %rbx, %r13 816 | je .L108 817 | leaq 8(%rsp), %r12 818 | .p2align 4,,10 819 | .p2align 3 820 | .L116: 821 | movq %r12, %rdx 822 | movq %r14, %rsi 823 | movq %r12, %rdi 824 | call _ZNSt24uniform_int_distributionIiEclISt23mersenne_twister_engineImLm32ELm624ELm397ELm31ELm2567483615ELm11ELm4294967295ELm7ELm2636928640ELm15ELm4022730752ELm18ELm1812433253EEEEiRT_RKNS0_10param_typeE 825 | addq $1, %rbx 826 | movb %al, -1(%rbx) 827 | cmpq %rbx, %r13 828 | jne .L116 829 | .L108: 830 | movq 10056(%rsp), %rsi 831 | xorq %fs:40, %rsi 832 | movq %rbp, %rax 833 | jne .L131 834 | addq $10064, %rsp 835 | .cfi_remember_state 836 | .cfi_def_cfa_offset 48 837 | popq %rbx 838 | .cfi_def_cfa_offset 40 839 | popq %rbp 840 | .cfi_def_cfa_offset 32 841 | popq %r12 842 | .cfi_def_cfa_offset 24 843 | popq %r13 844 | .cfi_def_cfa_offset 16 845 | popq %r14 846 | .cfi_def_cfa_offset 8 847 | ret 848 | .L130: 849 | .cfi_restore_state 850 | movb $0, (%rax) 851 | movq (%rsp), %rdx 852 | movq 0(%rbp), %rax 853 | jmp .L113 854 | .L129: 855 | movq %rsp, %rdi 856 | .LEHB3: 857 | call _ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEE9_M_createERmm.isra.38.constprop.51 858 | .LEHE3: 859 | movq (%rsp), %rdx 860 | movq %rax, 0(%rbp) 861 | movq %rdx, 16(%rbp) 862 | jmp .L112 863 | .L122: 864 | movq %r13, %rdi 865 | movq %rax, %rbx 866 | call _ZNSt13random_device7_M_finiEv@PLT 867 | .L118: 868 | movq 16(%rsp), %rdi 869 | addq $16, %r12 870 | cmpq %r12, %rdi 871 | je .L119 872 | call _ZdlPv@PLT 873 | .L119: 874 | movq %rbx, %rdi 875 | .LEHB4: 876 | call _Unwind_Resume@PLT 877 | .LEHE4: 878 | .L121: 879 | movq %rax, %rbx 880 | jmp .L118 881 | .L131: 882 | call __stack_chk_fail@PLT 883 | .cfi_endproc 884 | .LFE3439: 885 | .globl __gxx_personality_v0 886 | .section .gcc_except_table,"a",@progbits 887 | .LLSDA3439: 888 | .byte 0xff 889 | .byte 0xff 890 | .byte 0x1 891 | .uleb128 .LLSDACSE3439-.LLSDACSB3439 892 | .LLSDACSB3439: 893 | .uleb128 .LEHB0-.LFB3439 894 | .uleb128 .LEHE0-.LEHB0 895 | .uleb128 0 896 | .uleb128 0 897 | .uleb128 .LEHB1-.LFB3439 898 | .uleb128 .LEHE1-.LEHB1 899 | .uleb128 .L121-.LFB3439 900 | .uleb128 0 901 | .uleb128 .LEHB2-.LFB3439 902 | .uleb128 .LEHE2-.LEHB2 903 | .uleb128 .L122-.LFB3439 904 | .uleb128 0 905 | .uleb128 .LEHB3-.LFB3439 906 | .uleb128 .LEHE3-.LEHB3 907 | .uleb128 0 908 | .uleb128 0 909 | .uleb128 .LEHB4-.LFB3439 910 | .uleb128 .LEHE4-.LEHB4 911 | .uleb128 0 912 | .uleb128 0 913 | .LLSDACSE3439: 914 | .text 915 | .size _Z22generate_random_stringB5cxx11j, .-_Z22generate_random_stringB5cxx11j 916 | .section .text._ZNSt6vectorINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESaIS5_EE17_M_realloc_insertIJS5_EEEvN9__gnu_cxx17__normal_iteratorIPS5_S7_EEDpOT_,"axG",@progbits,_ZNSt6vectorINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESaIS5_EE17_M_realloc_insertIJS5_EEEvN9__gnu_cxx17__normal_iteratorIPS5_S7_EEDpOT_,comdat 917 | .align 2 918 | .p2align 4,,15 919 | .weak _ZNSt6vectorINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESaIS5_EE17_M_realloc_insertIJS5_EEEvN9__gnu_cxx17__normal_iteratorIPS5_S7_EEDpOT_ 920 | .type _ZNSt6vectorINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESaIS5_EE17_M_realloc_insertIJS5_EEEvN9__gnu_cxx17__normal_iteratorIPS5_S7_EEDpOT_, @function 921 | _ZNSt6vectorINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESaIS5_EE17_M_realloc_insertIJS5_EEEvN9__gnu_cxx17__normal_iteratorIPS5_S7_EEDpOT_: 922 | .LFB4066: 923 | .cfi_startproc 924 | pushq %r15 925 | .cfi_def_cfa_offset 16 926 | .cfi_offset 15, -16 927 | pushq %r14 928 | .cfi_def_cfa_offset 24 929 | .cfi_offset 14, -24 930 | movq %rsi, %r15 931 | pushq %r13 932 | .cfi_def_cfa_offset 32 933 | .cfi_offset 13, -32 934 | pushq %r12 935 | .cfi_def_cfa_offset 40 936 | .cfi_offset 12, -40 937 | movq %rsi, %r13 938 | pushq %rbp 939 | .cfi_def_cfa_offset 48 940 | .cfi_offset 6, -48 941 | pushq %rbx 942 | .cfi_def_cfa_offset 56 943 | .cfi_offset 3, -56 944 | movq %rdi, %r14 945 | subq $24, %rsp 946 | .cfi_def_cfa_offset 80 947 | movq (%rdi), %rcx 948 | movq 8(%rdi), %rax 949 | subq %rcx, %rax 950 | subq %rcx, %r13 951 | sarq $5, %rax 952 | testq %rax, %rax 953 | je .L150 954 | leaq (%rax,%rax), %rcx 955 | movq $-32, %r12 956 | cmpq %rcx, %rax 957 | jbe .L161 958 | .L134: 959 | movq %r12, %rdi 960 | movq %rdx, 8(%rsp) 961 | movq %rsi, (%rsp) 962 | call _Znwm@PLT 963 | movq (%rsp), %rsi 964 | movq 8(%rsp), %rdx 965 | leaq 32(%rax), %rbx 966 | movq %rax, %rbp 967 | addq %rax, %r12 968 | .L135: 969 | addq %rbp, %r13 970 | movq (%rdx), %rcx 971 | leaq 16(%r13), %rax 972 | movq %rax, 0(%r13) 973 | leaq 16(%rdx), %rax 974 | cmpq %rax, %rcx 975 | je .L162 976 | movq %rcx, 0(%r13) 977 | movq 16(%rdx), %rcx 978 | movq %rcx, 16(%r13) 979 | .L137: 980 | movq 8(%rdx), %rcx 981 | movq %rax, (%rdx) 982 | movq $0, 8(%rdx) 983 | movb $0, 16(%rdx) 984 | movq (%r14), %rdx 985 | movq %rcx, 8(%r13) 986 | cmpq %rsi, %rdx 987 | je .L138 988 | leaq 16(%rdx), %rax 989 | movq %rsi, %rdi 990 | addq $32, %rdx 991 | subq %rdx, %rdi 992 | movq %rbp, %rbx 993 | movq %rdi, %rdx 994 | andq $-32, %rdx 995 | leaq 32(%rbp,%rdx), %rcx 996 | jmp .L141 997 | .p2align 4,,10 998 | .p2align 3 999 | .L139: 1000 | movq %rdx, (%rbx) 1001 | movq (%rax), %rdx 1002 | movq %rdx, 16(%rbx) 1003 | .L140: 1004 | movq -8(%rax), %rdx 1005 | movq %rdx, 8(%rbx) 1006 | leaq 32(%rbx), %rdx 1007 | movq $0, -8(%rax) 1008 | movb $0, (%rax) 1009 | movq %rax, -16(%rax) 1010 | addq $32, %rax 1011 | cmpq %rcx, %rdx 1012 | je .L163 1013 | movq %rdx, %rbx 1014 | .L141: 1015 | leaq 16(%rbx), %rdx 1016 | movq %rdx, (%rbx) 1017 | movq -16(%rax), %rdx 1018 | cmpq %rax, %rdx 1019 | jne .L139 1020 | movdqu (%rax), %xmm0 1021 | movups %xmm0, 16(%rbx) 1022 | jmp .L140 1023 | .p2align 4,,10 1024 | .p2align 3 1025 | .L163: 1026 | addq $64, %rbx 1027 | .L138: 1028 | movq 8(%r14), %rdx 1029 | cmpq %rsi, %rdx 1030 | je .L142 1031 | leaq 16(%rsi), %rax 1032 | addq $32, %rsi 1033 | subq %rsi, %rdx 1034 | andq $-32, %rdx 1035 | leaq 32(%rbx,%rdx), %rcx 1036 | jmp .L145 1037 | .p2align 4,,10 1038 | .p2align 3 1039 | .L143: 1040 | movq %rdx, (%rbx) 1041 | movq (%rax), %rdx 1042 | movq %rdx, 16(%rbx) 1043 | .L144: 1044 | movq -8(%rax), %rdx 1045 | addq $32, %rbx 1046 | movq $0, -8(%rax) 1047 | movb $0, (%rax) 1048 | movq %rax, -16(%rax) 1049 | addq $32, %rax 1050 | movq %rdx, -24(%rbx) 1051 | cmpq %rcx, %rbx 1052 | je .L164 1053 | .L145: 1054 | leaq 16(%rbx), %rdx 1055 | movq %rdx, (%rbx) 1056 | movq -16(%rax), %rdx 1057 | cmpq %rax, %rdx 1058 | jne .L143 1059 | movdqu (%rax), %xmm0 1060 | movups %xmm0, 16(%rbx) 1061 | jmp .L144 1062 | .p2align 4,,10 1063 | .p2align 3 1064 | .L164: 1065 | movq 8(%r14), %r15 1066 | .L142: 1067 | movq (%r14), %r13 1068 | cmpq %r15, %r13 1069 | je .L146 1070 | .p2align 4,,10 1071 | .p2align 3 1072 | .L148: 1073 | movq 0(%r13), %rdi 1074 | leaq 16(%r13), %rax 1075 | cmpq %rax, %rdi 1076 | je .L147 1077 | call _ZdlPv@PLT 1078 | .L147: 1079 | addq $32, %r13 1080 | cmpq %r15, %r13 1081 | jne .L148 1082 | movq (%r14), %r15 1083 | .L146: 1084 | testq %r15, %r15 1085 | je .L149 1086 | movq %r15, %rdi 1087 | call _ZdlPv@PLT 1088 | .L149: 1089 | movq %rbp, (%r14) 1090 | movq %rbx, 8(%r14) 1091 | movq %r12, 16(%r14) 1092 | addq $24, %rsp 1093 | .cfi_remember_state 1094 | .cfi_def_cfa_offset 56 1095 | popq %rbx 1096 | .cfi_def_cfa_offset 48 1097 | popq %rbp 1098 | .cfi_def_cfa_offset 40 1099 | popq %r12 1100 | .cfi_def_cfa_offset 32 1101 | popq %r13 1102 | .cfi_def_cfa_offset 24 1103 | popq %r14 1104 | .cfi_def_cfa_offset 16 1105 | popq %r15 1106 | .cfi_def_cfa_offset 8 1107 | ret 1108 | .p2align 4,,10 1109 | .p2align 3 1110 | .L150: 1111 | .cfi_restore_state 1112 | movl $1, %ecx 1113 | .L133: 1114 | salq $5, %rcx 1115 | movq %rcx, %r12 1116 | jmp .L134 1117 | .p2align 4,,10 1118 | .p2align 3 1119 | .L162: 1120 | movdqu 16(%rdx), %xmm0 1121 | movups %xmm0, 16(%r13) 1122 | jmp .L137 1123 | .p2align 4,,10 1124 | .p2align 3 1125 | .L161: 1126 | movabsq $576460752303423487, %rax 1127 | cmpq %rax, %rcx 1128 | ja .L134 1129 | testq %rcx, %rcx 1130 | jne .L133 1131 | movl $32, %ebx 1132 | xorl %r12d, %r12d 1133 | xorl %ebp, %ebp 1134 | jmp .L135 1135 | .cfi_endproc 1136 | .LFE4066: 1137 | .size _ZNSt6vectorINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESaIS5_EE17_M_realloc_insertIJS5_EEEvN9__gnu_cxx17__normal_iteratorIPS5_S7_EEDpOT_, .-_ZNSt6vectorINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESaIS5_EE17_M_realloc_insertIJS5_EEEvN9__gnu_cxx17__normal_iteratorIPS5_S7_EEDpOT_ 1138 | .section .text._ZNSt6vectorINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESaIS5_EE12emplace_backIJS5_EEERS5_DpOT_,"axG",@progbits,_ZNSt6vectorINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESaIS5_EE12emplace_backIJS5_EEERS5_DpOT_,comdat 1139 | .align 2 1140 | .p2align 4,,15 1141 | .weak _ZNSt6vectorINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESaIS5_EE12emplace_backIJS5_EEERS5_DpOT_ 1142 | .type _ZNSt6vectorINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESaIS5_EE12emplace_backIJS5_EEERS5_DpOT_, @function 1143 | _ZNSt6vectorINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESaIS5_EE12emplace_backIJS5_EEERS5_DpOT_: 1144 | .LFB3967: 1145 | .cfi_startproc 1146 | movq 8(%rdi), %rax 1147 | cmpq 16(%rdi), %rax 1148 | je .L166 1149 | leaq 16(%rax), %rdx 1150 | movq %rdx, (%rax) 1151 | movq (%rsi), %rcx 1152 | leaq 16(%rsi), %rdx 1153 | cmpq %rdx, %rcx 1154 | je .L173 1155 | movq %rcx, (%rax) 1156 | movq 16(%rsi), %rcx 1157 | movq %rcx, 16(%rax) 1158 | .L168: 1159 | movq 8(%rsi), %rcx 1160 | movq %rcx, 8(%rax) 1161 | movb $0, 16(%rsi) 1162 | movq 8(%rdi), %rax 1163 | movq %rdx, (%rsi) 1164 | movq $0, 8(%rsi) 1165 | addq $32, %rax 1166 | movq %rax, 8(%rdi) 1167 | subq $32, %rax 1168 | ret 1169 | .p2align 4,,10 1170 | .p2align 3 1171 | .L173: 1172 | movdqu 16(%rsi), %xmm0 1173 | movups %xmm0, 16(%rax) 1174 | jmp .L168 1175 | .p2align 4,,10 1176 | .p2align 3 1177 | .L166: 1178 | pushq %rbx 1179 | .cfi_def_cfa_offset 16 1180 | .cfi_offset 3, -16 1181 | movq %rsi, %rdx 1182 | movq %rdi, %rbx 1183 | movq %rax, %rsi 1184 | call _ZNSt6vectorINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESaIS5_EE17_M_realloc_insertIJS5_EEEvN9__gnu_cxx17__normal_iteratorIPS5_S7_EEDpOT_ 1185 | movq 8(%rbx), %rax 1186 | popq %rbx 1187 | .cfi_def_cfa_offset 8 1188 | subq $32, %rax 1189 | ret 1190 | .cfi_endproc 1191 | .LFE3967: 1192 | .size _ZNSt6vectorINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESaIS5_EE12emplace_backIJS5_EEERS5_DpOT_, .-_ZNSt6vectorINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESaIS5_EE12emplace_backIJS5_EEERS5_DpOT_ 1193 | .section .text.startup,"ax",@progbits 1194 | .p2align 4,,15 1195 | .globl main 1196 | .type main, @function 1197 | main: 1198 | .LFB3447: 1199 | .cfi_startproc 1200 | .cfi_personality 0x9b,DW.ref.__gxx_personality_v0 1201 | .cfi_lsda 0x1b,.LLSDA3447 1202 | pushq %r12 1203 | .cfi_def_cfa_offset 16 1204 | .cfi_offset 12, -16 1205 | pushq %rbp 1206 | .cfi_def_cfa_offset 24 1207 | .cfi_offset 6, -24 1208 | movl $3000000, %esi 1209 | pushq %rbx 1210 | .cfi_def_cfa_offset 32 1211 | .cfi_offset 3, -32 1212 | subq $96, %rsp 1213 | .cfi_def_cfa_offset 128 1214 | leaq 48(%rsp), %rbx 1215 | movq $0, 16(%rsp) 1216 | movq $0, 24(%rsp) 1217 | movq %fs:40, %rax 1218 | movq %rax, 88(%rsp) 1219 | xorl %eax, %eax 1220 | movq %rbx, %rdi 1221 | movq $0, 32(%rsp) 1222 | leaq 16(%rsp), %rbp 1223 | .LEHB5: 1224 | call _Z22generate_random_stringB5cxx11j 1225 | .LEHE5: 1226 | leaq 16(%rsp), %rbp 1227 | movq %rbx, %rsi 1228 | movq %rbp, %rdi 1229 | .LEHB6: 1230 | call _ZNSt6vectorINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESaIS5_EE12emplace_backIJS5_EEERS5_DpOT_ 1231 | .LEHE6: 1232 | movq 48(%rsp), %rdi 1233 | leaq 16(%rbx), %rax 1234 | cmpq %rax, %rdi 1235 | je .L175 1236 | call _ZdlPv@PLT 1237 | .L175: 1238 | movl $3000000, %esi 1239 | movq %rbx, %rdi 1240 | .LEHB7: 1241 | call _Z22generate_random_stringB5cxx11j 1242 | .LEHE7: 1243 | movq %rbx, %rsi 1244 | movq %rbp, %rdi 1245 | .LEHB8: 1246 | call _ZNSt6vectorINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESaIS5_EE12emplace_backIJS5_EEERS5_DpOT_ 1247 | .LEHE8: 1248 | movq 48(%rsp), %rdi 1249 | leaq 16(%rbx), %rax 1250 | cmpq %rax, %rdi 1251 | je .L176 1252 | call _ZdlPv@PLT 1253 | .L176: 1254 | movl $3000000, %esi 1255 | movq %rbx, %rdi 1256 | .LEHB9: 1257 | call _Z22generate_random_stringB5cxx11j 1258 | .LEHE9: 1259 | movq %rbx, %rsi 1260 | movq %rbp, %rdi 1261 | .LEHB10: 1262 | call _ZNSt6vectorINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESaIS5_EE12emplace_backIJS5_EEERS5_DpOT_ 1263 | .LEHE10: 1264 | movq 48(%rsp), %rdi 1265 | leaq 16(%rbx), %rax 1266 | cmpq %rax, %rdi 1267 | je .L177 1268 | call _ZdlPv@PLT 1269 | .L177: 1270 | movl $3000000, %esi 1271 | movq %rbx, %rdi 1272 | .LEHB11: 1273 | call _Z22generate_random_stringB5cxx11j 1274 | .LEHE11: 1275 | movq %rbx, %rsi 1276 | movq %rbp, %rdi 1277 | .LEHB12: 1278 | call _ZNSt6vectorINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESaIS5_EE12emplace_backIJS5_EEERS5_DpOT_ 1279 | .LEHE12: 1280 | movq 48(%rsp), %rdi 1281 | addq $16, %rbx 1282 | cmpq %rbx, %rdi 1283 | je .L178 1284 | call _ZdlPv@PLT 1285 | .L178: 1286 | movq 16(%rsp), %rsi 1287 | leaq _Z16str_to_lowercaseRNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE(%rip), %rdi 1288 | .LEHB13: 1289 | call _ZNKUlOT_DpOT0_E_clIRFNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERSB_EJSC_EEEDaS0_S3_.isra.26 1290 | leaq _ZSt4cout(%rip), %rdi 1291 | movq %rax, %rsi 1292 | call _ZNSo9_M_insertIlEERSoT_@PLT 1293 | movq %rax, %rdi 1294 | call _ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_@PLT 1295 | movq 16(%rsp), %rax 1296 | leaq _Z17str_to_lowercase2RNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE(%rip), %rdi 1297 | leaq 32(%rax), %rsi 1298 | call _ZNKUlOT_DpOT0_E_clIRFNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERSB_EJSC_EEEDaS0_S3_.isra.26 1299 | leaq _ZSt4cout(%rip), %rdi 1300 | movq %rax, %rsi 1301 | call _ZNSo9_M_insertIlEERSoT_@PLT 1302 | movq %rax, %rdi 1303 | call _ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_@PLT 1304 | movq 16(%rsp), %rax 1305 | leaq _Z17str_to_lowercase3RNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE(%rip), %rdi 1306 | leaq 64(%rax), %rsi 1307 | call _ZNKUlOT_DpOT0_E_clIRFNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERSB_EJSC_EEEDaS0_S3_.isra.26 1308 | leaq _ZSt4cout(%rip), %rdi 1309 | movq %rax, %rsi 1310 | call _ZNSo9_M_insertIlEERSoT_@PLT 1311 | movq %rax, %rdi 1312 | call _ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_@PLT 1313 | movq 16(%rsp), %rax 1314 | leaq _Z17str_to_lowercase4RNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE(%rip), %rdi 1315 | leaq 96(%rax), %rsi 1316 | call _ZNKUlOT_DpOT0_E_clIRFNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERSB_EJSC_EEEDaS0_S3_.isra.26 1317 | leaq _ZSt4cout(%rip), %rdi 1318 | movq %rax, %rsi 1319 | call _ZNSo9_M_insertIlEERSoT_@PLT 1320 | movq %rax, %rdi 1321 | call _ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_@PLT 1322 | .LEHE13: 1323 | movq %rbp, %rdi 1324 | call _ZNSt6vectorINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESaIS5_EED1Ev 1325 | xorl %eax, %eax 1326 | movq 88(%rsp), %rdx 1327 | xorq %fs:40, %rdx 1328 | jne .L201 1329 | addq $96, %rsp 1330 | .cfi_remember_state 1331 | .cfi_def_cfa_offset 32 1332 | popq %rbx 1333 | .cfi_def_cfa_offset 24 1334 | popq %rbp 1335 | .cfi_def_cfa_offset 16 1336 | popq %r12 1337 | .cfi_def_cfa_offset 8 1338 | ret 1339 | .L201: 1340 | .cfi_restore_state 1341 | call __stack_chk_fail@PLT 1342 | .L193: 1343 | .L198: 1344 | movq 48(%rsp), %rdi 1345 | addq $16, %rbx 1346 | movq %rax, %r12 1347 | cmpq %rbx, %rdi 1348 | je .L187 1349 | call _ZdlPv@PLT 1350 | .L187: 1351 | movq %r12, %rax 1352 | .L181: 1353 | movq %rbp, %rdi 1354 | movq %rax, 8(%rsp) 1355 | call _ZNSt6vectorINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESaIS5_EED1Ev 1356 | movq 8(%rsp), %rax 1357 | movq %rax, %rdi 1358 | .LEHB14: 1359 | call _Unwind_Resume@PLT 1360 | .LEHE14: 1361 | .L192: 1362 | jmp .L198 1363 | .L191: 1364 | jmp .L198 1365 | .L190: 1366 | jmp .L198 1367 | .L189: 1368 | jmp .L181 1369 | .cfi_endproc 1370 | .LFE3447: 1371 | .section .gcc_except_table 1372 | .LLSDA3447: 1373 | .byte 0xff 1374 | .byte 0xff 1375 | .byte 0x1 1376 | .uleb128 .LLSDACSE3447-.LLSDACSB3447 1377 | .LLSDACSB3447: 1378 | .uleb128 .LEHB5-.LFB3447 1379 | .uleb128 .LEHE5-.LEHB5 1380 | .uleb128 .L189-.LFB3447 1381 | .uleb128 0 1382 | .uleb128 .LEHB6-.LFB3447 1383 | .uleb128 .LEHE6-.LEHB6 1384 | .uleb128 .L190-.LFB3447 1385 | .uleb128 0 1386 | .uleb128 .LEHB7-.LFB3447 1387 | .uleb128 .LEHE7-.LEHB7 1388 | .uleb128 .L189-.LFB3447 1389 | .uleb128 0 1390 | .uleb128 .LEHB8-.LFB3447 1391 | .uleb128 .LEHE8-.LEHB8 1392 | .uleb128 .L191-.LFB3447 1393 | .uleb128 0 1394 | .uleb128 .LEHB9-.LFB3447 1395 | .uleb128 .LEHE9-.LEHB9 1396 | .uleb128 .L189-.LFB3447 1397 | .uleb128 0 1398 | .uleb128 .LEHB10-.LFB3447 1399 | .uleb128 .LEHE10-.LEHB10 1400 | .uleb128 .L192-.LFB3447 1401 | .uleb128 0 1402 | .uleb128 .LEHB11-.LFB3447 1403 | .uleb128 .LEHE11-.LEHB11 1404 | .uleb128 .L189-.LFB3447 1405 | .uleb128 0 1406 | .uleb128 .LEHB12-.LFB3447 1407 | .uleb128 .LEHE12-.LEHB12 1408 | .uleb128 .L193-.LFB3447 1409 | .uleb128 0 1410 | .uleb128 .LEHB13-.LFB3447 1411 | .uleb128 .LEHE13-.LEHB13 1412 | .uleb128 .L189-.LFB3447 1413 | .uleb128 0 1414 | .uleb128 .LEHB14-.LFB3447 1415 | .uleb128 .LEHE14-.LEHB14 1416 | .uleb128 0 1417 | .uleb128 0 1418 | .LLSDACSE3447: 1419 | .section .text.startup 1420 | .size main, .-main 1421 | .p2align 4,,15 1422 | .type _GLOBAL__sub_I__Z22generate_random_stringB5cxx11j, @function 1423 | _GLOBAL__sub_I__Z22generate_random_stringB5cxx11j: 1424 | .LFB4281: 1425 | .cfi_startproc 1426 | leaq _ZStL8__ioinit(%rip), %rdi 1427 | subq $8, %rsp 1428 | .cfi_def_cfa_offset 16 1429 | call _ZNSt8ios_base4InitC1Ev@PLT 1430 | movq _ZNSt8ios_base4InitD1Ev@GOTPCREL(%rip), %rdi 1431 | leaq __dso_handle(%rip), %rdx 1432 | leaq _ZStL8__ioinit(%rip), %rsi 1433 | addq $8, %rsp 1434 | .cfi_def_cfa_offset 8 1435 | jmp __cxa_atexit@PLT 1436 | .cfi_endproc 1437 | .LFE4281: 1438 | .size _GLOBAL__sub_I__Z22generate_random_stringB5cxx11j, .-_GLOBAL__sub_I__Z22generate_random_stringB5cxx11j 1439 | .section .init_array,"aw" 1440 | .align 8 1441 | .quad _GLOBAL__sub_I__Z22generate_random_stringB5cxx11j 1442 | .local _ZStL8__ioinit 1443 | .comm _ZStL8__ioinit,1,1 1444 | .hidden DW.ref.__gxx_personality_v0 1445 | .weak DW.ref.__gxx_personality_v0 1446 | .section .data.rel.local.DW.ref.__gxx_personality_v0,"awG",@progbits,DW.ref.__gxx_personality_v0,comdat 1447 | .align 8 1448 | .type DW.ref.__gxx_personality_v0, @object 1449 | .size DW.ref.__gxx_personality_v0, 8 1450 | DW.ref.__gxx_personality_v0: 1451 | .quad __gxx_personality_v0 1452 | .hidden __dso_handle 1453 | .ident "GCC: (Ubuntu 7.3.0-27ubuntu1~18.04) 7.3.0" 1454 | .section .note.GNU-stack,"",@progbits 1455 | --------------------------------------------------------------------------------