Skip to content

Instantly share code, notes, and snippets.

@toretore

toretore/hey.rb Secret

Forked from anonymous/hey.rb
Last active February 10, 2017 17:04
Show Gist options
  • Save toretore/d2788393d03969e50740ec5219093c22 to your computer and use it in GitHub Desktop.
Save toretore/d2788393d03969e50740ec5219093c22 to your computer and use it in GitHub Desktop.
def revrot(str, size)
if size > str.length or size == 0
""
else
# Create an array of arrays of integers
# Ex: str="1234567", size=3 results in [[1,2,3], [4,5,6], [7]]
digit_groups = str.chars.map{|c| c.to_i }.each_slice(size).to_a
# If `digit_groups` contains a last incomplete array
# (`[7]` in the example above), remove it
if str.length.to_f / size % 1 != 0
digit_groups.delete_at(-1)
end
# Loop through `digit_groups`, operating on each `group` in sequence
# In the example from above `group` will first be `[1,2,3]`, then
# it will be `[4,5,6]`. The block (everything between `do` and `end`) will
# run twice, because `digit_groups` has two elements in it.
digit_groups.each do |group|
# `result` determines which action to take
result = group.reduce(:+).even?
if result
#do something
else
#do something else
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment