Ad
  • Default User Avatar

    Not allowed to use #rotate or #reverse for my upcoming interview assessment, so I'm doing all method manually.

  • Custom User Avatar

    warning, your current random tests are completely wrong. ;)

  • Custom User Avatar

    I temporarily forgot we were talking ruby, here... (was thinking about python). So:

    raise Exception.new("some message here if you want") if int >= arr.size
                                                     # warning: ^^
    

    and you catch it that way in the fixed tests:

    Test.expect_error("assertion message") { hindex(input_array) }
    

    for the random tests it's a bit more cumbersome. You could use somehting like this:

    nTests.times {
        arr = ... # randomly generated array
        i = ...
        begin
            expected = reference_solution(arr,i)
        rescue
            expected = nil
        end
        if expected.nil?
            Test.expect_error(...) {...}
        else
            Test.assert_equals(...)
        end
    }
    

    In my coding program...

    mmh... I'd say two things, here:

    • searching for the middle element of a normal array makes no point/interest. What makes your kata interesting is the reversing of a part of it.
    • a hack'n slash approach here would be to actually reverse the array, one way or the other (meaning, even doing it by hand like you did ;) ). If you really want users to think about the problem, the best way would be to enforce the O(1) solution (yours is O(n)). That would need a lot of tests on very long arrays. The main problem being that generating the arrays will be longer than the time needed to hack/slash the solution. So that would need some fine tuning in the random tests (it's doable but cumbersome). That or to use timed assertions. I don't remember if there is something like that in the ruby test framework... :/
  • Default User Avatar

    Okay! So would some code that would catch the ValueError be (in Ruby):

    raise ValueError if int > arr.size

    In my coding program there's a huge emphasis on understanding the whole problem before attemping it, instead of hack/slash coding. My problem isn't super complex, it's closer to 7 kyu than 6, really. The more problems cause people to go back and re-read after they've completed a solution to implement some fix, the more that people will slow down and really think about each step of the problem before tearing into it.

    Okay, I'll republish the kata once it's all corrected and hope for the best. :)

  • Custom User Avatar

    but what value is being returned when they raise an Exception?

    nothing is returned, the code send an actual exception, just like ValueError or something equivalent. Meaning that you have to catch the exception in the tests and chack that it happens only when needed

    I meant... ...contrived.

    Still not following, sorry. x)

    ...retired... republish

    well, once all has been corrected here, you can try to republish in a new kata. But there is no guarantee about how some power user will react about that. If you try, be sure to get it absolutely right, first.

  • Default User Avatar

    Okay! That's clear on what I should ask from them, but what value is being returned when they raise an Exception?

    I meant that just that for people at or near my level of problem solving ability (6 to 5 kyu), I think we may rush into solving a problem if we can see a probable solution right off the bat. Adding another layer of complexity, like returning the middle value of the new array, may add just a little more challenge and cause us to slow down and think about the problem harder. In hindsight, the different return value of this one seems a little contrived.

    I'm making a random test case method now, but since this is already retired can it be re-published with all the corrections I'm making?

  • Custom User Avatar

    no, you keep the current behavior, but you ask for raise Exception(...) instead of return string when the index isn't usable.

    I moreso put the requirement to return the middle value from the array just as a reading comprehension check, to make sure that people read the problem well in creating pseudocode.

    I'm not following what you mean, here.

  • Default User Avatar

    You make some great points, especially about if the array is holding the string 'Integer is too large'.
    Thank you for the feedback! I'm editing now.

    Since I'm new to this, what would be considered a good practice exception? Just returning the middle integer as it is, no reversing, if the integer is larger than the size of array?

    I moreso put the requirement to return the middle value from the array just as a reading comprehension check, to make sure that people read the problem well in creating pseudocode.

  • Custom User Avatar

    ;-)

  • Custom User Avatar

    The real issue is that for some reason if the "reversing-index" is out of bounds, we choose to ignore the whole purpose of the function, and return an arbitrary value which in turn could be the central element of the array itself...

  • Custom User Avatar

    then it's garbage code XD

    More "seriously", you don't have the hand on the content of the array, so on that point, that'd be the responsibility of the user of the function, not the implementer of the function. But the implementer has two options:

    1. return what's coming from the array, whatever it is, or raise an exception if something goes wrong
    2. return what's coming from the array, whatever it is, or return a string if something goes wrong, which doesn't sound reasonnable.

    so the point still stands.

    edit: and what if the array is holding strings like "Integer is too large."? So the string option is really inappropriate.

  • Custom User Avatar

    it's considered bad practice to return different data type

    But what if the array consists of values of different data types?

  • Custom User Avatar
    • it's considered bad practice to return different data type => you should ask for an exception to be raised when the "integer is too large"
    • need a test with an empty array
    • instead of removing the constraint on the reverse method, you could push enough tests to make the "actually reversing" approach inefficient.
  • Default User Avatar

    Thank you for pointing this out. Currently looking up how to make random tests. If you have any guides to pass along, I'd love any help.

  • Default User Avatar

    Thank you for the feedback. I think I'll remove that part from the problem. No need to restrain people!

  • Loading more items...