This file contains 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
characters = [ 'o', 'h', 'e', 'd', 'n', 'r', 'y', 'g', 'a', 'b', 'e' ] | |
positions = [ 1, 0, 4, 2, 6, 9, 8, 3, 7, 10, 5] | |
def rearrange(characters, positions) | |
positions.length.times do |i| | |
positions[i] = characters[positions[i]] | |
end | |
positions.each_with_index {|e, i| characters[i] = e} | |
end |
This file contains 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 find_index(ary, index = 0) | |
midpt = ary.length / 2 | |
if midpt <= 1 | |
return ary.first < ary.last ? index : index + 1 | |
end | |
left, right = ary[0...midpt], ary[midpt..-1] | |
if left.last > left.first && ary.last < ary.first | |
index += midpt | |
find_index(right, index) | |
else |
This file contains 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
source :rubygems | |
# We are not loading Active Record, nor Active Resources etc. | |
# We can do this in any app by simply replacing the rails gem | |
# by the parts we want to use. | |
gem "actionpack", "~> 3.2" | |
gem "railties", "~> 3.2" | |
gem "tzinfo" | |
# Let's use thin |
This file contains 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
(defn to-fizzword [number] | |
(let [fizzword (str (if (zero? (mod number 3)) "Fizz") | |
(if (zero? (mod number 5)) "Buzz"))] | |
(if (empty? fizzword) number fizzword))) | |
(defn fizzbuzz [largest] | |
(map to-fizzword (range 1 (inc largest)))) |
This file contains 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
(fn [string] | |
(let [br (re-seq #"[()\[\]{}]" string)] | |
(loop [brackets br acc []] | |
(if (empty? brackets) | |
true | |
(if-let [lb (#{"(" "[" "{"} (first brackets))] | |
(recur (rest brackets) (conj acc lb)) | |
(if (= (peek acc) ({")" "(" "}" "{" "[" "]"} (first brackets))) | |
(recur (rest brackets) (pop acc)) | |
false)))))) |
This file contains 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
juxt = ->(*fns) do | |
->(*args) do | |
fns.map do |fn| | |
args.reduce {|acc, e| fn.to_proc.call acc, e} | |
end | |
end | |
end | |
max = ->(a, b) {a > b ? a : b} | |
min = ->(a, b) {a < b ? a : b} |
This file contains 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
(ns shuffle) | |
(defn gcd [a b] (if (zero? b) a (recur b (mod a b)))) | |
(defn lcm [a b] (/ (* a b) (gcd a b))) | |
(defn- new-position [deck-size cut-size initial-position] | |
(let [cards-shuffled (* 2 cut-size) | |
offset (- deck-size cards-shuffled)] | |
(cond | |
(<= initial-position cut-size) (+ (dec (* 2 initial-position)) offset) |
OlderNewer