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

44 lines
1.6 KiB
Markdown

---
title: The Buridan's donkey in python
date: 2016-04-02
author: pesceWanda
layout: post
categories:
- PesceWanda
tags:
- 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](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).
{% 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](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