-
-
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 feature/search
- Loading branch information
Showing
33 changed files
with
539 additions
and
44 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -1,3 +1,5 @@ | ||
quicksnip | ||
slugified | ||
slugifyed | ||
sublanguage | ||
fastapi |
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: Bit Counting | ||
description: Counts the set bits in the binary representation of an integer | ||
author: Mcbencrafter | ||
tags: math,number,bits,bit-counting | ||
--- | ||
|
||
```java | ||
public static int countBits(int number) { | ||
int bits = 0; | ||
|
||
while (number > 0) { | ||
bits += number & 1; | ||
number >>= 1; | ||
} | ||
|
||
return bits; | ||
} | ||
|
||
// Usage: | ||
int number = 5; | ||
System.out.println(countBits(5)); // 2 (101) | ||
``` |
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,16 @@ | ||
--- | ||
title: Is Power Of Two | ||
description: Checks if a number is a power of two | ||
author: Mcbencrafter | ||
tags: math,number,bit,power-of-two | ||
--- | ||
|
||
```java | ||
public static boolean isPowerOfTwo(int number) { | ||
return (number > 0) && ((number & (number - 1)) == 0); | ||
} | ||
|
||
// Usage: | ||
int number = 16; | ||
System.out.println(isPowerOfTwo(5)); // true (2^4) | ||
``` |
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
2 changes: 1 addition & 1 deletion
2
snippets/java/date-time/duration-formatting-hours-minutes-seconds.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
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,24 @@ | ||
--- | ||
title: Checksum | ||
description: Calculates the checksum of an int | ||
author: Mcbencrafter | ||
tags: math,number,checksum | ||
--- | ||
|
||
```java | ||
public static int checksum(int number) { | ||
number = Math.abs(number); | ||
int sum = 0; | ||
|
||
while (number != 0) { | ||
sum += number % 10; | ||
number /= 10; | ||
} | ||
|
||
return sum; | ||
} | ||
|
||
// Usage: | ||
int number = 12345; | ||
System.out.println(checksum(number)); // 15 = 1+2+3+4+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,24 @@ | ||
--- | ||
title: Factorial | ||
description: Computes the factorial of a given number | ||
author: Mcbencrafter | ||
tags: math,number,factorial | ||
--- | ||
|
||
```java | ||
import java.math.BigInteger; | ||
|
||
public static BigInteger factorial(int number) { | ||
BigInteger result = BigInteger.ONE; | ||
|
||
for (int currentNumber = 1; currentNumber <= number; currentNumber++) { | ||
result = result.multiply(BigInteger.valueOf(currentNumber)); | ||
} | ||
|
||
return result; | ||
} | ||
|
||
// Usage: | ||
int number = 6; | ||
System.out.println(factorial(number)); // 720 = 6*5*4*3*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,19 @@ | ||
--- | ||
title: Fibonacci | ||
description: Calculates the nth fibonacci number | ||
author: Mcbencrafter | ||
tags: math,number,fibonacci | ||
--- | ||
|
||
```java | ||
public static int fibonacci(int number) { | ||
if (number <= 1) | ||
return number; | ||
|
||
return fibonacci(number - 1) + fibonacci(number - 2); | ||
} | ||
|
||
// Usage: | ||
int number = 5; | ||
System.out.println(fibonacci(number)) // 3 (0, 1, 1, 2, 3) | ||
``` |
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: Greatest Common Divisor | ||
description: Calculates the greatest common divisor (gcd) of two numbers | ||
author: Mcbencrafter | ||
tags: math,number,greatest-common-devisor,gcd,euclidean-algorithm | ||
--- | ||
|
||
```java | ||
public static int gcd(int number1, int number2) { | ||
while (number2 != 0) { | ||
int remainder = number2; | ||
number2 = number1 % number2; | ||
number1 = remainder; | ||
} | ||
|
||
return number1; | ||
} | ||
|
||
// Usage: | ||
int a = 16; | ||
int b = 12; | ||
System.out.println(gcd(a, b)); // 4 | ||
``` |
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: Least Common Multiple | ||
description: Calculates the least common multiple (lcm) of two numbers | ||
author: Mcbencrafter | ||
tags: math,number,least-common-multiple,lcm,euclidean-algorithm | ||
--- | ||
|
||
```java | ||
public static int lcm(int number1, int number2) { | ||
int gcdNumber1 = number1; | ||
int gcdNumber2 = number2; | ||
|
||
while (gcdNumber2 != 0) { | ||
int remainder = gcdNumber2; | ||
gcdNumber2 = gcdNumber1 % gcdNumber2; | ||
gcdNumber1 = remainder; | ||
} | ||
|
||
return (number1 / gcdNumber1) * number2; | ||
} | ||
|
||
// Usage: | ||
int a = 16; | ||
int b = 12; | ||
System.out.println(lcm(a, b)); // 48 | ||
``` |
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,31 @@ | ||
--- | ||
title: Prime Check | ||
description: Checks if a number is a prime | ||
author: Mcbencrafter | ||
tags: math,number,prime | ||
--- | ||
|
||
```java | ||
public static boolean isPrime(int number) { | ||
if (number <= 1) | ||
return false; | ||
|
||
if (number <= 3) | ||
return true; | ||
|
||
boolean prime = true; | ||
for (int divisor = 3; divisor < number; divisor++) { | ||
if (number % divisor != 0) | ||
continue; | ||
|
||
prime = false; | ||
break; | ||
} | ||
|
||
return prime; | ||
} | ||
|
||
// Usage: | ||
int number = 31; | ||
System.out.println(isPrime(number)); // 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,21 @@ | ||
--- | ||
title: Hello, World! | ||
description: Show Hello World on the page. | ||
author: ACR1209 | ||
tags: printing,hello-world | ||
--- | ||
|
||
```tsx | ||
import React from 'react'; | ||
import ReactDOM from 'react-dom'; | ||
|
||
const App = () => { | ||
return ( | ||
<div> | ||
<h1>Hello, World!</h1> | ||
</div> | ||
); | ||
}; | ||
|
||
ReactDOM.render(<App />, document.getElementById('root')); | ||
``` |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,32 @@ | ||
--- | ||
title: Compare Arrays | ||
description: Deeply compares two arrays to check if they are equal to each other (supports nested arrays and objects). | ||
author: KCSquid | ||
tags: array,object,compare,equal | ||
--- | ||
|
||
```js | ||
const compareArrays = (a, b) => { | ||
if (!Array.isArray(a) || !Array.isArray(b) || a.length !== b.length) return false; | ||
return a.every((v, i) => | ||
Array.isArray(v) && Array.isArray(b[i]) ? compareArrays(v, b[i]) : | ||
typeof v === "object" && typeof b[i] === "object" ? compareObjects(v, b[i]) : | ||
v === b[i] | ||
); | ||
}; | ||
|
||
const compareObjects = (a, b) => { | ||
if (typeof a !== "object" || typeof b !== "object" || Object.keys(a).length !== Object.keys(b).length) return false; | ||
return Object.keys(a).every(k => | ||
Array.isArray(a[k]) && Array.isArray(b[k]) ? compareArrays(a[k], b[k]) : | ||
typeof a[k] === "object" && typeof b[k] === "object" ? compareObjects(a[k], b[k]) : | ||
a[k] === b[k] | ||
); | ||
}; | ||
|
||
// Usage: | ||
compareArrays([1, 2, 3], [1, 2, 3]); // Returns: true | ||
compareArrays([1, 2, 3], [3, 2, 1]); // Returns: false | ||
compareArrays([{a:1}], [{a:1}]); // Returns: true | ||
compareArrays([{a:1}], null); // Returns: 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
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,21 @@ | ||
--- | ||
title: Hello, World! | ||
description: Returns Hello, World! when it recives a GET request made to the root endpoint. | ||
author: ACR1209 | ||
tags: printing,hello-world,web,api | ||
--- | ||
|
||
```py | ||
from typing import Union | ||
from fastapi import FastAPI | ||
|
||
app = FastAPI() | ||
|
||
|
||
@app.get("/") | ||
def read_root(): | ||
return {"msg": "Hello, World!"} | ||
|
||
# Usage: | ||
# -> Go to http://127.0.0.1:8000/ and you'll see {"msg", "Hello, World!"} | ||
``` |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.