Skip to content

Instantly share code, notes, and snippets.

View DanielOchoa's full-sized avatar

Daniel Ochoa DanielOchoa

View GitHub Profile
# Alice and Bob each created one problem for HackerRank. A reviewer rates the two challenges, awarding points on a scale from
# 1 to 100 for three categories: problem clarity, originality, and difficulty.
# We define the rating for Alice's challenge to be the triplet A = (a0, a1, a2), and the rating for Bob's challenge to be the
# triplet B = (b0, b1, b2).
# Your task is to find their comparison points by comparing a0 with b0, a1 with b1, and a2 with b2.
# If ai > bi, then Alice is awarded 1 point.
# If ai < bi, then Bob is awarded 1 point.
@DanielOchoa
DanielOchoa / Utf8.exs
Created January 18, 2017 03:17
Working with binary strings
defmodule Utf8 do
#Utf8.each "∂og", &IO.puts/1
def each(str, func) when is_binary(str), do: _each(str, func)
defp _each(<< head :: utf8, tail :: binary >>, func) do
func.(head)
_each(tail, func)
end
var arr = [1,2,3,4];
var int = 6;
// any two numbers in the array add to the second argument.
function takesTwo(anArray, anInt, currentIndex) {
currentIndex = currentIndex || 0;
var arrayLength = anArray.length;
if (currentIndex === arrayLength - 1) return false;
for (var i = 0; i < arrayLength; i++) {
defmodule MyList do
# returns list of numbers from 'from' up to 'to'
def span(from, to) when from < to do
[from | span(from + 1, to)]
end
def span(from, to) when from > to do
raise "first arguments needs to be less than the second"
end
def span(_, to) do
[to]
@DanielOchoa
DanielOchoa / my_list.exs
Last active December 24, 2015 19:41
List playground
# Implement reursive sum without an accumulator
defmodule MyList do
def sum([]), do: 0
def sum([ head | tail ]), do: head + sum(tail)
# reduce
def reduce([], value, _), do: value
def reduce([head | tail], value, func) do
reduce(tail, func.(head, value), func)
end
@DanielOchoa
DanielOchoa / dream_within_a_dream.exs
Last active December 22, 2015 03:46
Get the length of a list recursively plus other stuff
defmodule MyList do
def len([]), do: 0
def len([_ | tail]), do: 1 + len(tail)
def square([]), do: []
def square([ head | tail ]), do: [head*head | square(tail)]
def add_1([]), do: []
def add_1([ head | tail ]), do [ head + 1 | add_1(tail) ]
@DanielOchoa
DanielOchoa / general.exs
Created December 22, 2015 02:40
Module with various ways to do certain things as explained by the routine names
defmodule Example do
def float_to_string do
:io.format "~.2f~n", [2.23232]
end
def get_op_sys_env do
System.get_env "USER"
end
# takes an integer and a range. It will try to guess the n value by doing a
# binary search on the range. For example, the output would looke like the
# following for passing (4, 3..20) :
# Is it 11
# Is it 6
# Is it 4
# 4
defmodule Chop do
@DanielOchoa
DanielOchoa / math_stuff.exs
Created December 16, 2015 04:06
Greatest common denominator function(s) in elixir
defmodule MathStuff do
# Finds greatest common denominator between 2 numbers
def gcd(x, 0), do: x
def gcd(x, y), do: gcd(y, rem(x, y))
# Sum of 1..n numbers
def sum(0), do: 0
def sum(n) do
n + sum(n - 1)
end
@DanielOchoa
DanielOchoa / fizz_buzz.exs
Created December 15, 2015 04:53
Fizz buzz solution with no conditional logic in elixir
fizz_buzz = fn
0, 0, _ -> "FizzBuzz"
0, _, _ -> "Fizz"
_, 0, _ -> "Buzz"
_, _, a -> a
end
integrator = fn
n -> fizz_buzz.(rem(n, 3), rem(n, 5), n)
end