The Captain's Room

  • + 1 comment

    You're wrong.

    Firstly, because my idea isn't based on a maths trick, there's no need to parse the strings into numbers - you can just add them to the sets as they are. The integer parsing actually takes up a significant amount of the total effort; removing it brings even your implementation of my idea close to the speed of the maths trick.

    Secondly, if - having removed the unnecessary integer parsing - you just change

    for m in members:
        if m not in rooms:
            rooms.add(m)
        else:
            room_more_mem.add(m)
    

    to

    for m in members:
        if m not in room_more_mem:
    		target = room_more_mem if m in rooms else rooms
    		target.add(m)
    

    then the set-based solution becomes faster than the math trick, while also having all the other virtues mentioned (like not needing to load the entire data set into memory at once).

    It's surpising how many of the challenges on this site expect you to parse strings into numbers when it isn't actually necessary. Realising that has let me solve a number of the challenges in Haskell when it might otherwise have been difficult. In the case of this challenge, it helps deal with the problem that Python code is much slower than the built-in and core library functions (which are typically written in C or Java, depending upon the implementation used).