Last active
February 16, 2017 03:49
-
-
Save rodrigomaia/e94d73480d3609b20e83b55e66e09753 to your computer and use it in GitHub Desktop.
tibira
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
@keyboard = [] | |
@keyboard[0] = ['1','2','3','4','5','6','7','8','9','0'] | |
@keyboard[1] = ['q','w','e','r','t','y','u','i','o','p'] | |
@keyboard[2] = ['a','s','d','f','g','h','j','k','l',';'] | |
@keyboard[3] = ['z','x','c','v','b','n','m',',','.','/'] | |
def find_position(caractere) | |
@keyboard.each_with_index do |row, index| | |
col = row.index(caractere) | |
return [index, col] unless col.nil? | |
end | |
return nil | |
end | |
def h(char) | |
position = find_position(char) | |
return char if position.nil? | |
return @keyboard[position[0]].reverse[position[1]] | |
end | |
def v(char) | |
position = find_position(char) | |
return char if position.nil? | |
tmp_arr = [@keyboard[0][position[1]], | |
@keyboard[1][position[1]], | |
@keyboard[2][position[1]], | |
@keyboard[3][position[1]] | |
] | |
vert_position = tmp_arr.index(char) | |
return tmp_arr.reverse[vert_position] | |
end | |
def shf(char, index) | |
big_list = @keyboard.flatten | |
position = big_list.index(char) | |
return char if position.nil? | |
big_list.rotate(position + index).first | |
end | |
def transf(commands, text) | |
transformed_text = "" | |
text.split("").each do |char| | |
transformed_char = char | |
commands.split(",").each do |transformation| | |
transformed_char = h(transformed_char) if transformation == 'H' | |
transformed_char = v(transformed_char) if transformation == 'V' | |
transformed_char = shf(transformed_char, transformation.to_i) if transformation.to_i != 0 | |
end | |
transformed_text = transformed_text + transformed_char | |
end | |
transformed_text | |
end | |
transf("H,V,H,1", "rty") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
transf("H,V,H,5,V,-12", "some big text file")