All generations.

generate functionThu, 24 Nov 2022

fastest loop

function fast_loop(arr) { var sum = 0; for (var i=0; i<arr.length; i+=2) { sum += arr[i]; } return sum; } fast_loop([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])

Javascript
View
generate functionSun, 13 Nov 2022

class to create objects storing age, name, and cost

class Animal { constructor(age, name, cost) { this.age = age; this.name = name; this.cost = cost; } getDetails() { return `${this.name} is ${this.age} years old and costs ${this.cost}`; } } const animalDetails = new Animal(4, 'dog', '$4,000'); console.log(animalDetails.getDetails());

Javascript
View
generate functionSun, 13 Nov 2022

hash sort array

def hash_sort(array): hash = {} result = [] for element in array: if hash[element] hash[element] += 1 else: hash[element] = 1 for key in hash: for i in range(hash[key]): result.append(key) return result

Python
View
generate functionMon, 14 Nov 2022

create an object within an object and print its properties to console

var student = { firstName: "Mark", lastName: "Foo", age: 24, favoriteMovies: ["Star Wars", "Lord of the Rings", "Star Trek"] }; function print(person) { console.log(person.firstName + " " + person.lastName + "is" + person.age + "years old."); console.log("His favorite movies are:"); for (var i = 0; i < person.favoriteMovies.length; i++) { console.log("-" + person.favoriteMovies[i]); } } print(student);

Javascript
View
generate functionFri, 25 Nov 2022

get url and return content and save to file

def save_to_file(url, file_name='', overwrite=False): """ Get content from url and save to file. Default filename is url's name. Default will use overwrite=True. """ try: r = requests.get(url) if r.status_code == requests.codes.ok: file_name = file_name or url.split('/')[-1] if not overwrite and os.path.exists(file_name): print('File {} already exists.'.format(file_name)) else: with open(file_name, 'wb') as fd: for chunk in r.iter_content(chunk_size=1024): fd.write(chunk) else: r.raise_for_status() except requests.RequestException as e: print(e)

Python
View
generate functionSun, 13 Nov 2022

quick sort array

def quicksort(lst): if len(lst) <= 1: return lst pivot = lst[0] left = [x for x in lst[1:] if x < pivot] right = [x for x in lst[1:] if x >= pivot] return quicksort(left) + [pivot] + quicksort(right) quicksort([3, 2, 5, 1, 9])

Python
View
generate functionSat, 17 Dec 2022

read local file of json. split the json list into 6 lists and output to 6 files

const fs = require('fs') const jsonString = fs.readFile('./file.txt', 'utf8', function (err, data) { if (err) { console.log("Error reading file from disk:", err) return } try { const jsonObject = JSON.parse(data) const props = ['b', 'c', 'd', 'e', 'f', 'g'] props.forEach(prop => { const newObject = { [prop]: jsonObject[prop] } fs.writeFile(`./${prop}.txt`, JSON.stringify(newObject), err => { if (err) { console.log('Error writing file', err) } else { console.log(`Successfully wrote file ${prop}.txt`) } }) }) } catch(err) { console.log('Error parsing JSON string:', err) } })

Javascript
View
generate functionSun, 15 Jan 2023

read file and split the lines into 7 separate files

def split_to_7_files(filename): # read file f = open(filename, "r") lines = f.readlines() f.close() # split the lines into 7 separate files n = len(lines) k = n // 7 for i in range(7): # create output filename fout_name = filename.split(".")[0] + "_" + str(i) + "." + filename.split(".")[1] # write to file fout = open(fout_name, "w") for j in range(i*k, (i+1)*k): fout.write(lines[j]) fout.close() split_to_7_files("text.txt")

Python
View
generate functionFri, 25 Nov 2022

get url and return content and save to file

function getContent(url, timeout, callback) { setTimeout(() => { callback('content') }, timeout) } function saveContent(content, callback) { setTimeout(() => { callback('content saved') }, 1000) } function log(result) { console.log(result) } getContent(url, timeout, (content) => { saveContent(content, (result) => { log(result) }) })

Javascript
View

Questions about programming?Chat with your personal AI assistant