-
Notifications
You must be signed in to change notification settings - Fork 0
/
median.html
162 lines (156 loc) · 8.77 KB
/
median.html
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
<title>median.py</title>
<link rel="stylesheet" href="pycco.css">
<meta name="thumbnail" content="https://avatars0.githubusercontent.com/u/23156192?v=3&s=200" />
<link rel="shortcut icon" href="favicon.png" type="image/png">
</head>
<body>
<div id='container'>
<a href="https://github.com/ttv1/src/blob/master/median.py"><img style="z-index: 10; position: absolute; top: 0; left: 0; border: 0;" src="https://camo.githubusercontent.com/567c3a48d796e2fc06ea80409cc9dd82bf714434/68747470733a2f2f73332e616d617a6f6e6177732e636f6d2f6769746875622f726962626f6e732f666f726b6d655f6c6566745f6461726b626c75655f3132313632312e706e67" alt="Fork me on GitHub" data-canonical-src="https://s3.amazonaws.com/github/ribbons/forkme_left_darkblue_121621.png"></a>
<div id="background"></div>
<div class='section'>
<div class='docs'><h1> median.py</h1><p> This is ttv1 code (Timm tools, version 1).</p></div>
</div>
<div class='clearall'>
<div class='section' id='section-0'>
<div class='docs'>
<div class='octowrap'>
<a class='octothorpe' href='#section-0'>#</a>
</div>
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-55634425-2', 'auto');
ga('send', 'pageview');
</script>
<p><img
src="https://avatars0.githubusercontent.com/u/23156192?v=3&s=200"
align=left
width=120>
<br> <br>
<a href="http://ttv1.github.io">home</a> | <a href="#discussion">discuss</a> | <a href="https://github.com/ttv1/src/issues">report bug</a>
<br>
<script>
(function() {
var cx = '009630129455493240085:aja5uvdnjeo';
var gcse = document.createElement('script'); gcse.type = 'text/javascript'; gcse.async = true;
gcse.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') + '//www.google.com/cse/cse.js?cx=' + cx;
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(gcse, s);
})();
</script>
<div style="width:0px;overflow:hidden;height:0px;"> <!-- if you use display:none here, it doesn't work-->
<gcse:search></gcse:search>
</div>
<form style="margin-left: 20px;" id="searchbox_009630129455493240085:aja5uvdnjeo" action="">
<input value="009630129455493240085:aja5uvdnjeo" name="cx" type="hidden"/>
<input value="FORID:11" name="cof" type="hidden"/>
<input id="q" style="" name="q" size="25" type="text"/>
<button class="btn">Search</button>
</form></p>
<p><br clear=all></p>
<hr />
<p>Return the median of a list of numbers.
By default, assumes the list is unordered
and needs sorting (and this default can be over-loaded
at the command line).</p>
<p>Whether or not to sort by default <em>sounds</em> like a big
issue. However, after several timing studies, I can report
that unless lists are large (10,000 items or more),
in absolute terms, it makes
very little difference to the runtimes (since
Python's built in sort functions can sort thousands
of numbers in ten-thousands of a second).</p>
<hr />
<h2>Programmer's Guide</h2>
</div>
<div class='code'>
<div class="highlight"><pre></pre></div>
</div>
</div>
<div class='clearall'></div>
<div class='section' id='section-1'>
<div class='docs'>
<div class='octowrap'>
<a class='octothorpe' href='#section-1'>#</a>
</div>
<p>If <code>ordered</code> is <code>False</code>, do not sort <code>lst</code></p>
</div>
<div class='code'>
<div class="highlight"><pre><span class="k">def</span> <span class="nf">median</span><span class="p">(</span><span class="n">lst</span><span class="p">,</span><span class="n">ordered</span><span class="o">=</span><span class="bp">False</span><span class="p">):</span>
<span class="k">assert</span> <span class="n">lst</span><span class="p">,</span><span class="s">"median needs a non-empty list"</span>
<span class="n">n</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">lst</span><span class="p">)</span>
<span class="n">p</span> <span class="o">=</span> <span class="n">q</span> <span class="o">=</span> <span class="n">n</span><span class="o">//</span><span class="mi">2</span>
<span class="k">if</span> <span class="n">n</span> <span class="o"><</span> <span class="mi">3</span><span class="p">:</span>
<span class="n">p</span><span class="p">,</span><span class="n">q</span> <span class="o">=</span> <span class="mi">0</span><span class="p">,</span> <span class="n">n</span><span class="o">-</span><span class="mi">1</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">lst</span> <span class="o">=</span> <span class="n">lst</span> <span class="k">if</span> <span class="n">ordered</span> <span class="k">else</span> <span class="nb">sorted</span><span class="p">(</span><span class="n">lst</span><span class="p">)</span>
<span class="k">if</span> <span class="ow">not</span> <span class="n">n</span> <span class="o">%</span> <span class="mi">2</span><span class="p">:</span> <span class="c"># for even-length lists, use mean of mid 2 nums</span>
<span class="n">q</span> <span class="o">=</span> <span class="n">p</span> <span class="o">-</span><span class="mi">1</span>
<span class="k">return</span> <span class="n">lst</span><span class="p">[</span><span class="n">p</span><span class="p">]</span> <span class="k">if</span> <span class="n">p</span><span class="o">==</span><span class="n">q</span> <span class="k">else</span> <span class="p">(</span><span class="n">lst</span><span class="p">[</span><span class="n">p</span><span class="p">]</span><span class="o">+</span><span class="n">lst</span><span class="p">[</span><span class="n">q</span><span class="p">])</span><span class="o">/</span><span class="mi">2</span></pre></div>
</div>
</div>
<div class='clearall'></div>
<div class='section' id='section-2'>
<div class='docs'>
<div class='octowrap'>
<a class='octothorpe' href='#section-2'>#</a>
</div>
<hr />
<p><img align=right
src="https://raw.githubusercontent.com/timm/timm.github.io/master/timm.png"
width=170></p>
<h2>Copyleft</h2>
<p>Copyright © 2016,2017 Tim Menzies <a href="mailto:tim@menzies.us">tim@menzies.us</a>, MIT license v2.</p>
<p>Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject
to the following conditions:</p>
<p>The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.</p>
<p>THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.</p>
<p>Share and enjoy.</p>
<hr>
<a name="discussion"><h2>Discussion</h2></a>
<div id="disqus_thread"></div>
<script>
/**
* RECOMMENDED CONFIGURATION VARIABLES: EDIT AND UNCOMMENT THE SECTION BELOW TO INSERT DYNAMIC VALUES FROM YOUR PLATFORM OR CMS.
* LEARN WHY DEFINING THESE VARIABLES IS IMPORTANT: https://disqus.com/admin/universalcode/#configuration-variables*/
/*
var disqus_config = function () {
this.page.url = 'https://ttv1.github.ip/median.html'
this.page.identifier = 'median';
};
*/
(function() { // DON'T EDIT BELOW THIS LINE
var d = document, s = d.createElement('script');
s.src = '//ttv1.disqus.com/embed.js';
s.setAttribute('data-timestamp', +new Date());
(d.head || d.body).appendChild(s);
})();
</script>
<noscript>Please enable JavaScript to view the <a href="https://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
</div>
<div class='code'>
<div class="highlight"><pre></pre></div>
</div>
</div>
<div class='clearall'></div>
</div>
<script id="dsq-count-scr" src="//ttv1.disqus.com/count.js" async></script>
</body>
</html>