Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays &引用;如何从一个Ruby数组中删除一个元素,而该元素只是另一个反转的元素;_Arrays_Ruby_Duplicates - Fatal编程技术网

Arrays &引用;如何从一个Ruby数组中删除一个元素,而该元素只是另一个反转的元素;

Arrays &引用;如何从一个Ruby数组中删除一个元素,而该元素只是另一个反转的元素;,arrays,ruby,duplicates,Arrays,Ruby,Duplicates,我需要从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_

我需要从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```

您正在此处更改列表:

bword = word.reverse!
反向
实际上会反转变量
word
的内容。在该列表之后,一定会包括
bword
,因为您刚刚将
word
反转并将其分配给
bword


使用
reverse
(不带!)保持
word
不变。

您能否同时告诉我们示例输入和预期输出。请确保提供一个示例。您的示例既不是最小的(前12行与从数组中删除元素有什么关系?)也不是可复制的(它依赖于您没有向我们展示的外部输入)。问题描述:Ethan有可能破解安全帐户的密码列表。为了选择正确的密码,他研究了账户所有者,发现账户所有者非常喜欢缩写词。在消除了其余的模式之后,Ethan现在确定密码和密码的反面都出现在列表中。您需要在该列表中找到密码,并打印其长度及其最中间的字符。注意:列表中的所有字符串都是奇数长度。上面是编码提示。我已经到了有名单的地步了。我只需要从列表中删除反转的字符串,这就是我所遇到的问题。第一个测试用例的预期输出是
3y
只需尝试替换
反向带有“反向”。或者提供测试用例数据。