Skip to content

Instantly share code, notes, and snippets.

View petergi's full-sized avatar
💭
Just Busy Living On The Side Of A Square

Peter Giannopoulos petergi

💭
Just Busy Living On The Side Of A Square
View GitHub Profile
@petergi
petergi / Standard deviation in JavaScript.js
Last active January 29, 2024 23:22
Calculates the standard deviation of an array of numbers
/**
* Calculates the standard deviation of an array of numbers.
*
* @param {Array} arr - The array of numbers.
* @param {Boolean} usePopulation - Optional. If true, the function uses the population standard deviation formula. Default is false, which uses the sample standard deviation formula.
* @return {Number} - The standard deviation of the array.
*/
const standardDeviation = (arr, usePopulation = false) => {
const n = arr.length
let sum = 0

Curl cheatsheet

Options

-o <file>    # --output: write to file
-u user:pass # --user: Authentication
-v           # --verbose
-vv          # Even more verbose
-s           # --silent
@petergi
petergi / Heap Sort in JavaScript.js
Last active January 8, 2024 01:49
The algorithm has an average time complexity of O(n log n), where n is the size of the input array.
/**
* Sorts an array using the heapsort algorithm.
* Heapsort is a comparison-based sorting algorithm.
* Heapsort is an improved selection sort consisting of the use of a
* heap data structure instead of a linear-time search to find the maximum or minimum element.
*
* @param {Array} arr - The array to be sorted.
* @return {Array} - The sorted array.
*/
function heapsort(arr) {
@petergi
petergi / FizzBuzz in Java.java
Created January 8, 2024 01:31
By preallocating the ArrayList with the initial capacity of n, we avoid resizing the list during the loop, which improves performance. Instead of using multiple conditional statements, used StringBuilder to build the result string. Additionally, used sb.length() == 0 to check if the StringBuilder is empty, instead of using multiple else if state…
/**
* Generates a list of strings based on the given integer input.
*
* @param n the maximum integer value to generate strings for
* @return a list of strings containing the generated values
*/
public List<String> fizzBuzz(int n) {
List<String> res = new ArrayList<>(n)
for (int i = 1; i <= n; i++) {
StringBuilder sb = new StringBuilder()
@petergi
petergi / Javascript big O cheatsheet.md
Last active December 29, 2023 06:37
Learn everything you need to know about Big-O notation with this handy cheatsheet.
title tags excerpt dateModified
JS Big-O Cheat Sheet
algorithm
Learn everything you need to know about Big-O notation with this handy cheatsheet.
2023-01-08 05:00:00 -0400

Definition

let asciiValue: String = "1234567890ABCDEFGHIJ"
func asciiToHex(_ asciiValue: String) -> String {
let chars: [String.Element] = Array(asciiValue)
var hex: String = ""
for char: String.Element in chars {
hex += String(char.asciiValue!, radix: 16)
}
return hex
}
@petergi
petergi / Calculate array difference in Javascript.js
Created December 29, 2023 06:20
Calculates the difference between two arrays, without filtering duplicate values.
// 17% faster than using filter on set
function difference(a, b) {
const s = new Set(b)
const result = []
for (const x of a) {
if (!s.has(x)) {
result.push(x)
}
}
@petergi
petergi / Check for Pangram in Javascript.js
Last active December 28, 2023 05:50
Checks to see if a given sentence is a Pangram
/**
* Checks if a string is a Pangram.
*
* @param {string} string - The string to be checked.
* @return {boolean} Returns true if the string is a pangram, false otherwise.
*/
const isPangram = (string) => {
const alphabet = "abcdefghijklmnopqrstuvwxyz"
const lowerCaseString = string.toLowerCase()
@petergi
petergi / Sort string characters in Javascript.js
Last active December 28, 2023 05:46
Alphabetically sorts the characters in a string.
// - Use the `split()` method to convert the string `str` into an array of characters.
// - Use the `sort()` method with a compare function to sort the characters in the array.
// - Use the `join()` method to recombine the sorted characters into a string.
/**
* Sorts the characters in a string in ascending order.
*
* @param {string} str - The string to sort.
* @return {string} The sorted string.
*/
@petergi
petergi / Check for Palindrome in Javascript.js
Created December 28, 2023 05:43
Checks if the given string is a palindrome.
/**
* Check if a given string is a palindrome.
*
* @param {string} str - The string to be checked.
* @return {boolean} Returns true if the string is a palindrome, false otherwise.
*/
function isPalindrome(str) {
const formattedStr = str.toLowerCase().replace(/[\W_]/g, "");
const reversedStr = formattedStr.split("").reverse().join("");
return formattedStr === reversedStr;