如何使用Ruby计算数字数组的中位数?
我是初学者,在学习的过程中,我正努力坚持已经教过的东西.因此,我发现的其他问题超出了我的范围.
这是我的笔记和我的尝试:
取这两个中间数的平均值.
def median(array)
ascend = array.sort
if ascend % 2 != 0
(ascend.length + 1) / 2.0
else
((ascend.length/2.0) + ((ascend.length + 2)/2.0) / 2.0)
end
end
Run Code Online (Sandbox Code Playgroud)我有一个包含数组数组的字符串:
"[[1, 2], [3, 4], [5, 6]]"
Run Code Online (Sandbox Code Playgroud)
我可以将它转换为数组数组,而不使用eval
或使用正则表达式gsub
等吗?
我可以把它变成:
[[1, 2], [3, 4], [5, 6]]
Run Code Online (Sandbox Code Playgroud) 我只花了三天时间撞到墙上试图弄清楚为什么一个简单的"耙子"不能通过我的spec文件.
如果您遇到这种情况:在任何文件夹路径中都没有空格!认真.实际上,从这里开出的任何东西都没有空格.
这是我的控制台输出:
(中/Users/*****/Desktop/Learning Ruby/learn_ruby
)
$ rake
/Users/*******/Desktop/Learning Ruby/learn_ruby/00_hello/hello_spec.rb:116:
in `require': cannot load such file -- hello (LoadError)
Run Code Online (Sandbox Code Playgroud)