Skip to content

Commit

Permalink
Merge branch 'main' into tweak/fix-loading-state-blinking-arrow
Browse files Browse the repository at this point in the history
  • Loading branch information
ACR1209 authored Feb 3, 2025
2 parents 78c4d83 + d9bdf59 commit bc69c4a
Show file tree
Hide file tree
Showing 77 changed files with 2,230 additions and 347 deletions.
226 changes: 125 additions & 101 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"prismjs": "^1.29.0",
"react": "^18.3.1",
"react-dom": "^18.3.1",
"react-router-dom": "^6.27.0",
"react-router-dom": "^7.1.1",
"react-syntax-highlighter": "^15.6.1"
},
"devDependencies": {
Expand Down
1 change: 1 addition & 0 deletions public/_redirects
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/* /index.html 200
17 changes: 17 additions & 0 deletions snippets/c/bit-manipulation/clear-ith-bit.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
title: Clear ith bit
description: Clear the ith bit of a number and returns the resulting number
tags: bit-manipulation, number, clear
author: aelshinawy
---

```c
int clear_ith_bit(int n, int i) {
return n & ~(1 << i);
}


// Usage:
clear_ith_bit(10, 1); // Returns: 8
clear_ith_bit(10, 3); // Returns: 2
```
23 changes: 23 additions & 0 deletions snippets/c/bit-manipulation/count-set-bits.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---
title: Count Set Bits
description: Counts the number of set bits in an int
tags: bit-manipulation, count
author: aelshinawy
---

```c
int count_set_bits(int n) {
int count = 0;
while (n) {
n &= (n - 1);
count++;
}
return count;
}


// Usage:
count_set_bits(5); // Returns: 2
count_set_bits(255); // Returns: 8
count_set_bits(8); // Returns: 1
```
19 changes: 19 additions & 0 deletions snippets/c/bit-manipulation/get-ith-bit.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
---
title: Get ith bit
description: Get the i-th bit of a number
tags: bit-manipulation, number, get
author: aelshinawy
---

```c
int get_ith_bit(int n, int i) {
return (n >> i) & 1;
}


// Usage:
get_ith_bit(10, 0); // Returns: 0
get_ith_bit(10, 1); // Returns: 1
get_ith_bit(10, 2); // Returns: 0
get_ith_bit(10, 3); // Returns: 1
```
17 changes: 17 additions & 0 deletions snippets/c/bit-manipulation/is-odd.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
title: Is Odd
description: Check if a number is odd
tags: bit-manipulation, number, is-odd
author: aelshinawy
---

```c
bool is_odd(int n) {
return n & 1;
}


// Usage:
is_odd(10); // Returns: false
is_odd(11); // Returns: true
```
19 changes: 19 additions & 0 deletions snippets/c/bit-manipulation/set-ith-bit.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
---
title: Set ith bit
description: Set the i-th bit of a number and returns the resulting number
tags: bit-manipulation, number, set
author: aelshinawy
---

```c
int set_ith_bit(int n, int i) {
return n | (1 << i);
}


// Usage:
set_ith_bit(10, 0); // Returns: 11
set_ith_bit(10, 2); // Returns: 14
set_ith_bit(1, 8); // Returns: 257
set_ith_bit(1, 3); // Returns: 9
```
20 changes: 20 additions & 0 deletions snippets/c/bit-manipulation/swap-numbers.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
title: Swap Numbers
description: Swap two numbers without a temporary variable
tags: bit-manipulation, number, swap
author: aelshinawy
---

```c
void swap(int *a, int *b) {
*a ^= *b;
*b ^= *a;
*a ^= *b;
}


// Usage:
int x = 5, y = 10;
swap(&x, &y);
printf("x = %d, y = %d\n", x, y); // x = 10, y = 5
```
18 changes: 18 additions & 0 deletions snippets/c/bit-manipulation/toggle-ith-bit.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
title: Toggle ith bit
description: Toggle the i-th bit of a number and returns the resulting number
tags: bit-manipulation, number, toggle
author: aelshinawy
---

```c
int toggle_ith_bit(int n, int i) {
return n ^ (1 << i);
}


// Usage:
toggle_ith_bit(10, 0); // Returns: 11
toggle_ith_bit(10, 1); // Returns: 8
toggle_ith_bit(8, 1); // Returns: 10
```
8 changes: 5 additions & 3 deletions snippets/cpp/bit-manipulation/find-non-repeating-number.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ author: ashukr07
---

```cpp
int find_non_repeating(const std::vector<int>& nums) {
#include <vector>

int find_non_repeating(const std::vector<int> nums) {
int result = 0;
for (int num : nums) {
for (const int num : nums) {
result ^= num;
}
return result;
Expand All @@ -17,4 +19,4 @@ int find_non_repeating(const std::vector<int>& nums) {
// Usage:
std::vector<int> nums = {4, 1, 2, 1, 2};
find_non_repeating(nums); // Returns: 4
```
```
25 changes: 0 additions & 25 deletions snippets/cpp/math-and-numbers/binary-to-decimal-conversion.md

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
---
title: Binary to Unsigned Integer Conversion
description: Converts a binary number represented as a string to its decimal equivalent.
tags: binary, conversion, c++20
author: ashukr07
contributor: majvax
---

```cpp
#include <string>
#include <bitset>
#include <stdexcept>

template <std::unsigned_integral T>
T binary_to_uintegral(const std::string& binary) {
if (binary.size() > sizeof(T) * 8)
throw std::invalid_argument("binary string is too long");
return static_cast<T>(std::bitset<sizeof(T) * 8>(binary).to_ullong());
}

// Usage:
std::string binary(64, '1'); // Binary 8 bytes long with all bits set to 1
binary_to_uintegral<unsigned long long>(binary); // Returns: 18446744073709551615
binary_to_uintegral<long long>(binary); // Compiles error: signed/unsigned mismatch
binary_to_uintegral<unsigned long long>(std::string(65, '1')); // Throws: std::invalid_argument
```
4 changes: 2 additions & 2 deletions snippets/java/bit-manipulation/is-power-of-two.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ public static boolean isPowerOfTwo(int number) {

// Usage:
int number = 16;
System.out.println(isPowerOfTwo(5)); // true (2^4)
```
System.out.println(isPowerOfTwo(number)); // true (2^4)
```
23 changes: 23 additions & 0 deletions snippets/java/string-manipulation/ascii-to-string.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---
title: Ascii To String
description: Converts a list of ascii numbers into a string
author: Mcbencrafter
tags: string,ascii,encoding,decode,conversion
---

```java
import java.util.List;

public static String asciiToString(List<Integer> asciiCodes) {
StringBuilder text = new StringBuilder();

for (int asciiCode : asciiCodes) {
text.append((char) asciiCode);
}

return text.toString();
}

// Usage:
System.out.println(asciiToString(List.of(104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100))); // "hello world"
```
15 changes: 15 additions & 0 deletions snippets/java/string-manipulation/camelcase-to-snake-case.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
title: camelCase to snake_case
description: Converts a camelCase string into snake_case
author: Mcbencrafter
tags: string,conversion,camel-case,snake-case
---

```java
public static String camelToSnake(String camelCase) {
return camelCase.replaceAll("([a-z])([A-Z])", "$1_$2").toLowerCase();
}

// Usage:
System.out.println(camelToSnake("helloWorld")); // "hello_world"
```
27 changes: 27 additions & 0 deletions snippets/java/string-manipulation/capitalize-words.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
---
title: Capitalize Words
description: Capitalizes the first letter of each word in a string
author: Mcbencrafter
tags: string,capitalize,words
---

```java
public static String capitalizeWords(String text) {
String[] words = text.split("(?<=\\S)(?=\\s+)|(?<=\\s+)(?=\\S)"); // this is needed to preserve spaces (text.split(" ") would remove multiple spaces)
StringBuilder capitalizedText = new StringBuilder();

for (String word : words) {
if (word.trim().isEmpty()) {
capitalizedText.append(word);
continue;
}
capitalizedText.append(Character.toUpperCase(word.charAt(0)))
.append(word.substring(1));
}

return capitalizedText.toString();
}

// Usage:
System.out.println(capitalizeWords("hello world")); // "Hello World"
```
28 changes: 28 additions & 0 deletions snippets/java/string-manipulation/check-anagram.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
---
title: Check Anagram
description: Checks if two strings are anagrams, meaning they contain the same characters ignoring order, spaces and case sensitivity
author: Mcbencrafter
tags: string,anagram,compare,arrays
---

```java
import java.util.Arrays;

public static boolean isAnagram(String text1, String text2) {
String text1Normalized = text1.replaceAll("\\s+", "");
String text2Normalized = text2.replaceAll("\\s+", "");

if (text1Normalized.length() != text2Normalized.length())
return false;

char[] text1Array = text1Normalized.toCharArray();
char[] text2Array = text2Normalized.toCharArray();
Arrays.sort(text1Array);
Arrays.sort(text2Array);
return Arrays.equals(text1Array, text2Array);
}

// Usage:
System.out.println(isAnagram("listen", "silent")); // true
System.out.println(isAnagram("hello", "world")); // false
```
20 changes: 20 additions & 0 deletions snippets/java/string-manipulation/check-palindrome.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
title: Check Palindrome
description: Checks if a string reads the same backward as forward, ignoring whitespaces and case sensitivity
author: Mcbencrafter
tags: string,palindrome,compare,reverse
---

```java
public static boolean isPalindrome(String text) {
String cleanText = text.toLowerCase().replaceAll("\\s+", "");

return new StringBuilder(cleanText)
.reverse()
.toString()
.equals(cleanText);
}

// Usage:
System.out.println(isPalindrome("A man a plan a canal Panama")); // true
```
27 changes: 27 additions & 0 deletions snippets/java/string-manipulation/count-character-frequency.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
---
title: Count Character Frequency
description: Counts the frequency of each character in a string
author: Mcbencrafter
tags: string,character,frequency,character-frequency
---

```java
public static Map<Character, Integer> characterFrequency(String text, boolean countSpaces, boolean caseSensitive) {
Map<Character, Integer> frequencyMap = new HashMap<>();

for (char character : text.toCharArray()) {
if (character == ' ' && !countSpaces)
continue;

if (!caseSensitive)
character = Character.toLowerCase(character);

frequencyMap.put(character, frequencyMap.getOrDefault(character, 0) + 1);
}

return frequencyMap;
}

// Usage:
System.out.println(characterFrequency("hello world", false, false)); // {r=1, d=1, e=1, w=1, h=1, l=3, o=2}
```
Loading

0 comments on commit bc69c4a

Please sign in to comment.