Skip to content

Instantly share code, notes, and snippets.

View mizhi's full-sized avatar
🦍

Mitch Peabody mizhi

🦍
View GitHub Profile
@mizhi
mizhi / rpn.sc
Created November 14, 2018 05:09
Functional Implementation of a RPN calculator according to daily programmer specification
//*=== Tuesday Nov 13th 2018 - Daily Programmer ===*
//
//*[RPN Calculator]*
//
//"RPN" stands for "Reverse Polish Notation". In an RPN world, instead of using normal "infix" notation, e.g. `2 + 2`, you use "postfix" notation, e.g. `2 2 +`
//```1 2 + 3 * => (1 + 2) * 3 => 9
//1 2 3 * + => 1 + (2 * 3) => 7```
//
//Write an RPN calculator with the following methods:
// ```calculator.push() // Adds the element to the stack
// Create a calculator class that contains add, subtract, multiply, divide,
// and init functions that are chainable with a final result method to display
// the final value.
//
// Again pretty simple to build but was a fun change from all the crazy stuff
// I've been getting.
//
// calc.init(3).add(4).multiply(2).result(); => 14;
//
// Bonus 1 - do it in a statically typed language that only exposes available methods.
@mizhi
mizhi / missing.py
Last active October 24, 2018 14:53
# *=== Wednesday Oct 24th 2018 - Daily Programmer ===*
# *[Find a Missing Number]*
# You are given a list of positive integers from 1 to n. There are no duplicates in list.
# *One* of the integers is missing in the list.
# Write a function to find the two missing integers.
# Examples:
# ```listA = [1, 2, 4, 5] // 3
# *[Make Change]*
#
# Write a function that returns a list that contains the minimum list of coins that can meet to the provided value.
#
# ```>> makeChange(39)
# => [25, 10, 1, 1, 1, 1]
# ^ if no coins are passed in, it defaults to [25, 10, 5, 1]
#
# >> makeChange(14, [10, 7, 1])
# => [7, 7]```
# *[Bracket Match - O(1) Space]*
#
# A string of brackets is considered correctly matched if every opening bracket
# in the string can be paired up with a later closing bracket, and vice
# versa. For instance, “(())()” is correctly matched, whereas “)(“ and “((”
# aren’t. For instance, “((” could become correctly matched by adding two
# closing brackets at the end, so you’d return 2.
#
# Given a string that consists of brackets, write a function bracketMatch that
# takes a bracket string as an input and returns the minimum number of brackets
# From Operation Code slack #daily-programmer
#
# *=== Wednesday Oct 10th 2018 - Daily Programmer ===*
#
# *[Word Count Engine]*
#
# Implement a document scanning function `wordCountEngine`, which receives a
# string document and returns a list of all unique words in it and their number
# of occurrences, sorted by the number of occurrences in a descending order. If
# two or more words have the same count, they should be sorted according to
# This solution computes a memoization of solutions that reduces the exponential
# explosion of the depth-first recursive algorithm.
defmodule AllPaths do
# Computes a map with the answers and returns the answer at {width, height}
def all_paths(width, height) do
all_paths_helper(%{}, {width, height})
|> Map.get({width, height})
end
# Base case: at the left column
# This problem has the properties of having optimal substructure
# and overlapping sub problems. Meaning, if we have the optimal
# solution for smaller parts of the problem, we can compute
# the optimal solution for the containing problem.
#
# We compute the path count using a dynamic programming algorithm.
#
# We recognize that the number of ways to get to each position
# is the sum of the number of ways to get to the position to
# the left of the current position and the number of ways to
def reverse_number(number)
digit_counts = Hash.new(0)
while number > 0
digit_counts[number % 10] += 1
number /= 10
end
(1..9).inject(0) do |accum, digit|
(0...digit_counts[digit]).inject(accum) do |accum2, _|
accum2 * 10 + digit
# From Operation Code slack
# *=== Monday Oct 1st 2018 - Daily Programmer ===*
#
# *[Number Sort]*
#
# Given an integer, soft the digits in ascending order and return the new integer. Ignore leading zeros.
#
# Constraints: Do not convert the integer into a string or other data type.
# Time: O(N)
# Space: O(1)