104 lines
8.1 KiB
HTML
104 lines
8.1 KiB
HTML
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="utf-8">
|
|
<meta name="viewport" content="width=device-width">
|
|
<title>The Buridan's donkey in python | Caught in the Net</title>
|
|
<link rel="stylesheet" href="../../../../../assets/fonts/opensans.css">
|
|
<link href="../../../../../assets/css/normalize.css" rel="stylesheet" type="text/css">
|
|
<link href="../../../../../assets/css/cayman.css" rel="stylesheet" type="text/css">
|
|
<meta name="theme-color" content="#5670d4">
|
|
<meta name="generator" content="Nikola (getnikola.com)">
|
|
<link rel="alternate" type="application/rss+xml" title="RSS" hreflang="en" href="../../../../../rss.xml">
|
|
<link rel="canonical" href="francescomecca.eu/blog/2016/4/2/buridan_donkey/">
|
|
<meta name="author" content="Francesco Mecca">
|
|
<link rel="prev" href="../../../../2015/9/20/the-buridans-donkey-paradox/" title="The Buridan&#8217;s donkey paradox" type="text/html">
|
|
<link rel="next" href="../blog-migrated/" title="How I migrated to a static blog" type="text/html">
|
|
<meta property="og:site_name" content="Caught in the Net">
|
|
<meta property="og:title" content="The Buridan's donkey in python">
|
|
<meta property="og:url" content="francescomecca.eu/blog/2016/4/2/buridan_donkey/">
|
|
<meta property="og:description" content="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">
|
|
<meta property="og:type" content="article">
|
|
<meta property="article:published_time" content="2016-04-02T00:00:00Z">
|
|
<meta property="article:tag" content="buridan donkey">
|
|
<meta property="article:tag" content="mersenne twister">
|
|
<meta property="article:tag" content="PesceWanda">
|
|
<meta property="article:tag" content="python">
|
|
<meta property="article:tag" content="random">
|
|
</head>
|
|
<body>
|
|
<div id="container">
|
|
|
|
<section class="page-header"><h1 class="project-name">
|
|
Caught in the Net
|
|
</h1>
|
|
<h2 class="project-tagline">La rete ti cattura ma libera il pensiero</h2>
|
|
<a class="btn" href="../../../../../">Home</a>
|
|
<a class="btn" href="../../../../../pages/about/">About me</a>
|
|
<a class="btn" href="../../../../../pages/contattami/">Contact me</a>
|
|
<a class="btn" href="../../../../../archiveall.html">Archive</a>
|
|
<a class="btn" href="../../../../../rss.xml">RSS</a>
|
|
<a class="btn" href="http://francescomecca.eu/git/pesceWanda">Personal Git</a>
|
|
<a class="btn" href="https://github.com/FraMecca">Github</a>
|
|
<a class="btn" href="../../../../../wp-content/curriculum/CV_Mecca_Francesco.pdf">Curriculum</a>
|
|
</section><section class="main-content"><div class="post">
|
|
|
|
<header><h1 class="post-title">
|
|
|
|
<h1 class="p-name post-title" itemprop="headline name">The Buridan's donkey in python</h1>
|
|
|
|
</h1>
|
|
</header><p class="dateline post-date">02 April 2016</p>
|
|
</div>
|
|
|
|
|
|
|
|
<div class="e-content entry-content" itemprop="articleBody text">
|
|
<div>
|
|
<p>During the final weeks of my exam session I started reading a bit about python 3 using an excellent book: <a href="http://www.diveintopython.net/">Dive into Python</a>.
|
|
When I noted that python uses the <a href="https://en.wikipedia.org/wiki/Mersenne_Twister">Mersenne Twister PRNG</a> as well I decided to write another version of my <a href="http://francescomecca.eu/index.php/archives/207">Buridan's donkey program</a>.</p>
|
|
<p>.. code:: python</p>
|
|
<pre class="code literal-block"><span></span> <span class="s s-Atom">import</span> <span class="s s-Atom">random</span><span class="p">,</span> <span class="s s-Atom">sys</span>
|
|
|
|
<span class="s s-Atom">if</span> <span class="k">__</span><span class="s s-Atom">name__</span> <span class="o">==</span> <span class="s s-Atom">'__main__':</span>
|
|
<span class="s s-Atom">args</span> <span class="o">=</span> <span class="nf">list</span><span class="p">()</span>
|
|
<span class="s s-Atom">if</span> <span class="o">not</span> <span class="s s-Atom">sys</span><span class="p">.</span><span class="s s-Atom">stdin</span><span class="p">.</span><span class="nf">isatty</span><span class="p">()</span><span class="s s-Atom">:</span>
|
|
<span class="s s-Atom">for</span> <span class="s s-Atom">line</span> <span class="s s-Atom">in</span> <span class="s s-Atom">sys</span><span class="p">.</span><span class="nn">stdin</span><span class="p">:</span>
|
|
<span class="s s-Atom">if</span> <span class="s s-Atom">line</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span> <span class="o">is</span> <span class="s s-Atom">'\n':</span>
|
|
<span class="s s-Atom">line</span> <span class="o">=</span> <span class="s s-Atom">line</span><span class="p">[:-</span><span class="mi">1</span><span class="p">]</span>
|
|
<span class="s s-Atom">args</span><span class="p">.</span><span class="nf">append</span><span class="p">(</span><span class="s s-Atom">line</span><span class="p">)</span>
|
|
<span class="nn">else</span><span class="p">:</span>
|
|
<span class="s s-Atom">args</span> <span class="o">=</span> <span class="s s-Atom">sys</span><span class="p">.</span><span class="s s-Atom">argv</span><span class="p">[</span><span class="mi">1</span><span class="s s-Atom">:</span><span class="p">]</span>
|
|
<span class="s s-Atom">argRange</span> <span class="o">=</span> <span class="nf">len</span><span class="p">(</span><span class="s s-Atom">args</span><span class="p">)</span>
|
|
<span class="s s-Atom">for</span> <span class="s s-Atom">i</span> <span class="s s-Atom">in</span> <span class="nf">range</span><span class="p">(</span><span class="s s-Atom">argRange</span><span class="p">)</span><span class="s s-Atom">:</span>
|
|
<span class="nf">print</span><span class="p">(</span><span class="nf">str</span><span class="p">(</span><span class="s s-Atom">i</span><span class="o">+</span><span class="mi">1</span><span class="p">)</span> <span class="o">+</span> <span class="s s-Atom">'.'</span><span class="p">,</span> <span class="s s-Atom">args</span><span class="p">.</span><span class="nf">pop</span><span class="p">(</span><span class="s s-Atom">random</span><span class="p">.</span><span class="nf">randrange</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="nf">len</span><span class="p">(</span><span class="s s-Atom">args</span><span class="p">))))</span>
|
|
</pre>
|
|
|
|
|
|
<p>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.</p>
|
|
<p>Not satisfied enough, I wrote also a telegram bot using the <a href="https://github.com/eternnoir/pyTelegramBotAPI">telebot library</a> that works as the script above but inside the telegram app.
|
|
The bot can be added to your contact list by simply searching for <a href="http://telegram.me/duridan_donkey_bot">@duridan_donkey_bot</a> (yes, a typo!)</p>
|
|
<p>All the code is opensource and can be found on my github page.</p>
|
|
<p>Francesco Mecca</p>
|
|
</div>
|
|
</div>
|
|
<aside class="postpromonav"><nav><h4>Categories</h4>
|
|
|
|
<ul itemprop="keywords" class="tags">
|
|
<li><a class="tag p-category" href="../../../../../categories/buridan-donkey/" rel="tag">buridan donkey</a></li>
|
|
<li><a class="tag p-category" href="../../../../../categories/mersenne-twister/" rel="tag">mersenne twister</a></li>
|
|
<li><a class="tag p-category" href="../../../../../categories/pescewanda/" rel="tag">PesceWanda</a></li>
|
|
<li><a class="tag p-category" href="../../../../../categories/python/" rel="tag">python</a></li>
|
|
<li><a class="tag p-category" href="../../../../../categories/random/" rel="tag">random</a></li>
|
|
</ul></nav></aside><p class="sourceline"><a href="index.md" class="sourcelink">Source</a></p>
|
|
|
|
|
|
|
|
<footer class="site-footer" id="footer"><span> CC BY-SA 4.0 International.<br></span>
|
|
<span class="site-footer-credits"><a href="https://jekyllrb.com">Jekyll</a>, <a href="https://github.com/jasonlong/cayman-theme">Cayman theme</a>.</span>
|
|
</footer></section>
|
|
</div>
|
|
</body>
|
|
</html>
|