Created
June 13, 2011 08:24
-
-
Save tomohiro/1022457 to your computer and use it in GitHub Desktop.
My programming challenges in CodeEval
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
rvm 1.8.6 |
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
#!/usr/bin/env ruby | |
$<.each { |l| puts lambda { |a, b, n| (1..n).map { |x| x % (a * b) == 0 ? :FB : x % a == 0 ? :F : x % b == 0 ? :B : x } }.call(*l.split.map { |s| s.to_i }) * ' ' } |
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
#!/usr/bin/env ruby | |
puts lambda { n = $<.gets.to_i and $<.sort { |a, b| a.size <=> b.size }.reverse[0...n] }.call |
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
#!/usr/bin/env ruby | |
require 'mathn' | |
a = [] and Prime.new.each { |p| p < 1000 ? (a << p if p.to_s == p.to_s.reverse) : break } or puts a.last |
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
#!/usr/bin/env ruby | |
require 'mathn' | |
puts lambda { (p, s = Prime.new, 0) and 1000.times { s += p.succ } and s }.call |
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
#!/usr/bin/env ruby | |
$<.each { |l| puts l.split.reverse.join ' ' } |
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
#!/usr/bin/env ruby | |
$<.each { |l| a = l.split and i = a.pop.to_i and lambda { puts a[a.size - i] if a.size >= i }.call } |
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
#!/usr/bin/env ruby | |
$<.each { |l| puts lambda { s = l.split(', ') and s[0].delete(s[1]) }.call } |
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
#!/usr/bin/env ruby | |
class Array | |
def permutation n = self.size | |
return [[]] if n < 1 | |
perm = [] | |
self.each do |e| | |
x = self.dup | |
x.delete_at x.index(e) | |
x.permutation(n - 1).each { |p| perm << [e] + p } | |
end | |
perm | |
end | |
end | |
$<.each { |l| puts l.strip.split(//).permutation.sort.map { |x| x.join }.join ',' } |
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
#!/usr/bin/env ruby | |
puts [1].pack('s') == [1].pack('n') ? :BigEndian : :LittleEndian |
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
#!/usr/bin/env ruby | |
$<.each { |s| puts s.to_i.to_s(2).scan('1').size } |
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
#!/usr/bin/env ruby | |
$<.each do |l| | |
x, n = l.split(',').map { |s| s.to_i } | |
while (n <= x) | |
n += n | |
end | |
puts n | |
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
#!/usr/bin/env ruby | |
$<.each { |l| puts lambda { a = l.split(',').map { |s| s.to_i } and b = '%b' % a[0] and b[-a[1]] == b[-a[2]] }.call } |
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
#!/usr/bin/env ruby | |
$<.each { |l| puts l.downcase } |
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
#!/usr/bin/env ruby | |
$<.each { |l| puts l.split(//).map { |s| s.to_i }.inject { |a, b| a += b } } |
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
#!/usr/bin/env ruby | |
$<.each { |s| puts lambda { f = lambda { |i| i <= 2 ? 1 : f.call(i - 1) + f.call(i - 2) } and f.call(s.to_i) }.call } |
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
#!/usr/bin/env ruby | |
(1..12).each { |x| puts ((1..12).map { |y| format '%4i', x * y }.join) } |
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
#!/usr/bin/env ruby | |
puts $<.inject(0) { |r, s| r + s.to_i } |
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
#!/usr/bin/env ruby | |
(1..99).step(2) { |i| puts i } |
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
#!/usr/bin/env ruby | |
puts File.size $< |
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
#!/usr/bin/env ruby | |
$<.each { |s| puts s.to_i.to_s(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
#!/usr/bin/env ruby | |
$<.each { |l| puts l.split(',').map { |s| s.to_i }.uniq.sort.join ',' } |
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
#!/usr/bin/env ruby | |
$<.each { |l| puts lambda { (a, b = l.split(';')) and (a.split(',') & b.split(',')).join(',') }.call } |
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
#!/usr/bin/env ruby | |
$<.each { |l| puts lambda { s = l.split(',') and p = s[0].rindex(s[1].strip) or p ? p : -1 }.call } |
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
#!/usr/bin/env ruby | |
$<.each { |l| puts lambda { x = l.split(',') and x[0].scan(x[1].strip).empty? ? 0 : 1 }.call } |
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
#!/usr/bin/env ruby | |
$<.each { |l| puts l =~ /^\w+@\w+\.\D+$/ ? :true : :false } |
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
#!/usr/bin/env ruby | |
$<.each { |l| puts lambda { d = ('a'..'z').to_a - l.downcase.split(//) and d.empty? ? :NULL : d.sort.join }.call } |
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
#!/usr/bin/env ruby | |
$<.each { |l| puts lambda { h = lambda { |a| i = a.split(//).map { |s| s.to_i }.inject(0) { |x, y| x += y * y } and i == 1 ? 1 : i == (4 || 0) ? 0 : h.call(i.to_s) } and h.call(l.strip) }.call } |
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
#!/usr/bin/env ruby | |
$<.each { |l| puts lambda { (a, r = l.strip.split(//), nil) and a.each_index { |i| break unless r = a[i].to_i == l.scan(i.to_s).size } }.call ? 1 : 0 } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment