Skip to content

Instantly share code, notes, and snippets.

@pibako
pibako / test.rb
Last active August 29, 2015 14:10
[
[true, false], # => [true, false]
[false, true], # => [false, true]
[true, true], # => [true, true]
[false, false], # => [false, false]
[false, false, false], # => [false, false, false]
[false, false, true], # => [false, false, true]
pi@mac:~/company/minishrimp-container/minishrimp-server$ be rake -T -A | grep test
rake db:test:clone #
rake db:test:clone_structure #
rake db:test:load #
rake db:test:load_schema #
rake db:test:load_structure #
rake db:test:prepare #
rake db:test:purge #
rake emba:test # Extra rake tast
rake test # Runs test:units, test:functionals, test:integration together (also available: test:benchmark, test:profile, test:plugins)
def devise(n)
arr = n.to_s.split("").reverse.each_slice(3).to_a.reverse.map do |group| # => [["1"]], [["0", "1"]], [["0", "0", "1"]], [["1"], ["0", "0", "0"]], [["0", "1"], ["0", "0", "0"]], [["0", "0", "1"], ["0", "0", "0"]], [["1"], ["0", "0", "0"], ["0", "0", "0"]], [["5", "3"], ["5", "3", "2"], ["5", "3", "2"]]
group.reverse.join("") # => "1", "10", "100", "1", "000", "10", "000", "100", "000", "1", "000", "000", "35", "235", "235"
end # => ["1"], ["10"], ["100"], ["1", "000"], ["10", "000"], ["100", "000"], ["1", "000", "000"], ["35", "235", "235"]
arr.join(",") # => "1", "10", "100", "1,000", "10,000", "100,000", "1,000,000", "35,235,235"
end
devise(1) == "1" # => true
devise(10) == "10" # => true
# wl-test.txt
# asalsa
# heros
# horse
# idisd
# shore
# shores
# sshore
def sorted_letters(word)
input = "abc cba" # => "abc cba"
input = "cB!a This is a valid\n input, you have to read the specification carefully! abc" # => "cB!a This is a valid\n input, you have to read the specification carefully! abc"
input = input.downcase # => "cb!a this is a valid\n input, you have to read the specification carefully! abc"
input = input.gsub(/\W/) { |c| # => "cb!a this is a valid\n input, you have to read the specification carefully! abc"
c =~ /\s/ ? " " : "" # => "", " ", " ", " ", " ", " ", " ", "", " ", " ", " ", " ", " ", " ", " ", "", " "
} # => "cba this is a valid input you have to read the specification carefully abc"
arr = input.split(" ") # => ["cba", "this", "is", "a", "valid", "input", "you", "have", "to", "read", "the", "specification", "carefully", "abc"]
class Color
def initialize
@colors = ["R", "G", "B"] # => ["R", "G", "B"], ["R", "G", "B"]
end
def get
c = @colors.first # => "R", "G", "B", "R", "G", "B", "R", "G", "B", "R", "G", "B", "R", "G", "B", "R", "G", "B", "R", "G", "B", "R", "G", "B", "R"
@colors = @colors.rotate # => ["G", "B", "R"], ["B", "R", "G"], ["R", "G", "B"], ["G", "B", "R"], ["B", "R", "G"], ["R", "G", "B"], ["G", "B", "R"], ["B", "R", "G"], ["R", "G", "B"], ["G", "B", "R"], ["B", "R", "G"], ["R", "G", "B"], ["G", "B", "R"], ["B", "R", "G"], ["R", "G", "B"], ["G", "B", "R"], ["B", "R", "G"], ["R", "G", "B"], ["G", "B", "R"], ["B", "R", "G"], ["R", "G", "B"], ["G", "B", "R"], ["B", "R", "G"], ["R", "G", "B"], ["G", "B", "R"]
c # => "R", "G", "B", "R", "G", "B", "R", "G", "B", "R", "G", "B", "R", "G", "B", "R", "G", "B", "R", "G", "B", "R", "G", "B", "R"
end
#!/bin/sh
exec scala -savecompiled "$0" "$@"
!#
// This is how magic of Action object in Play Framework works.
object Action {
val foo = "foo"
def apply(text: String) = {
@pibako
pibako / S99_P15.scala
Created October 16, 2013 17:40
Scalania solution 15
package pl.japila.scalania.s99
object S99_P15 {
def duplicateN[T](n: Int, ts: Seq[T]): Seq[T] = ts match {
case Nil => Nil
case x :: xs =>
List.fill(n)(x) ++ duplicateN(n, xs)
// (0 until n toList).map { _ => x } ++ duplicateN(n, xs)
}
@pibako
pibako / lazy.rb
Created October 14, 2013 11:50
Lazy ruby 2.0
# works with ruby 2.0
def fib(x)
if x <= 1
1
else
fib(x-1) + fib(x-2)
end
end
@pibako
pibako / codility.rb
Last active December 25, 2015 09:29
Codility challenge
class Solution
attr_reader :array # => nil
def initialize(a)
@array = a # => [1, 5, 3, 4, 3, 4, 1, 2, 3, 4, 6, 2], [4, 5, 8, 5, 1, 4, 6, 8, 7, 2, 2, 5], [1, 5, 3, 4, 3, 4, 1, 2, 3, 4, 6, 2], [4, 5, 8, 5, 1, 4, 6, 8, 7, 2, 2, 5], [1, 5, 3, 4, 3, 4, 1, 2, 3, 4, 6, 2], [4, 5, 8, 5, 1, 4, 6, 8, 7, 2, 2, 5], [1, 5, 3, 4, 3, 4, 1, 2, 3, 4, 6, 2], [4, 5, 8, 5, 1, 4, 6, 8, 7, 2, 2, 5]
end
def peak?(left, center, right)
left < center && center > right # => true, false, true, false, true, false, false, false, false, true, false, true, false, false, false, false, true, false, false, false, false, true, false, true, false, true, false, true, false, false, false, false, true, false, true, false, false, false, false, true, false, false, false, true, false, true, false, true, false, false, false, false, true, false, true, false, false, false, false, true, false, false, false
end