I am taking inputs in Ruby like this:
我正在使用这样的Ruby输入:
lines = STDIN.readlines.map{|x| x.strip.to_i}.sort
This takes input from the command prompt, but I want to take input a specific number of times. Suppose my number of test cases is 3, how can I stop it after 3 lines of input?
这从命令提示符获取输入,但我想输入特定次数。假设我的测试用例数为3,如何在3行输入后停止测试?
4 个解决方案
#1
lines = []
3.times do
lines << STDIN.readline.strip.to_i
end
lines.sort
EDIT:
If you are saying that you want it to accept an arbitrary number of inputs, you have a couple options: The simplest is to first input how many lines of input you have, like this:
如果你说它希望它接受任意数量的输入,你有两个选择:最简单的是首先输入你有多少行输入,如下所示:
num_lines = STDIN.readline.strip.to_i
lines = []
num_lines.times do
lines << STDIN.readline.strip.to_i
end
lines.sort
Or, if you don't know how many lines to expect, you have to have some way of signifying that the data is complete. For example, if you want to have an empty line mean the end of the data:
或者,如果您不知道要预期多少行,则必须有一些方法来表示数据已完成。例如,如果您想要一个空行表示数据的结尾:
lines = []
STDIN.each_line do |line|
line.strip!
break if line == ''
lines << line.to_i
end
lines.sort
By the way, when the program is paused while awaiting input, this is not called an "infinite loop". It is "blocking" or simply "waiting for input".
顺便说一句,当程序在等待输入时暂停时,这不称为“无限循环”。它是“阻止”或只是“等待输入”。
#2
One line of code...
一行代码......
This will take only the number of lines you want to use - the desired count is in variable @requested_count:
这将只占用您想要使用的行数 - 所需的计数在变量@requested_count中:
lines = STDIN.readlines.values_at(0..@requested_count - 1).map{|x| x.strip.to_i}.sort
#3
Why not use gets
?
为什么不使用获取?
lines = []
1.upto(3) do
lines << gets.to_i
end
puts lines.sort
#4
A variation on the previous answers:
以前答案的变体:
lines = []
1.upto(3) do
lines << STDIN.readline.strip.to_i
end
puts lines.sort
#1
lines = []
3.times do
lines << STDIN.readline.strip.to_i
end
lines.sort
EDIT:
If you are saying that you want it to accept an arbitrary number of inputs, you have a couple options: The simplest is to first input how many lines of input you have, like this:
如果你说它希望它接受任意数量的输入,你有两个选择:最简单的是首先输入你有多少行输入,如下所示:
num_lines = STDIN.readline.strip.to_i
lines = []
num_lines.times do
lines << STDIN.readline.strip.to_i
end
lines.sort
Or, if you don't know how many lines to expect, you have to have some way of signifying that the data is complete. For example, if you want to have an empty line mean the end of the data:
或者,如果您不知道要预期多少行,则必须有一些方法来表示数据已完成。例如,如果您想要一个空行表示数据的结尾:
lines = []
STDIN.each_line do |line|
line.strip!
break if line == ''
lines << line.to_i
end
lines.sort
By the way, when the program is paused while awaiting input, this is not called an "infinite loop". It is "blocking" or simply "waiting for input".
顺便说一句,当程序在等待输入时暂停时,这不称为“无限循环”。它是“阻止”或只是“等待输入”。
#2
One line of code...
一行代码......
This will take only the number of lines you want to use - the desired count is in variable @requested_count:
这将只占用您想要使用的行数 - 所需的计数在变量@requested_count中:
lines = STDIN.readlines.values_at(0..@requested_count - 1).map{|x| x.strip.to_i}.sort
#3
Why not use gets
?
为什么不使用获取?
lines = []
1.upto(3) do
lines << gets.to_i
end
puts lines.sort
#4
A variation on the previous answers:
以前答案的变体:
lines = []
1.upto(3) do
lines << STDIN.readline.strip.to_i
end
puts lines.sort