Ad
  • Custom User Avatar

    Thanks for submitting a Swift translation for "do i get a bonus". I'm helping review them but I don't have privileges to approve/reject.

    Sample test cases:
    Get rid of the comment at the top (Example/TODO)

    You need multiple sample test cases. All you currently are doing is assering that 1 == 1

    Test cases:

    Rename testExample() to testFixed()
    Add a few more fixed tests
    Add random tests where you randomly generate the salary and whether bonus is true

    your function name bonus_time() should be renamed to bonusTime() to conform with Swift function naming practices.

    In tests consider giving meaningful feedback by adding a third parameter. Here's one of mine from my "six toast" Swift translation:
    XCTAssertEqual(sixToast(4), 2, "Incorrect answer for num = 4")
    (where num is the function parameter name)

    For reference, here's my "Swift translation checklist" that (usually) gets me past a moderator's reviews:

    1. Rename function to lower camel case
    2. Include messaging in test asserts like this: XCTAssertEqual(sixToast(4), 2, "Incorrect answer for num = 4") (where num is the function parameter name)
    3. Include tests for low, high, and edge cases
    4. Change testExample to testSample
    5. Include random tests
    6. Shuffle random tests if possible
    7. Change test names. Should not be “example”. Full tests should not be named “sample”
    8. Remove TODO comments from tests
    9. Make initial solution compilable without errors (adding “return 0” for example
    10. If floating point, consider conversion to integers or use the XCTAssertEqual(converter(mpg: 36),12.74,accuracy: 0.01, "Incorrect answer for mpg: 36)")
  • Custom User Avatar

    Count the number of divisors of a positive integer n.

    A positive integer is an integer greater than 0.

    But if you want to be sure, then yes you could validate the input too. Not needed here tho.

  • Default User Avatar

    If anyone accidentally calls divisors(0) then this will crash. There needs to be a check like:

    guard n != 0 else { return 0 }

    However I'm not sure what you should return. Returning 0 would technically be right. 0 has 0 divisors since 0 can't be divided.

  • Default User Avatar

    This is not a best practice. If you are testing the number 1000, you are applying n % $0 == 0 499 times more than needed.

  • Default User Avatar

    That won't happen, since 1 <= month <= 12 (see description)

  • Default User Avatar

    Looks cool and short, but if month will be equal 15, you'll get 5...

  • Default User Avatar

    Would you mind to elaborate? It's almost the same solution in C.

  • Default User Avatar

    everything brilliant is simple)

  • Default User Avatar

    Why would .to_i be necessary? The array consists of integers only.

  • Default User Avatar

    Sorry but even though this seems to pass the tests, the attempt looks wrong to me.

    Let's say the the hero has 5 bullets and 2 dragons to fight. This solutions returns 'false' then, even though the hero has enough bullets (he only needs four).

    Maybe I'm overlooking something here, but it misses the point of the instruction and doesn't do what is asked for.

  • Default User Avatar

    Nice! I think you could even omit "return".

  • Custom User Avatar

    Yeah absolutely sexy, this also prevents nasty user input because it enforces the use of a certain datatype. Thanks for the upload!

  • Default User Avatar

    This is just beautiful to look at. My personal favourite!