-
Notifications
You must be signed in to change notification settings - Fork 1
/
code-golf-output-fit-numbers.html
717 lines (378 loc) · 38.4 KB
/
code-golf-output-fit-numbers.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
<!doctype html>
<html class="theme-next mist use-motion">
<head>
<meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
<link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />
<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.4.0" rel="stylesheet" type="text/css" />
<link href="/css/main.css?v=5.0.1" rel="stylesheet" type="text/css" />
<meta name="keywords" content="黑科技,Code Golf,编程," />
<link rel="alternate" href="/atom.xml" title="清风轩" type="application/atom+xml" />
<link rel="shortcut icon" type="image/x-icon" href="/favicon.ico?v=5.0.1" />
<meta name="description" content="爱下棋的朋友应该对“排局”这个词并不陌生,与“排局”一样,编程领域也有跟这个类似的好玩的东西:有一种故意出一种看起来不切合实际的问题,要求使用尽量短的代码来实现对应的功能。这个叫做“Code Golf”的黑科技却吸引着众多的程序员参与其中,今天我们就来看看其中的一个例子。">
<meta property="og:type" content="article">
<meta property="og:title" content="Code Golf - Output Fit Numbers">
<meta property="og:url" content="http://xovel.cn/article/code-golf-output-fit-numbers.html">
<meta property="og:site_name" content="清风轩">
<meta property="og:description" content="爱下棋的朋友应该对“排局”这个词并不陌生,与“排局”一样,编程领域也有跟这个类似的好玩的东西:有一种故意出一种看起来不切合实际的问题,要求使用尽量短的代码来实现对应的功能。这个叫做“Code Golf”的黑科技却吸引着众多的程序员参与其中,今天我们就来看看其中的一个例子。">
<meta property="og:updated_time" content="2016-11-18T07:00:40.139Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Code Golf - Output Fit Numbers">
<meta name="twitter:description" content="爱下棋的朋友应该对“排局”这个词并不陌生,与“排局”一样,编程领域也有跟这个类似的好玩的东西:有一种故意出一种看起来不切合实际的问题,要求使用尽量短的代码来实现对应的功能。这个叫做“Code Golf”的黑科技却吸引着众多的程序员参与其中,今天我们就来看看其中的一个例子。">
<script type="text/javascript" id="hexo.configuration">
var NexT = window.NexT || {};
var CONFIG = {
scheme: 'Mist',
sidebar: {"position":"left","display":"post"},
fancybox: true,
motion: true,
duoshuo: {
userId: 0,
author: '博主'
}
};
</script>
<title> Code Golf - Output Fit Numbers | 清风轩 </title>
</head>
<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">
<div style="display: none;">
<script src="http://s6.cnzz.com/stat.php?id=1259096563&web_id=1259096563" type="text/javascript"></script>
</div>
<div class="container one-collumn sidebar-position-left page-post-detail ">
<div class="headband"></div>
<header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
<div class="header-inner"><div class="site-meta ">
<div class="custom-logo-site-title">
<a href="/" class="brand" rel="start">
<span class="logo-line-before"><i></i></span>
<span class="site-title">清风轩</span>
<span class="logo-line-after"><i></i></span>
</a>
</div>
<p class="site-subtitle">清风轩居 - 引仙阁</p>
</div>
<div class="site-nav-toggle">
<button>
<span class="btn-bar"></span>
<span class="btn-bar"></span>
<span class="btn-bar"></span>
</button>
</div>
<nav class="site-nav">
<ul id="menu" class="menu">
<li class="menu-item menu-item-home">
<a href="/" rel="section">
<i class="menu-item-icon fa fa-fw fa-home"></i> <br />
首页
</a>
</li>
<li class="menu-item menu-item-categories">
<a href="/categories" rel="section">
<i class="menu-item-icon fa fa-fw fa-th"></i> <br />
分类
</a>
</li>
<li class="menu-item menu-item-tags">
<a href="/tags" rel="section">
<i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
标签
</a>
</li>
<li class="menu-item menu-item-archives">
<a href="/archives" rel="section">
<i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
归档
</a>
</li>
<li class="menu-item menu-item-about">
<a href="/about" rel="section">
<i class="menu-item-icon fa fa-fw fa-user"></i> <br />
关于
</a>
</li>
<li class="menu-item menu-item-garden">
<a href="/garden" rel="section">
<i class="menu-item-icon fa fa-fw fa-life-ring"></i> <br />
花园
</a>
</li>
</ul>
</nav>
</div>
</header>
<main id="main" class="main">
<div class="main-inner">
<div class="content-wrap">
<div id="content" class="content">
<div id="posts" class="posts-expand">
<article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
<header class="post-header">
<h1 class="post-title" itemprop="name headline">
Code Golf - Output Fit Numbers
</h1>
<div class="post-meta">
<span class="post-time">
<span class="post-meta-item-icon">
<i class="fa fa-calendar-o"></i>
</span>
<span class="post-meta-item-text">发表于</span>
<time itemprop="dateCreated" datetime="2016-08-18T17:12:50+08:00" content="2016-08-18">
2016-08-18
</time>
</span>
<span class="post-category" >
|
<span class="post-meta-item-icon">
<i class="fa fa-folder-o"></i>
</span>
<span class="post-meta-item-text">分类于</span>
<span itemprop="about" itemscope itemtype="https://schema.org/Thing">
<a href="/categories/essay/" itemprop="url" rel="index">
<span itemprop="name">随笔</span>
</a>
</span>
</span>
</div>
</header>
<div class="post-body" itemprop="articleBody">
<p>爱下棋的朋友应该对“排局”这个词并不陌生,与“排局”一样,编程领域也有跟这个类似的好玩的东西:有一种故意出一种看起来不切合实际的问题,要求使用尽量短的代码来实现对应的功能。这个叫做“Code Golf”的黑科技却吸引着众多的程序员参与其中,今天我们就来看看其中的一个例子。</p>
<a id="more"></a>
<blockquote>
<p>本文根据<a href="http://stackexchange.com/" target="_blank" rel="external">Stack Exchange</a>上的<a href="http://codegolf.stackexchange.com/questions/90067/output-fit-numbers" target="_blank" rel="external">code golf - Output “Fit” numbers</a>翻译/整理而成。</p>
</blockquote>
<h2 id="题目"><a href="#题目" class="headerlink" title="题目"></a>题目</h2><p>现有这个一个需求,编程实现<a href="https://oeis.org/A090078" target="_blank" rel="external">OEIS数列A090078</a>。</p>
<p>这里简单介绍一下数列<code>A090078</code>,从基数开始,将序号转为二进制,再将二进制中的连在一起的<code>0</code>进行合并,得到的新的二进制再转换为十进制,便就是该数列的值。</p>
<p>示例:<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line">1 -> 1 -> 1 -> 1</div><div class="line">9 -> 1001 -> 101 -> 5</div><div class="line">15 ->1111 -> 1111 -> 15</div><div class="line">13 ->1101 -> 1101 -> 13</div><div class="line">16 -> 10000 -> 10 -> 2</div><div class="line">17 -> 10001 -> 101 -> 5</div><div class="line">65535 -> 1111111111111111 -> 1111111111111111 -> 65535</div><div class="line">65000 -> 1111110111101000 -> 11111101111010 -> 16250</div></pre></td></tr></table></figure></p>
<p>我们姑且把这种转换称之为<code>fit</code>,得到的数值叫做<code>fit number</code>。那么上面提到的需求就可以描述为:</p>
<blockquote>
<p>给定一个值,求该值的<code>fit number</code>。</p>
</blockquote>
<p>下面就来看看各路大牛各显神通吧!</p>
<h2 id="代码实现"><a href="#代码实现" class="headerlink" title="代码实现"></a>代码实现</h2><h3 id="Bash-GNU-utilities"><a href="#Bash-GNU-utilities" class="headerlink" title="Bash + GNU utilities"></a>Bash + GNU utilities</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">dc -e2o?p|tr -s 0|dc -e2i?p</div></pre></td></tr></table></figure>
<p>仅27字节。</p>
<h3 id="05AB1E"><a href="#05AB1E" class="headerlink" title="05AB1E"></a>05AB1E</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">b00¬:C</div></pre></td></tr></table></figure>
<p>这简直黑科技中的黑科技,仅仅8个字节就实现了。<a href="http://05ab1e.tryitonline.net/#code=YjAwwqw6Qw&input=NjUwMDA" target="_blank" rel="external">在线测试</a>。</p>
<blockquote>
<p><a href="https://github.com/Adriandmen/05AB1E" target="_blank" rel="external">05AB1E</a>这门编程语言,本身也是娱乐品,有兴趣的可以点击访问其项目主页。</p>
</blockquote>
<h3 id="Jellyfish"><a href="#Jellyfish" class="headerlink" title="Jellyfish"></a>Jellyfish</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line">p</div><div class="line">d</div><div class="line"># S</div><div class="line">,1</div><div class="line">*</div><div class="line">\dbi</div><div class="line"> 2</div></pre></td></tr></table></figure>
<p>仅20个字节。又是一款黑魔法,<a href="https://github.com/iatorm/jellyfish" target="_blank" rel="external">Jellyfish</a>是一款来自于<code>J</code>语言的用<code>Python 3</code>编写的编程语言。</p>
<h3 id="JavaScript"><a href="#JavaScript" class="headerlink" title="JavaScript"></a>JavaScript</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">n=>+`0b${n.toString(2).replace(/0+/g,0)}`</div></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">n=>'0b'+n.toString(2).replace(/0+/g,0)-0</div></pre></td></tr></table></figure>
<p>这是ES6的语法,走了个捷径,不能算作是正确答案,<del>因为没有输出语句,看不到结果</del>,不过这里也贴出来。</p>
<h3 id="Bash-sed-bc"><a href="#Bash-sed-bc" class="headerlink" title="Bash (sed + bc)"></a>Bash (sed + bc)</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">echo $[2#`bc<<<obase=2\;$1|sed s/00\*/0/g`]</div></pre></td></tr></table></figure>
<h3 id="MATL"><a href="#MATL" class="headerlink" title="MATL"></a>MATL</h3><p>传说中的MATLAB,看看:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">BFFOZtXB</div></pre></td></tr></table></figure>
<p><strong>仅仅只有8个字节</strong></p>
<p>难以置信吧?看看其解读:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line"> % Implicitly grab input</div><div class="line">B % Convert decimal to binary</div><div class="line">FF % Create the array [0 0]</div><div class="line">O % Number literal</div><div class="line">Zt % Replaces all [0 0] with [0] (will replace any number of 0's with 0)</div><div class="line">XB % Convert binary to decimal</div><div class="line"> % Implicitly display</div></pre></td></tr></table></figure>
<p>可能你不信,那么请访问在<a href="http://matl.tryitonline.net/#code=Qm9GRk9adFhC&input=OQ" target="_blank" rel="external">线测试页面</a>。</p>
<p><del>BoFFOZtXB</del></p>
<p><del>o % Convert from logical to double (due to a bug)…有个双精度的BUG</del></p>
<h3 id="Python-3"><a href="#Python-3" class="headerlink" title="Python 3"></a>Python 3</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">import</span> re</div><div class="line">f=<span class="keyword">lambda</span> x:eval(re.sub(<span class="string">'0+'</span>,<span class="string">'0'</span>,bin(x)))</div></pre></td></tr></table></figure>
<p>简单直接并粗暴,50字节的Python解决方案奉上。</p>
<h3 id="Perl-6"><a href="#Perl-6" class="headerlink" title="Perl 6"></a>Perl 6</h3><figure class="highlight perl"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">{:<span class="number">2</span>(.base(<span class="number">2</span>)~~{S:g/<span class="number">0</span>+<span class="regexp">/0/</span>})}</div></pre></td></tr></table></figure>
<p>解释:<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div></pre></td><td class="code"><pre><div class="line">-> $_ {</div><div class="line"> # convert from base 2</div><div class="line"> :2(</div><div class="line"></div><div class="line"> # convert to base 2</div><div class="line"> $_.base(2)</div><div class="line"></div><div class="line"> # substitute</div><div class="line"> .subst(</div><div class="line"> :global,</div><div class="line"> / 0+ /, # all substrings made of 0s</div><div class="line"> '0' # with one 0</div><div class="line"> )</div><div class="line"> )</div><div class="line">}</div></pre></td></tr></table></figure></p>
<p>示例:<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div></pre></td><td class="code"><pre><div class="line">my &fit-compress = {:2(.base(2)~~{S:g/0+/0/})}</div><div class="line">say fit-compress 1; # 1</div><div class="line">say fit-compress 9; # 5</div><div class="line">say fit-compress 15; # 15</div><div class="line">say fit-compress 13; # 13</div><div class="line">say fit-compress 16; # 2</div><div class="line">say fit-compress 17; # 5</div><div class="line">say fit-compress 65535; # 65535</div><div class="line">say fit-compress 65000; # 16250</div><div class="line"></div><div class="line"># number created with 「:2( [~] <0 1>.roll: 256 )」</div><div class="line">say fit-compress 80794946326210692074631955353531749442835289622757526957697718534769445507500</div><div class="line"># 4240335298301026395935723255481812004519990428936918</div></pre></td></tr></table></figure></p>
<h3 id="Ruby"><a href="#Ruby" class="headerlink" title="Ruby"></a>Ruby</h3><figure class="highlight ruby"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">->n{eval (<span class="string">"0b%b"</span>%n).squeeze <span class="string">?0</span>}</div></pre></td></tr></table></figure>
<p>Ruby的解决方案,31字节实现,<a href="https://repl.it/CnnQ/2" target="_blank" rel="external">在线测试</a>。</p>
<h3 id="Jelly"><a href="#Jelly" class="headerlink" title="Jelly"></a>Jelly</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">BŒgḄBFḄ</div></pre></td></tr></table></figure>
<p>黑科技无疑,<del>代码里面有叫不出名字的字母</del>。<a href="http://jelly.tryitonline.net/#code=QsWSZ-G4hEJG4biE&input=&args=NjUwMDA" target="_blank" rel="external">在线测试</a>。</p>
<p>这个<code>Jelly</code>是个什么鬼?请参阅其项目主页:<a href="https://github.com/DennisMitchell/jelly" target="_blank" rel="external">DennisMitchell/jelly</a>。</p>
<h3 id="PHP"><a href="#PHP" class="headerlink" title="PHP"></a>PHP</h3><figure class="highlight php"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="meta"><?</span>=bindec(preg_replace(<span class="string">"/0+/"</span>,<span class="number">0</span>,decbin($argv[<span class="number">1</span>])));</div></pre></td></tr></table></figure>
<p>PHP作为一个有逼格的编程语言,自然不能示弱,51字节实现,<del>虽然跟Javascript一样很尴尬</del>。</p>
<h3 id="C"><a href="#C" class="headerlink" title="C"></a>C</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">int x(int x)=>Convert.ToInt32(Regex.Replace(Convert.ToString(x,2),"0+","0"),2);</div></pre></td></tr></table></figure>
<h3 id="Java"><a href="#Java" class="headerlink" title="Java"></a>Java</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="function"><span class="keyword">int</span> <span class="title">f</span><span class="params">(Integer x)</span></span>{<span class="keyword">return</span> x.valueOf(x.toString(x,<span class="number">2</span>).replaceAll(<span class="string">"0+"</span>,<span class="string">"0"</span>),<span class="number">2</span>);}</div></pre></td></tr></table></figure>
<p>java不甘寂寞,也来一份,但也只有语法实现,并不能愉快的直接测试。</p>
<p>测试代码:<br><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Fit</span> </span>{</div><div class="line"> <span class="function"><span class="keyword">int</span> <span class="title">f</span><span class="params">(Integer x)</span></span>{<span class="keyword">return</span> x.valueOf(x.toString(x,<span class="number">2</span>).replaceAll(<span class="string">"0+"</span>,<span class="string">"0"</span>),<span class="number">2</span>);}</div><div class="line"></div><div class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(<span class="keyword">final</span> String... args)</span> </span>{</div><div class="line"> <span class="keyword">final</span> Fit x = <span class="keyword">new</span> Fit();</div><div class="line"> System.out.println(x.f(<span class="number">65000</span>));</div><div class="line"> }</div><div class="line">}</div></pre></td></tr></table></figure></p>
<p>这里还有一份很短的Java代码:<br><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="class"><span class="keyword">interface</span> <span class="title">C</span></span>{<span class="function"><span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[]b)</span></span>{Integer i=<span class="number">0</span>;System.out.print(i.parseInt(i.toString(i.parseInt(b[<span class="number">0</span>]),<span class="number">2</span>).replaceAll(<span class="string">"0+"</span>,<span class="string">"0"</span>),<span class="number">2</span>));}}</div></pre></td></tr></table></figure></p>
<h3 id="PowerShell-v2"><a href="#PowerShell-v2" class="headerlink" title="PowerShell v2+"></a>PowerShell v2+</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[convert]::ToInt32(([convert]::ToString($args[0],2)-replace'0+',0),2)</div></pre></td></tr></table></figure>
<p>年长的PowerShell同样的不敢寂寞,然而这测试过程也是有点无奈:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line">PS C:\Tools\Scripts\golfing> 1,9,15,13,16,17,65535,65000|%{"$_ -> " +(.\output-fit-number.ps1 $_)}</div><div class="line">1 -> 1</div><div class="line">9 -> 5</div><div class="line">15 -> 15</div><div class="line">13 -> 13</div><div class="line">16 -> 2</div><div class="line">17 -> 5</div><div class="line">65535 -> 65535</div><div class="line">65000 -> 16250</div></pre></td></tr></table></figure>
<h3 id="Pyth"><a href="#Pyth" class="headerlink" title="Pyth"></a>Pyth</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">i:.BQ"0+"\02</div></pre></td></tr></table></figure>
<p>这什么语言瞅着眼熟的很,然而这并不是<code>Python</code>,该语言的项目主页:<a href="https://github.com/isaacg1/pyth" target="_blank" rel="external">Pyth</a>。</p>
<p>代码解释:<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line"> .BQ # Convert input from base 10 to base 2</div><div class="line"> : "0+"\0 # Replace multiple zeroes with single zero</div><div class="line">i 2 # Convert back from base 2 to base 10</div></pre></td></tr></table></figure></p>
<p><a href="https://pyth.herokuapp.com/?code=i%3A.BQ%220%2B%22%5C02&input=65000&test_suite=1&test_suite_input=1%0A9%0A15%0A13%0A16%0A17%0A65535%0A65000&debug=0" target="_blank" rel="external">在线测试</a>。</p>
<h3 id="Retina"><a href="#Retina" class="headerlink" title="Retina"></a>Retina</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line">.+</div><div class="line">$*1;</div><div class="line">+`(1+)\1</div><div class="line">$1;</div><div class="line">1;</div><div class="line">1</div><div class="line">;+</div><div class="line">0</div></pre></td></tr></table></figure>
<p>讲真,我已经目瞪口呆。<a href="https://github.com/m-ender/retina" target="_blank" rel="external">Retina</a>似乎又是一款自编编程语言。<a href="http://retina.tryitonline.net/#code=LisKJCoxOworYCgxKylcMQokMTsKMTsKMQo7Kwow&input=NjUwMDA" target="_blank" rel="external">在线测试</a>。</p>
<h3 id="Dyalog-APL"><a href="#Dyalog-APL" class="headerlink" title="Dyalog APL"></a>Dyalog APL</h3><p><a href="http://goo.gl/9KrKoM" target="_blank" rel="external">Dyalog APL</a>来露个脸,19字节:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">{2⊥⍵/⍨~0 0⍷⍵}2∘⊥⍣¯1</div></pre></td></tr></table></figure>
<p><del>明显黑科技</del></p>
<h3 id="CJam"><a href="#CJam" class="headerlink" title="CJam"></a>CJam</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">q~2b1+0a%0a*);2b</div></pre></td></tr></table></figure>
<p><a href="https://sourceforge.net/projects/cjam/" target="_blank" rel="external">CJam</a>,<a href="http://cjam.aditsu.net/#code=q~2b1%2B0a%250a*%29%3B2b&input=65000" target="_blank" rel="external">在线测试</a>。</p>
<p>代码解释:<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line">q~ read and evaluate the input number</div><div class="line">2b convert to base 2 (array of 1s and 0s)</div><div class="line">1+ append a 1 to deal with trailing zeros</div><div class="line">0a% split by [0], dropping empty pieces; only chunks of 1s are left</div><div class="line">0a* join by [0]</div><div class="line">); discard the 1 we appended before</div><div class="line">2b convert back from base 2</div></pre></td></tr></table></figure></p>
<h2 id="后记"><a href="#后记" class="headerlink" title="后记"></a>后记</h2><p><del>抄了这么多的编程语言的实现之后</del>,颇有一种井底之蛙的感觉。还有很多编程语言也可以很简洁的实现该需求,这里就不再继续贴出了,有兴趣的可自行访问<a href="http://codegolf.stackexchange.com/questions/90067/output-fit-numbers" target="_blank" rel="external">该页面的源地址</a>。</p>
<p>全文完~</p>
</div>
<div>
</div>
<div>
</div>
<footer class="post-footer">
<div class="post-tags">
<a href="/tags/黑科技/" rel="tag">#黑科技</a>
<a href="/tags/Code-Golf/" rel="tag">#Code Golf</a>
<a href="/tags/编程/" rel="tag">#编程</a>
</div>
<div class="post-nav">
<div class="post-nav-next post-nav-item">
<a href="/article/webkit-filter.html" rel="next" title="-webkit-filter冰山一角">
<i class="fa fa-chevron-left"></i> -webkit-filter冰山一角
</a>
</div>
<div class="post-nav-prev post-nav-item">
<a href="/article/i-am-lost-in-the-colorful-world.html" rel="prev" title="迷失在这五彩斑斓的世界">
迷失在这五彩斑斓的世界 <i class="fa fa-chevron-right"></i>
</a>
</div>
</div>
</footer>
</article>
<div class="post-spread">
</div>
</div>
</div>
<div class="comments" id="comments">
<div class="ds-thread" data-thread-key="article/code-golf-output-fit-numbers.html"
data-title="Code Golf - Output Fit Numbers" data-url="http://xovel.cn/article/code-golf-output-fit-numbers.html">
</div>
</div>
</div>
<div class="sidebar-toggle">
<div class="sidebar-toggle-line-wrap">
<span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
<span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
<span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
</div>
</div>
<aside id="sidebar" class="sidebar">
<div class="sidebar-inner">
<ul class="sidebar-nav motion-element">
<li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap" >
文章目录
</li>
<li class="sidebar-nav-overview" data-target="site-overview">
站点概览
</li>
</ul>
<section class="site-overview sidebar-panel ">
<div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
<img class="site-author-image" itemprop="image"
src="/images/xovel.jpg"
alt="xovel" />
<p class="site-author-name" itemprop="name">xovel</p>
<p class="site-description motion-element" itemprop="description">缓缓之间,风云静变</p>
</div>
<nav class="site-state motion-element">
<div class="site-state-item site-state-posts">
<a href="/archives">
<span class="site-state-item-count">46</span>
<span class="site-state-item-name">日志</span>
</a>
</div>
<div class="site-state-item site-state-categories">
<a href="/categories">
<span class="site-state-item-count">15</span>
<span class="site-state-item-name">分类</span>
</a>
</div>
<div class="site-state-item site-state-tags">
<a href="/tags">
<span class="site-state-item-count">117</span>
<span class="site-state-item-name">标签</span>
</a>
</div>
</nav>
<div class="feed-link motion-element">
<a href="/atom.xml" rel="alternate">
<i class="fa fa-rss"></i>
RSS
</a>
</div>
<div class="links-of-author motion-element">
<span class="links-of-author-item">
<a href="https://github.com/xovel" target="_blank" title="github">
<i class="fa fa-fw fa-github"></i>
github
</a>
</span>
<span class="links-of-author-item">
<a href="http://hdk4.com" target="_blank" title="hdk4.com">
<i class="fa fa-fw fa-adjust"></i>
hdk4.com
</a>
</span>
</div>
<div class="cc-license motion-element" itemprop="license">
<a href="http://creativecommons.org/licenses/by-nc-sa/4.0" class="cc-opacity" target="_blank">
<img src="/images/cc-by-nc-sa.svg" alt="Creative Commons" />
</a>
</div>
</section>
<section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
<div class="post-toc">
<div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#题目"><span class="nav-number">1.</span> <span class="nav-text">题目</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#代码实现"><span class="nav-number">2.</span> <span class="nav-text">代码实现</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#Bash-GNU-utilities"><span class="nav-number">2.1.</span> <span class="nav-text">Bash + GNU utilities</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#05AB1E"><span class="nav-number">2.2.</span> <span class="nav-text">05AB1E</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Jellyfish"><span class="nav-number">2.3.</span> <span class="nav-text">Jellyfish</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#JavaScript"><span class="nav-number">2.4.</span> <span class="nav-text">JavaScript</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Bash-sed-bc"><span class="nav-number">2.5.</span> <span class="nav-text">Bash (sed + bc)</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#MATL"><span class="nav-number">2.6.</span> <span class="nav-text">MATL</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Python-3"><span class="nav-number">2.7.</span> <span class="nav-text">Python 3</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Perl-6"><span class="nav-number">2.8.</span> <span class="nav-text">Perl 6</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Ruby"><span class="nav-number">2.9.</span> <span class="nav-text">Ruby</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Jelly"><span class="nav-number">2.10.</span> <span class="nav-text">Jelly</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#PHP"><span class="nav-number">2.11.</span> <span class="nav-text">PHP</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#C"><span class="nav-number">2.12.</span> <span class="nav-text">C</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Java"><span class="nav-number">2.13.</span> <span class="nav-text">Java</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#PowerShell-v2"><span class="nav-number">2.14.</span> <span class="nav-text">PowerShell v2+</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Pyth"><span class="nav-number">2.15.</span> <span class="nav-text">Pyth</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Retina"><span class="nav-number">2.16.</span> <span class="nav-text">Retina</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Dyalog-APL"><span class="nav-number">2.17.</span> <span class="nav-text">Dyalog APL</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#CJam"><span class="nav-number">2.18.</span> <span class="nav-text">CJam</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#后记"><span class="nav-number">3.</span> <span class="nav-text">后记</span></a></li></ol></div>
</div>
</section>
</div>
</aside>
</div>
</main>
<footer id="footer" class="footer">
<div class="footer-inner">
<div class="copyright" >
©
<span itemprop="copyrightYear">2016</span>
<span class="with-love">
<i class="fa fa-heart"></i>
</span>
<span class="author" itemprop="copyrightHolder">xovel</span>
</div>
<div class="powered-by">
由 <a class="theme-link" href="http://hexo.io" rel="external nofollow" target="_blank">Hexo</a> 强力驱动
</div>
<div class="theme-info">
主题 -
<a class="theme-link" href="https://github.com/iissnan/hexo-theme-next" rel="external nofollow" target="_blank">
NexT.Mist
</a>
</div>
</div>
</footer>
<div class="back-to-top">
<i class="fa fa-arrow-up"></i>
</div>
</div>
<script type="text/javascript">
if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
window.Promise = null;
}
</script>
<script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
<script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
<script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
<script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
<script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
<script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
<script type="text/javascript" src="/js/src/utils.js?v=5.0.1"></script>
<script type="text/javascript" src="/js/src/motion.js?v=5.0.1"></script>
<script type="text/javascript" src="/js/src/scrollspy.js?v=5.0.1"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.0.1"></script>
<script type="text/javascript" src="/js/src/bootstrap.js?v=5.0.1"></script>
<script type="text/javascript">
var duoshuoQuery = {short_name:"xovel"};
(function() {
var ds = document.createElement('script');
ds.type = 'text/javascript';ds.async = true;
ds.id = 'duoshuo-script';
ds.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') + '//static.duoshuo.com/embed.js';
ds.charset = 'UTF-8';
(document.getElementsByTagName('head')[0]
|| document.getElementsByTagName('body')[0]).appendChild(ds);
})();
</script>
</body>
</html>