No Exercise Today

May 20, 2016

I’ve been busy at work and haven’t had time to prepare an exercise for today. I apologize.

Your task is to solve a previous exercise that you haven’t yet solved. Have fun!

One Response to “No Exercise Today”

  1. Jussi Piitulainen said

    A take on that interleaved sorting exercise: a minimal way to see even and odd positions as collections of their own right, backed by the actual store that contains them both. (This is Python.)

    class See(object):
    
        def __init__(self, store, length, index):
            self.store = store
            self.length = length
            self.index = index
    
        def __setitem__(self, x, vs):
            '''handle self[:] = vs when lengths match'''
            for k, o in enumerate(vs):
                self.store[self.index(k)] = o
    
        def __iter__(self):
            for k in range(self.length):
                yield self.store[self.index(k)]
    
    def demonstrate(data):
        every = See(data, len(data), lambda k: k)
        even = See(data, -(-len(data) // 2), lambda k: 2 * k)
        odd = See(data, len(data) // 2, lambda k: 2 * k + 1)
        print('before:', *map(''.join, (every, even, odd)))
        even[:] = sorted(even)
        odd[:] = sorted(odd, reverse = True)
        print('after:', *map(''.join, (every, even, odd)))
    
    demonstrate(list('abracadabra'))
    demonstrate(list('0123456789'))
    
    # before: abracadabra arcdba baaar
    # after: arabbacadar aabcdr rbaaa
    # before: 0123456789 02468 13579
    # after: 0927456381 02468 97531
    

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: