Ad
  • Custom User Avatar

    Please, mention the language you're talking about, there are many for this kata. If it is Python, this is the initial code:

    def decode_bits(bits):
        # ToDo: Accept 0's and 1's, return dots, dashes and spaces
        return bits.replace('111', '-').replace('000', ' ').replace('1', '.').replace('0', '')
    
    def decode_morse(morseCode):
        # ToDo: Accept dots, dashes and spaces, return human-readable message
        return morseCode.replace('.', MORSE_CODE['.']).replace('-', MORSE_CODE['-']).replace(' ', '')
    

    As you can see the names of both functions are ok. Only the var morseCode is using camelCase and should be changed to snake_case. Now, if you talk about the kata description, yes, they are both named in camelCase and the description is shared amongst all languages.

  • Custom User Avatar

    Easy problem to fix: the provided starting code has a function decodeMorse(), but shouldn't it be named decode_morse()?

  • Custom User Avatar

    Interesting! The program shouldn't indeed rely on the user not changing the inputs. I updated the code, passing copies of the deck instead of the original array. Thanks for your help :-)

  • Custom User Avatar

    Hey Zwyx! I finally got back to it, and figured it out. My program dissasembles the deck as it decodes it, and a successful decoding turns the deck into an empty list. Since Python passes lists by reference this change persists into the caller's scope. So, in the last set of tests with a randomized deck my code succeeds but empties out the deck, and then the tests call your control decode() with an empty list. I changed my decode() function to operate on a copy of the deck and everything worked; all tests passed.

    Two (not mutually exclusive) possibilities for closing this hole: (1) specify that user functions not change their arguments; or (2) change tests so that they call user functions with a copy of the deck. The second is easy enough; just pass "deck[:]" instead of "deck".

    Thanks for the challenge, and for the help debugging the problem!

  • Custom User Avatar

    Got it, thanks. It'll take at least a few days to get to it, though. I have some other commitments that need attention.

  • Custom User Avatar

    Haha :-) That is awesome! An actual real usecase of the algorithm, I'm a fan :-) Just sent you a message, the password is playingcards ;-)

  • Custom User Avatar

    Yeah, that puzzles me too --- that other people have completed the Python version of the kata.

    It's been years since I've used GPG and have lost track of my private keys. Maybe on some archive media somewhere. But I've also forgotten the passphrase!

    So, apropos the kata, here's my email, encoded in a deck, if you'd like to send the program that way:

    AC 2C 3C 4C 5C 6C 7C 8C 9C TC 3H 6D KH
    QS TH KS JD 3D 6H 2S 7S 2H AH 8S 2D 3S
    QD TS QC 8H 4S 9D 8D JH AD 9H 7H 5D TD
    5H KD KC AS 4H 6S 4D 5S 7D QH JC JS 9S

  • Custom User Avatar

    I did the translation... but I'm not a Python dev so it's almost a "literal" translation from Java, absolutely not in the Python style. It was three years ago and I didn't touch Python at all since then. So I won't be able to help you further I'm afraid.

    Altough, I'd be happy to give you the Python solution. So you'll be able to understand where the problem is, and if it's with the Codewars solution, please report it back so we can correct it :-)

    I guess if I post it here and mark it as spoiler, you won't be able to see it, so if you give me a link to your public PGP key, I'll encrypt it for you. (https://www.gnupg.org/gph/en/manual/c14.html)

    Already 23 people solved this kata in Python though, so it would be a bit surprising if has an error...

  • Custom User Avatar

    OK, I just looked at it again. I think there is a problem with the randomized-deck tests. Here's a test that my code failed today:

    QC 9S 8H QH 3C 6D 2D 5S 3S AC TC 7H 8C
    5D 8D 4D 8S KC JD 5C QS JH QD 6S KH 7S
    KD JC AD 3D TS TH 7D 9H 4S 6C 4H 9D 4C
    5H KS 9C JS 2H AH 2S 3H 6H 2C 7C TD AS

    'ZNSZZCXGCBGXTAAUVHSTLHVHBGXYNODYUJWTLEQASRHAMVI' should equal None

    However, my local tests suggest it should not be None. Here's output from a test where my program encodes the message to a deck, then decodes the deck back to a message. The round-trip succeeds:

    ZNSZZCXGCBGXTAAUVHSTLHVHBGXYNODYUJWTLEQASRHAMVI
    ['QC', '9S', '8H', 'QH', '3C', '6D', '2D', '5S', '3S', 'AC', 'TC', '7H', '8C',
    '5D', '8D', '4D', '8S', 'KC', 'JD', '5C', 'QS', 'JH', 'QD', '6S', 'KH', '7S',
    'KD', 'JC', 'AD', '3D', 'TS', 'TH', '7D', '9H', '4S', '6C', '4H', '9D', '4C',
    '5H', 'KS', '9C', 'JS', '2H', 'AH', '2S', '3H', '6H', '2C', '7C', 'TD', 'AS']
    => 'ZNSZZCXGCBGXTAAUVHSTLHVHBGXYNODYUJWTLEQASRHAMVI'

    Here's a test where I start with the deck as given in the test, and decode it to a message:

    ['QC', '9S', '8H', 'QH', '3C', '6D', '2D', '5S', '3S', 'AC', 'TC', '7H', '8C',
    '5D', '8D', '4D', '8S', 'KC', 'JD', '5C', 'QS', 'JH', 'QD', '6S', 'KH', '7S',
    'KD', 'JC', 'AD', '3D', 'TS', 'TH', '7D', '9H', '4S', '6C', '4H', '9D', '4C',
    '5H', 'KS', '9C', 'JS', '2H', 'AH', '2S', '3H', '6H', '2C', '7C', 'TD', 'AS']
    => 'ZNSZZCXGCBGXTAAUVHSTLHVHBGXYNODYUJWTLEQASRHAMVI'

    Again, everything is as it should be.

    Did someone else translate the kata into Python? Perhaps they could be encouraged to have a look.

  • Custom User Avatar

    No, I went on to try the Bernoulli Numbers kata and spent a couple of days optimizing code and reading up on Bernoulli numbers. :-)

    I was hoping you'd be able to find the bug. Might it be that the Python tests still expect "10" instead of "T" for ranks of ten?

    I'll have another look.

  • Custom User Avatar

    Thanks B4B ;-)

    Hey tranquility-base, have you succeeded to make your code work? I'm not fluent in Python, and if you say it works, I don't know where to look...

  • Custom User Avatar

    This comment is hidden because it contains spoiler information about the solution

  • Custom User Avatar

    I feel the same about the C# version. I'm not sure when the program is supposed to exit. It passes some tests if it exists when it's processed all the input but not others.

  • Custom User Avatar

    Trying to go through all permutations of the cards is probably the problem. The number of permutations is HUGE -- about 18 orders of magnitude larger than the estimated number of atoms making up the earth (about 10^50, see https://www.fnal.gov/pub/science/inquiring/questions/atoms.html). The crux of the kata is figuring out how to compute the permutation without brute-force enumeration (and then going the other way, figuring out where a given permutation would be in a sorted list of all possible permutations). Try working on small problems to develop intuition -- say, if you had a deck that had just 4 cards (A, B, C, and D), the possibilities are:

    A B C D

    A B D C

    A C B D

    A C D B

    A D B C

    A D C B

    ... 3 more 6-line blocks

    Note that this 6-line "A" block has sub blocks -- a 2-line "B" subblock, a 2-line "C" subblock, and a 2-line "D" subblock.

    Where would "B A C D" come -- in which 6-line block? Apply that thinking recursively. Within the "B" block, where would "A C D" come?

    Hopefully this will help get you started. And as a hint, div (//) and mod (%) are your friends!

  • Custom User Avatar

    python: test.assert_equals(user, expected)
    java: assertEquals(expected, user)

    (reminder)

  • Loading more items...