Most viewed recipes tagged "meta:requires=random"http://code.activestate.com/recipes/tags/meta:requires=random/views/2017-07-15T00:46:59-07:00ActiveState Code RecipesUno (Text-Based) (Python) 2017-07-15T00:46:59-07:00Brandon Martinhttp://code.activestate.com/recipes/users/4194238/http://code.activestate.com/recipes/580811-uno-text-based/ <p style="color: grey"> Python recipe 580811 by <a href="/recipes/users/4194238/">Brandon Martin</a> (<a href="/recipes/tags/artificial_intelligence/">artificial_intelligence</a>, <a href="/recipes/tags/cards/">cards</a>, <a href="/recipes/tags/game/">game</a>, <a href="/recipes/tags/text_game/">text_game</a>, <a href="/recipes/tags/uno/">uno</a>). </p> <p>A text based recreation of the classic card game featuring functional AIs to play with. Some rules have been modified. User interface is text based, non-curses, using only simple python commands to draw it. </p> Binary floating point summation accurate to full precision (Python) 2009-03-28T23:32:08-07:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/393090-binary-floating-point-summation-accurate-to-full-p/ <p style="color: grey"> Python recipe 393090 by <a href="/recipes/users/178123/">Raymond Hettinger</a> (<a href="/recipes/tags/algorithms/">algorithms</a>). Revision 5. </p> <p>Completely eliminates rounding errors and loss of significance due to catastrophic cancellation during summation. Achieves exactness by keeping full precision intermediate subtotals. Offers three alternative approaches, each using a different technique to store exact subtotals.</p> The Game of Battleships in Python (Python) 2014-02-22T06:13:41-08:00Captain DeadBoneshttp://code.activestate.com/recipes/users/4184772/http://code.activestate.com/recipes/578836-the-game-of-battleships-in-python/ <p style="color: grey"> Python recipe 578836 by <a href="/recipes/users/4184772/">Captain DeadBones</a> (<a href="/recipes/tags/beginner/">beginner</a>, <a href="/recipes/tags/game/">game</a>, <a href="/recipes/tags/python/">python</a>). </p> <p>Another fun game that is fun to program and play. No special AI (yet). But still and entertaining game. </p> <p>For more info about <a href="http://thelivingpearl.com/2014/02/17/the-game-of-battleships-in-python/">Battleships in Python</a> follow the link. </p> Threads, Tkinter and asynchronous I/O (Python) 2001-10-21T07:14:35-07:00Jacob Hallénhttp://code.activestate.com/recipes/users/136936/http://code.activestate.com/recipes/82965-threads-tkinter-and-asynchronous-io/ <p style="color: grey"> Python recipe 82965 by <a href="/recipes/users/136936/">Jacob Hallén</a> (<a href="/recipes/tags/threads/">threads</a>). </p> <p>This recipe shows the easiest way of handling access to sockets, serial ports and other asynchronous I/O ports while running a Tkinter based GUI. It allows for a worker thread to block in a select(). Whenever something arrives it will received and inserted in a queue. The main (GUI) thread then polls the queue 10 times per second (often enough so the user will not notice any significant delay), and processes all messages that have arrived.</p> A generic programming thread pool (Python) 2003-09-24T17:24:43-07:00Tim Lesherhttp://code.activestate.com/recipes/users/161138/http://code.activestate.com/recipes/203871-a-generic-programming-thread-pool/ <p style="color: grey"> Python recipe 203871 by <a href="/recipes/users/161138/">Tim Lesher</a> (<a href="/recipes/tags/threads/">threads</a>). Revision 3. </p> <p>A thread pool class that takes arbitrary callables as work units, and supports callbacks when the work unit is complete.</p> Using a Dictionary in place of a 'switch' statement (Python) 2003-02-16T14:25:05-08:00yaipa haahttp://code.activestate.com/recipes/users/519352/http://code.activestate.com/recipes/181064-using-a-dictionary-in-place-of-a-switch-statement/ <p style="color: grey"> Python recipe 181064 by <a href="/recipes/users/519352/">yaipa haa</a> (<a href="/recipes/tags/algorithms/">algorithms</a>). </p> <p>Description: Rock, Scissors, Paper Game. Shows a clean way of implementing a 'switch' statement in Python via a dictionary container. The dictionary is made up of known 'named states' that are tested in sequence for their current 'state'.</p> Decorator for BindingConstants at compile time (Python) 2010-11-16T08:36:38-08:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/277940-decorator-for-bindingconstants-at-compile-time/ <p style="color: grey"> Python recipe 277940 by <a href="/recipes/users/178123/">Raymond Hettinger</a> (<a href="/recipes/tags/programs/">programs</a>). Revision 9. </p> <p>Decorator for automatic code optimization. If a global is known at compile time, replace it with a constant. Fold tuples of constants into a single constant. Fold constant attribute lookups into a single constant.</p> Persistent dict with multiple standard file formats (Python) 2011-09-06T20:01:46-07:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/576642-persistent-dict-with-multiple-standard-file-format/ <p style="color: grey"> Python recipe 576642 by <a href="/recipes/users/178123/">Raymond Hettinger</a> (<a href="/recipes/tags/dbm/">dbm</a>, <a href="/recipes/tags/dictionary/">dictionary</a>, <a href="/recipes/tags/persistent/">persistent</a>, <a href="/recipes/tags/shelve/">shelve</a>). Revision 10. </p> <p>dbdict: a dbm based on a dict subclass.</p> <p>On open, loads full file into memory. On close, writes full dict to disk (atomically). Supported output file formats: csv, json, and pickle. Input file format automatically discovered.</p> <p>Usable by the shelve module for fast access.</p> Encode multipart form data for uploading files via POST (Python) 2013-09-22T21:27:24-07:00Ben Hoythttp://code.activestate.com/recipes/users/4170919/http://code.activestate.com/recipes/578668-encode-multipart-form-data-for-uploading-files-via/ <p style="color: grey"> Python recipe 578668 by <a href="/recipes/users/4170919/">Ben Hoyt</a> (<a href="/recipes/tags/form/">form</a>, <a href="/recipes/tags/http/">http</a>, <a href="/recipes/tags/multipart/">multipart</a>, <a href="/recipes/tags/upload/">upload</a>). </p> <p>This function lets you encode form fields <em>and</em> files in multipart/form-data format for uploading files via HTTP POST.</p> A simple Matrix class (Python) 2012-05-14T13:34:31-07:00Anand B Pillaihttp://code.activestate.com/recipes/users/4169530/http://code.activestate.com/recipes/578131-a-simple-matrix-class/ <p style="color: grey"> Python recipe 578131 by <a href="/recipes/users/4169530/">Anand B Pillai</a> (<a href="/recipes/tags/algebra/">algebra</a>, <a href="/recipes/tags/math/">math</a>, <a href="/recipes/tags/mathematics/">mathematics</a>, <a href="/recipes/tags/matrix/">matrix</a>). Revision 3. </p> <p>A simple class in Python representing a Matrix with basic operations, operator overloading and class factory methods to make Matrices from different sources.</p> Efficient Running Median using an Indexable Skiplist (Python) 2010-02-07T14:40:03-08:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/576930-efficient-running-median-using-an-indexable-skipli/ <p style="color: grey"> Python recipe 576930 by <a href="/recipes/users/178123/">Raymond Hettinger</a> (<a href="/recipes/tags/advanced_data_structure/">advanced_data_structure</a>, <a href="/recipes/tags/container/">container</a>, <a href="/recipes/tags/median/">median</a>, <a href="/recipes/tags/new_algorithm/">new_algorithm</a>, <a href="/recipes/tags/skiplist/">skiplist</a>, <a href="/recipes/tags/sliding_window/">sliding_window</a>). Revision 10. </p> <p>Maintains sorted data as new elements are added and old one removed as a sliding window advances over a stream of data. Also gives fast indexed access to value. Running time per median update is proportional to the log of the window size.</p> Simple Back-propagation Neural Network in Python source code (Python) 2012-05-30T17:09:49-07:00David Adlerhttp://code.activestate.com/recipes/users/4182015/http://code.activestate.com/recipes/578148-simple-back-propagation-neural-network-in-python-s/ <p style="color: grey"> Python recipe 578148 by <a href="/recipes/users/4182015/">David Adler</a> (<a href="/recipes/tags/back/">back</a>, <a href="/recipes/tags/back_propagation/">back_propagation</a>, <a href="/recipes/tags/neural/">neural</a>, <a href="/recipes/tags/neural_network/">neural_network</a>, <a href="/recipes/tags/propagation/">propagation</a>, <a href="/recipes/tags/python/">python</a>). </p> <p>This is a slightly different version of this <a href="http://arctrix.com/nas/python/bpnn.py" rel="nofollow">http://arctrix.com/nas/python/bpnn.py</a></p> Random selection of elements in a list, with no repeats (Python) 2002-06-05T05:50:10-07:00Iuri Wickerthttp://code.activestate.com/recipes/users/111694/http://code.activestate.com/recipes/59883-random-selection-of-elements-in-a-list-with-no-rep/ <p style="color: grey"> Python recipe 59883 by <a href="/recipes/users/111694/">Iuri Wickert</a> (<a href="/recipes/tags/search/">search</a>). Revision 2. </p> <p>The simplest, direct way of consuming a list in a random fashion is painfully slow for list with a few 100's of elements. There are equally simple, but much faster ways to do this.</p> Genetic Algorithm in Python source code - AI-Junkie tutorial (Python) 2012-06-19T12:59:13-07:00David Adlerhttp://code.activestate.com/recipes/users/4182015/http://code.activestate.com/recipes/578128-genetic-algorithm-in-python-source-code-ai-junkie-/ <p style="color: grey"> Python recipe 578128 by <a href="/recipes/users/4182015/">David Adler</a> (<a href="/recipes/tags/algorithm/">algorithm</a>, <a href="/recipes/tags/artificial/">artificial</a>, <a href="/recipes/tags/genetic/">genetic</a>, <a href="/recipes/tags/network/">network</a>, <a href="/recipes/tags/neural/">neural</a>, <a href="/recipes/tags/python/">python</a>). Revision 5. </p> <p>A simple genetic algorithm program. I followed this tutorial to make the program <a href="http://www.ai-junkie.com/ga/intro/gat1.html." rel="nofollow">http://www.ai-junkie.com/ga/intro/gat1.html.</a></p> <p>The objective of the code is to evolve a mathematical expression which calculates a user-defined target integer.</p> <hr /> <p>KEY:</p> <p>chromosome = binary list (this is translated/decoded into a protein in the format number --> operator --> number etc, any genes (chromosome is read in blocks of four) which do not conform to this are ignored.</p> <p>protein = mathematical expression (this is evaluated from left to right in number + operator blocks of two)</p> <p>output = output of protein (mathematical expression)</p> <p>error = inverse of difference between output and target</p> <p>fitness score = a fraction of sum of of total errors</p> <hr /> <p>OTHER:</p> <p>One-point crossover is used.</p> <p>I have incorporated <strong>elitism</strong> in my code, which somewhat deviates from the tutorial but made my code more efficient (top ~7% of population are carried through to next generation)</p> TKinter Listbox example (Python) 2005-04-21T14:33:13-07:00Nathan Regenerhttp://code.activestate.com/recipes/users/2316939/http://code.activestate.com/recipes/410646-tkinter-listbox-example/ <p style="color: grey"> Python recipe 410646 by <a href="/recipes/users/2316939/">Nathan Regener</a> . Revision 2. </p> <p>This is a simple modal listbox class. It is intended to be invoked about like this: results = ListBoxChoice(root, list=listOfItems).returnValue().</p> <p>It will return the list item selected. It will take any item in the list that can return a textual indicator.</p> Super Simple Sudoku Solver in Python source code (Python) 2012-06-23T14:56:05-07:00David Adlerhttp://code.activestate.com/recipes/users/4182015/http://code.activestate.com/recipes/578140-super-simple-sudoku-solver-in-python-source-code/ <p style="color: grey"> Python recipe 578140 by <a href="/recipes/users/4182015/">David Adler</a> (<a href="/recipes/tags/algorithm/">algorithm</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/recursive/">recursive</a>, <a href="/recipes/tags/recurssion/">recurssion</a>, <a href="/recipes/tags/sodoku/">sodoku</a>, <a href="/recipes/tags/sudoku/">sudoku</a>). Revision 5. </p> <p>A simple algorithm which uses a recursive function to solve the puzzle.</p> <hr /> <p>THE ALGORITHM</p> <p>The credit for this algorithm must go to Richard Buckland: <a href="http://www.youtube.com/watch?v=bjObm0hxIYY&amp;feature=autoplay&amp;list=PL6B940F08B9773B9F&amp;playnext=1" rel="nofollow">http://www.youtube.com/watch?v=bjObm0hxIYY&amp;feature=autoplay&amp;list=PL6B940F08B9773B9F&amp;playnext=1</a></p> <p>Takes a partially filled in grid, inserts the min value in a cell (could be a random cell, in this case the first free cell). If the min value is not legal it will increment until the max value is reached (number 9), checking each time if the incremented value is legal in that cell (ie does not clash with any already entered cells in square, col or row). If it is legal, it will call itself (the hasSolution function) thus using this slightly more filled in grid to find a new cell and check which value is legal in this next cell. If no values are legal in the next cell, it will clear the previous grid entry and try incrementing the value.</p> <p>isLegal = does not conflict with any other numbers in the same row, column or square</p> The Markov Chain Algorithm (Python) 2003-04-12T19:29:43-07:00Brian Chinhttp://code.activestate.com/recipes/users/1095547/http://code.activestate.com/recipes/194364-the-markov-chain-algorithm/ <p style="color: grey"> Python recipe 194364 by <a href="/recipes/users/1095547/">Brian Chin</a> (<a href="/recipes/tags/algorithms/">algorithms</a>). Revision 3. </p> <p>A classic algorithm which can produce entertaining output, given a sufficiently large input</p> Language detection using character trigrams (Python) 2004-11-07T01:08:26-08:00Douglas Bagnallhttp://code.activestate.com/recipes/users/1629020/http://code.activestate.com/recipes/326576-language-detection-using-character-trigrams/ <p style="color: grey"> Python recipe 326576 by <a href="/recipes/users/1629020/">Douglas Bagnall</a> (<a href="/recipes/tags/algorithms/">algorithms</a>). Revision 2. </p> <p>The Trigram class can be used to compare blocks of text based on their local structure, which is a good indicator of the language used. It could also be used within a language to discover and compare the characteristic footprints of various registers or authors. As all n-gram implementations should, it has a method to make up nonsense words.</p> Bloom Filter (Python) 2011-06-04T17:44:21-07:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/577684-bloom-filter/ <p style="color: grey"> Python recipe 577684 by <a href="/recipes/users/178123/">Raymond Hettinger</a> (<a href="/recipes/tags/big_table/">big_table</a>, <a href="/recipes/tags/bloom_filter/">bloom_filter</a>, <a href="/recipes/tags/sets/">sets</a>, <a href="/recipes/tags/spelling_checker/">spelling_checker</a>, <a href="/recipes/tags/spell_checker/">spell_checker</a>). Revision 18. </p> <p>Space efficient, probabilistic set membership tester. Has no False Negatives but allows a rare False Positive.</p> My first application server (Python) 2009-02-23T11:53:57-08:00Pierre Quentelhttp://code.activestate.com/recipes/users/1552957/http://code.activestate.com/recipes/392879-my-first-application-server/ <p style="color: grey"> Python recipe 392879 by <a href="/recipes/users/1552957/">Pierre Quentel</a> (<a href="/recipes/tags/web/">web</a>). Revision 8. </p> <p>For those who want to start dynamic web programming, but don't know what to choose among the many Python web frameworks, this program might be a good starting point</p> <p>ScriptServer is a minimalist application server, handling both GET and POST requests, including multipart/form-data for file uploads, HTTP redirections, and with an in-memory session management. It can run Python scripts and template files using the standard string substitution format</p> <p>The scripts are run in the same process as the server, avoiding the CGI overhead. The environment variables are provided in the namespace where the script runs</p> <p>To start the server, run </p> <pre class="prettyprint"><code>python ScriptServer.py </code></pre> <p>In your web browser, enter <a href="http://localhost" rel="nofollow">http://localhost</a>, this will show you a listing of the directory. Add the scripts in the same directory as ScriptServer</p>