100 lines
7.8 KiB
HTML
100 lines
7.8 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/blog/fonts/opensans.css">
|
|
<link href="../../../../../assets/blog/css/normalize.css" rel="stylesheet" type="text/css">
|
|
<link href="../../../../../assets/blog/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="bugs.lezzo.org">Personal Git</a>
|
|
<a class="btn" href="https://bugs.lezzo.org/bparodi/Curriculum_vitae/raw/branch/master/latex.dir/francesco_mecca_cv_eng.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">
|
|
<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>
|
|
<div class="code"><pre class="code literal-block"> <span class="kn">import</span> <span class="nn">random</span><span class="o">,</span> <span class="nn">sys</span>
|
|
|
|
<span class="k">if</span> <span class="vm">__name__</span> <span class="o">==</span> <span class="s1">'__main__'</span><span class="p">:</span>
|
|
<span class="n">args</span> <span class="o">=</span> <span class="nb">list</span><span class="p">()</span>
|
|
<span class="k">if</span> <span class="ow">not</span> <span class="n">sys</span><span class="o">.</span><span class="n">stdin</span><span class="o">.</span><span class="n">isatty</span><span class="p">():</span>
|
|
<span class="k">for</span> <span class="n">line</span> <span class="ow">in</span> <span class="n">sys</span><span class="o">.</span><span class="n">stdin</span><span class="p">:</span>
|
|
<span class="k">if</span> <span class="n">line</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span> <span class="ow">is</span> <span class="s1">'</span><span class="se">\n</span><span class="s1">'</span><span class="p">:</span>
|
|
<span class="n">line</span> <span class="o">=</span> <span class="n">line</span><span class="p">[:</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
|
|
<span class="n">args</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">line</span><span class="p">)</span>
|
|
<span class="k">else</span><span class="p">:</span>
|
|
<span class="n">args</span> <span class="o">=</span> <span class="n">sys</span><span class="o">.</span><span class="n">argv</span><span class="p">[</span><span class="mi">1</span><span class="p">:]</span>
|
|
<span class="n">argRange</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">args</span><span class="p">)</span>
|
|
<span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">argRange</span><span class="p">):</span>
|
|
<span class="nb">print</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">i</span><span class="o">+</span><span class="mi">1</span><span class="p">)</span> <span class="o">+</span> <span class="s1">'.'</span><span class="p">,</span> <span class="n">args</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="n">random</span><span class="o">.</span><span class="n">randrange</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="nb">len</span><span class="p">(</span><span class="n">args</span><span class="p">))))</span>
|
|
</pre></div>
|
|
|
|
<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>
|
|
<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://getnikola.com">Nikola</a>, <a href="https://github.com/jasonlong/cayman-theme">Cayman theme</a>.</span>
|
|
</footer></section>
|
|
</div>
|
|
</body>
|
|
</html>
|