Skip to content

Instantly share code, notes, and snippets.

@The-G
Created November 9, 2017 04:49
Show Gist options
  • Save The-G/bb369cbd9c83aaccf4d403aeefb69f68 to your computer and use it in GitHub Desktop.
Save The-G/bb369cbd9c83aaccf4d403aeefb69f68 to your computer and use it in GitHub Desktop.
ruby intro
# Part 1
def sum arr
return arr.sum() # 합
end
def max_2_sum arr
# arr.max(2).sum()
return arr.sort.reverse[0,2].sum() # 내림차순 sort하고 2개 합
end
def sum_to_n? arr, n
# result = false
# arr.combination(2).to_a.each do |arr_each|
# if arr_each.sum() == n
# result = true
# end
# end
# return result
arr.combination(2).to_a.map {|x| x.sum()}.include? n
# arr.combination(2).any? {|a,b| a+b==n}
end
# Part 2
def hello(name)
p "Hello, #{name}"
end
def starts_with_consonant? s
# if /^[^aeiou\W]/i.match(s) == nil then false else true end
/^[^aeiou\W]/i.match(s)==nil ? false : true
# !%w(a e i o u).include? s[0].downcase unless s.empty? or /^\W/.match(s)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment