Refactor
:my
=>
'code'
Codes
Refactorings
Popular
Best
Submit
Spam
Account
Logout
Login
JavaScript doesn't seem to be activated, expect things to be ugly and sloppy!
More Jobs
Recent
sort_by_multiple
Prepare links - friendly URLs
Rails Active Record Time Summary
Experimental Prime Number Generator
Pythonify my code
DVD Directory Archiver
Raytracer in haskell
Cryptography with MD5
Experimental Prime Number Generator
has_many :through collection saving
Popular
sort_by_multiple
Prepare links - friendly URLs
Cryptography with MD5
Rails Active Record Time Summary
Raytracer in haskell
DVD Directory Archiver
Pythonify my code
Experimental Prime Number Generator
Code to detect the web browser
simplify multiple gsub
Pastable version of
Pascal's Triangle
<div style="overflow:auto;border:solid 1px #ccc;background:#000;color:#F8F8F8"> <div class="section"> <pre style="float:left;margin:0 10px;border-right:0;color:#666;">1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57</pre> <pre class="sunburst"><span style="color:#65B042;"><span style="color:#65B042;">"""</span></span> <span style="color:#65B042;">For a given number, prints Pascal's Triangle upside-down up to that line.</span> <span style="color:#65B042;">Prints everything alligned at first, but starts screwing up at about 13.</span> <span style="color:#65B042;"></span> <span style="color:#65B042;">Some info:</span> <span style="color:#65B042;">0. Takes advantage of the fact that the Triangle is symmetric.</span> <span style="color:#65B042;">1. Uses the combinatorics property of the Triangle: </span> <span style="color:#65B042;"> For any NUMBER in position INDEX at row ROW:</span> <span style="color:#65B042;"> NUMBER = C(ROW, INDEX)</span> <span style="color:#65B042;">2. A dictionary stores the values of the combinatorics already calculated,</span> <span style="color:#65B042;"> so the comb function speeds up a little. Think of it as a recursive</span> <span style="color:#65B042;"> function with a memory.</span> <span style="color:#65B042;"></span> <span style="color:#65B042;">Pablo A. Torres Navarrete</span> <span style="color:#65B042;">tn.pablo@gmail.com</span> <span style="color:#65B042;">03/09/07</span> <span style="color:#65B042;"><span style="color:#65B042;">"""</span></span> <span style="color:#AEAEAE;font-style:italic;"><span style="color:#AEAEAE;font-style:italic;">#</span>! /usr/bin/env python</span> <span style="color:#E28964;">import</span> sys <span style="color:#99CF50;">def</span> <span style="color:#89BDFF;">comb</span>(<span style="color:#3E87E3;">N</span>, <span style="color:#3E87E3;">n</span>, <span style="color:#3E87E3;">combs</span>): <span style="color:#65B042;"><span style="color:#65B042;">"""</span> </span> <span style="color:#65B042;"> Returns the value of C(N,n). </span> <span style="color:#65B042;"> First the function looks it up in a dictionary and only calculates</span> <span style="color:#65B042;"> if it isn't present, in which case works recursively.</span> <span style="color:#65B042;"> <span style="color:#65B042;">"""</span></span> <span style="color:#E28964;">try</span>: ans <span style="color:#E28964;">=</span> combs[(N,n)] <span style="color:#E28964;">except</span> <span style="color:#9B859D;">KeyError</span>: <span style="color:#E28964;">if</span> n <span style="color:#E28964;">==</span> <span style="color:#3387CC;">0</span>: ans<span style="color:#E28964;">=</span><span style="color:#3387CC;">1</span> <span style="color:#E28964;">elif</span> n <span style="color:#E28964;">==</span> <span style="color:#3387CC;">1</span>: ans<span style="color:#E28964;">=</span>N <span style="color:#E28964;">else</span>: <span style="color:#AEAEAE;font-style:italic;"><span style="color:#AEAEAE;font-style:italic;">#</span> A little maths...</span> ans <span style="color:#E28964;">=</span> (comb(N, n<span style="color:#E28964;">-</span><span style="color:#3387CC;">1</span>, combs) <span style="color:#E28964;">*</span> (N<span style="color:#E28964;">-</span>n<span style="color:#E28964;">+</span><span style="color:#3387CC;">1</span>) <span style="color:#E28964;">*</span> <span style="color:#3387CC;">1</span><span style="color:#E28964;">/</span>n) combs[(N,n)] <span style="color:#E28964;">=</span> ans <span style="color:#E28964;">return</span> ans lines <span style="color:#E28964;">=</span> <span style="color:#9B859D;">int</span>(sys.argv[<span style="color:#3387CC;">1</span>]) <span style="color:#AEAEAE;font-style:italic;"><span style="color:#AEAEAE;font-style:italic;">#</span> stack that will contain the items of the row to be mirrored</span> mirror <span style="color:#E28964;">=</span> [] <span style="color:#AEAEAE;font-style:italic;"><span style="color:#AEAEAE;font-style:italic;">#</span> dictionary of combinatoric-value pairs</span> combs <span style="color:#E28964;">=</span> {} <span style="color:#E28964;">for</span> row <span style="color:#E28964;">in</span> <span style="color:#DAD085;">range</span>(lines, <span style="color:#E28964;">-</span><span style="color:#3387CC;">1</span>, <span style="color:#E28964;">-</span><span style="color:#3387CC;">1</span>): <span style="color:#AEAEAE;font-style:italic;"><span style="color:#AEAEAE;font-style:italic;">#</span> insert indentation</span> <span style="color:#E28964;">print</span> <span style="color:#65B042;"><span style="color:#65B042;">'</span> <span style="color:#65B042;">'</span></span> <span style="color:#E28964;">*</span> (lines <span style="color:#E28964;">-</span> row), <span style="color:#AEAEAE;font-style:italic;"><span style="color:#AEAEAE;font-style:italic;">#</span> first half of the row</span> limit <span style="color:#E28964;">=</span> (row<span style="color:#E28964;">/</span><span style="color:#E28964;">/</span><span style="color:#3387CC;">2</span>)<span style="color:#E28964;">+</span><span style="color:#3387CC;">1</span> <span style="color:#E28964;">for</span> index <span style="color:#E28964;">in</span> <span style="color:#DAD085;">range</span>(limit): num <span style="color:#E28964;">=</span> comb(row, index, combs) <span style="color:#E28964;">if</span> <span style="color:#E28964;">not</span>((row<span style="color:#E28964;">%</span><span style="color:#3387CC;">2</span> <span style="color:#E28964;">==</span> <span style="color:#3387CC;">0</span>) <span style="color:#E28964;">and</span> (index <span style="color:#E28964;">==</span> limit<span style="color:#E28964;">-</span><span style="color:#3387CC;">1</span>)): mirror.append(num) <span style="color:#E28964;">print</span> <span style="color:#65B042;"><span style="color:#65B042;">'</span><span style="color:#DDF2A4;">%i</span> <span style="color:#65B042;">'</span></span> <span style="color:#E28964;">%</span> num, <span style="color:#AEAEAE;font-style:italic;"><span style="color:#AEAEAE;font-style:italic;">#</span> for the second half, mirror the first</span> <span style="color:#E28964;">while</span> mirror: <span style="color:#E28964;">print</span> <span style="color:#65B042;"><span style="color:#65B042;">'</span><span style="color:#DDF2A4;">%i</span> <span style="color:#65B042;">'</span></span> <span style="color:#E28964;">%</span> mirror.pop(), <span style="color:#E28964;">print</span> </pre> </div> </div> <a href="http://refactormycode.com/codes/59-pascal-s-triangle" style="color:#fff" title="As seen on RefactorMyCode.com"><img alt="Small_logo" src="http://refactormycode.com/images/small_logo.gif" style="border:0" /></a>