νƒœκ·Έ 보관물: code-golf

code-golf

μ΅œλ‹¨ μžλ™ 파괴 루프 ν”„λ‘œκ·Έλž¨μ„ λ°œμƒν•˜μ§€

λ‹Ήμ‹ μ˜ μž‘μ—…μ€ μ–΄λ–€ μž…λ ₯을지지 μ•ŠμŠ΅λ‹ˆλ‹€ 및 루프 (λͺ¨λ“  μ’…λ₯˜μ˜ μ‹€ν–‰λ˜λŠ” 전체 ν”„λ‘œκ·Έλž¨μ΄λ‚˜ κΈ°λŠ₯을 μž‘μ„±ν•˜λŠ” κ²ƒμž…λ‹ˆλ‹€ while, for, foreach, do, do-while, do-loop, gotoν”„λ‘œκ·Έλž¨μ΄ μ‹€ν–‰ 자체λ₯Ό 쀑지해야 함을 μ˜λ―Έν•©λ‹ˆλ‹€ 였λ₯˜λ₯Ό μΌμœΌν‚€λŠ” 원인이 μ’…λ£Œλ©λ‹ˆλ‹€, μž¬κ·€ λ“±) 좜ꡬ.

κ·œμΉ™ :

  1. 였λ₯˜λŠ” λŸ°νƒ€μž„ 였λ₯˜, μ²˜λ¦¬λ˜μ§€ μ•Šμ€ μ˜ˆμ™Έ λ˜λŠ” ν”„λ‘œκ·Έλž¨ 자체λ₯Ό μ’…λ£Œμ‹œν‚€λŠ” λͺ¨λ“  ν•­λͺ©μ΄μ–΄μ•Όν•©λ‹ˆλ‹€.
  2. 였λ₯˜λŠ” exit;νŠΉμ • μ‹œμ μ—μ„œ λͺ…μ‹œ 적으둜 (λ˜λŠ” 이에 μƒμ‘ν•˜λŠ”) ν˜ΈμΆœν•˜μ§€ μ•Šκ³  ν”„λ‘œκ·Έλž¨μ„ μ€‘μ§€ν•˜κ³  μ’…λ£Œν•΄μ•Όν•©λ‹ˆλ‹€ .
  3. 같은 λ©”μ‹œμ§€ Warning:, Notice:자체 μ’…λ£Œ ν”„λ‘œκ·Έλž¨μ„ λ°œμƒν•˜μ§€ μ•ŠλŠ” λ“±, μœ νš¨ν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€. 예λ₯Ό λ“€μ–΄, PHP 0으둜 λ‚˜λˆ„λ©΄ Warningλ©”μ‹œμ§€κ°€ 생성 λ˜μ§€λ§Œ ν”„λ‘œκ·Έλž¨μ΄ μ€‘μ§€λ˜μ§€ μ•Šκ³  계속 μ‹€ν–‰λ©λ‹ˆλ‹€. μ΄λŠ” μ˜¬λ°”λ₯Έ 닡변이 μ•„λ‹™λ‹ˆλ‹€.
  4. λ£¨ν”„λŠ” ν•˜λ‚˜ μ΄μƒμ˜ 전체주기λ₯Ό μ‹€ν–‰ν•΄μ•Όν•©λ‹ˆλ‹€. 즉, 두 번째 μ‚¬μ΄ν΄μ—μ„œ μ‹œμž‘ν•˜μ—¬ 였λ₯˜κ°€ λ°œμƒν•  수 μžˆμŠ΅λ‹ˆλ‹€. μ΄λŠ” 잘λͺ»λœ μ½”λ“œ ꡬ문을 μ‚¬μš©ν•˜μ—¬ 였λ₯˜κ°€ λ°œμƒν•˜μ§€ μ•Šλ„λ‘ν•˜κΈ°μœ„ν•œ κ²ƒμž…λ‹ˆλ‹€. μ½”λ“œλŠ” ꡬ문 상 μ •ν™•ν•΄μ•Όν•©λ‹ˆλ‹€.
  5. 루프가 for(;;);μœ„μ— μ–ΈκΈ‰ 된 κ·œμΉ™μ„ μ€€μˆ˜ν•˜λŠ” 경우 λ¬΄ν•œλŒ€ (예 πŸ™‚ 일 수 μžˆμ§€λ§Œ λŸ°νƒ€μž„ 였λ₯˜λ‘œ 인해 자체 μ’…λ£Œν•˜λŠ” 데 2 ​​뢄 이상 걸리지 μ•Šμ•„μ•Όν•©λ‹ˆλ‹€.
  6. Tail Call Optimizationμ΄μ—†λŠ” μž¬κ·€λŠ” μœ νš¨ν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€ ( 1 , 2 ).
  7. 이것은 μ΄λ―€λ‘œ κ°€μž₯ 짧은 μ½”λ“œκ°€ μŠΉλ¦¬ν•©λ‹ˆλ‹€.
  8. ν‘œμ€€ ν—ˆμ  은 κΈˆμ§€λ˜μ–΄ μžˆμŠ΅λ‹ˆλ‹€.

C # 예제 ( 온라인 ν…ŒμŠ€νŠΈ ) :

using System;
public class Program {
    public static void Main() {
        int i;
        int[] n;
        n = new int[5];
        for(i=0; i<7; i++) {
            n[i] = i;
            Console.WriteLine(n[i]);
        }
    }
}


Output:

0
1
2
3
4
Run-time exception (line 9): Index was outside the bounds of the array.

Stack Trace:

[System.IndexOutOfRangeException: Index was outside the bounds of the array.]
  at Program.Main(): line 9

리더 λ³΄λ“œ :

var QUESTION_ID=104323,OVERRIDE_USER=59718;function answersUrl(e){return"https://api.stackexchange.com/2.2/questions/"+QUESTION_ID+"/answers?page="+e+"&pagesize=100&order=desc&sort=creation&site=codegolf&filter="+ANSWER_FILTER}function commentUrl(e,s){return"https://api.stackexchange.com/2.2/answers/"+s.join(";")+"/comments?page="+e+"&pagesize=100&order=desc&sort=creation&site=codegolf&filter="+COMMENT_FILTER}function getAnswers(){jQuery.ajax({url:answersUrl(answer_page++),method:"get",dataType:"jsonp",crossDomain:!0,success:function(e){answers.push.apply(answers,e.items),answers_hash=[],answer_ids=[],e.items.forEach(function(e){e.comments=[];var s=+e.share_link.match(/\d+/);answer_ids.push(s),answers_hash[s]=e}),e.has_more||(more_answers=!1),comment_page=1,getComments()}})}function getComments(){jQuery.ajax({url:commentUrl(comment_page++,answer_ids),method:"get",dataType:"jsonp",crossDomain:!0,success:function(e){e.items.forEach(function(e){e.owner.user_id===OVERRIDE_USER&&answers_hash[e.post_id].comments.push(e)}),e.has_more?getComments():more_answers?getAnswers():process()}})}function getAuthorName(e){return e.owner.display_name}function process(){var e=[];answers.forEach(function(s){var r=s.body;s.comments.forEach(function(e){OVERRIDE_REG.test(e.body)&&(r="<h1>"+e.body.replace(OVERRIDE_REG,"")+"</h1>")});var a=r.match(SCORE_REG);a&&e.push({user:getAuthorName(s),size:+a[2],language:a[1],link:s.share_link})}),e.sort(function(e,s){var r=e.size,a=s.size;return r-a});var s={},r=1,a=null,n=1;e.forEach(function(e){e.size!=a&&(n=r),a=e.size,++r;var t=jQuery("#answer-template").html();t=t.replace("{{PLACE}}",n+".").replace("{{NAME}}",e.user).replace("{{LANGUAGE}}",e.language).replace("{{SIZE}}",e.size).replace("{{LINK}}",e.link),t=jQuery(t),jQuery("#answers").append(t);var o=e.language;/<a/.test(o)&&(o=jQuery(o).text()),s[o]=s[o]||{lang:e.language,user:e.user,size:e.size,link:e.link}});var t=[];for(var o in s)s.hasOwnProperty(o)&&t.push(s[o]);t.sort(function(e,s){return e.lang>s.lang?1:e.lang<s.lang?-1:0});for(var c=0;c<t.length;++c){var i=jQuery("#language-template").html(),o=t[c];i=i.replace("{{LANGUAGE}}",o.lang).replace("{{NAME}}",o.user).replace("{{SIZE}}",o.size).replace("{{LINK}}",o.link),i=jQuery(i),jQuery("#languages").append(i)}}var ANSWER_FILTER="!t)IWYnsLAZle2tQ3KqrVveCRJfxcRLe",COMMENT_FILTER="!)Q2B_A2kjfAiU78X(md6BoYk",answers=[],answers_hash,answer_ids,answer_page=1,more_answers=!0,comment_page;getAnswers();var SCORE_REG=/<h\d>\s*([^\n,]*[^\s,]),.*?(\d+)(?=[^\n\d<>]*(?:<(?:s>[^\n<>]*<\/s>|[^\n<>]+>)[^\n\d<>]*)*<\/h\d>)/,OVERRIDE_REG=/^Override\s*header:\s*/i;
body{text-align:left!important;font-family:Arial,Helvetica; font-size:12px}#answer-list,#language-list{padding:10px;width:290px;float:left}table thead{font-weight:700}table td{padding:5px}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <link rel="stylesheet" type="text/css" href="//cdn.sstatic.net/codegolf/all.css?v=83c949450c8b"> <div id="answer-list"> <h2>Leaderboard</h2> <table class="answer-list"> <thead> <tr><td></td><td>Author</td><td>Language</td><td>Size</td></tr></thead> <tbody id="answers"> </tbody> </table> </div><div id="language-list"> <h2>Winners by Language</h2> <table class="language-list"> <thead> <tr><td>Language</td><td>User</td><td>Score</td></tr></thead> <tbody id="languages"> </tbody> </table> </div><table style="display: none"> <tbody id="answer-template"> <tr><td>{{PLACE}}</td><td>{{NAME}}</td><td>{{LANGUAGE}}</td><td>{{SIZE}}</td><td><a href="{{LINK}}">Link</a></td></tr></tbody> </table> <table style="display: none"> <tbody id="language-template"> <tr><td>{{LANGUAGE}}</td><td>{{NAME}}</td><td>{{SIZE}}</td><td><a href="{{LINK}}">Link</a></td></tr></tbody> </table>

리더 λ³΄λ“œ 슀 λ‹ˆνŽ«μ„ μœ„ν•œ Martin Ender μ—κ²Œ κ°μ‚¬ν•©λ‹ˆλ‹€



λ‹΅λ³€

MATL , 5 1 λ°”μ΄νŠΈ

@MartinEnder의 CJam λ‹΅λ³€ μ—μ„œ κ°€μ Έμ˜¨ 아이디어

`

온라인으둜 μ‚¬μš©ν•΄λ³΄μ‹­μ‹œμ˜€!

`    % Do...while loop
     % Implicit end. The loop continues if the top of the stack is true.
     % After the first iteration, since the stack is empty, the program
     % implicitly tries to take some non-existing input, and finishes
     % with an error

ꡬ 버전

2:t"x

온라인으둜 μ‚¬μš©ν•΄λ³΄μ‹­μ‹œμ˜€!

2:   % Push [1 2]
t    % Duplicate
"    % For each (i.e. do the following twice)
  x  %   Delete top of the stack. Works the first time. The second it tries to
     %   implicitly take some non-existing input, and finishes with an error

λ‹΅λ³€

파이썬, 16 λ°”μ΄νŠΈ

λΉ„ 관심 0 λΆ€λ¬Έ μ ‘κ·Ό :

for x in 1,0:x/x

첫 번째 λ°˜λ³΅μ€μ„ 계산 1 / 1ν•˜μ—¬ μ •μƒμ μœΌλ‘œ μž‘λ™ν•©λ‹ˆλ‹€. 두 번째 λ°˜λ³΅μ€ 계산을 μ‹œλ„ν•˜μ—¬ 0 / 0κ²°κ³Ό ZeroDivisionErrorκ°€ λ°œμƒν•©λ‹ˆλ‹€.

17 λ°”μ΄νŠΈ (개인 μ„ ν˜Έ)

i=1
while i:del i

μ²˜μŒμ—λŠ” i=1μ§„μ‹€ν•˜κΈ° λ•Œλ¬Έμ— 루프가 μ‹œμž‘λ©λ‹ˆλ‹€.

루프가 처음 싀행될 λ•Œ λ³€μˆ˜ iκ°€ μ‚­μ œλ©λ‹ˆλ‹€.

μ΄λŠ” 두 번째 i둜 더 이상 λ³€μˆ˜κ°€ μ•„λ‹ˆλ―€λ‘œ 평가가 μ‹€νŒ¨ 함을 μ˜λ―Έν•©λ‹ˆλ‹€.NameError: name 'i' is not defined.


νŒŒμ΄μ¬μ€ 꼬리 μž¬κ·€λ₯Ό μ΅œμ ν™”ν•˜μ§€ μ•ŠκΈ° λ•Œλ¬Έμ— 또 λ‹€λ₯Έ 15 λ°”μ΄νŠΈ μ†”λ£¨μ…˜μ€ def _():_()(newline) _()μž…λ‹ˆλ‹€. κ·ΈλŸ¬λ‚˜ 이것은 κ·œμΉ™ # 6을 μœ„λ°˜ν•©λ‹ˆλ‹€.


λ‹΅λ³€

저리 , 3 2 λ°”μ΄νŠΈ

Ṿß

λ©”λͺ¨λ¦¬κ°€ λΆ€μ‘±ν•˜μ—¬ 자체λ₯Ό μ’…λ£Œν•©λ‹ˆλ‹€. ~ 100 초 후에 λ‘œμ»¬μ—μ„œ μˆ˜ν–‰ν•©λ‹ˆλ‹€.

온라인으둜 μ‚¬μš©ν•΄λ³΄μ‹­μ‹œμ˜€! ( 디버그 λ“œλ‘œμ–΄μ˜ 사망 μΈμ¦μ„œ )

μž‘λ™ 원리

Ṿß  Main link. Argument: x. Implicit first argument: 0

αΉΎ   Uneval; yield a string representation of x.
 ß  Recursively call the main link.
    Jelly uses TCO, so the first cycle finishes successfully before entering
    the next one.

처음 λͺ‡ 번의 λ°˜λ³΅μ€ λ‹€μŒκ³Ό κ°™μŠ΅λ‹ˆλ‹€.

'0'
'”0'
'””,”0'
'””,””,”,,””,”0'
'””,””,”,,””,””,”,,””,”,,”,,””,””,”,,””,”0'
'””,””,”,,””,””,”,,””,”,,”,,””,””,”,,””,””,”,,””,”,,”,,””,””,”,,””,”,,”,,””,”,,”,,””,””,”,,””,””,”,,””,”,,”,,””,””,”,,””,”0'

κ·Έ ν›„, 그것은 μΆ”μ•…ν•˜κ³  μ§„μ§œ λΉ λ¦…λ‹ˆλ‹€.


λ‹΅λ³€

V , 2 λ°”μ΄νŠΈ

Γ²l

온라인으둜 μ‚¬μš©ν•΄λ³΄μ‹­μ‹œμ˜€!

λ‚΄κ°€ 항상 κ·Έλ ‡κ²Œν–ˆκΈ° λ•Œλ¬Έμ— 이것은 V에 λŒ€ν•œ μ™„λ²½ν•œ λ„μ „μž…λ‹ˆλ‹€! μ‹€μ œλ‘œ Vμ—λŠ” 쑰건이 μ—†μœΌλ©° 였λ₯˜κ°€ λ°œμƒν•˜λŠ” κΈ°λŠ₯ 만 μžˆμŠ΅λ‹ˆλ‹€. 이 경우 Γ²β€œμ˜μ›νžˆ λ°˜λ³΅β€μ΄λΌλŠ” lμ˜λ―Έμ™€ β€œμ˜€λ₯Έμͺ½μœΌλ‘œ μ΄λ™β€μ΄λΌλŠ” μ˜λ―Έμž…λ‹ˆλ‹€.

빈 버퍼 (μž…λ ₯ μ—†μŒ)μ—μ„œλŠ” 첫 번째 νŒ¨μŠ€μ—μ„œ μ€‘λ‹¨λ˜κ³  좜λ ₯이 μƒμ„±λ˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€. μž…λ ₯ μ΄μžˆλŠ” 경우 μž…λ ₯의 λ§ˆμ§€λ§‰ 문자λ₯Ό 이동 ν•œ ν›„ μž…λ ₯이 μ€‘λ‹¨λ˜κ³  λͺ¨λ“  μž…λ ₯이 좜λ ₯λ©λ‹ˆλ‹€ (고양이 ν”„λ‘œκ·Έλž¨μ΄κΈ°λ„ 함)


λ‹΅λ³€

μžλ°” 슀크립트 (ES6), 13 λ°”μ΄νŠΈ

f=_=>f(_?a:1)

이 κΈ°λŠ₯은 μž¬κ·€ ν•¨μˆ˜λ‘œ ν•œ 번 μ‹€ν–‰ 된 λ‹€μŒ λ˜μ§€κ³  ReferenceError: a is not definedμ’…λ£Œλ©λ‹ˆλ‹€.

λ‹€μŒμ€ 15 λ°”μ΄νŠΈ λΉ„ ES6 λ²„μ „μž…λ‹ˆλ‹€.

for(i=0;;)i=i.a

이것은 ν•œ 번 잘 μ‹€ν–‰ 된 λ‹€μŒ λ˜μ§€κ³  TypeError: i is undefinedμ’…λ£Œλ©λ‹ˆλ‹€.


λ‹΅λ³€

배쉬 4.2, 22 λ°”μ΄νŠΈ

exec $0 $@ $[2**$#%-1]

Bash 4.3이 있기 λ•Œλ¬Έμ— TIOμ—μ„œ μž‘λ™ν•˜μ§€ μ•ŠμœΌλ©° λ‚΄κ°€ μ˜μ‘΄ν•˜λŠ” 버그가 λ§ˆμΉ¨λ‚΄ μˆ˜μ •λ˜μ—ˆμŠ΅λ‹ˆλ‹€.

확인

$ xxd -c 22 -g 22 self-destruct
0000000: 6578656320243020244020245b322a2a2423252d315d  exec $0 $@ $[2**$#%-1]
$ ./self-destruct
Floating point exception

ν”„λ‘œκ·Έλž¨μ΄ 2 63 mod -1 을 κ³„μ‚°ν•˜λ €κ³  μ‹œλ„ ν•˜λ©΄ μ•Œλ €μ§„ λ²„κ·Έλ‘œ 인해 Bash 4.2 및 이전 λ²„μ „μ—μ„œ 좩돌이 λ°œμƒν•˜λ©΄ 좩돌이 λ°œμƒν•©λ‹ˆλ‹€ .


λ‹΅λ³€

PHP, 22 21 20 18 λ°”μ΄νŠΈ

이것은 PHP에 μ˜μ‘΄ν•˜μ—¬ λ³€μˆ˜μ— ν•¨μˆ˜ 이름을 λΆ€μ—¬ν•˜κ³  μ‹€ν–‰ν•˜λ €κ³ ν•©λ‹ˆλ‹€.

이것은 λ‹¨μˆœνžˆ piν•¨μˆ˜ 이름을 두 번 μ—°κ²°ν•©λ‹ˆλ‹€ . μ΄κ²ƒμ€λ‘œ PHPλ₯Ό μ£½ Fatal Error: Uncaught Error: Call to undefined function pipi() in [...][...]μž…λ‹ˆλ‹€.

while($x.=pi)$x();

이것은 λ‚˜μ˜ μ˜› λŒ€λ‹΅κ³Ό λΉ„μŠ·ν•˜κ²Œ μž‘λ™ν•©λ‹ˆλ‹€.


이전 λ‹΅λ³€, 20 λ°”μ΄νŠΈ

PHPμ—μ„œλŠ” 증가 μ—°μ‚°μžλ₯Ό μ‚¬μš©ν•˜μ—¬ 문자λ₯Ό μ¦κ°€μ‹œν‚¬ 수 μžˆμŠ΅λ‹ˆλ‹€. a-zλ²”μœ„ μ—μ„œλ§Œ μž‘λ™ ν•˜μ§€λ§Œ μΆ©λΆ„ν•©λ‹ˆλ‹€.

for($x=pi;;)$x=$x();

λ‚˜λŠ” 이것이 λͺ¨λ“  ν•„μˆ˜ 포인트λ₯Ό μΆ©μ‘±μ‹œν‚€κ³  루프가 ν•œ 번 μ‹€ν–‰λœλ‹€κ³  μƒκ°ν•©λ‹ˆλ‹€.

였λ₯˜κ°€ λ°œμƒν•˜κΈ° λ•Œλ¬Έμ— μ•Œ 수 μžˆμŠ΅λ‹ˆλ‹€ Fatal error: Function name must be a string.


이것이 μ–΄λ–»κ²Œ μž‘λ™ν•˜λŠ”μ§€ λ‹¨κ³„λ³„λ‘œ :

  • ν• λ‹Ή pi에 $x.
    이후 piμΌμ •μœΌλ‘œ μ‚¬μš©λ˜κ³ μžˆλŠ” 경우, PHP ν™•μΈν•œλ‹€.
    PHPλŠ” Use of undefined constant pi - assumed 'pi'μ‘΄μž¬ν•˜μ§€ μ•ŠκΈ° λ•Œλ¬Έμ— (기본적으둜 μƒμˆ˜κ°€ μ‘΄μž¬ν•˜μ§€ μ•ŠκΈ° λ•Œλ¬Έμ— λ¬Έμžμ—΄μ΄λΌκ³  κ°€μ •ν•©λ‹ˆλ‹€) κ²½κ³  λ©”μ‹œμ§€λ₯Ό ν‘œμ‹œν•©λ‹ˆλ‹€
  • 처음으둜 루프
    • κΈ°λŠ₯을 μ‹€ν–‰ν•˜μ‹­μ‹œμ˜€ $x().
      λ•Œλ¬Έμ— $x값을 κ°€μ§€κ³  pi,이 κΈ°λŠ₯을 μ‹€ν–‰ν•©λ‹ˆλ‹€ pi().
  • 에 값을 μ €μž₯ν•˜μ‹­μ‹œμ˜€ $x.
    $x이제 λŒ€μ‹ μ— Ο€λ₯Ό κ°–μŠ΅λ‹ˆλ‹€.pi
  • 두 번째 루프
    • κΈ°λŠ₯을 μ‹€ν–‰ν•˜μ‹­μ‹œμ˜€ $x().
      이후 $xΟ€ 있으며,이 κΈ°λŠ₯을 μ‹€ν–‰ν•©λ‹ˆλ‹€ 3.14159...().
    • Ο€λŠ” λ¬Έμžμ—΄μ΄ μ•„λ‹ˆλ―€λ‘œμ΄ μ‹œμ μ—μ„œμ„ μ‚¬μš©ν•˜μ—¬ ν”„λ‘œκ·Έλž¨μ„ μ’…λ£Œν•˜μ‹­μ‹œμ˜€ Fatal Error.

ν•¨μˆ˜ λ₯Ό μ°Ύμ•„μ„œ 1 λ°”μ΄νŠΈλ₯Ό μ ˆμ•½ ν•œ @Titus μ—κ²Œ κ°μ‚¬λ“œλ¦½λ‹ˆλ‹€ !pi()