Skip to content

Instantly share code, notes, and snippets.

@tomohiro
Created June 13, 2011 08:24
Show Gist options
  • Save tomohiro/1022457 to your computer and use it in GitHub Desktop.
Save tomohiro/1022457 to your computer and use it in GitHub Desktop.
My programming challenges in CodeEval
rvm 1.8.6
#!/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 }) * ' ' }
#!/usr/bin/env ruby
puts lambda { n = $<.gets.to_i and $<.sort { |a, b| a.size <=> b.size }.reverse[0...n] }.call
#!/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
#!/usr/bin/env ruby
require 'mathn'
puts lambda { (p, s = Prime.new, 0) and 1000.times { s += p.succ } and s }.call
#!/usr/bin/env ruby
$<.each { |l| puts l.split.reverse.join ' ' }
#!/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 }
#!/usr/bin/env ruby
$<.each { |l| puts lambda { s = l.split(', ') and s[0].delete(s[1]) }.call }
#!/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 ',' }
#!/usr/bin/env ruby
puts [1].pack('s') == [1].pack('n') ? :BigEndian : :LittleEndian
#!/usr/bin/env ruby
$<.each { |s| puts s.to_i.to_s(2).scan('1').size }
#!/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
#!/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 }
#!/usr/bin/env ruby
$<.each { |l| puts l.downcase }
#!/usr/bin/env ruby
$<.each { |l| puts l.split(//).map { |s| s.to_i }.inject { |a, b| a += b } }
#!/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 }
#!/usr/bin/env ruby
(1..12).each { |x| puts ((1..12).map { |y| format '%4i', x * y }.join) }
#!/usr/bin/env ruby
puts $<.inject(0) { |r, s| r + s.to_i }
#!/usr/bin/env ruby
(1..99).step(2) { |i| puts i }
#!/usr/bin/env ruby
puts File.size $<
#!/usr/bin/env ruby
$<.each { |s| puts s.to_i.to_s(2) }
#!/usr/bin/env ruby
$<.each { |l| puts l.split(',').map { |s| s.to_i }.uniq.sort.join ',' }
#!/usr/bin/env ruby
$<.each { |l| puts lambda { (a, b = l.split(';')) and (a.split(',') & b.split(',')).join(',') }.call }
#!/usr/bin/env ruby
$<.each { |l| puts lambda { s = l.split(',') and p = s[0].rindex(s[1].strip) or p ? p : -1 }.call }
#!/usr/bin/env ruby
$<.each { |l| puts lambda { x = l.split(',') and x[0].scan(x[1].strip).empty? ? 0 : 1 }.call }
#!/usr/bin/env ruby
$<.each { |l| puts l =~ /^\w+@\w+\.\D+$/ ? :true : :false }
#!/usr/bin/env ruby
$<.each { |l| puts lambda { d = ('a'..'z').to_a - l.downcase.split(//) and d.empty? ? :NULL : d.sort.join }.call }
#!/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 }
#!/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