Ad
  • Custom User Avatar

    Passes the tests but doesn't solve the problem posed.

  • Default User Avatar

    Nice idea, but it doesn't quite work.

    Yes, anagrams must have equal sums, but equal sums are not necessarily anagrams.

    This passed the example tests because they were inadequate to reveal the error. The following test would fail using your code:

    Test.assert_equals(anagrams('add', ['ace', 'dad', 'ebb', 'fab', 'gaa']), ['dad'])

  • Default User Avatar

    It looks like the tests are broken.

  • Default User Avatar

    Very cool idea! Thanks.

    However, I have a couple of comments:

    1. reduce(:+) can be replaced by sum.
    2. I believe the resulting probability distribution is not precisely uniform - 1, 2 and 3 are produced in proportions 341 : 341 : 340. This can be shown by noticing that a sum of Bernoulli random variables follows a Binomial distribution. With your array size of 10 you therefore produce sums
      10, 11, 12, ... 20
      with (Binomial) proportions
      1 : 10, : 45 : 120 : 210 : 252 : 210 : 120 : 45 : 10 : 1
      therefore (%3 + 1) values 1, 2, 3 occur with proportions
      1+120+210+10 : 10+210+120+1 : 45+252+45
      i.e.
      341 : 341 : 342

    The deviation from uniformity is small, and gets smaller with larger array size.