Skip to content

Instantly share code, notes, and snippets.

@jeanbaptistebeck
Last active June 5, 2017 19:22
Show Gist options
  • Save jeanbaptistebeck/41e4c3c1857ad7b2f8f03e280b944405 to your computer and use it in GitHub Desktop.
Save jeanbaptistebeck/41e4c3c1857ad7b2f8f03e280b944405 to your computer and use it in GitHub Desktop.
Codewars

Find the missing letter

def find_missing_letter(arr)
  arr.each_cons(2) do |letter, next_letter| 
    return letter.next if letter.next != next_letter
  end
end

def find_missing_letter(arr)
  ((arr.first..arr.last).to_a - arr).first
end

Test.assert_equals(find_missing_letter(["a","b","c","d","f"]), "e")

Reverse or rotate

def revrot(str, sz)
    return "" if sz <= 0 or sz.nil? or sz > str.length
    return_str = []
    chunks = str.scan(/.{#{sz}}/)
    chunks.each do |chunk|
      cubed_digits = chunk.split('').map(&:to_i).map { |digit| digit*digit*digit }
      chunk = cubed_digits.reduce(:+).even? ? chunk.reverse : chunk[1..-1] + chunk[0]
      return_str << chunk
    end
    return_str.join
end

def revrot(str, sz)
  return '' if sz <= 0
  chunks = str.scan(/.{#{sz}}/)
  chunks.map do |chunk|
    digits = chunk.chars
    sum = digits.map {|x| x.to_i ** 3 }.inject(:+)
    sum.even? ? digits.reverse : digits.rotate
  end.join
end

testing(revrot("733049910872815764", 5), "330479108928157")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment