1
我正在從一個文件獲取輸入並將每行轉換爲一個數組,然後將該數組轉換爲一個集合。但在轉換時,返回如下所示:數組設置返回一組十六進制值的轉換
<Set:0x6268f8>
但是,在IRB上運行相同的事件會返回正確的值。
require 'set'
n,p = gets.chomp.split.map { |e| e.to_i }
arr = gets.chomp.split().map{|x| x.to_i}
print arr
puts
old_set = arr.to_set
print old_set
if old_set.length != 1
print "NO"
exit
end
輸入文件:
3 6
0 0 0 0 0 0
1 1 1 1 1 1
2 2 2 2 2 2
在運行此我得到:
C:\Ruby\kumar>ruby so.rb < abc.txt
[0, 0, 0, 0, 0, 0]
#<Set:0x3aad30>
在IRB:
irb(main):010:0> arr = gets.chomp.split("")
aabbddefyy
=> ["a", "a", "b", "b", "d", "d", "e", "f", "y", "y"]
irb(main):011:0> se=arr.to_set
=> #<Set: {"a", "b", "d", "e", "f", "y"}>
irb(main):012:0> se
=> #<Set: {"a", "b", "d", "e", "f", "y"}>
感謝它工作正常,將盡快接受答案。 – 2013-03-09 18:44:37