Last active
August 26, 2016 20:29
-
-
Save Fingel/240bcc0d4bf082e184e16c0c43f3534f to your computer and use it in GitHub Desktop.
A really simple ponysort implementation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
""" | |
My 'probably good enough' implementation of the ponysort. | |
Create enough buckets (rakes) for each rider. Iterate over the | |
riders and only add a rider to a rake if it is not already | |
there. Does not attempt to optimize spacing. | |
There is a case where the callup can not be created: if a rider | |
appears more than NUM_RIDERS/RAKE_SIZE times. | |
""" | |
ALPHABET = 'abcdefghijklmnopqrstuvwxyz' | |
NUM_RIDERS = 42 | |
RAKE_SIZE = 5 | |
# Generate NUM_RIDERS random riders in the form 'aaa', 'bbb' ... | |
riders = [random.choice(ALPHABET) * 3 for i in range(NUM_RIDERS)] | |
# Create enough rakes for all the riders | |
num_rakes = len(riders) // RAKE_SIZE | |
if len(riders) % RAKE_SIZE != 0: | |
num_rakes += 1 | |
callup = [[] for j in range(num_rakes)] | |
for rider in riders: | |
for rake in callup: | |
if rider not in rake and len(rake) < RAKE_SIZE: | |
rake.append(rider) | |
break | |
# Flatten the callup | |
callup = [i for l in callup for i in l] | |
# Test to make sure all riders are accounted for | |
assert(sorted(callup) == sorted(riders)) | |
print(callup) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment