francescomecca.eu/output/blog/2016/4/2/buridan_donkey/index.md

42 lines
1.7 KiB
Markdown
Raw Normal View History

2018-11-10 18:19:00 +01:00
<!--
.. title: The Buridan's donkey in python
.. slug: buridan_donkey
.. date: 2016-04-02
.. tags: buridan donkey,python,random,mersenne twister,PesceWanda
.. category: PesceWanda
.. link:
.. description:
.. type: text
-->
2016-05-01 11:13:57 +02:00
During the final weeks of my exam session I started reading a bit about python 3 using an excellent book: [Dive into Python](http://www.diveintopython.net/).
When I noted that python uses the [Mersenne Twister PRNG](https://en.wikipedia.org/wiki/Mersenne_Twister) as well I decided to write another version of my [Buridan's donkey program](http://francescomecca.eu/index.php/archives/207).
2018-11-10 18:19:00 +01:00
.. 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))))
2016-05-01 11:13:57 +02:00
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](https://github.com/eternnoir/pyTelegramBotAPI) 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](http://telegram.me/duridan_donkey_bot) (yes, a typo!)
All the code is opensource and can be found on my github page.
Francesco Mecca
2017-03-20 00:46:14 +01:00