Price fraction: Difference between revisions
Content added Content deleted
(Price fraction in True BASIC and Yabasic) |
(→{{header|Ruby}}: add bsearch example) |
||
Line 4,438: | Line 4,438: | ||
1 tests, 22 assertions, 0 failures, 0 errors, 0 skips</pre> |
1 tests, 22 assertions, 0 failures, 0 errors, 0 skips</pre> |
||
Another option is using a built-in binary search: |
|||
<syntaxhighlight lang="ruby"> |
|||
keys = [ 0.06, 0.11, 0.16, 0.21, 0.26, 0.31, 0.36, 0.41, 0.46, 0.51, 0.56, 0.61, 0.66, 0.71, 0.76, 0.81, 0.86, 0.91, 0.96, 1.01] |
|||
prices = [0.10, 0.18, 0.26, 0.32, 0.38, 0.44, 0.50, 0.54, 0.58, 0.62, 0.66, 0.70, 0.74, 0.78, 0.82, 0.86, 0.90, 0.94, 0.98, 1.00] |
|||
tests = [0.3793, 0.4425, 0.0746, 0.6918, 0.2993, 0.5486, 0.7849, 0.9383, 0.2292] |
|||
tests.each do |test| |
|||
price = prices[ keys.bsearch_index{|key| key >= test } ] |
|||
puts [test, price].join(": ") |
|||
end |
|||
</syntaxhighlight> |
|||
=={{header|Rust}}== |
=={{header|Rust}}== |