francescomecca.eu/posts/2016/4/2/buridan_donkey.md
2018-11-10 18:19:00 +01:00

1.7 KiB

During the final weeks of my exam session I started reading a bit about python 3 using an excellent book: Dive into Python. When I noted that python uses the Mersenne Twister PRNG as well I decided to write another version of my Buridan's donkey program.

.. code:: python

	import random, sys

	if __name__ == '__main__':
	    args = list()
	    if not sys.stdin.isatty():
    	    for line in sys.stdin:
        	    if line[-1] is '\n':
            	    line = line[:-1]
	            args.append(line)
	    else:
    	    args = sys.argv[1:]
	    argRange = len(args)
    	for i in range(argRange):
        	print(str(i+1) + '.', args.pop(random.randrange(0, len(args))))

This script works in a different way than the one in c++. Rather than shuffling a list made by the entries in the arguments, it pops randomly one entry from the list till the list is empty.

Not satisfied enough, I wrote also a telegram bot using the telebot library that works as the script above but inside the telegram app. The bot can be added to your contact list by simply searching for @duridan_donkey_bot (yes, a typo!)

All the code is opensource and can be found on my github page.

Francesco Mecca