Arrays 如何制作一个破译字符串的ruby方法?
我正在尝试一种破译加密字符串的方法,如下所示:Arrays 如何制作一个破译字符串的ruby方法?,arrays,ruby,string,encryption,methods,Arrays,Ruby,String,Encryption,Methods,我正在尝试一种破译加密字符串的方法,如下所示: "65 119esi 111dl 111lw 108dvei 105n 97n 111ka" == "A wise old owl lived in an oak" "84eh 109ero 104e 115wa 116eh 108sse 104e 115eokp" == "The more he saw the less he spoke" "84kanh
"65 119esi 111dl 111lw 108dvei 105n 97n 111ka" == "A wise old owl lived in an oak"
"84eh 109ero 104e 115wa 116eh 108sse 104e 115eokp" == "The more he saw the less he spoke"
"84kanh 121uo 80roti 102ro 97ll 121ruo 104ple" == "Thank you for all your help"
def decipher(string)
ints_and_chars = string.scan(/(\d+)(\S*)/)
ints_and_chars.map { |int, chars|
[int.to_i.chr, chars[-1], chars[1..-2], chars[0]].join
}.join(' ')
end
decipher("65 119esi 111dl 111lw 108dvei 105n 97n 111ka")
#=> "A wise old owl lived in an oak"
decipher("84eh 109ero 104e 115wa 116eh 108sse 104e 115eokp")
#=> "The more he saw the less he spoke"
decipher("84kanh 121uo 80roti 102ro 97ll 121ruo 104ple")
#=> "Thank you Piotr for all your help"
每一个字,
第二个和最后一个字母被切换(例如,Hello变为Holle),第一个字母被其字符代码替换(例如,H变为72)
说到这里,它给了我两个数组,一个是字节值转换成字母字符的数组,另一个是其余的单词字符的数组,但是我很难弄清楚如何切换第二个和最后一个字母的位置并将它们重新组合在一起
def decipher(string)
str = string.split(" ")
str.map do |word|
bytes = word.match(/\d+/).to_s.to_i.chr
words = word.gsub(/\d+/, "")
end
end
也许有更好的方法,我还是个新手,还在学习。谢谢 我会这样做:
"65 119esi 111dl 111lw 108dvei 105n 97n 111ka" == "A wise old owl lived in an oak"
"84eh 109ero 104e 115wa 116eh 108sse 104e 115eokp" == "The more he saw the less he spoke"
"84kanh 121uo 80roti 102ro 97ll 121ruo 104ple" == "Thank you for all your help"
def decipher(string)
ints_and_chars = string.scan(/(\d+)(\S*)/)
ints_and_chars.map { |int, chars|
[int.to_i.chr, chars[-1], chars[1..-2], chars[0]].join
}.join(' ')
end
decipher("65 119esi 111dl 111lw 108dvei 105n 97n 111ka")
#=> "A wise old owl lived in an oak"
decipher("84eh 109ero 104e 115wa 116eh 108sse 104e 115eokp")
#=> "The more he saw the less he spoke"
decipher("84kanh 121uo 80roti 102ro 97ll 121ruo 104ple")
#=> "Thank you Piotr for all your help"
您可以使用并行赋值这样交换字符
word[1],word[-1]=word[-1],word[1]
对于完整的程序,我的方式如下:
"65 119esi 111dl 111lw 108dvei 105n 97n 111ka" == "A wise old owl lived in an oak"
"84eh 109ero 104e 115wa 116eh 108sse 104e 115eokp" == "The more he saw the less he spoke"
"84kanh 121uo 80roti 102ro 97ll 121ruo 104ple" == "Thank you for all your help"
def decipher(string)
ints_and_chars = string.scan(/(\d+)(\S*)/)
ints_and_chars.map { |int, chars|
[int.to_i.chr, chars[-1], chars[1..-2], chars[0]].join
}.join(' ')
end
decipher("65 119esi 111dl 111lw 108dvei 105n 97n 111ka")
#=> "A wise old owl lived in an oak"
decipher("84eh 109ero 104e 115wa 116eh 108sse 104e 115eokp")
#=> "The more he saw the less he spoke"
decipher("84kanh 121uo 80roti 102ro 97ll 121ruo 104ple")
#=> "Thank you Piotr for all your help"
def密码(字符串)
string.split.map do| word|
单词[1],单词[-1]=单词[-1],如果word.size>1,则单词[1]
word[0]。ord.to_+word[1..-1]
结束。连接(“”)
结束
def解密(字符串)
string.split.map do| word|
数字部分=单词到字母i到字母s
word=编号\u零件至\u i.chr+word[编号\u零件尺寸..-1]
单词[-1],单词[1]=单词[1],如果word.size>1,则单词[-1]
单词
结束。连接(“”)
结束
text=cipher(‘一只聪明的老猫头鹰住在橡树里’)
输入文字#65 119esi 111dl 111lw 108dvei 105n 97n 111ka
一只聪明的老猫头鹰住在一棵橡树上
您可以使用正则表达式,避免将字符串转换为单词数组,修改字符串中的每个单词,然后加入结果数组
R_DECRYPT = /\d+|(?<=\d)\p{L}(?=\p{L})|(?<=\p{L})\p{L}(?!\p{L})/
我们可以以自由间距模式编写这两个正则表达式,使它们能够自我记录 以自由间距模式写入的R_解密
(?是一种消极的后视。“但我很难弄清楚如何切换第二个和最后一个字母的位置”。这是内在的问题。假设问题的其余部分不存在。只关注像“oellh”、“e”、“pu”这样的字符串写一个处理所有它们的方法。当你有这个问题时,更大的解决方案落入你的圈中。提示:如果字符串比两个字母长,那么内部部分是代码> [1…-2 ] < /代码>。现在考虑<代码> s的功率[ 1…-2 ]=s [ 1…-2 ]。
def encrypt(str)
second_ltr_idx = nil
last_idx = str.length-1
str.gsub(R_ENCRYPT) do |ch|
i = Regexp.last_match.begin(0)
case
when i.zero? || str[i-1] == ' '
second_ltr_idx = i + 1
ch.ord.to_s
when i == last_idx || str[i+1] == ' '
str[second_ltr_idx]
else
str[str.index(/\p{L}(?!\p{L})/, i+1)]
end
end
end
encrypt(ds1) == s1
#=> true
encrypt(ds2) == s2
#=> true
encrypt(ds3) == s3
#=> true
/
# Match digits at beginning of each word
\d+ # match 1+ digits
| # or
# Match first letter of each word
(?<=\d) # assert following match is preceded by a digit
\p{L} # match a Unicode letter
(?=\p{L}) # assert previous match is followed by a letter
| # or
# Match last letter of each word
(?<=\p{L}) # assert following match is preceded by a letter
\p{L} # match a letter
(?!\p{L}) # assert previous match is not followed by a letter
/x # invoke free-spacing regex definition mode
/
# Match letter at beginning of a word
(?<!\p{L}) # assert following match is not preceded by a Unicode letter
\p{L} # match a letter
| # or
# Match second letter of a word
(?: # begin a non-capture class
(?<=\A\p{L}) # Assert following match is preceded by a letter at the
# beginning of the string
| # or
(?<=[ ]\p{L}) # Assert following match is preceded by a space followed
# by a letter (space is in character class for protection
) # end non-capture group
\p{L} # match a letter
(?=\p{L}) # Assert previous match is followed by a letter
| # or
# Match last letter of a word
(?<=\p{L}) # Assert following match is preceded by a letter
\p{L} # match a letter
(?!\p{L}) # Assert previous match is not followed by a letter
/x # Invoke free-spacing regex definition mode