snipt

Ctrl+h for KB shortcuts

Python

return hexadecimal representation of integer

def dec2hex(n):
    return "%X" % n
https://snipt.net/embed/6ae348bffd5515cb0e73e3f94e5b6db5/
https://snipt.net/raw/6ae348bffd5515cb0e73e3f94e5b6db5/
6ae348bffd5515cb0e73e3f94e5b6db5
python
Python
2
2016-09-29T22:53:50
True
False
False
/api/public/snipt/28373/
return-hexadecimal-representation-of-integer
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a></pre></div></td><td class="code"><div class="highlight"><pre><span id="L-1"><a name="L-1"></a><span class="k">def</span> <span class="nf">dec2hex</span><span class="p">(</span><span class="n">n</span><span class="p">):</span> </span><span id="L-2"><a name="L-2"></a> <span class="k">return</span> <span class="s">&quot;</span><span class="si">%X</span><span class="s">&quot;</span> <span class="o">%</span> <span class="n">n</span> </span></pre></div> </td></tr></table>
base, hex, python

Python

An elegant way to calculate a^n with positive integer values. O(log(n))

1
2
3
4
5
6
def mypow(a,n):
    """Returns base raised to the power exponent."""
    if n == 1:
        return a
    u = mypow(a,n/2)
    return a*u*u if n&1 else u*u 
https://snipt.net/embed/204fbe8091e51340ce4913238f43f8c0/
https://snipt.net/raw/204fbe8091e51340ce4913238f43f8c0/
204fbe8091e51340ce4913238f43f8c0
python
Python
6
2016-09-25T10:01:29
True
False
False
/api/public/snipt/20398/
an-elegant-way-to-calculate-an-with-integer-values-ologn
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a> <a href="#L-3">3</a> <a href="#L-4">4</a> <a href="#L-5">5</a> <a href="#L-6">6</a></pre></div></td><td class="code"><div class="highlight"><pre><span id="L-1"><a name="L-1"></a><span class="k">def</span> <span class="nf">mypow</span><span class="p">(</span><span class="n">a</span><span class="p">,</span><span class="n">n</span><span class="p">):</span> </span><span id="L-2"><a name="L-2"></a> <span class="sd">&quot;&quot;&quot;Returns base raised to the power exponent.&quot;&quot;&quot;</span> </span><span id="L-3"><a name="L-3"></a> <span class="k">if</span> <span class="n">n</span> <span class="o">==</span> <span class="mi">1</span><span class="p">:</span> </span><span id="L-4"><a name="L-4"></a> <span class="k">return</span> <span class="n">a</span> </span><span id="L-5"><a name="L-5"></a> <span class="n">u</span> <span class="o">=</span> <span class="n">mypow</span><span class="p">(</span><span class="n">a</span><span class="p">,</span><span class="n">n</span><span class="o">/</span><span class="mi">2</span><span class="p">)</span> </span><span id="L-6"><a name="L-6"></a> <span class="k">return</span> <span class="n">a</span><span class="o">*</span><span class="n">u</span><span class="o">*</span><span class="n">u</span> <span class="k">if</span> <span class="n">n</span><span class="o">&amp;</span><span class="mi">1</span> <span class="k">else</span> <span class="n">u</span><span class="o">*</span><span class="n">u</span> </span></pre></div> </td></tr></table>
a^n, base, exponent, pow, python, raise
Copyrighted, illegal, or inappropriate content? Email support@snipt.net.