snipt

Ctrl+h for KB shortcuts

Python

PowerStrings problem solved in one line

power = lambda s : re.match('^(\w+?)\\1+$', s) and (len(s) / len(re.match('^(\w+?)\\1+$', s).group(1))) or 1
https://snipt.net/embed/ef12a091faf3f4403074eff1ac87e548/
https://snipt.net/raw/ef12a091faf3f4403074eff1ac87e548/
ef12a091faf3f4403074eff1ac87e548
python
Python
1
2016-09-29T11:43:55
True
False
False
/api/public/snipt/8567/
powerstrings-problem-solved-in-one-line
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a></pre></div></td><td class="code"><div class="highlight"><pre><span id="L-1"><a name="L-1"></a><span class="n">power</span> <span class="o">=</span> <span class="k">lambda</span> <span class="n">s</span> <span class="p">:</span> <span class="n">re</span><span class="o">.</span><span class="n">match</span><span class="p">(</span><span class="s">&#39;^(\w+?)</span><span class="se">\\</span><span class="s">1+$&#39;</span><span class="p">,</span> <span class="n">s</span><span class="p">)</span> <span class="ow">and</span> <span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">s</span><span class="p">)</span> <span class="o">/</span> <span class="nb">len</span><span class="p">(</span><span class="n">re</span><span class="o">.</span><span class="n">match</span><span class="p">(</span><span class="s">&#39;^(\w+?)</span><span class="se">\\</span><span class="s">1+$&#39;</span><span class="p">,</span> <span class="n">s</span><span class="p">)</span><span class="o">.</span><span class="n">group</span><span class="p">(</span><span class="mi">1</span><span class="p">)))</span> <span class="ow">or</span> <span class="mi">1</span> </span></pre></div> </td></tr></table>
acm, power, strings
Copyrighted, illegal, or inappropriate content? Email support@snipt.net.