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 / 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 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;
@petergi
petergi / Convert digits to roman numeral in Swift.swift
Last active December 27, 2023 22:23
Converts an integer to its roman numeral representation
// This updated version should be more efficient due to the constant-time lookup in the dictionary, the use of StringBuilder for efficient string concatenation, and the iteration over the lookup keys in descending order.
func toRomanNumeral(_ num: Int) -> String {
let lookup: [Int: String] = [
1000: "M",
900: "CM",
500: "D",
400: "CD",
100: "C",
90: "XC",
50: "L",
@petergi
petergi / Check for Pangram in Swift.swift
Created December 27, 2023 21:56
Checks to see if a given sentence is a Pangram
func isPangram(string: String) -> Bool {
let alphabet = "abcdefghijklmnopqrstuvwxyz"
let lowerCaseString = string.lowercased()
for letter in alphabet {
if !lowerCaseString.contains(letter) {
return false
}
}
@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 array alphabetically in Javascript.js
Last active December 28, 2023 05:36
Sorts an array of objects alphabetically based on a given property.
// Removed the localeCompare function call
// Replaced it with a more efficient comparison using the less-than and greater-than operators.
// This avoids the overhead of string comparison and should make the function faster.
/**
* Sorts an array of objects alphabetically based on a specified getter function.
*
* @param {Array} arr - The array to be sorted.
* @param {Function} getter - The getter function used to retrieve the value to compare.
* @param {string} [order="asc"] - The order in which to sort the array. Default is "asc" (ascending).
@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 / Stable Sort in Javascript.js
Last active December 27, 2023 23:20
Performs stable sorting of an array, preserving the initial indexes of items when their values are the same.
// Instead of using an object with item and index properties, used an array with [item, index] values.
// This reduces the memory footprint and improves performance.
// Modified the .map() and .sort() functions to work with the array of [item, index] pairs.
// Used destructuring assignment in the .map() function to extract only the item value from each pair.
/**
* Sorts an array in a stable manner using a custom compare function.
*
* @param {Array} arr - The array to be sorted.
* @param {Function} compare - The compare function used to determine the order of elements.
@petergi
petergi / Weighted Average in Swift.swift
Created December 27, 2023 20:52
Calculates the weighted average of two or more numbers
func weightedAverage(nums: [Double], weights: [Double]) -> Double {
var sum = 0.0
var weightSum = 0.0
for i in 0..<nums.count {
sum += nums[i] * weights[i]
weightSum += weights[i]
}
return sum / weightSum
@petergi
petergi / Weighted Average in Rust.rs
Created December 27, 2023 20:35
Calculates the weighted average of two or more numbers
///
///
/// # Arguments
///
/// * `nums`:
/// * `weights`:
///
/// returns: f64
///
/// # Examples