763 lines
14 KiB
HTML
763 lines
14 KiB
HTML
<!DOCTYPE html>
|
|
<html lang="en-us">
|
|
|
|
<head>
|
|
<link href="http://gmpg.org/xfn/11" rel="profile">
|
|
<meta http-equiv="X-UA-Compatible" content="IE=edge">
|
|
<meta http-equiv="content-type" content="text/html; charset=utf-8">
|
|
|
|
<!-- Enable responsiveness on mobile devices-->
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1">
|
|
|
|
<title>
|
|
|
|
The Buridan's donkey in python · Caught in the Net
|
|
|
|
</title>
|
|
|
|
<!-- CSS -->
|
|
<link rel="stylesheet" href="/public/css/poole.css">
|
|
<link rel="stylesheet" href="/public/css/syntax.css">
|
|
<link rel="stylesheet" href="/public/css/hyde.css">
|
|
|
|
<!-- Icons -->
|
|
<link rel="apple-touch-icon-precomposed" sizes="144x144" href="/public/apple-touch-icon-144-precomposed.png">
|
|
<link rel="shortcut icon" href="/public/favicon.ico">
|
|
|
|
<!-- RSS -->
|
|
<link rel="alternate" type="application/rss+xml" title="RSS" href="/atom.xml">
|
|
</head>
|
|
|
|
|
|
<body class="theme-base-09">
|
|
|
|
<div class="sidebar">
|
|
<div class="container sidebar-sticky">
|
|
<div class="sidebar-about">
|
|
<h1>
|
|
<a href="/">
|
|
Caught in the Net
|
|
</a>
|
|
</h1>
|
|
<p class="lead"></p>
|
|
</div>
|
|
|
|
<nav class="sidebar-nav">
|
|
<a class="sidebar-nav-item" href="/">Home</a>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<a class="sidebar-nav-item" href="/about/">About</a>
|
|
|
|
|
|
|
|
|
|
|
|
<a class="sidebar-nav-item" href="/archive/">Archive</a>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<a class="sidebar-nav-item" href="/contattami/">Contact me</a>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<a class="sidebar-nav-item" href="/feed.xml">RSS</a>
|
|
<a class="sidebar-nav-item" href="http://francescomecca.eu:3000/explore/repos">Personal Git</a>
|
|
<span class="sidebar-nav-item" href="" >Powered by Jekyll and Hyde</span>
|
|
</nav>
|
|
|
|
<p>© 2016. CC BY-SA 4.0 International </p>
|
|
</div>
|
|
</div>
|
|
|
|
<h3 class="masthead-title">
|
|
<a href="/" title="Home">Caught in the Net</a>
|
|
|
|
|
|
</h3>
|
|
|
|
<div class="content container">
|
|
<div class="post">
|
|
<h1 class="post-title">The Buridan's donkey in python</h1>
|
|
<span class="post-date">02 Apr 2016</span>
|
|
<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>
|
|
|
|
<figure class="highlight"><pre><code class="language-python" data-lang="python"> <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="n">__name__</span> <span class="o">==</span> <span class="s">'__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="s">'</span><span class="se">\n</span><span class="s">'</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="k">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="s">'.'</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></code></pre></figure>
|
|
|
|
<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 class="related">-->
|
|
<!--<related-posts />-->
|
|
<!--<h2>Related Posts</h2>-->
|
|
<!--<ul class="related-posts">-->
|
|
<!---->
|
|
<!--<li>-->
|
|
<!--<h3>-->
|
|
<!--<a href="/pescewanda/2016/11/11/machine-learning-PARTE2/">-->
|
|
<!--Capire il Machine Learning (parte 2)-->
|
|
<!--<small>11 Nov 2016</small>-->
|
|
<!--</a>-->
|
|
<!--</h3>-->
|
|
<!--</li>-->
|
|
<!---->
|
|
<!--<li>-->
|
|
<!--<h3>-->
|
|
<!--<a href="/pescewanda/2016/11/10/machine-learning-intro/">-->
|
|
<!--Capire il Machine Learning (parte 1)-->
|
|
<!--<small>10 Nov 2016</small>-->
|
|
<!--</a>-->
|
|
<!--</h3>-->
|
|
<!--</li>-->
|
|
<!---->
|
|
<!--<li>-->
|
|
<!--<h3>-->
|
|
<!--<a href="/pescewanda/2016/07/07/pres-berk/">-->
|
|
<!--A short talk about cryptography at the Berkman Klein Center-->
|
|
<!--<small>07 Jul 2016</small>-->
|
|
<!--</a>-->
|
|
<!--</h3>-->
|
|
<!--</li>-->
|
|
<!---->
|
|
<!--<li>-->
|
|
<!--<h3>-->
|
|
<!--<a href="/pescewanda/2016/07/06/IM-services/">-->
|
|
<!--Phone messaging apps comparison-->
|
|
<!--<small>06 Jul 2016</small>-->
|
|
<!--</a>-->
|
|
<!--</h3>-->
|
|
<!--</li>-->
|
|
<!---->
|
|
<!--<li>-->
|
|
<!--<h3>-->
|
|
<!--<a href="/pescewanda/2016/07/05/arduino_keyboard/">-->
|
|
<!--Arduino Uno as HID keyboard-->
|
|
<!--<small>05 Jul 2016</small>-->
|
|
<!--</a>-->
|
|
<!--</h3>-->
|
|
<!--</li>-->
|
|
<!---->
|
|
<!--</ul>-->
|
|
<!--</div>-->
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<h4>Similar Posts</h4>
|
|
<ul>
|
|
|
|
<li class="relatedPost">
|
|
<a href="/pescewanda/2016/05/15/genetic-alg/">Interpolation using a genetic algorithm
|
|
|
|
</a>
|
|
</li>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<li class="relatedPost">
|
|
<a href="/pescewanda/2016/04/17/kpd-player/">Kyuss Music Player
|
|
|
|
</a>
|
|
</li>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
</ul>
|
|
|
|
|
|
</div>
|
|
<!--<link rel="alternate" type="application/rss+xml" title="Francesco Mecca RSS" href="/feed.xml">-->
|
|
|
|
</body>
|
|
</html>
|