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
function getRandomItemsFromArray(sourceArray, numOfItems) { | |
if (numOfItems > sourceArray.length) { | |
throw new Error('Trying to get more items than the array length!!!'); | |
} | |
const shuffledArray = sourceArray.concat().sort((() => Math.random() - 0.5); | |
return shuffledArray.slice(0, (numOfItems - 1)); | |
} | |
// The concat() function returns a copy of the sourceArray so that the original array is not modified. |
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
function sortArray(arr) { | |
return arr.sort((a, b) => a - b); | |
} | |
const arr = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5]; | |
console.log(sortArray(arr)); |
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
# ensure to run 'pip install passlib' | |
from passlib.context import CryptContext | |
plain_text = 'Password@123' | |
pwd_context = CryptContext( | |
schemes=['pbkdf2_sha256'], | |
default='pbkdf2_sha256', | |
pbkdf2_sha256__default_rounds=40000 | |
) |
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
import copy | |
# Base list | |
list1 = ["apple", "banana", "cherry"] | |
print("By default Python creates variables by reference") | |
list2 = list1 | |
print("Now while list2 and list1 seems like two variables they refer to the same memory reference") | |
print("So modifying one variable will modify the others") |
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
import json | |
# No need to specify the "r" (ie. open the file in read-only mode) as it is the default | |
def parse_json(json_file_path): | |
json_data = None | |
try: | |
file = open(json_file_path, "r") | |
json_data = json.load(file) | |
except: | |
print("Something went wrong while opening JSON file: " + json_file_path) |
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
// a refValue of 0.5 will generate equal amount of true and false | |
// a refValue greater than 0.5 will generate more true values | |
// a refValue less than 0.5 will generate more false values | |
const randomBoolean = (positiveBias = false) => { | |
const refValue = positiveBias ? 0.8 : 0.5; | |
return Math.random() < refValue; | |
}; |
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
import Nano from 'nano'; | |
const { | |
DB_HOST, | |
DB_PORT, | |
DB_USER, | |
DB_PASSWORD, | |
DB_NAME | |
} = process.env |
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
import mysql from 'mysql'; | |
const { | |
DB_HOST, | |
DB_USER, | |
DB_PASSWORD, | |
DB_NAME | |
} = process.env | |
export const database = mysql.createPool({ |
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
// Using util.promisify() | |
async function sleep(ms) { | |
console.log('Waiting...'); | |
await require('util').promisify(setTimeout)(ms); | |
} | |
console.log('initial log'); | |
sleep(5000).then(() => { | |
console.log('after sleep'); | |
}); |
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
async function capitalize(text, timeout) { | |
return new Promise((resolve, reject) => { | |
setTimeout(() => { | |
let firstChar = text.charAt(0); | |
if (text.charAt(0).toUpperCase() === firstChar) { | |
reject('First character is already capitalized'); | |
} else { | |
resolve(text.charAt(0).toUpperCase() + text.slice(1)); | |
} | |
}, timeout); |
NewerOlder