Popular recipes tagged "meta:loc=145"http://code.activestate.com/recipes/tags/meta:loc=145/2017-06-06T03:48:09-07:00ActiveState Code RecipesInteractive Mandelbrot Fractal Using HTML5 Canvas (JavaScript) 2017-06-06T03:48:09-07:00FB36http://code.activestate.com/recipes/users/4172570/http://code.activestate.com/recipes/580804-interactive-mandelbrot-fractal-using-html5-canvas/ <p style="color: grey"> JavaScript recipe 580804 by <a href="/recipes/users/4172570/">FB36</a> (<a href="/recipes/tags/canvas/">canvas</a>, <a href="/recipes/tags/fractal/">fractal</a>, <a href="/recipes/tags/graphics/">graphics</a>, <a href="/recipes/tags/math/">math</a>). </p> <p>Interactive Mandelbrot Fractal Using HTML5 Canvas.</p> <p>(Download and save as html file and open it.)</p> <p>(Tested only using Firefox browser.)</p> adventure game base (Python) 2013-08-03T20:51:02-07:00Andrew Wayne Teesdale Jr.http://code.activestate.com/recipes/users/4187305/http://code.activestate.com/recipes/578623-adventure-game-base/ <p style="color: grey"> Python recipe 578623 by <a href="/recipes/users/4187305/">Andrew Wayne Teesdale Jr.</a> (<a href="/recipes/tags/base_class/">base_class</a>, <a href="/recipes/tags/text_game/">text_game</a>). </p> <p>simple module that holds the needed classes to make an adventure game plus a ascii anamation class for ascii art</p> NPV / IRR / Payback Analysis (Python) 2013-09-06T23:04:55-07:00Alia Khourihttp://code.activestate.com/recipes/users/4169084/http://code.activestate.com/recipes/576686-npv-irr-payback-analysis/ <p style="color: grey"> Python recipe 576686 by <a href="/recipes/users/4169084/">Alia Khouri</a> (<a href="/recipes/tags/finance/">finance</a>). Revision 18. </p> <p>A few financial functions for quick analysis of an investment opportunity and a series of associated cashflows.</p> <p>As a module, it currently provides straightforward and easy to understand implementations of the Net Present Value (NPV), Internal Rate of Return (IRR), and Payback Period functions.</p> <p>As a script, it provides a simple command line interface which integrates the above functions into a concise analysis of the investment opportunity.</p> <pre class="prettyprint"><code> usage: invest discount_rate [cashflow0, cashflow1, ..., cashflowN] where discount_rate is the rate used to discount future cashflows to their present values cashflow0 is the investment (always a negative value) cashflow1 .. cashflowN values can be positive (net inflows) or negative (net outflows) </code></pre> <p>Here is an example of actual usage and output:</p> <pre class="prettyprint"><code>$ ./invest 0.05 -10000 6000 6000 6000 ---------------------------------------------------------------------- year 0 1 2 3 cashflow -10,000 6,000 6,000 6,000 Discount Rate: 5.0% Payback: 1.67 years IRR: 36.31% NPV: 6339.49 ==&gt; Approve Investment of 10,000 ---------------------------------------------------------------------- </code></pre> <p><em>Note</em>: A check of the output of the Microsoft Excel NPV function against that of the function implemented here reveals a curious discrepancy/bug in the way Excel calculates its NPV. For further details see: <a href="http://www.tvmcalcs.com/blog/comments/the_npv_function_doesnt_calculate_net_present_value/" rel="nofollow">http://www.tvmcalcs.com/blog/comments/the_npv_function_doesnt_calculate_net_present_value/</a></p> <p>Furthermore, the method used to calculate the IRR is rough to say the least and fails at fewer than 3 entries. Please use the secant method along the lines of the following haskell code from (<a href="http://www.haskell.org/haskellwiki/Haskell_Quiz/Internal_Rate_of_Return/Solution_Dolio" rel="nofollow">http://www.haskell.org/haskellwiki/Haskell_Quiz/Internal_Rate_of_Return/Solution_Dolio</a>) for greater accuracy.</p> <pre class="prettyprint"><code>secant :: (Double -&gt; Double) -&gt; Double -&gt; Double secant f delta = fst $ until err update (0,1) where update (x,y) = (x - (x - y) * f x / (f x - f y), x) err (x,y) = abs (x - y) &lt; delta npv :: Double -&gt; [Double] -&gt; Double npv i = sum . zipWith (\t c -&gt; c / (1 + i)**t) [0..] irr :: [Double] -&gt; Double irr cashflows = secant (`npv` cashflows) (0.1**4) </code></pre> Records (Python) 2008-11-04T06:52:42-08:00George Sakkishttp://code.activestate.com/recipes/users/2591466/http://code.activestate.com/recipes/576555-records/ <p style="color: grey"> Python recipe 576555 by <a href="/recipes/users/2591466/">George Sakkis</a> (<a href="/recipes/tags/datastructures/">datastructures</a>, <a href="/recipes/tags/namedtuple/">namedtuple</a>, <a href="/recipes/tags/record/">record</a>). </p> <p>This is a recipe similar in functionality and exec-style optimized implementation to the very well received namedtuple (<a href="http://code.activestate.com/recipes/500261/" rel="nofollow">http://code.activestate.com/recipes/500261/</a>) that was included in Python 2.6. The main difference is that <strong>records</strong>, unlike named tuples, are mutable. In addition, fields can have a default value. Instead of subclassing tuple or list, the implementation create a regular class with __slots__.</p> Fast posmax (Python) 2008-01-27T02:48:07-08:00bearophile -http://code.activestate.com/recipes/users/2403049/http://code.activestate.com/recipes/543271-fast-posmax/ <p style="color: grey"> Python recipe 543271 by <a href="/recipes/users/2403049/">bearophile -</a> (<a href="/recipes/tags/algorithms/">algorithms</a>). </p> <p>To quickly find the index of the first maximum item of a sequence.</p> Setting Win32 System Clock Using SNTP (Python) 2004-02-22T05:49:50-08:00Robin Beckerhttp://code.activestate.com/recipes/users/880795/http://code.activestate.com/recipes/270423-setting-win32-system-clock-using-sntp/ <p style="color: grey"> Python recipe 270423 by <a href="/recipes/users/880795/">Robin Becker</a> . </p> <p>Simon Foster's recipe "Simple (very) SNTP client" (see <a href="http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/117211" rel="nofollow">http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/117211</a>) inspired me to get SNTP to do something useful when our office moved to an ISP that didn't do client 37 time setting. This recipe uses SNTP to get an estimate of the time offset and uses Thomas Heller's wonderful ctypes module to allow getting/setting the win32 system time. I apologise in advance for the rather awful int vs long bit twiddling in _L2U32 etc.</p>