price_fraction.sf 978 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. #!/usr/bin/ruby
  2. #
  3. ## https://rosettacode.org/wiki/Price_fraction
  4. #
  5. var table = <<'EOT'.lines.map { .words.grep{.looks_like_number}.map{.to_num} };
  6. >= 0.00 < 0.06 := 0.10
  7. >= 0.06 < 0.11 := 0.18
  8. >= 0.11 < 0.16 := 0.26
  9. >= 0.16 < 0.21 := 0.32
  10. >= 0.21 < 0.26 := 0.38
  11. >= 0.26 < 0.31 := 0.44
  12. >= 0.31 < 0.36 := 0.50
  13. >= 0.36 < 0.41 := 0.54
  14. >= 0.41 < 0.46 := 0.58
  15. >= 0.46 < 0.51 := 0.62
  16. >= 0.51 < 0.56 := 0.66
  17. >= 0.56 < 0.61 := 0.70
  18. >= 0.61 < 0.66 := 0.74
  19. >= 0.66 < 0.71 := 0.78
  20. >= 0.71 < 0.76 := 0.82
  21. >= 0.76 < 0.81 := 0.86
  22. >= 0.81 < 0.86 := 0.90
  23. >= 0.86 < 0.91 := 0.94
  24. >= 0.91 < 0.96 := 0.98
  25. >= 0.96 < 1.01 := 1.00
  26. EOT
  27. func price(money) {
  28. table.each { |row|
  29. (row[0] <= money) ->
  30. && (row[1] > money) ->
  31. && return row[2];
  32. }
  33. die "Out of range";
  34. }
  35. %n(0.3793 0.4425 0.0746 0.6918 0.2993 0.5486 0.7848 0.9383 0.2292).each { |n|
  36. say price(n);
  37. }