Skip to content

Instantly share code, notes, and snippets.

@ltfschoen
Last active August 22, 2017 23:01
Show Gist options
  • Select an option

  • Save ltfschoen/c2835ce50a5d47ab066db3c3ea0b18b9 to your computer and use it in GitHub Desktop.

Select an option

Save ltfschoen/c2835ce50a5d47ab066db3c3ea0b18b9 to your computer and use it in GitHub Desktop.
codewars solutions
def get_middle(s)
len = s.length
if len == 1
return s
elsif len.even?
return s[(len/2)-1] + s[(len/2)]
else
return s[len/2]
end
end
puts get_middle("blah")
puts get_middle("helpm")
def nb_year(p0, percent, aug, p)
pop_init = p0
pop_max = p
pop_curr = pop_init
pc_increase_yearly = 1 + percent.to_f/100
pop_extras_yearly = aug
years = 0
until pop_curr >= pop_max
pop_curr = ((pop_curr * pc_increase_yearly) + pop_extras_yearly).to_i
puts pop_curr
years += 1
end
years
end
puts nb_year(1500, 5, 100, 5000)
puts nb_year(1000, 2.0, 50, 1214) # 4
def XO(str)
str.downcase.count('o').eql? str.downcase.count('x')
end
def iq_test(numbers)
h = { evens: [], odds: [] }
numbers.split(" ").each_with_index do |val, index|
h[:evens].push({val: val, index: index}) if val.to_i.even?
h[:odds].push({val: val, index: index}) if val.to_i.odd?
end
return h[:evens].length < h[:odds].length ? h[:evens][0][:index]+1 : h[:odds][0][:index]+1
end
puts iq_test("2 4 7 8 10")
puts iq_test("1 2 2")
def disemvowel(str)
new_str = str.split("").map { |char| char if not ["a","e","i","o","u"].include? char.downcase }.join("")
end
disemvowel("This website is for losers LOL!")
def high_and_low(numbers)
numbers.split(" ").map { |s| s.to_i }.minmax.reverse.join(" ")
end
puts high_and_low("4 5 29 54 4 0 -214 542 -64 1 -3 6 -6")
def DNA_strand(dna)
new_dna = []
dna.split("").each do |char|
if char.downcase == "a"
new_dna.push("T")
elsif char.downcase == "t"
new_dna.push("A")
elsif char.downcase == "c"
new_dna.push("G")
elsif char.downcase == "g"
new_dna.push("C")
else
new_dna.push(char)
end
end
new_dna.join("")
end
puts DNA_strand("ATTGC")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment