There is Ruby's #OneLiners solutions for Sololearn Code Coach challenges. All of the solutions have passed the tests in Sololearn. Currently there are 61 out of 72 or 85% of code challenges that I can solve in one line of code.
puts [gets.to_i, gets.to_i].reverse.inject(:%) == 0 ? 'give away' : 'eat them yourself'
puts (100 * 2.0 / gets.to_i).ceil
puts gets.to_i / 6
puts '%.2f' % [((menu={Nachos: 6, Pizza: 6, Cheeseburger: 10, Water: 4, Coke: 5}).default = menu[:Coke]), gets.split.map {|i| menu[i.to_sym]*1.07}.inject(:+)][1]
puts gets.to_i / 12 >= gets.to_i ? "Buy it!" : "Try again"
puts (1.1 * (5 * gets.to_i + 40)).round
puts gets.to_i < gets.to_i * 50 ? "Pesos" : "Dollars"
puts "Apartment " + (Array.new(2){gets.split(",").map(&:to_i).inject(:*)}.inject(:>) ? "A": "B")
puts 9 * gets.to_i + 9
puts (2.0 * gets.to_i * gets.to_i / 10).ceil
puts gets.to_i > gets.to_i + gets.to_i ? 'Keep Hunting' : 'Candy Time'
puts gets.to_i + gets.to_i
puts ["I got this!", "Help me Batman", "Good Luck out there!"][((gets.to_i-5)/6<=>0)+1]
puts ['Loss','Broke Even','Profit'][1+(gets.to_i<=>7)]
puts gets.split.map {|item| {Lettuce: 5, Carrot: 4, Mango: 9, Cheeseburger: 0}[item.to_sym]}.inject(:+) >= 10 ? 'Come on Down!' : 'Time to wait'
puts gets.split.map{|sound| {Grr:"Lion",Rawr:"Tiger",Ssss:"Snake",Chirp:"Bird"}[sound.to_sym]}.join(' ')
puts gets.to_i / 20 * 20 + 10
puts 'My twin is %d years old and they are %d years older than me' % [gets.to_i + y = gets.to_i, y]
puts gets.reverse
puts nil!=(gets=~/^\d{5}$/)
puts gets.gsub(/[^AEIOUaeiou]/, '').size
puts gets.match(/(.).*\1/) == nil
puts (0...gets.to_i).filter {|i| i % 3 == 0 || i % 5 == 0}.inject(:+)
puts gets.to_i.to_s(2).gsub(/0/,'').size
puts gets.split.map {|w| w[1,w.size] + w[0] + 'ay'}.join(' ')
puts gets.chomp.split(',').index(gets) * 5 + 5
puts [t=(s=gets.to_f)/gets.to_i,gets.to_i>(50+s)/t ?'Yum': 'Meep Meep'][1]
puts Array.new(6){gets.to_i}.map{|i| i % 3 > 0 ? i % 2 > 0 ? "Snap" : "Crackle" : "Pop"}.join(' ')
puts gets =~ /(.).*\1/ ? 'Deja Vu' : 'Unique'
puts gets.gsub(/[^A-Za-z ]/, '').reverse
printf("#%02x%02x%02x", gets.to_i, gets.to_i, gets.to_i)
puts gets.gsub(/[^A-Za-z 0-9]/, '')
puts [7 - gets.to_i % gets.to_i].map{|x| x > 0 ? 'I need to buy %d more' % x : 'Cake Time'}
puts (1.07 * 0.30 * gets.split(',').map(&:to_f).sort.reverse.drop(1).push(0).inject(:+)).to_i
puts gets.split.map(&:to_f).any?{|x| 20 < 1.1 * x} ? 'Back to the store': 'On to the terminal'
puts Array.new(4){gets.to_i % 15}.inject(:+)
puts gets.chomp + ' ' + gets =~ /\b(\w).*\b\1\w*$/ ? 'Compare notes' : 'No such luck'
puts Array.new(gets.to_i){gets.to_i}.select(&:even?).push(0).inject(:+)
puts gets.split.map{|w| w=~/1?\d/ ? ['zero','one','two','three','four','five','six','seven','eight','nine','ten'][w.to_i]: w}.join(' ')
puts gets.downcase.chars.map{|c| c=~/^[a-z]$/?('az'.sum-c.ord).chr: c}.join
puts [n=gets.to_i,gets.split.map(&:to_i).any?{|x| n%x>0} ? 'not divisible by all': 'divisible by all'][1]
puts gets.split.map(&:to_i).select(&:even?).join(' ')
puts gets.split(',').map(&:to_f).map{|x| x < 20 ? 1.07 * x : x}.inject(:+)
puts gets.gsub(/^B*[HP](B*)[HP]B*$/,'\1').size
puts ((essay=gets).gsub(/[^a-zA-Z]+/,'').size.to_f / essay.split.size).ceil
puts gets.gsub(/.*[=\/](?=[^=\/]*$)/, '')
puts gets.gsub(/(?<=[^\/b])([A-Z])/, '_\1').downcase
puts gets.split(/(?<=\d)/).map{|x| x[0] * x[1].to_i}.join()
puts 0 < Array.new(gets.to_i){gets.to_i}.reduce{|a,b| a > 0 && b > a ? a + b : 0}
puts nil!=(gets =~/^(\S*(\S) \2\S*)*$/)
puts gets.chomp.split(Regexp.new '(?<=\G.{%d})' % gets.to_i).join('-')
print ((a=Array.new(gets.to_i){gets.to_i})[0].upto(a[-1]).to_a - a).join(' ')
puts Array.new(gets.to_i){gets.chomp.gsub(/(\w)\w* (\w)\w*/,'\1\2')}.join(' ')
puts (n=gets).size == 16 && n.reverse.chars.map(&:to_i).map.with_index{|x,i| i.odd?? x>4 ? x*2-9: x*2: x}.sum%10==0 ? 'valid': 'not valid'
puts '%d,%d,%d' % (a=Array.new(4){1-gets.to_f}).first(3).map{|x| (255*x*a.last).round}
puts [hero=gets, agents=gets.to_i, 20 * (1 + gets.split.select{|name| name < hero}.size / agents)][2]
puts Array.new(4){gets.chomp}.any?{|s| s == s.reverse} ? 'Open' : 'Trash'
puts [gets].map{|x| x.length > 6 && x =~ /\d.*\d/ && x =~ /([!@#$%&*].*){2,}/ ? "Strong": "Weak"}
puts gets =~ /[$T].*G.*[$T]/ ? 'quiet': 'ALARM'
puts (b=(m=gets).index('P',1+a=m.index('P')))/6-a/6+(a%6-b%6).abs
puts [q=Array.new(2,1),2.upto(gets.to_i-1){|i| q<<q[i-q.last]+q[i-q[i-2]]},q.last].last
The one-liner is equals to the following code:
def Q(n)
q = Array.new(2, 1)
2.upto(n - 1){|i| q << q[i - q.last] + q[i - q[i - 2]]}
return q.last
end
n = gets.to_i
puts Q(n)
Using the fact that required digits of pi limited by 1000 this problem can be solved by coping first 1000 pi digits from Wikipedia and writing one-liner like this:
puts '.1415926...'[gets.to_i]
You can check program in python that uses this approach: DigitsPi.py
Hovewer there is program to compute pi digits by using The Gauss–Legendre iterative algorithm:
require 'bigdecimal'
# The Gauss–Legendre iterative algorithm
def calculatePi(precision = 800)
# The algorithm has quadratic convergence, which essentially means that
# the number of correct digits doubles with each iteration of the algorithm.
BigDecimal.limit(precision);
one = BigDecimal(1)
two = BigDecimal(2)
four = BigDecimal(4)
# Initial value setting:
a = one
b = one / two.sqrt(precision)
t = one / four
p = one
# Repeat the following instructions until
# the difference of a and b is within the desired accuracy:
c = one / 10 ** precision
until (a - b).abs <= c do
an = (a + b) / two
bn = (a * b).sqrt(precision)
tn = t - (p * ((a - an) ** two))
pn = two * p
a = an
b = bn
t = tn
p = pn
end
# π is then approximated as:
((a + b) ** 2) / (t * four)
end
n = gets.to_i + 2
pi = calculatePi(n).to_s
puts pi[n]