Skip to content

Instantly share code, notes, and snippets.

View loschtreality's full-sized avatar

Michael Losch loschtreality

  • San Francisco, California
View GitHub Profile
@loschtreality
loschtreality / perm.js
Created January 27, 2017 20:08
Permutations in JS & Ruby
// Given a string, write a function that uses recursion to output a list of all the possible permutations of that string.
// For example, given s='abc' the function should return ['abc', 'acb', 'bac', 'bca', 'cab', 'cba']
// Note: If a character is repeated, treat each occurence as distinct,
// for example an input of 'xxx' would return a list with 6 "versions" of 'xxx'
const permutations = (string) => {
let output = []
@loschtreality
loschtreality / fifth_largest.js
Created February 22, 2017 22:32
Find fifth largest element in the array. Don't use any native sort methods
const fifth_largest = (array) => {
const cache = []
let highest = -Infinity
let highest_index = -1
for (var i = 0; i < 5; i++) {
for (var index = 0; index < array.length; index++) {
if (array[index] > highest) {
highest = array[index]
@loschtreality
loschtreality / mx.js
Created March 20, 2017 15:54
Max slice with merge
const solution = (array) => {
if (array.length === 0) return 0
const midPoint = Math.floor(array.length / 2)
const left = array.slice(1, midPoint - 1)
const right = array.slice(midPoint, array.length - 2)
const leftSum = left.reduce((sum, current) => { return sum + current }, 0)
const rightSum = right.reduce((sum, current) => { return sum + current }, 0)
@loschtreality
loschtreality / maxDoubleSlice.js
Created March 20, 2017 17:06
max double slice
const solution = (array) => {
const maxEndings = [0, 0, 0, 0, 0, 0, 0, 0]
const maxBeginnings = [0, 0, 0, 0, 0, 0, 0, 0]
for (let i = 1, len = array.length - 1; i < len; i++) {
maxEndings[i] = Math.max(maxEndings[i - 1] + array[i], 0)
}
for (let j = array.length - 2; j > 0; j--) {
@loschtreality
loschtreality / ls.sh
Created April 25, 2017 03:16
cd & ls
cd() { builtin cd "$@"; ls; }

Keybase proof

I hereby claim:

  • I am loschtreality on github.
  • I am loschtreality (https://keybase.io/loschtreality) on keybase.
  • I have a public key ASBxYuByFwAEGGXVq2PAthoc1kL_vK2tQkHQg0G5qVEjTwo

To claim this, I am signing this object:

// Write a function which takes in an array of elements and prints the elements in reverse to the console
function printBackwards(inputArray) {
// write your code here
}
// A few tests
console.log(printBackwards([1, 2, 3, 4, 5])) // prints "5 4 3 2 1"
console.log(printBackwards(["o", "l", "l", "e", "h"]) // prints "h e l l o"