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
#!/usr/bin/env bash
LOG_FILE="appPrint.log"
. "Check ownership and permissions of a file.sh"
function log()
{
@petergi
petergi / Weighted Average in Javascript.js
Last active December 27, 2023 20:16
Calculates the weighted average of two or more numbers
/**
* Calculates the weighted average of an array of numbers.
*
* @param {number[]} nums - The array of numbers to be averaged.
* @param {number[]} weights - The array of weights corresponding to each number in `nums`.
* @return {number} The weighted average of the numbers.
*/
const weightedAverage = (nums, weights) => {
let sum = 0;
let weightSum = 0;
@petergi
petergi / Unique Array Elements in Javascript.js
Last active December 27, 2023 22:15
Finds all unique values in an array
/**
* Returns an array containing the unique elements from the input array.
*
* @param {Array} arr - The input array.
* @return {Array} An array containing the unique elements from the input array.
*/
function uniqueElements(arr) {
const uniqueArr = []
const seen = new Set()
@petergi
petergi / Vector Distance in Javascript.js
Last active December 27, 2023 22:12
Calculates the distance between two vectors
/**
* Calculates the distance between two vectors.
*
* @param {Array} x - The first vector.
* @param {Array} y - The second vector.
* @return {number} The distance between the two vectors.
*/
function vectorDistance(x, y) {
return Math.sqrt(x.reduce((acc, val, i) => acc + (val - y[i]) ** 2, 0))
}
@petergi
petergi / Convert digits to roman numeral in Javascript.js
Last active December 27, 2023 22:19
Converts an integer to its roman numeral representation
/**
* Converts a given number to a Roman numeral representation.
*
* @param {number} num - The number to be converted.
* @return {string} The Roman numeral representation of the given number.
*/
function toRomanNumeral(num) {
const lookup = new Map([
[1000, "M"],
[900, "CM"],
@petergi
petergi / String to Words in Javascript.js
Last active December 27, 2023 22:31
Converts a given string into an array of words
// Replaced the split method with the match method, which uses a regular expression to split the string into an array of words.
// Also removed the filter(Boolean) call since the match method already returns an array with only the matched words.
/**
* Returns an array of all words in a given string that match a given pattern.
*
* @param {string} str - The input string to search for words.
* @param {RegExp} pattern - The pattern to match words against. Defaults to /[a-zA-Z-]+/g.
* @return {Array} - An array of words that match the given pattern.
*/
@petergi
petergi / Starts with substring in Javascript.js
Last active December 27, 2023 22:37
Checks if a given string starts with a substring of another string
// Changed the loop to start from the end of the word string and iterate backwards.
// This allows us to avoid unnecessary slicing operations.
//Used substring instead of slice since we only need to get the substring starting from i to the end.
/**
* Finds the longest substring of the word that is a prefix of the text.
*
* @param {string} text - The text to search for the substring.
* @param {string} word - The word to find the substring from.
* @return {string|undefined} The longest substring that is a prefix of the text, or undefined if no such substring exists.
@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
@petergi
petergi / Asciimojis Cheatsheet.md
Created December 27, 2023 16:02
Flip a table or shrug, in ASCII !
ʘ‿ʘ (`·ω·´) ( ͡° ͜ʖ ͡°)
Innocent face feel perky 4chan emoticon
ಠ_ಠ (╬ ಠ益ಠ) ಥ_ಥ
Reddit disapproval face angry face crying face
(╯°□°)╯︵ ┻━┻ ☜(⌒▽⌒)☞ ᕙ(⇀‸↼‶)ᕗ
Table Flip / Flipping Table excited flexing
@petergi
petergi / Swift Basics Cheatsheet.md
Created December 27, 2023 20:00
Strict Minimum to get started with

Examples

Classes

class MyClass : OptionalSuperClass, OptionalProtocol {
    var myProperty: String