https://github.com/dtinth/tubtim
A collection of Ruby helper methods, created for use in Thailand Code Jom 2013. (Team: irb / ratamani)
https://github.com/dtinth/tubtim
competitive-programming ruby
Last synced: 11 months ago
JSON representation
A collection of Ruby helper methods, created for use in Thailand Code Jom 2013. (Team: irb / ratamani)
- Host: GitHub
- URL: https://github.com/dtinth/tubtim
- Owner: dtinth
- Created: 2013-01-22T06:52:19.000Z (over 13 years ago)
- Default Branch: master
- Last Pushed: 2014-01-25T15:45:29.000Z (over 12 years ago)
- Last Synced: 2025-01-23T19:48:07.022Z (over 1 year ago)
- Topics: competitive-programming, ruby
- Language: Ruby
- Homepage:
- Size: 180 KB
- Stars: 2
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
Tubtim — helper library for speed coding
===
This library contains utility methods to make it faster to write code
in speed-coding competitions, such as [Thailand Code Jom](http://codejom.thailandoi.org/).
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.
API: Global Functions
---------------------
### strs → ary
Reads a line of text from standard input and split by whitespace.
Input:
hello world, this is a test
Example:
```ruby
strs # => ["hello", "world,", "this", "is", "a", "test"]
```
### ints → ary
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:
```ruby
ints # => [3, 1, 4, 1, 59265, 3, 5, 8, 9]
```
### eat(n) → ary
Read `n` lines from standard input and return as array.
Input:
hello
world
test
Example:
```ruby
eat(2) # => ["hello", "world"]
eat(1) # => ["test"]
```
### eat → ary
Reads a number `n` and then reads `n` lines and put them in an array.
Input:
3
hello
world
test
cool
Example:
```ruby
eat # => ["hello", "world", "test"]
```
### yesno(bool) → nil
Prints `yes` if true is given, `no` otherwise.
Example:
```ruby
yesno 5 == 8
yesno 7 == 9
```
### cases { |index| block }
Reads in a number `n`, then runs the specified block `n` times.
Input:
2
1 2 3
4 5 6
Example:
```ruby
cases do
p ints
end
```
### memoize { |recurse, *args| block } → proc → any
memoize(proc) → proc → any
Create a proc which memoizes a given block/proc.
```ruby
memoize(-> f, x { x < 2 ? x : f[x - 1] + f[x - 2] })[50] # => 12586269025
# (this is a the fibonacci function)
```
### trampoline { |recurse, *args| block } → proc → any
trampoline(proc) → proc → any
Performs trampoline on a block/proc.
As long as it returns a block/proc, it will be invoked.
```ruby
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)
```
API: Object
-----------
These methods may be called on any object.
### self → self
The identity method—just returns self
```ruby
:wtf.self # => :wtf
```
### apply(times) { |obj| block } → obj
Applys the block `times` times.
```ruby
1.apply(3) { |x| x * 2 + 1 } # => 15
# (1 → 3 → 7 → 15)
100000.apply(10, &:prev_prime) # => 99877
```
API: Integer
------------
### array { |index| block } → ary
Equivalent to `Array.new(n) { |index| block }`
```ruby
5.array { |x| x ** 2 } # => [0, 1, 4, 9, 16]
```
### factorize → ary
Performs prime factorization.
```ruby
480.factorize # => [2, 2, 2, 2, 2, 3, 5]
```
### bound(length) → bool
Returns true if 0 <= self < length
```ruby
-1.bound(10) # => false
0.bound(10) # => true
9.bound(10) # => true
```
API: Enumerable
---------------
### sum → any
Equivalent to `reduce(&:+)`.
```ruby
(1..100).sum # => 5050
```
### stat → hash
Returns a hash containing the frequency of values in this enumerable.
```ruby
[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}
```
### compact → array
Rejects `nil`. Equivalent to `reject(&:nil?)`.
API: Fixnum
-----------
### next_prime(n=1) → fixnum
Returns the nth next prime.
```ruby
1024.next_prime # => 1031
1024.next_prime(10) # => 1091
```
### prev_prime(n=1) → fixnum
Returns the nth previous prime.
```ruby
1024.prev_prime # => 1021
1024.prev_prime(10) # => 967
```
### th_prime → fixnum
st_prime → fixnum
nd_prime → fixnum
rd_prime → fixnum
Returns the (self)th time.
```ruby
21.st_prime # => 73
42.nd_prime # => 181
1023.th_prime # => 8147
7.th_prime # => 17
```
API: Array
----------
### lower_bound { |elem| block } → int
Like `bsearch`, but returns the index.
Additionally, if the index is not in the array,
returns the array length.
For example,
```ruby
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
```