K

@kenbitanga

CPP: Fibonacci - Memoized

C++
2 years ago
#include <iostream> #include <unordered_map> int fib(int n) { std::unordered_map<int, int> memo {}; if (n < 1) return 0; if (n < 3) return 1; if (!memo.count(n)) {

JS: Fibonacci - Memoization

NodeJS
2 years ago
function fib(n) { if (n < 1) return 0 if (n < 3) return 1 return fib(n - 1) + fib(n - 2) } function memoize(f) { const memo = {} return (n) => { if (!memo[n]) {

JS: Decorators

NodeJS
2 years ago
function fib(n) { if (n < 1) return 0 if (n < 3) return 1 return fib(n-1) + fib(n-2) } // decorator function function memoize(func) { const memo = new Map()

Python: Numpy Sum Method

Python
2 years ago
import numpy as np arr = np.arange(18).reshape(6, 3) print(arr) print('sum of all elements:', arr.sum()) print('sum of rows :', arr.sum(axis=1)) print('sum of columns :', arr.sum(axis=0))

JS: Destructuring Object Properties

NodeJS
2 years ago
// array of nested objects const persons = [ { name: { first: 'jake', last: 'make' }, age: 21 }, { name: { first: 'jake', last: 'cake' }, age: 22 }, { name: { first: 'jake', last: 'take' }, age: 23 }, ] // beginner method const make =

CPP: Black Jack By Dave's Garage (YouTube)

C++
2 years ago
#include <iostream> #include <vector> #include <memory> #include <random> #include <ctime> #include <algorithm> // for "shuffle" using namespace std; enum Rank {

CPP: Convert Unique Pointer to Shared Pointer

C++
2 years ago
#include <iostream> #include <memory> auto makeUnique(std::string s) { return std::make_unique<std::string>(s); } int main() { auto unique = makeUnique("Hello world!");

CPP: Shared Pointer to a Vector

C++
2 years ago
#include <iostream> #include <memory> #include <vector> int main() { auto a = std::make_shared<std::vector<int>>(); for (int i = 0; i < 3; i++) { a->push_back(i); }

CPP: Shared Pointer to a String

C++
2 years ago
#include <iostream> #include <memory> int main() { auto p = std::make_shared<std::string>("Hello"); *p += ", world!"; std::cout << *p << std::endl; auto q = p;

CPP: Smart Pointers - shared_ptr

C++
2 years ago
#include <iostream> #include <memory> int main() { std::shared_ptr<int> b = std::make_shared<int>(100); auto c = b; *c += 1; std::cout << *b << std::endl; return 0; }

CPP: Smart Pointers - Unique Pointer

C++
2 years ago
#include <iostream> #include <memory> std::unique_ptr<int> getData() { std::unique_ptr<int> a = std::make_unique<int>(5); return a; } int main() { std::unique_ptr<int> b = getData(); // std::unique_ptr<int> c = b;

CPP: Smart Pointers - Shared Pointer

C++
2 years ago
#include <iostream> #include <memory> std::shared_ptr<int> getData() { std::shared_ptr<int> a = std::make_shared<int>(5); return a; } int main() { auto b = getData(); auto c = b;

CPP: Smart Pointers - Shared Pointer

C++
2 years ago
#include <iostream> #include <memory> std::shared_ptr<int> getData() { std::shared_ptr<int> a = std::make_shared<int>(5); return a; } int main() { std::shared_ptr<int> b = getData(); auto c = b;

JS: Word frequency counter

NodeJS
2 years ago
words = 'Lorem ipsum dolor sit amet consectetur adipiscing elit Duis sed tortor quis ex malesuada tristique non a ante Proin sollicitudin nibh vel ullamcorper ullamcorper lorem tortor accumsan diam vitae commodo erat lorem ac turpis Aliquam et effic

Python: Read file, word count, most frequent, least frequent

Python
2 years ago
with open('file.txt') as f: words = f.read().split() counts = dict() for word in words: counts[word] = counts.get(word, 0) + 1 sorted_asc = sorted(counts.items(), key=lambda x: x[1]) sorted_desc = sorted(counts.items(), key=lambda x: x[1],

Python: Read file and find most frequent and least frequent words

Python
2 years ago
counts = dict() with open('file.txt') as f: for word in f.read().split(): counts[word] = counts.get(word, 0) + 1 sorted_asc = sorted(counts.items(), key=lambda x: x[1]) sorted_desc = sorted(counts.items(), key=lambda x: x[1], reverse=T

JS: Fetch - Async/Await

NodeJS
2 years ago
const getUserEmails = async () => { const response = await fetch("https://jsonplaceholder.typicode.com/users"); const jsonUserData = await response.json(); const userEmailArray = jsonUserData.map(user => { return user.email;

JS: Fetch

NodeJS
2 years ago
fetch("https://jsonplaceholder.typicode.com/users") .then(response => { return response.json(); }) .then(data => { data.forEach(user => { console.log(user); }); });

Kotlin - Two Sum - List<Int>

Kotlin
2 years ago
class Solution { fun twoSum(nums: List<Int>, target: Int): List<Int> { val prevMap = hashMapOf<Int, Int>() for ((i, num) in nums.withIndex()) { val diff = target - num if (prevMap.containsKey(diff

Kotlin - Two Sum - Array<Int>

Kotlin
2 years ago
class Solution { fun twoSum(nums: Array<Int>, target: Int): Array<Int> { val prevMap = hashMapOf<Int, Int>() for ((i, num) in nums.withIndex()) { val diff = target - num if (prevMap.containsKey(di