Ruby on rails 如何在Ruby中不允许在变量中输入数字
给定长度为N、索引范围为0到N-1的字符串S,将其偶数索引字符和奇数索引字符作为空格分隔的字符串打印在一行上(有关详细信息,请参阅下面的示例)。 样本输入:Ruby on rails 如何在Ruby中不允许在变量中输入数字,ruby-on-rails,ruby,rspec,Ruby On Rails,Ruby,Rspec,给定长度为N、索引范围为0到N-1的字符串S,将其偶数索引字符和奇数索引字符作为空格分隔的字符串打印在一行上(有关详细信息,请参阅下面的示例)。 样本输入: 2 Hacker Rank 样本输出: Hce akr Rn ak 说明: S=“黑客”S[0]=“H”,S[1]=“a”,S[2]=“c”,S[3]=“k”,S[4]=“e”,S[5]=“r” 然而,使用下面的代码,我无法完成挑战。如何将输入作为整数 S = gets.chomp.chars.to_a for i in 0..S.
2
Hacker
Rank
样本输出:
Hce akr
Rn ak
说明:
S=“黑客”S[0]=“H”,S[1]=“a”,S[2]=“c”,S[3]=“k”,S[4]=“e”,S[5]=“r”
然而,使用下面的代码,我无法完成挑战。如何将输入作为整数
S = gets.chomp.chars.to_a
for i in 0..S.length
if i%2 == 0
s1 = S[i]
else
s2 = S[i]
end
end
puts s1.to_s + " " + s2.to_s
第一个输入应被视为整数(即以下字符串的数量): 现在,我们要获取
金额字符串并执行我们的工作(使用):
第一个输入应被视为整数(即以下字符串的数量):
现在,我们要获取金额字符串并执行我们的工作(使用):
好吧,你的问题是,如果我使用正确的术语,一个使用错误。您的代码将s1和s2设置为最后检查的字母,而不是串联。修改代码时,我想您需要的是这样的:
S = gets.chomp.chars.to_a
s1 = ""
s2 = ""
for i in 0...S.length
if i%2 == 0
s1.concat(S[i])
else
s2.concat(S[i])
end
end
puts s1.to_s + " " + s2.to_s
好吧,你的问题是,如果我使用正确的术语,一个使用错误。您的代码将s1和s2设置为最后检查的字母,而不是串联。修改代码时,我想您需要的是这样的:
S = gets.chomp.chars.to_a
s1 = ""
s2 = ""
for i in 0...S.length
if i%2 == 0
s1.concat(S[i])
else
s2.concat(S[i])
end
end
puts s1.to_s + " " + s2.to_s
这是需要O(n)的基本算法
基准:
require 'benchmark'
def using_ugly_algo(tests, string)
# tests = gets.to_i
tests.times do
string = string
s_length = string.length # String length N
new_string = " " * s_length # creat of string of length N
even_index = 0
odd_index = s_length - (s_length/2) + 1
0.upto(s_length-1) do |i|
if i%2 == 0
new_string[even_index] = string[i]
even_index += 1
elsif
new_string[odd_index] = string[i]
odd_index += 1
end
end
# new_string
end
end
def with_partition(amount, string)
amount.times do
input = string
(input.split('').partition.with_index do |_, i|
i.even?
end.map(&:join).join(' '))
end
end
n = 10_000
string = (0...500).map { ('a'..'z').to_a[rand(26)] }.join
Benchmark.bmbm(100) do |x|
x.report("using_ugly_algo "){ n.times { using_ugly_algo(5, string) } }
x.report("with_partition "){ n.times { with_partition(5, string) } }
end
报告:
Rehearsal ----------------------------------------------------------------------------------------------------------------------------------------
using_ugly_algo 13.790000 0.030000 13.820000 ( 13.843560)
with_partition 16.790000 0.030000 16.820000 ( 16.830992)
------------------------------------------------------------------------------------------------------------------------------ total: 30.640000sec
user system total real
using_ugly_algo 13.930000 0.060000 13.990000 ( 14.070378)
with_partition 18.640000 0.210000 18.850000 ( 19.392816)
这是需要O(n)的基本算法
基准:
require 'benchmark'
def using_ugly_algo(tests, string)
# tests = gets.to_i
tests.times do
string = string
s_length = string.length # String length N
new_string = " " * s_length # creat of string of length N
even_index = 0
odd_index = s_length - (s_length/2) + 1
0.upto(s_length-1) do |i|
if i%2 == 0
new_string[even_index] = string[i]
even_index += 1
elsif
new_string[odd_index] = string[i]
odd_index += 1
end
end
# new_string
end
end
def with_partition(amount, string)
amount.times do
input = string
(input.split('').partition.with_index do |_, i|
i.even?
end.map(&:join).join(' '))
end
end
n = 10_000
string = (0...500).map { ('a'..'z').to_a[rand(26)] }.join
Benchmark.bmbm(100) do |x|
x.report("using_ugly_algo "){ n.times { using_ugly_algo(5, string) } }
x.report("with_partition "){ n.times { with_partition(5, string) } }
end
报告:
Rehearsal ----------------------------------------------------------------------------------------------------------------------------------------
using_ugly_algo 13.790000 0.030000 13.820000 ( 13.843560)
with_partition 16.790000 0.030000 16.820000 ( 16.830992)
------------------------------------------------------------------------------------------------------------------------------ total: 30.640000sec
user system total real
using_ugly_algo 13.930000 0.060000 13.990000 ( 14.070378)
with_partition 18.640000 0.210000 18.850000 ( 19.392816)
代码
def doit(str)
str.each_char.each_slice(2).with_object(['','']) do |(c_even, c_odd), (s_even, s_odd)|
s_even << c_even
s_odd << c_odd unless c_odd.nil?
end.join(' ')
end
解释
为了
如果检查在构造enum1
和enum2
时获得的返回值,您将看到它们可以被视为“复合”enugenerator
生成enum2
的第一个元素并将其传递给块,将值分配给四个块变量1:
现在执行块计算
s_even << c_even
#=> "a"
s_odd << c_odd unless c_odd.nil?
# s_odd << c_odd unless false
# s_odd << c_odd
#=> "b"
以这种方式继续计算,直到生成enum2
的最后一个值:[“a”]
。这会将nil
赋值给c_odd
,因此不会执行块的第二行2。最后,两个字符串的数组被一个分隔空间连接起来
另一种方式
def doit(str)
str.each_char.with_index.with_object(' ') { |(c,i),s|
s.insert(i.even? ? s.index(' ') : s.size, c) }
end
doit "abracadabra"
#=> "arcdba baaar"
1以下表达式采用并行赋值(有时称为多重赋值)和消歧(有时称为分解)为变量赋值
2第二行也可以写成s_oddcode
def doit(str)
str.each_char.each_slice(2).with_object(['','']) do |(c_even, c_odd), (s_even, s_odd)|
s_even << c_even
s_odd << c_odd unless c_odd.nil?
end.join(' ')
end
解释
为了
如果检查在构造enum1
和enum2
时获得的返回值,您将看到它们可以被视为“复合”enugenerator
生成enum2
的第一个元素并将其传递给块,将值分配给四个块变量1:
现在执行块计算
s_even << c_even
#=> "a"
s_odd << c_odd unless c_odd.nil?
# s_odd << c_odd unless false
# s_odd << c_odd
#=> "b"
以这种方式继续计算,直到生成enum2
的最后一个值:[“a”]
。这会将nil
赋值给c_odd
,因此不会执行块的第二行2。最后,两个字符串的数组被一个分隔空间连接起来
另一种方式
def doit(str)
str.each_char.with_index.with_object(' ') { |(c,i),s|
s.insert(i.even? ? s.index(' ') : s.size, c) }
end
doit "abracadabra"
#=> "arcdba baaar"
1以下表达式采用并行赋值(有时称为多重赋值)和消歧(有时称为分解)为变量赋值
2第二行也可以写成s_odd注意,不必检查每个字符的索引,还可以使用:
或者,使用临时变量:
s1 = ''
s2 = ''
'Hacker'.scan(/(.)(.?)/) { |a, b| s1 << a ; s2 << b }
puts "#{s1} #{s2}"
s1=''
s2=“”
“黑客”。扫描(/(.?)/){| a,b | s1注意,除了检查每个字符的索引,您还可以使用:
或者,使用临时变量:
s1 = ''
s2 = ''
'Hacker'.scan(/(.)(.?)/) { |a, b| s1 << a ; s2 << b }
puts "#{s1} #{s2}"
s1=''
s2=“”
“黑客”。扫描(/(?)/){a,b | s10..S.length
是错误的,应该有三点非贪婪范围:0…S.length
。谢谢,我不记得是哪一个,所以我只是假设他答对了那一部分。虽然我现在意识到我只回答了他的部分问题,但忘了排除数字条目。0..S.length
是错误的,应该有错误勒点非贪婪范围:0…S.length
。谢谢,我记不起是哪一个,所以我只是假设他答对了那一部分。虽然我现在意识到我只回答了他问题的一部分,但忘了排除数字项。此外,它绝对不是惯用的ruby,我相信它在字母数量奇数的字符串上会失败。还有,当你你说O(n)
值得一提的是在这种特殊情况下n
是什么。@mudasobwa它不会失败。我在发布它之前已经测试过。而且,如果你是软件爱好者,那么每个人都知道n
在O(n)中是什么此外,因为我们已经习惯了ruby的语法糖,所以我们不要忘记基本的东西。如果使用map
然后join
然后使用另一个操作,比如分区,那么我就是在浪费周期。想想对一个大字符串进行这样的操作,比如说10^10,你的解决方案将需要很多时间来计算答案“比如说10^10,”-在控制台中键入?代码应该可以解决问题,而不是试图成为一把瑞士刀。这里使用复杂的算法是过早优化的完美例子,会导致不可读的代码,通常是带有气味的糟糕代码。请参阅更新的答案以理解我试图说的内容。您试图这样说,以获得18%的produ以μs(如果不是ns的话)为单位的活跃度值得添加一个累赘的逻辑来纠缠未来的读者。这基本上是错误的。此外,它绝对不是惯用的ruby,我相信它在有奇数个字母的字符串上会失败。另外,当你说O(n)
时,值得一提的是n
I
'Hacker'.scan(/(.)(.?)/) #=> [["H", "a"], ["c", "k"], ["e", "r"]]
.transpose #=> [["H", "c", "e"], ["a", "k", "r"]]
.map(&:join) #=> ["Hce", "akr"]
.join(' ') #=> "Hce akr"
s1 = ''
s2 = ''
'Hacker'.scan(/(.)(.?)/) { |a, b| s1 << a ; s2 << b }
puts "#{s1} #{s2}"