Last active
August 29, 2015 14:01
-
-
Save yoaquim/041182806c9d265e277b to your computer and use it in GitHub Desktop.
Programming Exercise
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
=begin | |
Exercise: | |
Write a function that takes a string and returns a rot13 version | |
of that string with every other word reversed. If the function is | |
f, f(f(x)) should return x. Remember to take non-alphanumeric | |
non-alphanumeric characters into account. | |
Example Input: “Hello world!” | |
Example Output:”byyrU jbeyq!” | |
=end | |
#Assuming non-alphabetical characters can also | |
#be reversed - e.g., '$%@!' in an odd position | |
#produces '!@%$' - then: | |
def rot(rotateMe) | |
rotated="" | |
reversed= Array.new | |
rotateMe.bytes.to_a.each do |c| | |
if (c>=65 && c<=77) || (c>=97 && c<=109) then | |
c+=13 | |
elsif(c>77 && c<=90)|| (c>109 && c<=122) then | |
c-=13 | |
end | |
rotated+=c.chr | |
end | |
reversed = rotated.split; | |
reversed.each_with_index do |word, i| | |
if i%2==0 then | |
reversed[i]=word.reverse | |
else | |
reversed[i]=word | |
end | |
end | |
return reversed.join(' '); | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment