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
# http://nabetani.sakura.ne.jp/kanagawa.rb/evalex/ | |
def evaluate(str) | |
%w(| & + *).each_with_object(str.dup) { |op, e| while e.sub!(/\d+#{Regexp.escape(op)}\d+/) { |s| eval(s) }; end } | |
end | |
[ | |
[ 0, "4*5+6&7|8", "44" ], | |
[ 1, "15*5", "75" ], | |
[ 2, "15+5", "20" ], |
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
# 1. Find the sum of all the multiples of 3 or 5 below 1000. | |
1..999 |> Enum.filter(&(rem(&1, 3) == 0 || rem(&1, 5) == 0)) |> Enum.reduce(&+/2) | |
# 2. By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms. | |
Stream.unfold({1, 2}, fn {n1, n2} -> {n1, {n2, n1 + n2}} end) |> Stream.filter(&(rem(&1, 2) == 0)) |> Enum.take_while(&(&1 < 4000_000)) |> Enum.reduce(&+/2) |
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
-- 1. Find the sum of all the multiples of 3 or 5 below 1000. | |
sum [ x | x <- [1..999], x `mod` 3 == 0 || x `mod` 5 == 0 ] | |
-- 2. By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms. | |
let p2 = sum [ x | x <- takeWhile (< 4000000) fibs, even x] where fibs = 1 : 2 : zipWith (+) fibs (tail fibs) |
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
(define atom? | |
(lambda (x) | |
(and (not (pair? x)) (not (null? x))))) | |
(define add1 (lambda (n) (+ n 1))) | |
(define sub1 (lambda (n) (- n 1))) | |
(define first car) |
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
class Board | |
def initialize(size = 8) | |
@size = size | |
@board = [] | |
@size.times do | |
row = [] | |
@size.times { row << :none } | |
@board << row | |
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
package main | |
import ( | |
"bufio" | |
"fmt" | |
"os" | |
"strconv" | |
) | |
var N, M = 19, 5 |