Ad
  • Custom User Avatar

    You may be right, but I like to avoid boilerplate whenever possible. In my opinion it enhances readability to use situational syntactic sugar like this: because it can only be used in certain circumstances, its use highlights that you are in those circumstances.

  • Custom User Avatar

    Perfectly servicable and efficient. Wouldn't best practices encourage adding a return line? Obviously not necessary for such simple code.

  • Default User Avatar

    You can save one byte by using an operator different from the !=, but it's not well defined whether or not that's the desired behaviour, and I don't want to take any steps towards turning this into a solution remotely resembling lambda*_:532. If you make a fork that changes something other than that and improve the rules in a way that allows a shorter operator to be used, it might be a good thing to copmare against a hypothetical one byte shorter version of this that utilizes the < operator.

  • Custom User Avatar

    Yeah in JS yesterday it took me about an hour to get it working, but way too slow! Still haven't been able to improve it. May need to start over.

  • Custom User Avatar

    It took me about an hour to write it in Python mostly because I was having a difficult time returning the solution even as I was able to print it.
    What took me the longest time was to rewrite it in C++ as that is the language that I wanted to solve it.

  • Custom User Avatar

    Still there's the same problem with Python3, there's print() expression issue in test,
    as well as another one i get, if needed:
    Traceback (most recent call last):
    File "main.py", line 14, in
    testAndPrint(decodeMorse(decodeBits('1')), 'E')
    File "/home/codewarrior/solution.py", line 6, in decodeMorse
    return ' '.join(''.join(MORSE_CODE[letter] for letter in word.split(' ')) for word in morseCode.strip().split(' '))
    File "/home/codewarrior/solution.py", line 6, in
    return ' '.join(''.join(MORSE_CODE[letter] for letter in word.split(' ')) for word in morseCode.strip().split(' '))
    File "/home/codewarrior/solution.py", line 6, in
    return ' '.join(''.join(MORSE_CODE[letter] for letter in word.split(' ')) for word in morseCode.strip().split(' '))
    KeyError: '1'

  • Default User Avatar

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

  • Default User Avatar
  • Custom User Avatar

    no, sum just happens to accept a generator. That ord(c) for c in ... is a generator expression, and is not a list.

  • Custom User Avatar

    ord(c) for c in s.replace(' ', '')- I thought this semantic is for list comprehension, can i use it for every loop?

  • Custom User Avatar

    A generator expression gives the values (in this case the integers coming from ord(c)) one by one, instead of storing them in a list.

  • Custom User Avatar

    You're right, asymptotically it doesn't change anything.
    But a string takes much less space than a list of the same size so we're using at least two times less memory.

    But I'm nitpicking, honestly I just removed the brackets because it looks nicer that way.

  • Custom User Avatar

    str.replace has O(n) space complexity, so you're not really saving much.

  • Default User Avatar

    Can you please explain what is a generator expression?

  • Custom User Avatar

    Quite right. I never used this before but it works like a charm.

  • Loading more items...