Skip to content

Instantly share code, notes, and snippets.

View pete-murphy's full-sized avatar

Pete Murphy pete-murphy

View GitHub Profile
@pete-murphy
pete-murphy / almostIncreasingSequence.js
Last active December 28, 2018 16:46
Almost increasing sequence challenge from CodeSignal
// Tests if an array is strictly increasing
const isIncreasing = xs =>
xs.reduce((acc, x, i) => (i > 0 ? acc && x > xs[i - 1] : true), true)
// Drops the element at a given index from a given array
const dropAtIndex = i => xs => xs.filter((_, j) => i !== j)
// Returns all the elements except for the first
const tail = ([_, ...xs]) => xs
@pete-murphy
pete-murphy / README.md
Last active December 30, 2018 16:22
firstNonRepeatingChar Challenge from CodeSignal

Note: Write a solution that only iterates over the string once and uses O(1) additional memory, since this is what you would be asked to do during a real interview.

Given a string s, find and return the first instance of a non-repeating character in it. If there is no such character, return '_'.


Examples 1--3 below pass all visible tests but fail due to timeout on very long strings. I am pretty sure this is because [x, ...xs] is expensive, as is xs.shift(). Example 4 doesn't time out, and the only difference between 3 and 4 is xs.shift() has been replaced with xs.pop() (and the input array has therefore been reversed to accomodate, which I thought would be prohibitively expensive but... 🤷‍♂️). A much more elegant solution is 5--no for loops and no mutation 🙂--and I will never again try to replace reduce with explicit recursion.

@pete-murphy
pete-murphy / clock-angles.js
Last active January 8, 2019 03:44
Clock angles
const getAngleBetweenHands = (hour, min) =>
180 - Math.abs(Math.abs(((hour % 12) + min / 60) * 30 - min * 6) - 180)
@pete-murphy
pete-murphy / longest-string.js
Created January 8, 2019 18:08
Longest string
const longestString = arr =>
arr.reduce((acc, x) => (x.length > acc.length ? x : acc), "")
@pete-murphy
pete-murphy / reverseString.js
Last active January 9, 2019 16:42
Reversing a string
const Iso = (to, from) => ({ to, from })
const string = Iso(cs => cs.join(""), str => [...str])
const reverse = xs => xs.reverse()
const pipe = (...fns) => x => fns.reduce((acc, fn) => fn(acc), x)
// Reverses a string
const reverseString = pipe(
string.from,
reverse,
@pete-murphy
pete-murphy / password.js
Created January 14, 2019 16:27
Curios (AKA stealing functions from Haskell)
// This one is based off a Mark Seeman blog post.
// He gives examples in Haskell (using `sprintf` magic)
// The cool thing is that in JavaScript we have
// n-ary (AKA variadic) functions. So this works:
liftN(6, (...args) => args.join(""))(
["P", "p"],
["a", "4"],
["ssw"],
["o", "0"],
@pete-murphy
pete-murphy / isZigZag.js
Last active January 20, 2019 03:13
Zig Zag
const isZigZag = str => {
const nums = str.split(" ").map(Number)
return nums.every(
(num, i) =>
num > Math.max(nums[i - 1] || -Infinity, nums[i + 1] || -Infinity) ||
num < Math.min(nums[i - 1] || +Infinity, nums[i + 1] || +Infinity)
)
}
isZigZag("4 2 3 1 5 3")
@pete-murphy
pete-murphy / Toggle.js
Last active January 21, 2019 15:41
Simple SVG Toggle in React
import React, { Component } from "react"
export default class Toggle extends Component {
static defaultProps = {
on: false,
onClick: () => {},
colors: {
on: "tomato",
off: "whitesmoke",
fill: "white",
@pete-murphy
pete-murphy / longestZigZag.js
Last active January 21, 2019 17:11
Longest Zig Zagging subsequence
// Using assert for basic tests in Node
const assert = require("assert")
// Utility functions
const pipe = (...fns) => x => fns.reduce((acc, fn) => fn(acc), x)
const map = fn => xs => xs.map(fn)
const filter = fn => xs => xs.filter(fn)
const words = str => str.split(" ")
const length = xs => xs.length
const maximum = xs => Math.max(...xs)
module NewVigenere where
import Data.Char
import Test.Hspec
import Test.QuickCheck
caesar :: Int -> Char -> Char
caesar _ ' ' = ' '
caesar n c = chr $ ordAlpha c + mod (ord c - ordAlpha c + n) 26