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
| {-# LANGUAGE GADTs, DataKinds, | |
| TypeFamilies, UndecidableInstances #-} | |
| module OddsAndEvens where | |
| -- | The natural numbers. | |
| data Nat = Z | S Nat | |
| -- | The axioms of even numbers. | |
| data Even (a :: Nat) :: * where |
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
| 04c258f70b834445ce3ba8854e71dccdae356383ee13dad47631a7f2917997b2eba2189498dfbea9cd047e14a680e2e73ced2bc2630fcefc54421f1b93b67d03db |
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
| def edit_distance(a, b) | |
| return b.size if a.size == 0 | |
| return a.size if b.size == 0 | |
| init = {} | |
| init[0] = (0...b.size).map{|j| [j, b[0, j+1].include?(a[0]) ? j : j + 1]}.to_h | |
| (1...a.size).each do |i| | |
| init[i] = {0 => a[0, i+1].include?(b[0]) ? i : i + 1} | |
| end | |
| (1...a.size).to_a.product((1...b.size).to_a).reduce(init) do |acc, (i, j)| |
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
| def solve | |
| constriants = { | |
| betty: (1..5), | |
| kitty: (1..5), | |
| mary: (1..5), | |
| ethel: (1..5), | |
| joan: (1..5), | |
| } | |
| names = constriants.keys | |
| products = names.map {|name| constriants[name].to_a }.reduce(&:product).map(&:flatten) |
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
| def longest_slide_down(pyramid) | |
| pyramid.reverse.reduce([0]*(pyramid.size + 1)) {|result, x|(0...result.size-1).reduce([]) {|acc, i| acc + [[result[i], result[i+1]].max + x[i]]}}[0] | |
| end |