francescomecca.eu/_posts/2016-04-02-buridan_donkey.md
2017-03-20 00:46:14 +01:00

1.6 KiB

title date author layout categories tags
The Buridan's donkey in python 2016-04-02 pesceWanda post
PesceWanda
Buridan donkey
python
random
mersenne twister

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.

{% highlight 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))))

{% endhighlight %} 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