我试图用Project Euler解决Ruby中的数学问题.这是我尝试过的第一个:
如果我们列出10以下的所有自然数是3或5的倍数,我们得到3,5,6和9.这些倍数的总和是23.
求出1000以下3或5的所有倍数的总和.
请帮我改进我的代码.
total = 0
(0...1000).each do |i|
total += i if (i%3 == 0 || i%5 == 0)
end
puts total
Run Code Online (Sandbox Code Playgroud) 我想解析一个有3个条目的日志文件.它看起来像这样:
Start: foo
Parameters: foo
End: foo
Start: other foo
Parameters: other foo
End: other foo
....
Run Code Online (Sandbox Code Playgroud)
foo就是我想要的.如果结果如下所示会很好:
logs = [
{
:start=>"foo",
:parameters=>"foo",
:end=>"foo"
},
{
:start=>"other foo",
:parameters=>"other foo",
:end=>"other foo"
}
]
Run Code Online (Sandbox Code Playgroud)
我知道一些正则表达式,但是我很难理解我如何通过多行来解决这个问题.谢谢!