工作学习中遇到过哪些让你觉得眼前一亮的代码,可以分享出来让大家学习,无论代码简单还是复杂,也不限语言,只要你觉得写法很巧妙,或者有新意,或者原来还可以这样,或者你从中学到了什么,都可以分享!(不一定要非常有技巧,看上去很美的简短代码都可以,并非只要酷炫罕见的代码)
<code class="language-js"><span class="s2">"★★★★★☆☆☆☆☆"</span><span class="p">.</span><span class="nx">substring</span><span class="p">(</span><span class="mi">5</span> <span class="o">-</span> <span class="nx">rating</span><span class="p">,</span> <span class="mi">10</span> <span class="o">-</span> <span class="nx">rating</span><span class="p">);</span> </code>
<code class="language-text">echo 15 > /sys/class/acpi_backlight=vendor/acpi_osi=Linux/brightness </code>
<code class="language-text">_='_=%r;print _%%_';print _%_ </code>
<code class="language-text">/* * 在IE下,document.write执行的顺序会错乱 * 假设使用document.write写入script标签,顺序为a1->a2->a3,并在a3中定义了变量hello * 则同样使用document.write写入b1脚本,无法读取hello * 必须使用至少对应的3层嵌套,形成b1->b2->b3顺序的document.write,在b3中读取hello变量才可行 * 因此,使用checkStatus函数自调用递归5层来读取全局的FOO变量 * * 如果在5层嵌套没到的情况下发现全局FOO变量已经被赋值,则可以提前退出 */ </code>
<code class="language-text">^1?$|^(11+)\1+$ </code>
<code class="language-js"><span class="p">[].</span><span class="nx">forEach</span><span class="p">.</span><span class="nx">call</span><span class="p">(</span><span class="nx">$$</span><span class="p">(</span><span class="s2">"*"</span><span class="p">),</span><span class="kd">function</span><span class="p">(</span><span class="nx">a</span><span class="p">){</span> <span class="nx">a</span><span class="p">.</span><span class="nx">style</span><span class="p">.</span><span class="nx">outline</span><span class="o">=</span><span class="s2">"1px solid #"</span><span class="o">+</span><span class="p">(</span><span class="o">~~</span><span class="p">(</span><span class="nb">Math</span><span class="p">.</span><span class="nx">random</span><span class="p">()</span><span class="o">*</span><span class="p">(</span><span class="mi">1</span><span class="o"><span class="mi">24</span><span class="p">))).</span><span class="nx">toString</span><span class="p">(</span><span class="mi">16</span><span class="p">)</span> <span class="p">})</span> </span></code>
void memcpy( char* to, char* from, size_t count )
{
size_t n = (count+7)/8;
switch( count%8 )
{
case 0: do{ *to++ = *from++;
case 7: *to++ = *from++;
case 6: *to++ = *from++;
case 5: *to++ = *from++;
case 4: *to++ = *from++;
case 3: *to++ = *from++;
case 2: *to++ = *from++;
case 1: *to++ = *from++;
}while(--n>0);
}
}
@装配脑袋 当年写过一个42个字符的python代码,打印代码自身。神技。 趣味程序:打印自己代码的程序 segmentation fault<code class="language-text">Python 一行打印小九九 </code>