Skip to content

Instantly share code, notes, and snippets.

@yancya
Last active September 12, 2022 06:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yancya/0d983c707a25610bd5f32103446b641f to your computer and use it in GitHub Desktop.
Save yancya/0d983c707a25610bd5f32103446b641f to your computer and use it in GitHub Desktop.
# You can call `func1`
p func1(0) #=> 1
p func1(1) #=> 2
p func1(2) #=> 3
# Can you tell how `func1` is defined?
# Hint: def func1(n) = n + ???
# Define `answer1` that works like `func1`
def answer1(n)
n.succ
end
# Congrats! You've solved the first puzzle!
# Next, challenge func2!
p func2("Hello") # => ??? (press "Run Ruby" to see output)
p func2("world") # => ???
def answer2(str)
str.upcase
end
# Next, challenge func3!
p func3(1)
p func3(2)
p func3(3)
def answer3(n)
n.succ.times.to_a
end
# Still want to play?
# You may want to try func4
# This call raises an error!
# Try to find a correct way to call it.
func4{9}
def answer4(&f)
f[] + 42
end
p func5(0)
p func5(1)
p func5(2)
p func5(0,2)
def answer5(a, b=1)
a + b
end
# Hint: Check the parameters
def answer6(n)
case
when n < 10 then n
else
n2 = eval(n.to_s.chars.join('+'))
n2 < 10 ? eval(n2.to_s.chars.join('+')) : answer6(n2)
end
end
0.upto(1000) do |n|
func, ans = func6(n), answer6(n)
puts "#{func == ans ? 'OK' : '----NG'} n: #{n}, func: #{func}, ans: #{ans}"
end
p func7(0)
p func7(1)
p func7(2)
# Hint: Try to pass non-Integer!
def answer7(n)
n.succ
end
# Hint: %b
def answer8(n)
sprintf("%b", n).chars.select{|c| c == '1'}.size
end
1.upto(1000) do |n|
f = func8(n)
a = answer8(n)
p [n, f, a]
end
# Can you tell what string is replaced?
# Hint: Pass a spy (or mock) object to func9
class Hoge
def gsub(a, b)
p [a, b]
end
end
def answer9(s)
s.gsub("u-g0t-me", "yikes")
end
((%w[foo bar baz])+[Hoge.new]).each do |c|
res = func9(c)
p [c, res]
end
def answer10(reset=false)
if reset
@counter = nil
end
@counter ||= -1
@counter += 1
end
p func10(true)
p answer10(true)
def answer11(o)
o.hash
end
def answer12(n)
n.
to_s.
chars.
sort.
chunk_while{|i,j| i==j}.
to_a.
map(&:size).
reduce(:*)
end
TABLE = Encoding.list.map{|e|e.to_s[0..1]}
def answer13(n)
TABLE[n]
end
def answer14(n)
case n.to_s
when '0' then 'Z' # Zero
when '10' then 'T' # Ten
when '1000' then 'T' # Thousand
when /^(2|3)/ then 'T' # Two, Twenty Three, Thirty
when /^(4|5)/ then 'F' # Four, Five, Forty, Fifty
when /^(6|7)/ then 'S' # Six, Seven, Sixty, Seventy
when /^8/ then 'E' # Eight, Eighty
when /^9/ then 'N' # Nine, Ninety
when '11' then 'E' # Eleven
when '12' then 'T' # Twelve
when '13' then 'T' # Thirteen
when '14' then 'F' # Fourteen
when '15' then 'F' # Fifteen
when '16' then 'S' # Sixteen
when '17' then 'S' # Seventeen
when '18' then 'E' # Eighteen
when '19' then 'N' # Nineteen
when /^1/ then 'O' # One
end
end
0.upto(1000) do |i|
f, a = func14(i), answer14(i)
next if f == a
p [i, f, a, f == a]
end
TABLE = Object.constants.sort.map{ |s| s[0..1] }
def answer15(n)
TABLE[n]
end
def answer16(&f)
return false if f.parameters.empty?
f.call do
true
end
end
def answer17(msg)
JS.eval("alert('#{msg}')")
end
def answer18(s)
a = s.scan(/\d+/)
b = s.scan(/[^\d]+/)
pair = s.match?(/^\d/) ? [a,b] : [b,a]
pair.reduce(:zip).flatten.compact.map { |s|
case s
when /\d+/
s.to_i
else
s.split(//)
end
}.flatten
end
def polandnize(ast)
case ast
in String => s
return s
in ['value', Integer]
return ast
else
# do nothing
end
case ast.children
in [Array, nil, node]
polandnize(node)
in [Integer => n]
['value', n]
in [node1, operator, node2]
[operator.to_s, node1, node2].map { |n| polandnize(n) }
in [next_ast, nil]
polandnize(next_ast)
end
end
def answer19(a)
ast = RubyVM::AbstractSyntaxTree.parse(a.map(&:to_s).join)
polandnize(ast)
end
def solve(input)
case input
in ["value", Integer => n]
["value", n]
in [String => op, ["value", Integer => a], ["value", Integer => b]]
solve(["value", eval("#{a} #{op} #{b}")])
in [String => op, Array => a, Array => b]
solve([op, solve(a), solve(b)])
in [String => op, ["value", Integer] => a, Array => b]
solve([op, a, solve(b)])
in [String => op, Array => a, ["value", Integer] => b]
solve([op, solve(a), b])
in [String => op, ["value", Integer] => a, ["value", Integer] => b]
solve([op, solve(a), solve(b)])
end
end
def answer20(input)
solve(input)[1]
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment