我需要从Ruby数组中删除一个反向词,但在获取正确的输出时遇到问题
我尝试了一种不同的循环方式,我使用了exec。
# Were going to get the first line of input which is the length of the wordlist and coerce to type fixnum
pword_list_length = gets.chomp.to_i
# Initialize an empty array to hold pword_list_length
pword_list = Array.new
# loop until we have all the lines of input pushed into the array
pword_list_length.times do
pword_list.push(gets.chomp)
end
# Next were going to check to see what word we have both forward and backwards
pword_list.each do |word|
bword = word.reverse!
if pword_list.include? bword
print word.length
print bword[bword.length/2]
end
end```
```Expected Output:
3 y
Output
3y3c3e3y```
答案 0 :(得分:0)
您要在此处更改列表
bword = word.reverse!
reverse!
实际上将反转变量word
的内容。在该列表之后,您一定会像刚将bword
颠倒并将其分配给word
一样包含bword
。
使用reverse
(不带!)保持word
不变。