Skip to content

Latest commit

 

History

History
436 lines (302 loc) · 9.74 KB

OneLinersRuby.md

File metadata and controls

436 lines (302 loc) · 9.74 KB

OneLiners solutions in Ruby

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.

Easy

Popsicles

puts [gets.to_i, gets.to_i].reverse.inject(:%) == 0 ? 'give away' : 'eat them yourself'

Halloween Candy

puts (100 * 2.0 / gets.to_i).ceil

Fruit Bowl

puts gets.to_i / 6

Ballpark Orders

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]

Skee-ball

puts gets.to_i / 12 >= gets.to_i ? "Buy it!" : "Try again"

Paint Costs

puts (1.1 * (5 * gets.to_i + 40)).round

Argentina

puts gets.to_i < gets.to_i * 50 ? "Pesos" : "Dollars"

Balconies

puts "Apartment " + (Array.new(2){gets.split(",").map(&:to_i).inject(:*)}.inject(:>) ? "A": "B")

Candles

puts 9 * gets.to_i + 9

Duct Tape

puts (2.0 * gets.to_i * gets.to_i / 10).ceil

Easter Eggs

puts gets.to_i > gets.to_i + gets.to_i ? 'Keep Hunting' : 'Candy Time'

Guard Flamingos

puts gets.to_i + gets.to_i

Gotham City

puts ["I got this!", "Help me Batman", "Good Luck out there!"][((gets.to_i-5)/6<=>0)+1]

Hovercraft

puts ['Loss','Broke Even','Profit'][1+(gets.to_i<=>7)]

Izzy the Iguana

puts gets.split.map {|item| {Lettuce: 5, Carrot: 4, Mango: 9, Cheeseburger: 0}[item.to_sym]}.inject(:+) >= 10 ? 'Come on Down!' : 'Time to wait'

Jungle Camping

puts gets.split.map{|sound| {Grr:"Lion",Rawr:"Tiger",Ssss:"Snake",Chirp:"Bird"}[sound.to_sym]}.join(' ')

Land Ho!

puts gets.to_i / 20 * 20 + 10

Neverland

puts 'My twin is %d years old and they are %d years older than me' % [gets.to_i + y = gets.to_i, y]

Extra-Terrestrials

puts gets.reverse

Zip Code Validator

puts nil!=(gets=~/^\d{5}$/)

Vowel Counter

puts gets.gsub(/[^AEIOUaeiou]/, '').size

Isogram Detector

puts gets.match(/(.).*\1/) == nil

Multiples

puts (0...gets.to_i).filter {|i| i % 3 == 0 || i % 5 == 0}.inject(:+)

Number of Ones

puts gets.to_i.to_s(2).gsub(/0/,'').size

Medium

Pig Latin

puts gets.split.map {|w| w[1,w.size] + w[0] + 'ay'}.join(' ')

Safety Deposit Boxes

puts gets.chomp.split(',').index(gets) * 5 + 5

Roadrunner

puts [t=(s=gets.to_f)/gets.to_i,gets.to_i>(50+s)/t ?'Yum': 'Meep Meep'][1]

Snap, Crackle and Pop

puts Array.new(6){gets.to_i}.map{|i| i % 3 > 0 ? i % 2 > 0 ? "Snap" : "Crackle" : "Pop"}.join(' ')

Deja Vu

puts gets =~ /(.).*\1/ ? 'Deja Vu' : 'Unique'

The Spy Life

puts gets.gsub(/[^A-Za-z ]/, '').reverse

Hex Color Code Generator

printf("#%02x%02x%02x", gets.to_i, gets.to_i, gets.to_i)

Symbols

puts gets.gsub(/[^A-Za-z 0-9]/, '')

Carrot Cake

puts [7 - gets.to_i % gets.to_i].map{|x| x > 0 ? 'I need to buy %d more' % x : 'Cake Time'}

Super Sale

puts (1.07 * 0.30 * gets.split(',').map(&:to_f).sort.reverse.drop(1).push(0).inject(:+)).to_i

Duty Free

puts gets.split.map(&:to_f).any?{|x| 20 < 1.1 * x} ? 'Back to the store': 'On to the terminal'

Building Blocks

puts Array.new(4){gets.to_i % 15}.inject(:+)

Name Buddy

puts gets.chomp + ' ' + gets =~ /\b(\w).*\b\1\w*$/ ? 'Compare notes' : 'No such luck'

That's odd...

puts Array.new(gets.to_i){gets.to_i}.select(&:even?).push(0).inject(:+)

No Numerals

puts gets.split.map{|w| w=~/1?\d/ ? ['zero','one','two','three','four','five','six','seven','eight','nine','ten'][w.to_i]: w}.join(' ')

Secret Message

puts gets.downcase.chars.map{|c| c=~/^[a-z]$/?('az'.sum-c.ord).chr: c}.join

Divisible

puts [n=gets.to_i,gets.split.map(&:to_i).any?{|x| n%x>0} ? 'not divisible by all': 'divisible by all'][1]

Even Numbers

puts gets.split.map(&:to_i).select(&:even?).join(' ')

Tax Free

puts gets.split(',').map(&:to_f).map{|x| x < 20 ? 1.07 * x : x}.inject(:+)

How Far?

puts gets.gsub(/^B*[HP](B*)[HP]B*$/,'\1').size

Average Word Length

puts ((essay=gets).gsub(/[^a-zA-Z]+/,'').size.to_f / essay.split.size).ceil

YouTube Link Finder

puts gets.gsub(/.*[=\/](?=[^=\/]*$)/, '')

Camel to Snake

puts gets.gsub(/(?<=[^\/b])([A-Z])/, '_\1').downcase

Text Decompressor

puts gets.split(/(?<=\d)/).map{|x| x[0] * x[1].to_i}.join()

Snowballing Numbers

puts 0 < Array.new(gets.to_i){gets.to_i}.reduce{|a,b| a > 0 && b > a ? a + b : 0}

Flowing Words

puts nil!=(gets =~/^(\S*(\S) \2\S*)*$/)

Splitting Strings

puts gets.chomp.split(Regexp.new '(?<=\G.{%d})' % gets.to_i).join('-')

Missing Numbers

print ((a=Array.new(gets.to_i){gets.to_i})[0].upto(a[-1]).to_a - a).join(' ')

Initials

puts Array.new(gets.to_i){gets.chomp.gsub(/(\w)\w* (\w)\w*/,'\1\2')}.join(' ')

Credit Card Validator

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'

CMYK to RGB

puts '%d,%d,%d' % (a=Array.new(4){1-gets.to_f}).first(3).map{|x| (255*x*a.last).round}

Hard

New Driver's License

puts [hero=gets, agents=gets.to_i, 20 * (1 + gets.split.select{|name| name < hero}.size / agents)][2]

It's a Sign

puts Array.new(4){gets.chomp}.any?{|s| s == s.reverse} ? 'Open' : 'Trash'

Password Validation

puts [gets].map{|x| x.length > 6 && x =~ /\d.*\d/ && x =~ /([!@#$%&*].*){2,}/ ? "Strong": "Weak"}

Security

puts gets =~ /[$T].*G.*[$T]/ ? 'quiet': 'ALARM'

2D Map

puts (b=(m=gets).index('P',1+a=m.index('P')))/6-a/6+(a%6-b%6).abs

Hofstadter's Q-Sequence

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)

Digits of Pi

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]