From 16ad0a72e59798fdde7dbd86d0e3fd99f6737622 Mon Sep 17 00:00:00 2001 From: ashukr07 Date: Thu, 9 Jan 2025 00:32:01 +0530 Subject: [PATCH 1/4] Add code snippets for math-and-numbers, searching, sorting, bit manipulation --- .../bit-manipulation/check-power-of-two.md | 16 +++++++++ .../find-non-repeating-number.md | 20 +++++++++++ snippets/cpp/bit-manipulation/reverse-bits.md | 21 ++++++++++++ .../cpp/bit-manipulation/set-bit-count.md | 20 +++++++++++ snippets/cpp/bit-manipulation/xor-of-range.md | 18 ++++++++++ snippets/cpp/debuging/vector-print.md | 29 ---------------- .../binary-to-decimal-conversion.md | 25 ++++++++++++++ .../math-and-numbers/check-perfect-number.md | 22 +++++++++++++ .../cpp/math-and-numbers/compound-interest.md | 21 ++++++++++++ snippets/cpp/math-and-numbers/factorial.md | 16 +++++++++ .../cpp/math-and-numbers/fibonacci-number.md | 16 +++++++++ snippets/cpp/math-and-numbers/gcd.md | 15 +++++++++ snippets/cpp/math-and-numbers/lcm.md | 15 +++++++++ .../cpp/math-and-numbers/sum-of-digits.md | 20 +++++++++++ snippets/cpp/searching/binary-search.md | 26 +++++++++++++++ snippets/cpp/searching/linear-search.md | 20 +++++++++++ snippets/cpp/sorting/bubble-sort.md | 22 +++++++++++++ snippets/cpp/sorting/insertion-sort.md | 24 ++++++++++++++ snippets/cpp/sorting/merge-sort.md | 33 +++++++++++++++++++ snippets/cpp/sorting/quick-sort.md | 32 ++++++++++++++++++ snippets/cpp/sorting/selection-sort.md | 24 ++++++++++++++ 21 files changed, 426 insertions(+), 29 deletions(-) create mode 100644 snippets/cpp/bit-manipulation/check-power-of-two.md create mode 100644 snippets/cpp/bit-manipulation/find-non-repeating-number.md create mode 100644 snippets/cpp/bit-manipulation/reverse-bits.md create mode 100644 snippets/cpp/bit-manipulation/set-bit-count.md create mode 100644 snippets/cpp/bit-manipulation/xor-of-range.md delete mode 100644 snippets/cpp/debuging/vector-print.md create mode 100644 snippets/cpp/math-and-numbers/binary-to-decimal-conversion.md create mode 100644 snippets/cpp/math-and-numbers/check-perfect-number.md create mode 100644 snippets/cpp/math-and-numbers/compound-interest.md create mode 100644 snippets/cpp/math-and-numbers/factorial.md create mode 100644 snippets/cpp/math-and-numbers/fibonacci-number.md create mode 100644 snippets/cpp/math-and-numbers/gcd.md create mode 100644 snippets/cpp/math-and-numbers/lcm.md create mode 100644 snippets/cpp/math-and-numbers/sum-of-digits.md create mode 100644 snippets/cpp/searching/binary-search.md create mode 100644 snippets/cpp/searching/linear-search.md create mode 100644 snippets/cpp/sorting/bubble-sort.md create mode 100644 snippets/cpp/sorting/insertion-sort.md create mode 100644 snippets/cpp/sorting/merge-sort.md create mode 100644 snippets/cpp/sorting/quick-sort.md create mode 100644 snippets/cpp/sorting/selection-sort.md diff --git a/snippets/cpp/bit-manipulation/check-power-of-two.md b/snippets/cpp/bit-manipulation/check-power-of-two.md new file mode 100644 index 00000000..88d41782 --- /dev/null +++ b/snippets/cpp/bit-manipulation/check-power-of-two.md @@ -0,0 +1,16 @@ +--- +title: Check if a Number is Power of Two +description: Checks if a given number is a power of two using bitwise operations. +tags: bit-manipulation, power-of-two +author: ashukr07 +--- + +```cpp +bool is_power_of_two(int n) { + return n > 0 && (n & (n - 1)) == 0; +} + +// Usage: +is_power_of_two(16); // Returns: true +is_power_of_two(18); // Returns: false +``` \ No newline at end of file diff --git a/snippets/cpp/bit-manipulation/find-non-repeating-number.md b/snippets/cpp/bit-manipulation/find-non-repeating-number.md new file mode 100644 index 00000000..1195587d --- /dev/null +++ b/snippets/cpp/bit-manipulation/find-non-repeating-number.md @@ -0,0 +1,20 @@ +--- +title: Find Non-Repeating Number +description: Finds the number that appears only once in an array where every other number appears twice. +tags: bit-manipulation, xor +author: ashukr07 +--- + +```cpp +int find_non_repeating(const std::vector& nums) { + int result = 0; + for (int num : nums) { + result ^= num; + } + return result; +} + +// Usage: +std::vector nums = {4, 1, 2, 1, 2}; +find_non_repeating(nums); // Returns: 4 +``` \ No newline at end of file diff --git a/snippets/cpp/bit-manipulation/reverse-bits.md b/snippets/cpp/bit-manipulation/reverse-bits.md new file mode 100644 index 00000000..52bd9210 --- /dev/null +++ b/snippets/cpp/bit-manipulation/reverse-bits.md @@ -0,0 +1,21 @@ +--- +title: Reverse Bits +description: Reverses the bits of a given integer. +tags: bit-manipulation, reverse-bits +author: ashukr07 +--- + +```cpp +unsigned int reverse_bits(unsigned int n) { + unsigned int result = 0; + for (int i = 0; i < 32; ++i) { + result <<= 1; + result |= n & 1; + n >>= 1; + } + return result; +} + +// Usage: +reverse_bits(43261596); // Returns: 964176192 (Binary: 00000010100101000001111010011100 -> 00111001011110000010100101000000) +``` \ No newline at end of file diff --git a/snippets/cpp/bit-manipulation/set-bit-count.md b/snippets/cpp/bit-manipulation/set-bit-count.md new file mode 100644 index 00000000..39611fc3 --- /dev/null +++ b/snippets/cpp/bit-manipulation/set-bit-count.md @@ -0,0 +1,20 @@ +--- +title: Count Set Bits +description: Counts the number of 1 bits in the binary representation of a number. +tags: bit-manipulation, set-bits +author: ashukr07 +--- + +```cpp +int count_set_bits(int n) { + int count = 0; + while (n) { + count += n & 1; + n >>= 1; + } + return count; +} + +// Usage: +count_set_bits(13); // Returns: 3 (Binary: 1101) +``` \ No newline at end of file diff --git a/snippets/cpp/bit-manipulation/xor-of-range.md b/snippets/cpp/bit-manipulation/xor-of-range.md new file mode 100644 index 00000000..4a37d990 --- /dev/null +++ b/snippets/cpp/bit-manipulation/xor-of-range.md @@ -0,0 +1,18 @@ +--- +title: XOR of Range +description: Finds XOR of all numbers from 1 to n using properties of XOR. +tags: bit-manipulation, xor +author: ashukr07 +--- + +```cpp +int xor_upto_n(int n) { + if (n % 4 == 0) return n; + if (n % 4 == 1) return 1; + if (n % 4 == 2) return n + 1; + return 0; +} + +// Usage: +xor_upto_n(5); // Returns: 1 (1 ^ 2 ^ 3 ^ 4 ^ 5 = 1) +``` diff --git a/snippets/cpp/debuging/vector-print.md b/snippets/cpp/debuging/vector-print.md deleted file mode 100644 index 9fe8550b..00000000 --- a/snippets/cpp/debuging/vector-print.md +++ /dev/null @@ -1,29 +0,0 @@ ---- -title: Vector Print -description: Overloads the << operator to print the contents of a vector just like in python. -author: Mohamed-faaris -tags: printing,debuging,vector ---- - -```cpp -#include -#include - -template -std::ostream& operator<<(std::ostream& os, const std::vector& vec) { - os << "["; - for (size_t i = 0; i < vec.size(); ++i) { - os << vec[i]; // Print each vector element - if (i != vec.size() - 1) { - os << ", "; // Add separator - } - } - os << "]"; - return os; // Return the stream -} - -// Usage: -std::vector numbers = {1, 2, 3, 4, 5}; -std::cout << numbers << std::endl; // Outputs: [1, 2, 3, 4, 5] - -``` diff --git a/snippets/cpp/math-and-numbers/binary-to-decimal-conversion.md b/snippets/cpp/math-and-numbers/binary-to-decimal-conversion.md new file mode 100644 index 00000000..b2a607cb --- /dev/null +++ b/snippets/cpp/math-and-numbers/binary-to-decimal-conversion.md @@ -0,0 +1,25 @@ +--- +title: Binary to Decimal Conversion +description: Converts a binary number represented as a string to its decimal equivalent. +tags: binary, conversion +author: ashukr07 +--- + +```cpp +int binary_to_decimal(const std::string& binary) { + int decimal = 0; + int base = 1; // Base value for the least significant bit + + for (int i = binary.length() - 1; i >= 0; --i) { + if (binary[i] == '1') { + decimal += base; + } + base *= 2; // Move to the next power of 2 + } + return decimal; +} + +// Usage: +std::string binary = "1011"; // Binary representation of 11 +binary_to_decimal(binary); // Returns: 11 +``` \ No newline at end of file diff --git a/snippets/cpp/math-and-numbers/check-perfect-number.md b/snippets/cpp/math-and-numbers/check-perfect-number.md new file mode 100644 index 00000000..41052dc8 --- /dev/null +++ b/snippets/cpp/math-and-numbers/check-perfect-number.md @@ -0,0 +1,22 @@ +--- +title: Check Perfect Number +description: Checks if a number is a perfect number. A perfect number is a positive integer that is equal to the sum of its proper divisors (excluding itself). +tags: math, perfect-number +author: ashukr07 +--- + +```cpp +bool is_perfect(int n) { + int sum = 1; // 1 is a divisor for all n > 1 + for (int i = 2; i * i <= n; ++i) { + if (n % i == 0) { + sum += i; + if (i != n / i) sum += n / i; + } + } + return sum == n && n != 1; +} + +// Usage: +is_perfect(28); // Returns: true +``` \ No newline at end of file diff --git a/snippets/cpp/math-and-numbers/compound-interest.md b/snippets/cpp/math-and-numbers/compound-interest.md new file mode 100644 index 00000000..8eeb24bd --- /dev/null +++ b/snippets/cpp/math-and-numbers/compound-interest.md @@ -0,0 +1,21 @@ +--- +title: Compound Interest +description: Calculates the compound interest for a given principal, rate, time, and number of times interest applied per time period. +tags: math, finance +author: ashukr07 +--- + +```cpp +#include + +double compound_interest(double principal, double rate, double time, int n) { + return principal * std::pow(1 + rate / n, n * time); +} + +// Usage: +double principal = 1000.0; // Initial amount +double rate = 0.05; // Annual interest rate (5%) +double time = 2; // Time in years +int n = 4; // Compounded quarterly +compound_interest(principal, rate, time, n); // Returns: 1104.081632653061 +``` \ No newline at end of file diff --git a/snippets/cpp/math-and-numbers/factorial.md b/snippets/cpp/math-and-numbers/factorial.md new file mode 100644 index 00000000..7cd83be6 --- /dev/null +++ b/snippets/cpp/math-and-numbers/factorial.md @@ -0,0 +1,16 @@ +--- +title: Factorial of a Number +description: Calculates the factorial of a given non-negative integer. +tags: math, factorial +author: ashukr07 +--- + +```cpp +int factorial(int n) { + if (n <= 1) return 1; // Base case + return n * factorial(n - 1); // Recursive step +} + +// Usage: +factorial(5); // Returns: 120 +``` \ No newline at end of file diff --git a/snippets/cpp/math-and-numbers/fibonacci-number.md b/snippets/cpp/math-and-numbers/fibonacci-number.md new file mode 100644 index 00000000..23196303 --- /dev/null +++ b/snippets/cpp/math-and-numbers/fibonacci-number.md @@ -0,0 +1,16 @@ +--- +title: Fibonacci Sequence +description: Calculates the nth Fibonacci number using recursion. +tags: math, fibonacci, recursion +author: ashukr07 +--- + +```cpp +int fibonacci(int n) { + if (n <= 1) return n; + return fibonacci(n - 1) + fibonacci(n - 2); +} + +// Usage: +fibonacci(6); // Returns: 8 +``` \ No newline at end of file diff --git a/snippets/cpp/math-and-numbers/gcd.md b/snippets/cpp/math-and-numbers/gcd.md new file mode 100644 index 00000000..b0b1fca7 --- /dev/null +++ b/snippets/cpp/math-and-numbers/gcd.md @@ -0,0 +1,15 @@ +--- +title: Calculate GCD +description: Computes the greatest common divisor (GCD) of two integers. +tags: math, gcd +author: ashukr07 +--- + +```cpp +int gcd(int a, int b) { + return b == 0 ? a : gcd(b, a % b); +} + +// Usage: +gcd(48, 18); // Returns: 6 +``` \ No newline at end of file diff --git a/snippets/cpp/math-and-numbers/lcm.md b/snippets/cpp/math-and-numbers/lcm.md new file mode 100644 index 00000000..40e05c4e --- /dev/null +++ b/snippets/cpp/math-and-numbers/lcm.md @@ -0,0 +1,15 @@ +--- +title: Calculate LCM +description: Computes the least common multiple (LCM) of two integers. +tags: math, lcm +author: ashukr07 +--- + +```cpp +int lcm(int a, int b) { + return (a / gcd(a, b)) * b; // Using GCD to calculate LCM +} + +// Usage: +lcm(12, 18); // Returns: 36 +``` \ No newline at end of file diff --git a/snippets/cpp/math-and-numbers/sum-of-digits.md b/snippets/cpp/math-and-numbers/sum-of-digits.md new file mode 100644 index 00000000..766eaf9f --- /dev/null +++ b/snippets/cpp/math-and-numbers/sum-of-digits.md @@ -0,0 +1,20 @@ +--- +title: Sum of Digits +description: Calculates the sum of the digits of an integer. +tags: math, digits +author: ashukr07 +--- + +```cpp +int sum_of_digits(int n) { + int sum = 0; + while (n != 0) { + sum += n % 10; + n /= 10; + } + return sum; +} + +// Usage: +sum_of_digits(123); // Returns: 6 +``` \ No newline at end of file diff --git a/snippets/cpp/searching/binary-search.md b/snippets/cpp/searching/binary-search.md new file mode 100644 index 00000000..928fbd23 --- /dev/null +++ b/snippets/cpp/searching/binary-search.md @@ -0,0 +1,26 @@ +--- +title: Binary Search +description: Searches for a target value in a sorted array using binary search. +tags: searching, binary-search +author: ashukr07 +--- + +```cpp +int binary_search(const std::vector& arr, int target) { + int left = 0, right = arr.size() - 1; + while (left <= right) { + int mid = left + (right - left) / 2; // Avoids overflow + if (arr[mid] == target) return mid; + if (arr[mid] < target) + left = mid + 1; + else + right = mid - 1; + } + return -1; // Element not found +} + +// Usage: +std::vector nums = {1, 3, 5, 7, 9}; +binary_search(nums, 7); // Returns: 3 (index of element) +binary_search(nums, 2); // Returns: -1 (not found) +``` \ No newline at end of file diff --git a/snippets/cpp/searching/linear-search.md b/snippets/cpp/searching/linear-search.md new file mode 100644 index 00000000..faf1bb81 --- /dev/null +++ b/snippets/cpp/searching/linear-search.md @@ -0,0 +1,20 @@ +--- +title: Linear Search +description: Searches for a target value in an array using linear search. +tags: searching, linear-search +author: ashukr07 +--- + +```cpp +int linear_search(const std::vector& arr, int target) { + for (int i = 0; i < arr.size(); ++i) { + if (arr[i] == target) return i; // Return index if found + } + return -1; // Element not found +} + +// Usage: +std::vector nums = {4, 2, 8, 5, 1}; +linear_search(nums, 5); // Returns: 3 (index of element) +linear_search(nums, 10); // Returns: -1 (not found) +``` \ No newline at end of file diff --git a/snippets/cpp/sorting/bubble-sort.md b/snippets/cpp/sorting/bubble-sort.md new file mode 100644 index 00000000..cea22a85 --- /dev/null +++ b/snippets/cpp/sorting/bubble-sort.md @@ -0,0 +1,22 @@ +--- +title: Bubble Sort +description: Sorts an array using the bubble sort algorithm. +tags: sorting, bubble-sort +author: ashukr07 +--- + +```cpp +void bubble_sort(std::vector& arr) { + for (int i = 0; i < arr.size() - 1; ++i) { + for (int j = 0; j < arr.size() - i - 1; ++j) { + if (arr[j] > arr[j + 1]) { + std::swap(arr[j], arr[j + 1]); + } + } + } +} + +// Usage: +std::vector nums = {5, 3, 8, 6, 2}; +bubble_sort(nums); // nums becomes: {2, 3, 5, 6, 8} +``` \ No newline at end of file diff --git a/snippets/cpp/sorting/insertion-sort.md b/snippets/cpp/sorting/insertion-sort.md new file mode 100644 index 00000000..a3788ba7 --- /dev/null +++ b/snippets/cpp/sorting/insertion-sort.md @@ -0,0 +1,24 @@ +--- +title: Insertion Sort +description: Sorts an array using the insertion sort algorithm. +tags: sorting, insertion-sort +author: ashukr07 +--- + +```cpp +void insertion_sort(std::vector& arr) { + for (int i = 1; i < arr.size(); ++i) { + int key = arr[i]; + int j = i - 1; + while (j >= 0 && arr[j] > key) { + arr[j + 1] = arr[j]; + --j; + } + arr[j + 1] = key; + } +} + +// Usage: +std::vector nums = {12, 11, 13, 5, 6}; +insertion_sort(nums); // nums becomes: {5, 6, 11, 12, 13} +``` \ No newline at end of file diff --git a/snippets/cpp/sorting/merge-sort.md b/snippets/cpp/sorting/merge-sort.md new file mode 100644 index 00000000..f0c79ab7 --- /dev/null +++ b/snippets/cpp/sorting/merge-sort.md @@ -0,0 +1,33 @@ +--- +title: Merge Sort +description: Sorts an array using the merge sort algorithm. +tags: sorting, merge-sort +author: ashukr07 +--- + +```cpp +void merge(std::vector& arr, int left, int mid, int right) { + int n1 = mid - left + 1; + int n2 = right - mid; + std::vector L(n1), R(n2); + for (int i = 0; i < n1; ++i) L[i] = arr[left + i]; + for (int j = 0; j < n2; ++j) R[j] = arr[mid + 1 + j]; + int i = 0, j = 0, k = left; + while (i < n1 && j < n2) arr[k++] = (L[i] <= R[j]) ? L[i++] : R[j++]; + while (i < n1) arr[k++] = L[i++]; + while (j < n2) arr[k++] = R[j++]; +} + +void merge_sort(std::vector& arr, int left, int right) { + if (left < right) { + int mid = left + (right - left) / 2; + merge_sort(arr, left, mid); + merge_sort(arr, mid + 1, right); + merge(arr, left, mid, right); + } +} + +// Usage: +std::vector nums = {38, 27, 43, 3, 9, 82, 10}; +merge_sort(nums, 0, nums.size() - 1); // nums becomes: {3, 9, 10, 27, 38, 43, 82} +``` \ No newline at end of file diff --git a/snippets/cpp/sorting/quick-sort.md b/snippets/cpp/sorting/quick-sort.md new file mode 100644 index 00000000..6699c200 --- /dev/null +++ b/snippets/cpp/sorting/quick-sort.md @@ -0,0 +1,32 @@ +--- +title: Quick Sort +description: Sorts an array using the quick sort algorithm. +tags: sorting, quick-sort +author: ashukr07 +--- + +```cpp +void quick_sort(std::vector& arr, int low, int high) { + auto partition = [](std::vector& arr, int low, int high) { + int pivot = arr[high]; + int i = low - 1; + for (int j = low; j < high; ++j) { + if (arr[j] <= pivot) { + std::swap(arr[++i], arr[j]); + } + } + std::swap(arr[i + 1], arr[high]); + return i + 1; + }; + + if (low < high) { + int pi = partition(arr, low, high); + quick_sort(arr, low, pi - 1); + quick_sort(arr, pi + 1, high); + } +} + +// Usage: +std::vector nums = {10, 7, 8, 9, 1, 5}; +quick_sort(nums, 0, nums.size() - 1); // nums becomes: {1, 5, 7, 8, 9, 10} +``` \ No newline at end of file diff --git a/snippets/cpp/sorting/selection-sort.md b/snippets/cpp/sorting/selection-sort.md new file mode 100644 index 00000000..94da3c87 --- /dev/null +++ b/snippets/cpp/sorting/selection-sort.md @@ -0,0 +1,24 @@ +--- +title: Selection Sort +description: Sorts an array using the selection sort algorithm. +tags: sorting, selection-sort +author: ashukr07 +--- + +```cpp +void selection_sort(std::vector& arr) { + for (int i = 0; i < arr.size() - 1; ++i) { + int min_idx = i; + for (int j = i + 1; j < arr.size(); ++j) { + if (arr[j] < arr[min_idx]) { + min_idx = j; + } + } + std::swap(arr[i], arr[min_idx]); + } +} + +// Usage: +std::vector nums = {64, 25, 12, 22, 11}; +selection_sort(nums); // nums becomes: {11, 12, 22, 25, 64} +``` \ No newline at end of file From 53ce70c027b2a31b06ca0a8a6bc3c9f5d85d8c0c Mon Sep 17 00:00:00 2001 From: ashukr07 Date: Thu, 9 Jan 2025 00:46:17 +0530 Subject: [PATCH 2/4] Add code snippets for math-and-numbers, searching, sorting, bit manipulation --- snippets/cpp/bit-manipulation/check-power-of-two.md | 2 +- .../bit-manipulation/{set-bit-count.md => count-set-bits.md} | 0 snippets/cpp/math-and-numbers/factorial.md | 2 +- snippets/cpp/math-and-numbers/fibonacci-number.md | 2 +- snippets/cpp/math-and-numbers/gcd.md | 2 +- snippets/cpp/math-and-numbers/lcm.md | 2 +- 6 files changed, 5 insertions(+), 5 deletions(-) rename snippets/cpp/bit-manipulation/{set-bit-count.md => count-set-bits.md} (100%) diff --git a/snippets/cpp/bit-manipulation/check-power-of-two.md b/snippets/cpp/bit-manipulation/check-power-of-two.md index 88d41782..f7ab40ec 100644 --- a/snippets/cpp/bit-manipulation/check-power-of-two.md +++ b/snippets/cpp/bit-manipulation/check-power-of-two.md @@ -1,5 +1,5 @@ --- -title: Check if a Number is Power of Two +title: Check Power of Two description: Checks if a given number is a power of two using bitwise operations. tags: bit-manipulation, power-of-two author: ashukr07 diff --git a/snippets/cpp/bit-manipulation/set-bit-count.md b/snippets/cpp/bit-manipulation/count-set-bits.md similarity index 100% rename from snippets/cpp/bit-manipulation/set-bit-count.md rename to snippets/cpp/bit-manipulation/count-set-bits.md diff --git a/snippets/cpp/math-and-numbers/factorial.md b/snippets/cpp/math-and-numbers/factorial.md index 7cd83be6..64a572aa 100644 --- a/snippets/cpp/math-and-numbers/factorial.md +++ b/snippets/cpp/math-and-numbers/factorial.md @@ -1,5 +1,5 @@ --- -title: Factorial of a Number +title: Factorial description: Calculates the factorial of a given non-negative integer. tags: math, factorial author: ashukr07 diff --git a/snippets/cpp/math-and-numbers/fibonacci-number.md b/snippets/cpp/math-and-numbers/fibonacci-number.md index 23196303..bbd5c835 100644 --- a/snippets/cpp/math-and-numbers/fibonacci-number.md +++ b/snippets/cpp/math-and-numbers/fibonacci-number.md @@ -1,5 +1,5 @@ --- -title: Fibonacci Sequence +title: Fibonacci Number description: Calculates the nth Fibonacci number using recursion. tags: math, fibonacci, recursion author: ashukr07 diff --git a/snippets/cpp/math-and-numbers/gcd.md b/snippets/cpp/math-and-numbers/gcd.md index b0b1fca7..d2c1440b 100644 --- a/snippets/cpp/math-and-numbers/gcd.md +++ b/snippets/cpp/math-and-numbers/gcd.md @@ -1,5 +1,5 @@ --- -title: Calculate GCD +title: GCD description: Computes the greatest common divisor (GCD) of two integers. tags: math, gcd author: ashukr07 diff --git a/snippets/cpp/math-and-numbers/lcm.md b/snippets/cpp/math-and-numbers/lcm.md index 40e05c4e..8b2a0b50 100644 --- a/snippets/cpp/math-and-numbers/lcm.md +++ b/snippets/cpp/math-and-numbers/lcm.md @@ -1,5 +1,5 @@ --- -title: Calculate LCM +title: LCM description: Computes the least common multiple (LCM) of two integers. tags: math, lcm author: ashukr07 From f95b751cc5fa841a0afef04c53ccfcde316d747e Mon Sep 17 00:00:00 2001 From: ashukr07 Date: Fri, 10 Jan 2025 11:15:30 +0530 Subject: [PATCH 3/4] move bit manipulation to c --- .../bit-manipulation/check-power-of-two.md | 6 ++-- .../bit-manipulation/reverse-bits.md | 10 +++--- .../bit-manipulation/xor-of-range.md | 4 +-- .../cpp/bit-manipulation/count-set-bits.md | 20 ----------- snippets/cpp/math-and-numbers/gcd.md | 15 --------- snippets/cpp/math-and-numbers/lcm.md | 15 --------- snippets/cpp/searching/binary-search.md | 26 --------------- snippets/cpp/searching/linear-search.md | 20 ----------- snippets/cpp/sorting/bubble-sort.md | 22 ------------- snippets/cpp/sorting/insertion-sort.md | 24 -------------- snippets/cpp/sorting/merge-sort.md | 33 ------------------- snippets/cpp/sorting/quick-sort.md | 32 ------------------ snippets/cpp/sorting/selection-sort.md | 24 -------------- 13 files changed, 11 insertions(+), 240 deletions(-) rename snippets/{cpp => c}/bit-manipulation/check-power-of-two.md (67%) rename snippets/{cpp => c}/bit-manipulation/reverse-bits.md (59%) rename snippets/{cpp => c}/bit-manipulation/xor-of-range.md (96%) delete mode 100644 snippets/cpp/bit-manipulation/count-set-bits.md delete mode 100644 snippets/cpp/math-and-numbers/gcd.md delete mode 100644 snippets/cpp/math-and-numbers/lcm.md delete mode 100644 snippets/cpp/searching/binary-search.md delete mode 100644 snippets/cpp/searching/linear-search.md delete mode 100644 snippets/cpp/sorting/bubble-sort.md delete mode 100644 snippets/cpp/sorting/insertion-sort.md delete mode 100644 snippets/cpp/sorting/merge-sort.md delete mode 100644 snippets/cpp/sorting/quick-sort.md delete mode 100644 snippets/cpp/sorting/selection-sort.md diff --git a/snippets/cpp/bit-manipulation/check-power-of-two.md b/snippets/c/bit-manipulation/check-power-of-two.md similarity index 67% rename from snippets/cpp/bit-manipulation/check-power-of-two.md rename to snippets/c/bit-manipulation/check-power-of-two.md index f7ab40ec..904c6bac 100644 --- a/snippets/cpp/bit-manipulation/check-power-of-two.md +++ b/snippets/c/bit-manipulation/check-power-of-two.md @@ -5,9 +5,11 @@ tags: bit-manipulation, power-of-two author: ashukr07 --- -```cpp +```c +#include // Include the standard boolean library + bool is_power_of_two(int n) { - return n > 0 && (n & (n - 1)) == 0; + return n > 0 && (n & (n - 1)) == 0; // Bitwise check for power of two } // Usage: diff --git a/snippets/cpp/bit-manipulation/reverse-bits.md b/snippets/c/bit-manipulation/reverse-bits.md similarity index 59% rename from snippets/cpp/bit-manipulation/reverse-bits.md rename to snippets/c/bit-manipulation/reverse-bits.md index 52bd9210..23c6279f 100644 --- a/snippets/cpp/bit-manipulation/reverse-bits.md +++ b/snippets/c/bit-manipulation/reverse-bits.md @@ -1,17 +1,17 @@ --- title: Reverse Bits -description: Reverses the bits of a given integer. +description: Reverses the bits of a given unsigned integer. tags: bit-manipulation, reverse-bits author: ashukr07 --- -```cpp +```c unsigned int reverse_bits(unsigned int n) { unsigned int result = 0; for (int i = 0; i < 32; ++i) { - result <<= 1; - result |= n & 1; - n >>= 1; + result <<= 1; // Shift result left by 1 + result |= n & 1; // Add the least significant bit of n to result + n >>= 1; // Shift n right by 1 } return result; } diff --git a/snippets/cpp/bit-manipulation/xor-of-range.md b/snippets/c/bit-manipulation/xor-of-range.md similarity index 96% rename from snippets/cpp/bit-manipulation/xor-of-range.md rename to snippets/c/bit-manipulation/xor-of-range.md index 4a37d990..4afed9bd 100644 --- a/snippets/cpp/bit-manipulation/xor-of-range.md +++ b/snippets/c/bit-manipulation/xor-of-range.md @@ -5,7 +5,7 @@ tags: bit-manipulation, xor author: ashukr07 --- -```cpp +```c int xor_upto_n(int n) { if (n % 4 == 0) return n; if (n % 4 == 1) return 1; @@ -15,4 +15,4 @@ int xor_upto_n(int n) { // Usage: xor_upto_n(5); // Returns: 1 (1 ^ 2 ^ 3 ^ 4 ^ 5 = 1) -``` +``` \ No newline at end of file diff --git a/snippets/cpp/bit-manipulation/count-set-bits.md b/snippets/cpp/bit-manipulation/count-set-bits.md deleted file mode 100644 index 39611fc3..00000000 --- a/snippets/cpp/bit-manipulation/count-set-bits.md +++ /dev/null @@ -1,20 +0,0 @@ ---- -title: Count Set Bits -description: Counts the number of 1 bits in the binary representation of a number. -tags: bit-manipulation, set-bits -author: ashukr07 ---- - -```cpp -int count_set_bits(int n) { - int count = 0; - while (n) { - count += n & 1; - n >>= 1; - } - return count; -} - -// Usage: -count_set_bits(13); // Returns: 3 (Binary: 1101) -``` \ No newline at end of file diff --git a/snippets/cpp/math-and-numbers/gcd.md b/snippets/cpp/math-and-numbers/gcd.md deleted file mode 100644 index d2c1440b..00000000 --- a/snippets/cpp/math-and-numbers/gcd.md +++ /dev/null @@ -1,15 +0,0 @@ ---- -title: GCD -description: Computes the greatest common divisor (GCD) of two integers. -tags: math, gcd -author: ashukr07 ---- - -```cpp -int gcd(int a, int b) { - return b == 0 ? a : gcd(b, a % b); -} - -// Usage: -gcd(48, 18); // Returns: 6 -``` \ No newline at end of file diff --git a/snippets/cpp/math-and-numbers/lcm.md b/snippets/cpp/math-and-numbers/lcm.md deleted file mode 100644 index 8b2a0b50..00000000 --- a/snippets/cpp/math-and-numbers/lcm.md +++ /dev/null @@ -1,15 +0,0 @@ ---- -title: LCM -description: Computes the least common multiple (LCM) of two integers. -tags: math, lcm -author: ashukr07 ---- - -```cpp -int lcm(int a, int b) { - return (a / gcd(a, b)) * b; // Using GCD to calculate LCM -} - -// Usage: -lcm(12, 18); // Returns: 36 -``` \ No newline at end of file diff --git a/snippets/cpp/searching/binary-search.md b/snippets/cpp/searching/binary-search.md deleted file mode 100644 index 928fbd23..00000000 --- a/snippets/cpp/searching/binary-search.md +++ /dev/null @@ -1,26 +0,0 @@ ---- -title: Binary Search -description: Searches for a target value in a sorted array using binary search. -tags: searching, binary-search -author: ashukr07 ---- - -```cpp -int binary_search(const std::vector& arr, int target) { - int left = 0, right = arr.size() - 1; - while (left <= right) { - int mid = left + (right - left) / 2; // Avoids overflow - if (arr[mid] == target) return mid; - if (arr[mid] < target) - left = mid + 1; - else - right = mid - 1; - } - return -1; // Element not found -} - -// Usage: -std::vector nums = {1, 3, 5, 7, 9}; -binary_search(nums, 7); // Returns: 3 (index of element) -binary_search(nums, 2); // Returns: -1 (not found) -``` \ No newline at end of file diff --git a/snippets/cpp/searching/linear-search.md b/snippets/cpp/searching/linear-search.md deleted file mode 100644 index faf1bb81..00000000 --- a/snippets/cpp/searching/linear-search.md +++ /dev/null @@ -1,20 +0,0 @@ ---- -title: Linear Search -description: Searches for a target value in an array using linear search. -tags: searching, linear-search -author: ashukr07 ---- - -```cpp -int linear_search(const std::vector& arr, int target) { - for (int i = 0; i < arr.size(); ++i) { - if (arr[i] == target) return i; // Return index if found - } - return -1; // Element not found -} - -// Usage: -std::vector nums = {4, 2, 8, 5, 1}; -linear_search(nums, 5); // Returns: 3 (index of element) -linear_search(nums, 10); // Returns: -1 (not found) -``` \ No newline at end of file diff --git a/snippets/cpp/sorting/bubble-sort.md b/snippets/cpp/sorting/bubble-sort.md deleted file mode 100644 index cea22a85..00000000 --- a/snippets/cpp/sorting/bubble-sort.md +++ /dev/null @@ -1,22 +0,0 @@ ---- -title: Bubble Sort -description: Sorts an array using the bubble sort algorithm. -tags: sorting, bubble-sort -author: ashukr07 ---- - -```cpp -void bubble_sort(std::vector& arr) { - for (int i = 0; i < arr.size() - 1; ++i) { - for (int j = 0; j < arr.size() - i - 1; ++j) { - if (arr[j] > arr[j + 1]) { - std::swap(arr[j], arr[j + 1]); - } - } - } -} - -// Usage: -std::vector nums = {5, 3, 8, 6, 2}; -bubble_sort(nums); // nums becomes: {2, 3, 5, 6, 8} -``` \ No newline at end of file diff --git a/snippets/cpp/sorting/insertion-sort.md b/snippets/cpp/sorting/insertion-sort.md deleted file mode 100644 index a3788ba7..00000000 --- a/snippets/cpp/sorting/insertion-sort.md +++ /dev/null @@ -1,24 +0,0 @@ ---- -title: Insertion Sort -description: Sorts an array using the insertion sort algorithm. -tags: sorting, insertion-sort -author: ashukr07 ---- - -```cpp -void insertion_sort(std::vector& arr) { - for (int i = 1; i < arr.size(); ++i) { - int key = arr[i]; - int j = i - 1; - while (j >= 0 && arr[j] > key) { - arr[j + 1] = arr[j]; - --j; - } - arr[j + 1] = key; - } -} - -// Usage: -std::vector nums = {12, 11, 13, 5, 6}; -insertion_sort(nums); // nums becomes: {5, 6, 11, 12, 13} -``` \ No newline at end of file diff --git a/snippets/cpp/sorting/merge-sort.md b/snippets/cpp/sorting/merge-sort.md deleted file mode 100644 index f0c79ab7..00000000 --- a/snippets/cpp/sorting/merge-sort.md +++ /dev/null @@ -1,33 +0,0 @@ ---- -title: Merge Sort -description: Sorts an array using the merge sort algorithm. -tags: sorting, merge-sort -author: ashukr07 ---- - -```cpp -void merge(std::vector& arr, int left, int mid, int right) { - int n1 = mid - left + 1; - int n2 = right - mid; - std::vector L(n1), R(n2); - for (int i = 0; i < n1; ++i) L[i] = arr[left + i]; - for (int j = 0; j < n2; ++j) R[j] = arr[mid + 1 + j]; - int i = 0, j = 0, k = left; - while (i < n1 && j < n2) arr[k++] = (L[i] <= R[j]) ? L[i++] : R[j++]; - while (i < n1) arr[k++] = L[i++]; - while (j < n2) arr[k++] = R[j++]; -} - -void merge_sort(std::vector& arr, int left, int right) { - if (left < right) { - int mid = left + (right - left) / 2; - merge_sort(arr, left, mid); - merge_sort(arr, mid + 1, right); - merge(arr, left, mid, right); - } -} - -// Usage: -std::vector nums = {38, 27, 43, 3, 9, 82, 10}; -merge_sort(nums, 0, nums.size() - 1); // nums becomes: {3, 9, 10, 27, 38, 43, 82} -``` \ No newline at end of file diff --git a/snippets/cpp/sorting/quick-sort.md b/snippets/cpp/sorting/quick-sort.md deleted file mode 100644 index 6699c200..00000000 --- a/snippets/cpp/sorting/quick-sort.md +++ /dev/null @@ -1,32 +0,0 @@ ---- -title: Quick Sort -description: Sorts an array using the quick sort algorithm. -tags: sorting, quick-sort -author: ashukr07 ---- - -```cpp -void quick_sort(std::vector& arr, int low, int high) { - auto partition = [](std::vector& arr, int low, int high) { - int pivot = arr[high]; - int i = low - 1; - for (int j = low; j < high; ++j) { - if (arr[j] <= pivot) { - std::swap(arr[++i], arr[j]); - } - } - std::swap(arr[i + 1], arr[high]); - return i + 1; - }; - - if (low < high) { - int pi = partition(arr, low, high); - quick_sort(arr, low, pi - 1); - quick_sort(arr, pi + 1, high); - } -} - -// Usage: -std::vector nums = {10, 7, 8, 9, 1, 5}; -quick_sort(nums, 0, nums.size() - 1); // nums becomes: {1, 5, 7, 8, 9, 10} -``` \ No newline at end of file diff --git a/snippets/cpp/sorting/selection-sort.md b/snippets/cpp/sorting/selection-sort.md deleted file mode 100644 index 94da3c87..00000000 --- a/snippets/cpp/sorting/selection-sort.md +++ /dev/null @@ -1,24 +0,0 @@ ---- -title: Selection Sort -description: Sorts an array using the selection sort algorithm. -tags: sorting, selection-sort -author: ashukr07 ---- - -```cpp -void selection_sort(std::vector& arr) { - for (int i = 0; i < arr.size() - 1; ++i) { - int min_idx = i; - for (int j = i + 1; j < arr.size(); ++j) { - if (arr[j] < arr[min_idx]) { - min_idx = j; - } - } - std::swap(arr[i], arr[min_idx]); - } -} - -// Usage: -std::vector nums = {64, 25, 12, 22, 11}; -selection_sort(nums); // nums becomes: {11, 12, 22, 25, 64} -``` \ No newline at end of file From 5a6d50b34494a7830510a33bd7e153135f6e031d Mon Sep 17 00:00:00 2001 From: ashukr07 Date: Fri, 10 Jan 2025 23:26:58 +0530 Subject: [PATCH 4/4] moved some snippets from C++ to C --- .../check-perfect-number.md | 12 +++++++++--- .../mathematical-functions}/compound-interest.md | 8 +++++--- .../mathematical-functions}/fibonacci-number.md | 3 ++- .../mathematical-functions}/sum-of-digits.md | 3 ++- snippets/cpp/math-and-numbers/factorial.md | 16 ---------------- 5 files changed, 18 insertions(+), 24 deletions(-) rename snippets/{cpp/math-and-numbers => c/mathematical-functions}/check-perfect-number.md (74%) rename snippets/{cpp/math-and-numbers => c/mathematical-functions}/compound-interest.md (82%) rename snippets/{cpp/math-and-numbers => c/mathematical-functions}/fibonacci-number.md (84%) rename snippets/{cpp/math-and-numbers => c/mathematical-functions}/sum-of-digits.md (82%) delete mode 100644 snippets/cpp/math-and-numbers/factorial.md diff --git a/snippets/cpp/math-and-numbers/check-perfect-number.md b/snippets/c/mathematical-functions/check-perfect-number.md similarity index 74% rename from snippets/cpp/math-and-numbers/check-perfect-number.md rename to snippets/c/mathematical-functions/check-perfect-number.md index 41052dc8..a155378a 100644 --- a/snippets/cpp/math-and-numbers/check-perfect-number.md +++ b/snippets/c/mathematical-functions/check-perfect-number.md @@ -5,8 +5,13 @@ tags: math, perfect-number author: ashukr07 --- -```cpp +```c +#include + +// Function to check if a number is a perfect number bool is_perfect(int n) { + if (n <= 1) return false; + int sum = 1; // 1 is a divisor for all n > 1 for (int i = 2; i * i <= n; ++i) { if (n % i == 0) { @@ -14,9 +19,10 @@ bool is_perfect(int n) { if (i != n / i) sum += n / i; } } - return sum == n && n != 1; + return sum == n; } -// Usage: +// Usage is_perfect(28); // Returns: true +is_perfect(12); // Returns: false ``` \ No newline at end of file diff --git a/snippets/cpp/math-and-numbers/compound-interest.md b/snippets/c/mathematical-functions/compound-interest.md similarity index 82% rename from snippets/cpp/math-and-numbers/compound-interest.md rename to snippets/c/mathematical-functions/compound-interest.md index 8eeb24bd..230b3b4e 100644 --- a/snippets/cpp/math-and-numbers/compound-interest.md +++ b/snippets/c/mathematical-functions/compound-interest.md @@ -5,11 +5,12 @@ tags: math, finance author: ashukr07 --- -```cpp -#include +```c +#include +// Function to calculate compound interest double compound_interest(double principal, double rate, double time, int n) { - return principal * std::pow(1 + rate / n, n * time); + return principal * pow(1 + rate / n, n * time); } // Usage: @@ -17,5 +18,6 @@ double principal = 1000.0; // Initial amount double rate = 0.05; // Annual interest rate (5%) double time = 2; // Time in years int n = 4; // Compounded quarterly + compound_interest(principal, rate, time, n); // Returns: 1104.081632653061 ``` \ No newline at end of file diff --git a/snippets/cpp/math-and-numbers/fibonacci-number.md b/snippets/c/mathematical-functions/fibonacci-number.md similarity index 84% rename from snippets/cpp/math-and-numbers/fibonacci-number.md rename to snippets/c/mathematical-functions/fibonacci-number.md index bbd5c835..8855718f 100644 --- a/snippets/cpp/math-and-numbers/fibonacci-number.md +++ b/snippets/c/mathematical-functions/fibonacci-number.md @@ -5,7 +5,8 @@ tags: math, fibonacci, recursion author: ashukr07 --- -```cpp +```c +// Function to calculate the nth Fibonacci number int fibonacci(int n) { if (n <= 1) return n; return fibonacci(n - 1) + fibonacci(n - 2); diff --git a/snippets/cpp/math-and-numbers/sum-of-digits.md b/snippets/c/mathematical-functions/sum-of-digits.md similarity index 82% rename from snippets/cpp/math-and-numbers/sum-of-digits.md rename to snippets/c/mathematical-functions/sum-of-digits.md index 766eaf9f..bc6c4837 100644 --- a/snippets/cpp/math-and-numbers/sum-of-digits.md +++ b/snippets/c/mathematical-functions/sum-of-digits.md @@ -5,7 +5,8 @@ tags: math, digits author: ashukr07 --- -```cpp +```c +// Function to calculate the sum of the digits of an integer int sum_of_digits(int n) { int sum = 0; while (n != 0) { diff --git a/snippets/cpp/math-and-numbers/factorial.md b/snippets/cpp/math-and-numbers/factorial.md deleted file mode 100644 index 64a572aa..00000000 --- a/snippets/cpp/math-and-numbers/factorial.md +++ /dev/null @@ -1,16 +0,0 @@ ---- -title: Factorial -description: Calculates the factorial of a given non-negative integer. -tags: math, factorial -author: ashukr07 ---- - -```cpp -int factorial(int n) { - if (n <= 1) return 1; // Base case - return n * factorial(n - 1); // Recursive step -} - -// Usage: -factorial(5); // Returns: 120 -``` \ No newline at end of file