This library contains utility methods to make it faster to write code in speed-coding competitions, such as Thailand Code Jom.
Upon importing this library, mathn
will be require'd too.
Diving integer with an integer will result in a Rational.
matrix
and prime
is also imported.
Reads a line of text from standard input and split by whitespace.
Input:
hello world, this is a test
Example:
strs # => ["hello", "world,", "this", "is", "a", "test"]
Read several integers from standard input, separated by whitespace.
Equivalent to strs.map(&:to_i)
Input:
3 1 4 1 59265 3 5 8 9
Example:
ints # => [3, 1, 4, 1, 59265, 3, 5, 8, 9]
Read n
lines from standard input and return as array.
Input:
hello
world
test
Example:
eat(2) # => ["hello", "world"]
eat(1) # => ["test"]
Reads a number n
and then reads n
lines and put them in an array.
Input:
3
hello
world
test
cool
Example:
eat # => ["hello", "world", "test"]
Prints yes
if true is given, no
otherwise.
Example:
yesno 5 == 8
yesno 7 == 9
Reads in a number n
, then runs the specified block n
times.
Input:
2
1 2 3
4 5 6
Example:
cases do
p ints
end
Create a proc which memoizes a given block/proc.
memoize(-> f, x { x < 2 ? x : f[x - 1] + f[x - 2] })[50] # => 12586269025
# (this is a the fibonacci function)
Performs trampoline on a block/proc. As long as it returns a block/proc, it will be invoked.
f = trampoline -> f, a, b { a == 0 ? b : -> { f[a - 1, b + 1] } }
f[100000,50000] # => 150000
# (f[a, b] returns the sum of a + b, given a and b are positive)
These methods may be called on any object.
The identity method—just returns self
:wtf.self # => :wtf
Applys the block times
times.
1.apply(3) { |x| x * 2 + 1 } # => 15
# (1 → 3 → 7 → 15)
100000.apply(10, &:prev_prime) # => 99877
Equivalent to Array.new(n) { |index| block }
5.array { |x| x ** 2 } # => [0, 1, 4, 9, 16]
Performs prime factorization.
480.factorize # => [2, 2, 2, 2, 2, 3, 5]
Returns true if 0 <= self < length
-1.bound(10) # => false
0.bound(10) # => true
9.bound(10) # => true
Equivalent to reduce(&:+)
.
(1..100).sum # => 5050
Returns a hash containing the frequency of values in this enumerable.
[3,1,4,1,5,9,2,6,5,3,5,8,9].stat
# => {3=>2, 1=>2, 4=>1, 5=>3, 9=>2, 2=>1, 6=>1, 8=>1}
Rejects nil
. Equivalent to reject(&:nil?)
.
Returns the nth next prime.
1024.next_prime # => 1031
1024.next_prime(10) # => 1091
Returns the nth previous prime.
1024.prev_prime # => 1021
1024.prev_prime(10) # => 967
Returns the (self)th time.
21.st_prime # => 73
42.nd_prime # => 181
1023.th_prime # => 8147
7.th_prime # => 17
Like bsearch
, but returns the index.
Additionally, if the index is not in the array, returns the array length.
For example,
a = [3, 5, 9, 20, 21]
a.lower_bound { |x| x >= 0 } # => 0
a.lower_bound { |x| x >= 2 } # => 0
a.lower_bound { |x| x >= 3 } # => 0
a.lower_bound { |x| x >= 4 } # => 1
a.lower_bound { |x| x >= 5 } # => 1
a.lower_bound { |x| x >= 18 } # => 3
a.lower_bound { |x| x >= 20 } # => 3
a.lower_bound { |x| x >= 21 } # => 4
a.lower_bound { |x| x >= 40 } # => 5