-
-
Notifications
You must be signed in to change notification settings - Fork 138
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into tweak/fix-loading-state-blinking-arrow
- Loading branch information
Showing
77 changed files
with
2,230 additions
and
347 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/* /index.html 200 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 0 additions & 25 deletions
25
snippets/cpp/math-and-numbers/binary-to-decimal-conversion.md
This file was deleted.
Oops, something went wrong.
26 changes: 26 additions & 0 deletions
26
snippets/cpp/math-and-numbers/binary-to-unsigned-integer-conversion.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
15
snippets/java/string-manipulation/camelcase-to-snake-case.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
27
snippets/java/string-manipulation/count-character-frequency.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} | ||
``` |
Oops, something went wrong.