francescomecca.eu/_site/pescewanda/2016/04/02/buridan_donkey/index.html

765 lines
14 KiB
HTML
Raw Normal View History

2016-05-01 11:13:57 +02:00
<!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 &middot; 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>
2016-07-22 21:12:39 +02:00
<a class="sidebar-nav-item" href="/contattami/">Contact me</a>
2016-05-01 11:13:57 +02:00
2016-09-17 15:13:02 +02:00
2016-05-01 11:13:57 +02:00
2016-05-03 23:38:08 +02:00
2016-07-15 04:40:39 +02:00
2016-05-01 11:13:57 +02:00
2016-09-17 15:13:02 +02:00
2016-05-01 11:13:57 +02:00
2016-09-17 15:13:02 +02:00
<a class="sidebar-nav-item" href="/feed.xml">RSS</a>
2016-11-18 20:45:39 +01:00
<a class="sidebar-nav-item" href="http://francescomecca.eu:3000/explore/repos">Personal Git</a>
2016-05-01 11:13:57 +02:00
<span class="sidebar-nav-item" href="" >Powered by Jekyll and Hyde</span>
</nav>
2017-02-24 07:32:48 +01:00
<p>&copy; 2017. CC BY-SA 4.0 International </p>
2016-05-01 11:13:57 +02:00
</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&#39;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>
2016-09-17 15:13:02 +02:00
<!--<div class="related">-->
<!--<related-posts />-->
<!--<h2>Related Posts</h2>-->
<!--<ul class="related-posts">-->
2017-02-24 07:32:48 +01:00
<!---->
<!--<li>-->
<!--<h3>-->
<!--<a href="/pescewanda/2017/01/11/spazio-digitale-rant-facebook/">-->
<!--Titolo-->
<!--<small>11 Jan 2017</small>-->
<!--</a>-->
<!--</h3>-->
<!--</li>-->
2016-11-19 18:38:14 +01:00
<!---->
<!--<li>-->
<!--<h3>-->
<!--<a href="/pescewanda/2016/11/15/machine-learning-PARTE3/">-->
<!--Capire il Machine Learning (parte 3)-->
<!--<small>15 Nov 2016</small>-->
<!--</a>-->
<!--</h3>-->
<!--</li>-->
2016-09-17 15:13:02 +02:00
<!---->
<!--<li>-->
<!--<h3>-->
2016-11-18 20:45:39 +01:00
<!--<a href="/pescewanda/2016/11/11/machine-learning-PARTE2/">-->
<!--Capire il Machine Learning (parte 2)-->
<!--<small>11 Nov 2016</small>-->
2016-09-17 15:13:02 +02:00
<!--</a>-->
<!--</h3>-->
<!--</li>-->
<!---->
<!--<li>-->
<!--<h3>-->
2016-11-18 20:45:39 +01:00
<!--<a href="/pescewanda/2016/11/10/machine-learning-intro/">-->
<!--Capire il Machine Learning (parte 1)-->
<!--<small>10 Nov 2016</small>-->
2016-09-17 15:13:02 +02:00
<!--</a>-->
<!--</h3>-->
<!--</li>-->
<!---->
<!--<li>-->
<!--<h3>-->
2016-11-18 20:45:39 +01:00
<!--<a href="/pescewanda/2016/07/07/pres-berk/">-->
<!--A short talk about cryptography at the Berkman Klein Center-->
<!--<small>07 Jul 2016</small>-->
2016-09-17 15:13:02 +02:00
<!--</a>-->
<!--</h3>-->
<!--</li>-->
<!---->
<!--</ul>-->
<!--</div>-->
2017-02-24 07:32:48 +01:00
2016-09-17 15:13:02 +02:00
2017-02-24 07:32:48 +01:00
2016-09-17 15:13:02 +02:00
2017-02-24 07:32:48 +01:00
2016-11-18 20:45:39 +01:00
2016-09-17 15:13:02 +02:00
2016-11-19 18:38:14 +01:00
2016-09-17 15:13:02 +02:00
2016-11-18 20:45:39 +01:00
2017-02-24 07:32:48 +01:00
2016-11-18 20:45:39 +01:00
2016-09-17 15:13:02 +02:00
2016-11-18 20:45:39 +01:00
2016-09-17 15:13:02 +02:00
2016-11-18 20:45:39 +01:00
2016-11-19 18:38:14 +01:00
2016-11-18 20:49:28 +01:00
2016-11-18 20:45:39 +01:00
2016-09-17 15:13:02 +02:00
2017-02-24 07:32:48 +01:00
2016-09-17 15:13:02 +02:00
2017-02-24 07:32:48 +01:00
2016-11-18 20:45:39 +01:00
2016-09-17 15:13:02 +02:00
2017-02-24 07:32:48 +01:00
<h4>Similar Posts</h4>
<ul>
<li class="relatedPost">
<a href="/pescewanda/2016/05/15/genetic-alg/">Interpolation using a genetic algorithm
</a>
</li>
2016-09-17 15:13:02 +02:00
2016-11-18 20:45:39 +01:00
2017-02-24 07:32:48 +01:00
2016-11-18 20:45:39 +01:00
2016-09-17 15:13:02 +02:00
</ul>
2016-05-01 11:13:57 +02:00
</div>
2016-09-17 15:13:02 +02:00
<!--<link rel="alternate" type="application/rss+xml" title="Francesco Mecca RSS" href="/feed.xml">-->
2016-05-01 11:13:57 +02:00
</body>
</html>