Skip to content

Instantly share code, notes, and snippets.

View primaryobjects's full-sized avatar

Kory Becker primaryobjects

View GitHub Profile
@primaryobjects
primaryobjects / hotspot-keep-alive.ps1
Last active September 8, 2026 21:59
Script to Enable Windows 10 Mobile Hotspot Automatically After Reboot
# https://superuser.com/a/1434648
Add-Type -AssemblyName System.Runtime.WindowsRuntime
$asTaskGeneric = ([System.WindowsRuntimeSystemExtensions].GetMethods() | ? { $_.Name -eq 'AsTask' -and $_.GetParameters().Count -eq 1 -and $_.GetParameters()[0].ParameterType.Name -eq 'IAsyncOperation`1' })[0]
Function Await($WinRtTask, $ResultType) {
$asTask = $asTaskGeneric.MakeGenericMethod($ResultType)
$netTask = $asTask.Invoke($null, @($WinRtTask))
$netTask.Wait(-1) | Out-Null
$netTask.Result
@primaryobjects
primaryobjects / palindrome.js
Last active January 28, 2021 02:45
Longest palindrome subsequence, solved using recursion and dynamic programming in javascript. https://jsitor.com/tLzj2p6xv
/* A subsequence is palindromic if it is the same whether read left to right or right to left. For instance,
the sequence A, C, G, T, G, T, C, A, A, A, A, T, C, G has many palindromic subsequences, including
A, C, G, C, A and A, A, A, A (on the other hand, the subsequence A, C, T is not palindromic). Devise
an O(n*n)-time algorithm that takes a sequence x[1 . . . n] and returns the (length of the) longest
palindromic subsequence. */
const palindrome = (arr, i=0, j) => {
// Solved using recursion O(2^n).
j = j === undefined ? arr.length - 1 : j;
@primaryobjects
primaryobjects / change.js
Last active January 27, 2021 03:45
Makig change from coins using recursion and dynamic programming. https://jsitor.com/pHtkCVa4n
// Given a supply of coins of specific denomination, determine if you can make change for the target value.
// https://www.velvetreactor.com/posts/dpv-6-17-change-making/
const set = [];
const change = (items, value, n) => {
// Solved using recursion O(2^n).
n = n === undefined ? items.length : n;
if (value === 0) {
// We've succeeded in matching the value!
return true;
@primaryobjects
primaryobjects / knapsack.js
Last active January 24, 2021 04:05
Knapsack problem using recursion in javascript. https://jsitor.com/j3zzC0PLN
// Solved using recursion. O(2^n)
const knapsack = (items, max, n) => {
n = n === undefined ? items.length : n;
if (n === 0 || max === 0) {
// We ran out of values or achieved a max weight.
return 0;
}
else {
if (items[n - 1].weight > max) {
@primaryobjects
primaryobjects / partition.js
Last active January 23, 2021 02:25
Partition an array into two equal sums and return true if possible. https://jsitor.com/54Mngv8B0
/*
Split an array of values into two equal sums.
Solution:
1. Calculate the sum of the array.
2. If the sum is odd, return false.
3. If the sum is even, divide the sum in half, and find a subset of array equal to half.
*/
const canPartition = (arr, n, sum) => {
@primaryobjects
primaryobjects / yuckdonalds.js
Last active September 19, 2025 15:31
Yuckdonalds restaurant location placement using dynamic programming in javascript https://jsitor.com/VTypCaesW
/*
Yuckdonald's is opening restaurants along a strip of highway.
You are given the distances from the starting point of the possible locations of the highways
and the profits at each of these locations.
You can only have one restaurant at each location.
Each location must be at least (k = min) miles apart.
P[i] = maximum profit at location i
P[i] = max(j<i) { P[j] + (di-dj < k ? 0 : P[i]), P[i] }
@primaryobjects
primaryobjects / msa.js
Created January 20, 2021 02:55
Find the substring with the maximum sum, using dynamic programming in Javascript. https://jsfiddle.net/nsj8q7p4/
/**
* @param {number[]} nums
* @return {number}
*/
var maxSubArray = function(nums) {
// Start with the max sum being the first value.
const S = [nums[0]];
// Store the first value so that we can output the substring of values.
const V = [[nums[0]]];
let maxIndex = 0;
@primaryobjects
primaryobjects / lcs.js
Last active January 20, 2021 02:26
Find the longest common subsequence length using dynamic programming in Javascript. https://jsfiddle.net/dpuf4mwx/2/
/*
Find the longest common subsequence (non-consecutive characters) betweenen two strings.
L(i,j) = length of LCS in X1..Xi and Y1..Yj
X = BCDBCDA
Y = ABECBA
Output = BCBA
if Xi == Yj: L(i,j) = 1 + L(i-1, j-1)
else: L(i,j) = max { L(i-1, j), L(i, j-1) }
*/
@primaryobjects
primaryobjects / lis.js
Last active January 19, 2021 15:04
Calculate the longest increasing subsequence length in an array, using dynamic programming. https://jsfiddle.net/5wgsuhfz/3/
const lis = arr => {
// Calculate the longest increasing subsequence length in an array.
// A subsequence is array of items that can be obtained by removing other elements.
const L = []; // Store the LIS length for each i-index in the array.
let maxIndex = 0; // Maintain the max length index.
for (let i=0; i<arr.length; i++) {
// We start with the LIS of a[i] = 1.
L[i] = 1;
@primaryobjects
primaryobjects / fib.js
Last active January 19, 2021 02:12
Calculate the nth Fibonacci number using dynamic programming in javascript. https://jsfiddle.net/gx9Lh170/1/
const fib = n => {
// Populate the first two fibonacci values.
const F = [0, 1];
for (let i=2; i<=n; i++) {
F[i] = F[i-1] + F[i-2];
}
return F[n];
};