7

Let's see how many of us can use programming skill outside in real world.

Pair socks from a pile efficiently?

Comments
  • 5
  • 4
    Pick random socks
  • 6
    Only buy plain black socks of the same kind... the OddSockError no longer occurs.
  • 3
    If it's be me will make it null, don't wear any so you don't have a tension of sorting.
  • 2
    @RamChandra But then surely when trying to call any sock related method you would have a NullRef and have to deal with null propagation.
  • 0
    @ridecar2 thinking about that situation, maybe creating a new object shoes with socks declared as final so you don't have to pick one.
  • 4
    Foreach (Sock sock in socks)
    {
    If (sock.pattern.hasMatch(patternArray))
    {
    int pair = patternArray.Match(sock);
    patterArray.moveToDrawer[pair]
    }
    else
    {
    patternArray.Add(sock)
    }
    }

    1: yes, there is a function for "hasMatching"

    2: yes, it could be more efficient, but this way its a more accurate representation of how I actually sort socks.

    3: why would you wear odd socks? That's just annoying.
  • 5
    Quantum bogo sort! (Like I'd care if I have matching socks :P)
  • 2
    @monr0e No one sees them anyway ¯\_(ツ)_/¯
  • 2
    Im colorblind so any 2 socks is a pair :^)
  • 1
    Sleepsort by hash of pattern. Human won't act faster than that anyway..^^

    But if you want it efficient, call

    function getSock() {
    const i = Math.floor(Math.random() * socks.length);
    return socks.splice(i, 1)[0];
    }

    Twice.😋
Add Comment