5 kyu

Cargo-Bot Interpreter

57 of 59scarecrw
Description
Loading description...
Interpreters
Games
Algorithms
  • Please sign in or sign up to leave a comment.
  • tobeannouncd Avatar

    Haskell translation

    The description has been updated only to distinguish language-specific information.

  • DallogFheir Avatar

    Crate from final tests is not the same as Crate from sample tests.

    def cargobot(initial_state, programs, max_steps):
        crate = initial_state[0][0]
        
        match crate:
            case Crate():
                print(f"{crate} is a Crate.")
            case _:
                print(f"{crate} is NOT a Crate.")
    

    This prints Crate(color='YELLOW') is a Crate. while testing, but Crate(color='YELLOW') is NOT a Crate. while attempting.

    • scarecrw Avatar

      Unfortunately, Crate needs to be redefined in the testing code to be inaccessable to the solver, so you only have access to the definition from preloaded. namedtuples can still be compared with __eq__ from two definitions, but I'm not sure if there's a way to compare them with is or with match statements.

      It's an unfortunate effect from a poor choice made early on to use namedtuples, but I'm not sure what difference it makes when solving. There are no invalid inputs and you can still match on crate.color perfectly fine.

    • natan Avatar

      the test code could likewise read out the data and compare that instead of insisting on its own separate definition. could also get fancy and render the states like the drawings in the instuctions. or, only show the preloaded one to the user and then compare with a separately defined type for the expected value. codewars_test does do actual == expected though which is arguably wrong and it should instead do expected == actual

    • scarecrw Avatar

      Alright, swapped to pass preloaded.Crate objects and then compare with a test-suite defined version. The example provided now works as expected.

      Issue marked resolved by scarecrw 2 years ago
  • SuppliedOrange Avatar

    I think my code works fine. I logged every action and found it working as expected. I think the solutions may be wrong..?

    • scarecrw Avatar

      It doesn't look like you're handling conditional flags on program commands correctly.

      In the future, please only raise issues if you have a specific example that you can show isn't working according the description.

      Issue marked resolved by scarecrw 2 years ago
    • DallogFheir Avatar

      Thanks. Other than that, great kata.

  • zjadaczfantow Avatar

    I'm not sure if this kata is not forcing timeout too quick, i think my answer was not too bad

    • FArekkusu Avatar

      There're no performance requirements in this kata, and your solution passes the tests in ~100 ms. What are you talking about?

    • zjadaczfantow Avatar

      im pretty sure there is 12k ms and deep recursion test is over 10k moves

    • scarecrw Avatar

      All kata have a timeout (for python this is 12 seconds) but this kata does not include tests intended to restrict solutions based on execution time. Most submitted solutions can solve all the tests (including the deep recursion test) in well under a second. If your code is timing out, I would look for unintended infinite loops or similar. You can check out these troubleshooting tips for more info.

      Question marked resolved by scarecrw 3 years ago
    • FArekkusu Avatar

      10k is a very small number of operations - a reasonable solution can pass the "deep recursion" test in ~10 ms. If your solution times out, either you aren't using the max_steps limiter, or your code is genuinely 1200+ times slower than what other people have written which is unacceptably bad.

  • FArekkusu Avatar

    Crate and Command should not be regular classes at all.

    • Blind4Basics Avatar

      provide information or just close this issue. Thx

    • FArekkusu Avatar

      Provide info about what? How to choose a meaningful data representation? Which representation is fitting in this particular case? How to get rid of the delusion that "OOP is used to model real-life objects"?

    • Blind4Basics Avatar

      Which representation is fitting in this particular case?

      that would be enough

    • FArekkusu Avatar

      With the current setup Crate is a str (or namedtuple if the author really wants it to imitate a separate class) and Command is clearly a tuple/namedtuple.

    • scarecrw Avatar

      I'll be honest, I'm not very familiar with namedtuples, but from reading, it does seem like a better choice. I originally went with an OOP aproach before deciding exactly how the Kata would work, and looking back see why it may not be a good choice.

      I have some time today, so I'll give a shot to using them.

      Thanks for the feedback!

    • scarecrw Avatar

      Alright, I've replaced the Crates and Commands with namedtuple objects. Let me know if there are any errors in my attempt or any other issues.

      Thanks!

    • FArekkusu Avatar

      And why are you inheriting from the namedtuple-classes instead of using them directly? This is pointless, and you're overriding __repr__ incorrectly anyway.

    • scarecrw Avatar

      I was inheriting from them in order to override __repr__. Is there a better way to do this? Or would it be best to just leave the default __repr__ as is?

    • FArekkusu Avatar

      And why are you inheriting from the namedtuple-classes instead of using them directly?

      I was inheriting from them in order to override __repr__

      That was a rhetorical question... And you don't need to inherit from a class to change its __repr__ method.

      Or would it be best to just leave the default __repr__ as is?

      Do you know how __repr__ (and __str__) should be used at all? As I see, you don't, so you really should read the documentation.

    • scarecrw Avatar

      Alright, I've removed the unnecessary inheritance. Let me know if there's anything else I should change. Thanks!

    • FArekkusu Avatar

      Damn, I clicked on edit instead of translate to check the current setup... It looks fine now, and if all the issues are resolved, I can approve the kata since I'm (probably) a contributor anyway.

      Issue marked resolved by FArekkusu 3 years ago
    • scarecrw Avatar

      Awesome, thanks for the feedback.

  • Blind4Basics Avatar

    Hi,

    • there is an undocumented __eq__ method in the Crate class
    • that method is wrongly implemented (it isn't guarded againstother not being a Crate instance), meaning the user can end up with weird error messages about a missing color attribute out of the blue.
    • Don't rely on something exposed in the preloaded section to validate the results. That leads too different things/problems like some of the above and an user could mess with your class(es). About that, the very least would be to redefine locally (in the test suite) the two classes, so that you're sure about what the tests are using. The other way around would be to make everything immutable... (using namedtuples and defining constants, for example)

    Crates come in four different colors: RED, YELLOW, GREEN, and BLUE and are defined as follows:

    It's not totally clear by reading the description only if what is defined in preloaded is 4 "constants", or just the class.
    That wording suggests that we are dealing with 4 constants, while only the class is in preloaded. => Needs a rewording.


    In the actual Cargo-Bot game, the claw will "break" if it is moved out of bounds or a stack becomes too tall; this is not the case in this kata.

    that info could be provided when you describe the moves (makes more sense to have everything in the same place, imo. But that's a suggestion only)

    the info just below about the length of the programs could be suppressed, imo


    • Remove the extra assertion message in the random tests (the info is already provided in the message of the it block)

    Cheers

    • scarecrw Avatar

      Thanks for all the helpful feedback!

      I've implemented your latter three points. As for the first one, I fixed the __eq__ method to account for non-crate objects, but I had a couple questions as to the rest:

      • I'm not sure of the protocol for documenting preloaded in the description. Is it best to just include everything even if it's not necessarily relevant to solving? (e.g. __eq__ and __repr__) My concern was overcomplicating the description, but I can see how leaving it out could lead to confusion.
      • For reimplementing the Crate and Command classes in the test suite, am I correct that this would invalidate any solution that attempted to construct its own crates? I wouldn't expect this to be a common approach, but I do see one current solution (here line 30) that makes use of this.
    • Blind4Basics Avatar

      About documentation, you can just say what methods are defined for each class without giving the code (as long as it's working in all situations ;o ).

      About redefining the classes: the users will use the classes in preloaded, that will have the exact same interface, hence behaviors, than those you'll copy/paste in the tests. So that shouldn't cause troubles as long as the users don't try to add methods on the fly or stuff like that (might be worth of adding a note about that, tho / about that solution, as far as I can see, they didn't touch to your classes and built a State class that is only relying on the data held by your classes, and not their "implementation").

    • scarecrw Avatar

      Alright, I've updated the description and copied the classes to the test suite.

      Let me know if there's anything else amiss. Thanks again for your help!

    • Blind4Basics Avatar

      your __eq__ implementation still isn't correct. Use this instead:

      return isinstance(other, Crate) and self.color == other.color
      

      Oh, and you need to add the __hash__ method, since you provide __eq__ (that shouldn't be useful, but just so that the objects behaves properly if an user tries to build sets of dicts):

      def __hash__(self):
          return hash(self.color)
          
      

      Other than that, the kata looks ok.

    • scarecrw Avatar

      That's the implementation I had for __eq__ at first, but it was causing a problem with a solution which constructed its own Crate object using the class in preloaded and compared it to a Crate object constructed in the test suite.

      I took FArekkusu's note and switched to namedtuple objects. Am I correct in assuming that, as tuples, they should be hashable and comparable by default?

    • Blind4Basics Avatar

      yes, namedtuples should resolve all the problems at once. But as FArekkusu pointed out, you're currently using them the wrong way ;)

    • scarecrw Avatar

      Well, learned something new! Looks like it's resolved now. Thanks again for the help!

      Issue marked resolved by scarecrw 3 years ago
  • dfhwze Avatar

    Ambigious description of "directly below":

    To me directly below means that there can't be a vertical gap between the claw and crate. But your example shows it means any crate anywhere vertically below the claw.

    If the claw is empty and there are no crates directly below, nothing happens.

    I suggest to make the description more clear.

    • scarecrw Avatar

      Good point.

      I've changed it to:

      If the claw is empty and there are no crates in the stack below, nothing happens.

      Hope that's clearer!

      Suggestion marked resolved by scarecrw 3 years ago
  • Voile Avatar

    Please add a proper __repr__ method to Command as well so commands are displayed as something readable and not something like <preloaded.Command object at 0x7fdbb97e8970>.

  • scarecrw Avatar

    Still new to authoring, so don't hold back with feedback! Thanks!

    • dfhwze Avatar

      It's a fun kata, and you've put alot of effort in the description and test cases. Well done :)