1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | #!/usr/bin/python3 ''' SI 335 Fall 2013 Project 1 This program reads jobs from jobs.txt and names from names.txt, then uses the values of n and k given on the command line to generate random input for the program. This generated input file is written to standard out. ''' from sys import argv, exit from random import shuffle, sample def usage(): print("Usage: {} n k".format(argv[0])) print("jobs.txt and names.txt must exist in current directory.") exit(1) if len(argv) != 3: usage() n, k = map(int, argv[1:]) if n < 0 or k < 0: usage() with open('names.txt') as namesfile: names = [line.strip() for line in namesfile] with open('jobs.txt') as jobsfile: jobs = [line.strip() for line in jobsfile] if n > len(names): print("ERROR: not enough names in names.txt") exit(2) if k > len(jobs): print("ERROR: not enough jobs in jobs.txt") exit(2) rankings = [list(range(1,n+1)) for i in range(k)] for ranks in rankings: shuffle(ranks) print(k) for job in sample(jobs, k): print(job) print(n) for nameAndRanks in zip(sample(names, n), *rankings): print(*nameAndRanks) |