fork(3) download
  1. # coding: utf-8
  2. require 'minitest/autorun'
  3.  
  4. F=
  5.  
  6. ->s{r=[1.0]
  7. s.lines.map{|l|
  8. n=[i=0.0]*(r.size+1)
  9. l.scan(/\S/).map{|e|a,b=e>?/?e>?]?[0.5]*2:[0,1]:[1,0]
  10. z=r[i]
  11. n[i]+=z*a
  12. n[i+=1]+=z*b}
  13. r=n}
  14. r}
  15.  
  16. describe '#F' do
  17. def test_0_levels
  18. assert_equal [1.0], F['']
  19. end
  20.  
  21. def test_1_level
  22. assert_equal [0.5, 0.5], F['^']
  23. assert_equal [1.0, 0.0], F['/']
  24. assert_equal [0.0, 1.0], F['\\']
  25. end
  26.  
  27. def test_2_levels
  28. assert_equal [0.5, 0.5, 0.0], F['
  29. /
  30. ^ ^
  31. '.strip]
  32. end
  33.  
  34. def test_3_levels
  35. assert_equal [0.125, 0.375, 0.375, 0.125], F['
  36. ^
  37. ^ ^
  38. ^ ^ ^
  39. '.strip]
  40.  
  41. assert_equal [0.0, 0.0, 0.0, 1.0], F['
  42. \
  43. / \
  44. / / \
  45. '.strip]
  46. end
  47.  
  48. def test_4_levels
  49. assert_equal [0.0, 0.1875, 0.5625, 0.125, 0.125], F['
  50. ^
  51. \ ^
  52. ^ ^ \
  53. \ ^ / ^
  54. '.strip]
  55. end
  56.  
  57. def test_7_levels
  58. assert_equal [0.0, 0.09375, 0.28125, 0.4375, 0.1875, 0.0, 0.0, 0.0], F['
  59. ^
  60. / ^
  61. ^ ^ /
  62. / \ / \
  63. ^ ^ / ^ \
  64. ^ \ ^ \ / ^
  65. \ ^ ^ ^ \ ^ /
  66. '.strip]
  67. end
  68.  
  69. end
Success #stdin #stdout 0.05s 8528KB
stdin
Standard input is empty
stdout
Run options: --seed 52762

# Running tests:

......

Finished tests in 0.001213s, 4945.6270 tests/s, 7418.4404 assertions/s.

6 tests, 9 assertions, 0 failures, 0 errors, 0 skips