This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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++) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
NewerOlder