Created
December 7, 2017 05:35
-
-
Save q3yi/1b0e0e7381c511831078b326d1f70a08 to your computer and use it in GitHub Desktop.
Who wanna to be the next?
This file contains hidden or 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 | |
import operator | |
import argparse | |
from collections import defaultdict | |
def count(usernames, iter_times): | |
result = defaultdict(lambda: 0) | |
for _ in range(0, iter_times): | |
result[random.choice(usernames)] += 1 | |
return result | |
def pretty_print(count_result): | |
sorted_users = sorted(count_result.items(), | |
key=operator.itemgetter(1), | |
reverse=True) | |
max_username_length = max([len(x) for x in count_result.keys()]) | |
format_str = '{:%s}|{:>10}' % (max_username_length + 1) | |
print('') | |
print(format_str.format('User', 'Count')) | |
print(format_str.format('-' * (max_username_length + 1), 10 * '-')) | |
for username, count in sorted_users: | |
print(format_str.format(username, count)) | |
print('\nThe winner is: {}'.format(sorted_users[0][0])) | |
def main(): | |
parser = argparse.ArgumentParser(description='Select the next one.') | |
parser.add_argument('usernames', metavar='competitors', type=str, nargs='+', | |
help='Who wanna join the Game?') | |
parser.add_argument('-t', '--times', metavar='N', type=int, | |
default=100, help='iter times(default:100)') | |
args = parser.parse_args() | |
pretty_print(count(args.usernames, args.times)) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment