Vier ist die magische Zahl

Ich habe die Sandbox nicht überprüft, bevor ich diese Herausforderung gepostet habe – Es sieht so aus, als ob diese Herausforderung von Cᴏɴᴏʀ O „Bʀɪᴇɴ vorgeschlagen wurde.

Schreiben Sie bei einer ganzzahligen Eingabe ein Programm, das die“ vier „druckt ist eine magische Zahl „Rätsel

  • Vier ist die magische Zahl
  • Fünf ist vier und vier ist die magische Zahl
  • Sechs ist drei und drei ist fünf und fünf ist vier und vier ist die magische Zahl
  • Elf ist sechs und sechs ist drei und drei ist fünf und fünf ist vier und vier ist die magische Zahl
  • Fünfhundert ist elf und elf ist sechs und sechs ist drei und drei ist fünf und fünf ist vier und vier ist die magische Zahl

Wenn Sie das Rätsel bereits kennen oder zu faul sind, um es zu lösen Um herauszufinden, was das Rätsel ist, finden Sie hier eine Erklärung.

Die nächste Zahl ist die Anzahl der Buchstaben in der vorherigen Zahl. Also, für e Beispiel: fünf hat vier Buchstaben, daher lautet die nächste Zahl vier .

sechs hat drei Buchstaben, daher lautet die nächste Zahl 3 und drei hat fünf Buchstaben, daher lautet die nächste Zahl 5 und fünf hat vier Buchstaben, daher lautet die nächste Zahl 4

Der Grund, warum das Rätsel bei vier endet, ist, dass vier vier Buchstaben hat und vier vier und vier vier und vier vier sind … (vier ist die magische Zahl )

Testfälle

0 => Zero is four and four is the magic number 1 => One is three and three is five and five is four and four is the magic number 2 => Two is three and three is five and five is four and four is the magic number 3 => Three is five and five is four and four is the magic number 4 => Four is the magic number 5 => Five is four and four is the magic number 6 => Six is three and three is five and five is four and four is the magic number 7 => Seven is five and five is four and four is the magic number 8 => Eight is five and five is four and four is the magic number 9 => Nine is four and four is the magic number 10 => Ten is three and three is five and five is four and four is the magic number 17 => Seventeen is nine and nine is four and four is the magic number 100 => One Hundred is ten and ten is three and three is five and five is four and four is the magic number 142 => One Hundred Forty Two is eighteen and eighteen is eight and eight is five and five is four and four is the magic number 1,000 => One Thousand is eleven and eleven is six and six is three and three is five and five is four and four is the magic number 1,642 => One Thousand Six Hundred Forty Two is twenty nine and twenty nine is ten and ten is three and three is five and five is four and four is the magic number 70,000 => Seventy Thousand is fifteen and fifteen is seven and seven is five and five is four and four is the magic number 131,072 => One Hundred Thirty One Thousand Seventy Two is thirty seven and thirty seven is eleven and eleven is six and six is three and three is five and five is four and four is the magic number 999,999 => Nine Hundred Ninety Nine Thousand Nine Hundred Ninety Nine is fifty and fifty is five and five is four and four is the magic number 

Regeln

  • Die Eingabe kann entweder von STDIN oder als Argument für eine Funktion erfolgen tion
  • Die Eingabe ist eine positive Zahl zwischen 0 und 999.999
  • Die Eingabe enthält nur Zahlen (sie folgt dem regulären Ausdruck ^[0-9]+$)
  • Die Eingabe kann entweder als Ganzzahl oder als Zeichenfolge verwendet werden.
  • Bei der Konvertierung in eine Wortzeichenfolge sollten Leerzeichen und Bindestriche nicht in die Zeichenfolge aufgenommen werden count (100 [Einhundert] besteht aus 10 Zeichen, nicht aus 11. 1.742 [Eintausendsiebenhundertzweiundvierzig] besteht aus 31 Zeichen, nicht aus 36)
  • Bei der Konvertierung in eine Zeichenfolge sollten 100 Einhundert sein. Nicht Hundert oder Hundert, 1000 sollten Tausend sein, nicht Tausend oder Tausend.
  • Bei der Konvertierung in eine Zeichenfolge 142 sollte Einhundertzweiundvierzig sein, nicht Einhundert und Vierzig Zwei
  • Die Ausgabe ist ohne Berücksichtigung der Groß- / Kleinschreibung und sollte dem Format “ N ist K und K ist M und M ist … und vier ist die magische Zahl „(es sei denn, die Eingabe ist 4, in diesem Fall sollte die Ausgabe einfach“ vier ist die magische Zahl „sein)
  • Die Ausgabe kann Zahlen anstelle von Buchstaben verwenden („5 ist 4 und 4 ist die magische Zahl“ anstelle von „fünf ist vier und vier ist die magische Zahl“), solange Ihr Programm immer konsistent ist
  • Die Ausgabe kann entweder der Rückgabewert einer Funktion sein oder an STDOUT
  • Standardschlupflöcher gelten
  • Dies ist , sodass das kürzeste Programm in Bytes gewinnt. Viel Glück!

Bonus

-30 Bytes if Das Programm funktioniert, wenn die Eingabe zwischen -999.999 und 999.999 liegt.

Negative Zahlen haben bei der Konvertierung in Wörter nur „negative“ vor sich. Zum Beispiel ist -4 „Negative Four“, Negative Four ist zwölf und zwölf ist sechs und sechs ist drei und drei ist fünf und fünf ist vier und vier ist die magische Zahl

-150 Byte , wenn das Programm keine integrierten Funktionen zum Generieren verwendet Die Zeichenfolgendarstellung der Nummer

Bestenliste

Dies ist ein Stapelausschnitt, der sowohl eine Bestenliste als auch eine Übersicht der Gewinner nach Sprache generiert.

Um sicherzustellen, dass Ihre Antwort angezeigt wird, beginnen Sie Ihre Antwort mit einer Überschrift unter Verwendung der folgenden Markdown-Vorlage.

## Language Name, N bytes 

Wobei N die Größe ist , in Bytes, von Ihrer Übermittlung

Wenn Sie mehrere Zahlen in Ihren Header aufnehmen möchten (z. B. durch alte Punktzahlen streichen oder Flags in die Byteanzahl aufnehmen), stellen Sie einfach sicher, dass die tatsächliche Punktzahl ist die letzte Nummer in Ihrem Header

## Language Name, <s>K</s> X + 2 = N bytes 
 var QUESTION_ID=67344;var OVERRIDE_USER=20634;function answersUrl(e){return"http://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"http://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}#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> <h2>Leaderboard</h2> <table class="answer-list"> <thead> <tr><td></td><td>Author</td><td>Language</td><td>Size</td></tr></thead> <tbody> </tbody> </table> </div><div> <h2>Winners by Language</h2> <table class="language-list"> <thead> <tr><td>Language</td><td>User</td><td>Score</td></tr></thead> <tbody> </tbody> </table> </div><table style="display: none"> <tbody> <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> <tr><td>{{LANGUAGE}}</td><td>{{NAME}}</td><td>{{SIZE}}</td><td><a href="{{LINK}}">Link</a></td></tr></tbody> </table> 

Kommentare

  • Gibt es eine maximal mögliche Eingabe?
  • Überprüfen Sie in Zukunft auch die Sandbox, um festzustellen, ob

    jemand anderes hatte Ihre Idee .

  • @El ' endiaStarman Okay, ich habe Text an der hinzugefügt Anfang der Herausforderung, die mit diesem Beitrag verknüpft ist
  • Ihr integrierter Funktionsbonus sollte eher -150 bis -200 Byte betragen.
  • I ' Ich werde das einfach da rauswerfen – selbst die wahnsinnig optimierten Konverter von Zahlen zu Namen werden für die meisten Sprachen kaum weniger als 150 Bytes kosten, da die -150 derzeit eher eine Falle als ein Bonus ist.

Antwort

Bash + allgemeine Dienstprogramme (einschließlich BSD-Spiele), 123 – 30 = 93 Bytes

 for((n=$1;n-4;n=m)){ m=`number -l -- $n|sed "s/nus/&&/;s/\W//g"` s+="$n is $[m=${#m}] and " } echo $s 4 is the magic number  

Zum Glück die Ausgabe der bsd-Spiele Das Dienstprogramm number ist fast genau das, was wir brauchen. Die Ausgabenummern werden alle numerisch und nicht in Worten gemäß dem 8. Aufzählungspunkt geschrieben:

$ ./4magic.sh 131072 131072 is 37 and 37 is 11 and 11 is 6 and 6 is 3 and 3 is 5 and 5 is 4 and 4 is the magic number $ ./4magic.sh -4 -4 is 12 and 12 is 6 and 6 is 3 and 3 is 5 and 5 is 4 and 4 is the magic number $ 

Antwort

C, 263 261 Bytes – 180 = 81

 char*i="jmmonnmoonmpprrqqsrrjjddeeecdd",x;f(n,c){return!n?n:n<0?f(-n,8):n<100?c+i[n<20?n:n%10]-i[20+n/10]:f(n/1000,8)+f(n/100%10,7)+f(n/100%10,0)+c;}main(int c,char**v){for(c=atoi(*++v);c-4;c=x)printf("%d is %d and ",c,x=c?f(c,0)):4;puts("4 is the magic number");}  

Inspiriert von der Antwort von Cole Cameron . Ich dachte, ich könnte es ohne die Makrodefinition besser machen. Obwohl ich es schließlich geschafft habe, hat es einige Zeit gedauert, bis ich es geschafft habe!

Es erfordert einen Host-Zeichensatz mit aufeinanderfolgenden Buchstaben (ASCII ist also in Ordnung, aber EBCDIC funktioniert nicht). Das ist für das Paar von Nachschlagetabellen. Ich habe j als Nullzeichen gewählt und zwei Lookups benötigt, damit ich eins vom anderen subtrahieren kann, anstatt meine Null von beiden subtrahieren zu müssen.

Kommentierte Version:

 char*i= "jmmonnmoon" /* 0 to 9 */ "mpprrqqsrr" /* 10 to 19 */ "jjddeeecdd"; /* tens */ char x; /* current letter count */ f(n,c){ return !n?n /* zero - return 0 (ignore c) */ :n<0?f(-n,8) /* negative n (only reached if c==0) */ :n<100?c+i[n<20?n:n%10]-i[20+n/10] /* lookup tables */ : f(n/1000,8) /* thousand */ + f(n/100%10,7) /* hundred */ + f(n%100,0) /* rest */ + c; /* carry-in */ } main(int c, char**v) { for(c=atoi(*++v);c-4;c=x) printf("%d is %d and ",c,x=c?f(c,0):4); puts("4 is the magic number"); }  

Es gibt eine offensichtliche Erweiterung, um Millionen zu unterstützen durch Ersetzen von f(n/1000,8) durch f(n/1000000,7)+f(n/1000%1000,8).

Testausgabe

0 is 4 and 4 is the magic number 1 is 3 and 3 is 5 and 5 is 4 and 4 is the magic number 2 is 3 and 3 is 5 and 5 is 4 and 4 is the magic number 3 is 5 and 5 is 4 and 4 is the magic number 4 is the magic number 5 is 4 and 4 is the magic number 6 is 3 and 3 is 5 and 5 is 4 and 4 is the magic number 7 is 5 and 5 is 4 and 4 is the magic number 8 is 5 and 5 is 4 and 4 is the magic number 9 is 4 and 4 is the magic number 10 is 3 and 3 is 5 and 5 is 4 and 4 is the magic number 17 is 9 and 9 is 4 and 4 is the magic number 100 is 10 and 10 is 3 and 3 is 5 and 5 is 4 and 4 is the magic number 142 is 18 and 18 is 8 and 8 is 5 and 5 is 4 and 4 is the magic number 1000 is 11 and 11 is 6 and 6 is 3 and 3 is 5 and 5 is 4 and 4 is the magic number 1642 is 29 and 29 is 10 and 10 is 3 and 3 is 5 and 5 is 4 and 4 is the magic number 70000 is 15 and 15 is 7 and 7 is 5 and 5 is 4 and 4 is the magic number 131072 is 37 and 37 is 11 and 11 is 6 and 6 is 3 and 3 is 5 and 5 is 4 and 4 is the magic number 999999 is 50 and 50 is 5 and 5 is 4 and 4 is the magic number 

Antwort

Mathematica, 156 – 30 = 126 Byte

a=ToString;({a@#," is ",a@#2," and "}&@@@Partition[NestWhileList[#~IntegerName~"Words"~StringCount~LetterCharacter&,#,#!=4&],2,1])<>"4 is the magic number"& 

Ich bin einfach überrascht, dass dies Zeichenfolgen verwendet und nicht lächerlich lang ist.

Antwort

Swift 2 , 408 419 – 30 = 389 Bytes

würde ich bekommen können 176 Bytes entfernen, wenn Swift nicht so ausführlich mit regulären Ausdrücken war (Entfernen von Bindestrichen und Leerzeichen) * starrt Apple an *

 func c(var s:Int)->String{var r="";while(s != 4){r+="\(s)";let f=NSNumberFormatter();f.numberStyle=NSNumberFormatterStyle.SpellOutStyle;let v=f.stringFromNumber(s)!;s=v.stringByReplacingOccurrencesOfString("[- ]",withString:"",options:NSStringCompareOptions.RegularExpressionSearch,range:Range<String.Index>(start:v.startIndex,end:v.endIndex)).utf8.count+(s<0 ?3:0);r+=" is \(s) and "};return r+"4 is the magic number"}  

Dies kann auf swiftstub.com hier

Ich habe eine kleine for-Schleife ausgeführt, und es stellt sich heraus, dass 100003 die Zahl zwischen 0 und 999999 ist und das längste Zeichenfolgenergebnis mit 6 Iterationen und

aufweist

100003 ist 23 und 23 ist 11 und 11 ist 6 und 6 ist 3 und 3 ist 5 und 5 ist 4 und 4 ist die magische Zahl

Ungolfed

 func a(var s: Int) -> String{ var r = "" while(s != 4){ r+="\(s)" let f = NSNumberFormatter() f.numberStyle = NSNumberFormatterStyle.SpellOutStyle let v = f.stringFromNumber(s)! s = v.stringByReplacingOccurrencesOfString( "[- ]", withString: "", options: NSStringCompareOptions.RegularExpressionSearch, range: Range<String.Index>(start: v.startIndex, end: v.endIndex) ).utf8.count + (s < 0 ? 3 : 0) r+=" is \(s) and " } return r+"4 is the magic number" }  

Kommentare

  • NSStringCompareOptions.RegularExpressionSearch Und ich dachte JS ' s String.fromCharCode war ausführlich. : P
  • Python und Ruby: string.replace.Swift: String.stringByReplacingOccurrencesOfString

Antwort

Haskell, 285 – 180 = 105 Bytes

Tatsächlich ist überhaupt keine integrierte Anzeige für die Anzeige von Nummern vorhanden. Ich bin immer noch unzufrieden mit der Punktzahl. Fühlen Sie sich frei zu kommentieren. Ich werde jedoch weiter experimentieren. Trotzdem ist die Punktzahl besser als die von Swift

 c n|n<0=8+c(-n)|n>999=r 1000+8|n>99=7+r 100|n>19=r 10+2-g[30..59]+g[20..29]|n>15=r 10-1|2>1=[0,3,3,5,4,4,3,5,5,4,3,6,6,8,8,7]!!n where{g=fromEnum.elem n;r k=c(mod n k)+c(div n k)} m 4="4 is the magic number" m 0="0 is 4 and "++m 4 m n=show n++" is "++show(c n)++" and "++m(c n)  

Verwendung

 m 7 "7 is 5 and 5 is 4 and 4 is the magic number" m 999999 "999999 is 50 and 50 is 5 and 5 is 4 and 4 is the magic number"  

Erläuterung.

m ist trivial genug, die c jedoch nicht. c ist die Funktion zum Zählen der Zeichenanzahl des englischen Namens der Anzahl.

c n |n<0=8+c(-n) -- Add word "negative" in front of it, the length is 8 |n>999=r 1000+8 -- the english name for number with form xxx,yyy is xxx thousand yyy |n>99=7+r 100 -- the english name for number with form xyy is x hundred yy |n>19=r 10+2-g[30..59]+g[20..29] -- the english name for number with form xy with x more -- than 1 is x-ty. However *twoty>twenty, -- *threety>thirty, *fourty>forty, *fivety>fifty. |n>10=r 10-1-g(15:18:[11..13]) -- the english name for number with form 1x is x-teen. -- However, *oneteen>eleven, *twoteen>twelve, -- *threeteen>thirteen, *fiveteen>fifteen, -- *eightteen>eighteen |2>1=[0,3,3,5,4,4,3,5,5,4,3]!!n -- for number 0-10, the length is memorized. 0 is 0 -- because it is omitted. Input zero is handled -- separately. If we defined 0 to be 4, then -- 20 => twenty zero. where g =fromEnum.elem n -- Check if n is element of argument array, if true, 1 else 0 r k=c(mod n k)+c(div n k) -- Obvious. 

Kommentare

  • Oh ja? Nun, Swift hat … ähm. .. eine höhere Punktzahl … (Ich ' bin nicht sehr gut bei Comebacks)

Antwort

C, 268 – 180 = 88 Byte

 #define t(x,o)n<x?o:f(n/x)+(n%x?f(n%x):0) char*i="4335443554366887798866555766";f(n){return t(1000,t(100,n<20?n<0?8+f(-n):i[n]-48:i[n/10+18]-48+(n%10?f(n%10):0))+7)+8;}main(n){for(scanf("%d",&n);n^4;n=f(n))printf("%d is %d and ",n,f(n));puts("4 is the magic number");}  

Probieren Sie es aus hier .

Ungolfed

 /* Encode number length in string (shorter representation than array) */ char*i="4335443554366887798866555766"; f(n) { return n < 1000 ? n < 100 ? n < 20 ? n < 0 ? 8 + f(-n) /* "Negative x" */ : i[n] - 48 /* "x" */ : i[n/10+18] + (n%10 ? f(n%10) : 0) /* 20-99 */ : f(n/100) + (n%100 ? f(n%100) : 0) + 7 /* x hundred y */ : f(n/1000) + (n%1000 ? f(n%1000) : 0) + 8; /* x thousand y */ } main(n) { /* Keep printing until you get to the magic number */ for(scanf("%d",&n);n^4;n=f(n)) printf("%d is %d and ",n,f(n)); puts("4 is the magic number"); }  

Antwort

Java, 800 – 150 = 650 Bytes

class G{static String e="",i="teen",j="ty",k="eigh",y="thir",d="zero",l="one",n="two",m="three",h="four",s="five",c="six",t="seven",b=k+"t",g="nine",D="ten",L="eleven",N="twelve",M=y+i,H=h+i,S="fif"+i,C=c+i,T=t+i,B=k+i,G=g+i,o="twen"+j,p=y+j,q="for"+j,r="fif"+j,u=c+j,v=t+j,w=k+j,x=g+j,A=" ",O=" hundred ",z,E;public static void main(String a[]){z=e;int l=new Integer(a[0]);do{E=a(l,1,e);l=E.replace(A,e).length();z=z+E+" is "+a(l,1,e)+" and ";}while(l!=4);System.out.print(z+h+" is the magic number");}static String a(int P,int _,String Q){String[]f={e,l,n,m,h,s,c,t,b,g,D,L,N,M,H,S,C,T,B,G,e,D,o,p,q,r,u,v,w,x};int R=20,X=10,Y=100,Z=1000;return P==0?(_>0?d:e):(P<R?f[P]+Q:(P<Y?(f[R+(P/X)]+" "+a(P%X,0,e)).trim()+Q:(P<Z?a(P/Y,0,O)+a(P%Y,0,e)+Q:a(P/Z,0," thousand ")+a((P/Y)%X,0,O)+a(P%Y,0,e)+Q)));}} 

De-golfed

class G { static String e="",i="teen",j="ty",k="eigh",y="thir",d="zero",l="one",n="two",m="three",h="four",s="five",c="six",t="seven",b=k+"t",g="nine",D="ten",L="eleven",N="twelve",M=y+i,H=h+i,S="fif"+i,C=c+i,T=t+i,B=k+i,G=g+i,o="twen"+j,p=y+j,q="for"+j,r="fif"+j,u=c+j,v=t+j,w=k+j,x=g+j,A=" ",O=" hundred ",z,E; public static void main(String a[]){ z = e; int l = new Integer(a[0]); do { E = a(l,1,e); l = E.replace(A,e).length(); z = z+E+" is "+a(l,1,e)+" and "; } while(l!=4); System.out.println(z+h+" is the magic number"); } static String a(int P,int _,String Q) { String[] f = {e,l,n,m,h,s,c,t,b,g,D,L,N,M,H,S,C,T,B,G,e,D,o,p,q,r,u,v,w,x}; int R=20,X=10,Y=100,Z=1000; return P==0?(_>0?d:e):(P<R?f[P]+Q:(P<Y?(f[R+(P/X)]+" "+a(P%X,0,e)).trim()+Q:(P<Z?a(P/Y,0,O)+a(P%Y,0,e)+Q:a(P/Z,0," thousand ")+ a((P/Y)%X,0,O)+a(P%Y,0,e)+Q))); } } 

Kommentare

  • Ich weiß, dass ' mehr als ein Jahr her ist, aber Sie können einige Klammern in dieser ternären Zuordnung entfernen sowie ==0 bis <1. Also: return P<1?_>0?d:e:P<R?f[P]+Q:P<Y?(f[R+(P/X)]+" "+a(P%X,0,e)).trim()+Q:P<Z?a(P/Y,0,O)+a(P%Y,0,e)+Q:a(P/Z,0," thousand ")+a((P/Y)%X,0,O)+a(P%Y,0,e)+Q; ( – 10 Bytes )

Antwort

QC, 265 – 30 – 150 = 85 Byte

(✵1:oaT%=ta100%=ha100/⌋T%=X[0 3 3 5 4 4 3 5 5 4 3 6 6 8 8 7 7 9 8 8]=Y[6 6 5 5 5 7 6 6]=a0≟4a20<Xt☌YtT/⌋2-☌Xo☌+▲▲hXh☌7+0▲+)(❆1:na0<8*=ba‖1000/⌋=ca1000%=nbb✵8+0▲a✵++){I4≠:EEI" is "++=II❆=EEI" and "++=E!}E"4 is the magic number"+ 

Testsuite

Ungolfed:

(✵1: oaT%= # ones ta100%= # tens ha100/⌋T%= # hundreds X[0 3 3 5 4 4 3 5 5 4 3 6 6 8 8 7 7 9 8 8]= # length of "zero", "one", "two", ..., "nineteen" Y[6 6 5 5 5 7 6 6]= # length of "twenty", ..., "ninety" a0≟ 4 a20< Xt☌ YtT/⌋2-☌ Xo☌ + ▲ ▲ hXh☌7+0▲+) (❆1: na0<8*= # if negative, add 8 ba‖1000/⌋= # split aaaaaa into bbbccc ca1000%= n bb✵8+0▲ a✵ ++) {I4≠:EEI" is "++=II❆=EEI" and "++=E!}E"4 is the magic number"+ 

Kommentare

  • Wenn Sie ' keine integrierte Funktion zum Abrufen der Länge des verwenden Sie können also tatsächlich weitere 150 von Ihrer Punktzahl abziehen.

Antwort

JavaScript, 382 – 150 – 30 = 202 Byte

var o=[0,3,3,5,4,4,3,5,5,4],f=s=>(s[1]==1?[3,6,6,8,8,7,7,9,8,8][s[0]]:o[s[0]]+(s.length>1?[0,3,6,6,5,5,5,7,6,6][s[1]]:0))+(s.length==3?(7+o[s[2]]-(o[s[2]]==0?7:0)):0),l=n=>{var s=(""+n).split("").reverse();return f(s.slice(0,3))+(s.length>3?(f(s.slice(3,6))+8):0)};(n=>{var s="";while(n!=4){s+=n+" is ";n=n>=0?l(n):(l(-n)+8);s+=n+" and ";}console.log(s+"4 is the magic number");})() 

Die Eingabe wird als Parameter für den sofort aufgerufenen Funktionsausdruck angegeben.

Testeingabe:

999999 -> 999999 is 50 and 50 is 5 and 5 is 4 and 4 is the magic number 17 -> 17 is 9 and 9 is 4 and 4 is the magic number -404 -> -404 is 23 and 23 is 11 and 11 is 6 and 6 is 3 and 3 is 5 and 5 is 4 and 4 is the magic number 

De-golfed:

// array of the lengths of digits in ones place: // one is 3, two is 3, three is 5, etc... zero is a special case // and is assigned zero length because zero is never written out in a number name var o=[0,3,3,5,4,4,3,5,5,4], // function that computes the length of a substring of the input // because the input is 6 digits, it can be broken into two 3 digit subsections // each of which can have it"s length calculated separately f=s=> ( s[1]==1? // check for if the tens digit is a one // when the tens is a one, pull the string length from an array that represents // ten, eleven, twelve, thirteen, etc... [3,6,6,8,8,7,7,9,8,8][s[0]] : // when the tens digit is not a one, add the ones digit normally and... o[s[0]] + // add the tens digit length from the array that represents // zero, ten, twenty, thirty, forty, fifty, sixty, seventy, eighty, ninety (s.length>1?[0,3,6,6,5,5,5,7,6,6][s[1]]:0) ) + ( s.length==3? // check if the length is 3 and weren"t not accidentally trying to do something wierd with a minus sign // if so, then we have to add a hundred (7 characters) to the length and the // length of the ones digit that is in the hundreds place like // "one" hundred or "two" hundred (7+o[s[2]]- ( // also, if the hundreds place was a zero, subtract out those 7 characters // that were added because "hundred" isn"t added if there"s a zero in its // place o[s[2]]==0? 7 : 0 ) ) : // if the length wasn"t 3, then don"t add anything for the hundred 0 ), // function that computes the length of the whole six digit number l=n=>{ // coerce the number into a string and then reverse the string so that the // ones digit is the zeroth element instead of last element var s=(""+n).split("").reverse(); return // calculate the character length of the first 3 characters // like in the number 999888, this does the "888" f(s.slice(0,3)) + // then if there actually are any characters after the first 3 (s.length>3? // parse the character length of the second 3 characters (f(s.slice(3,6))+8) : 0 ) }; // lastly is the Immediately-Invoked Function Expression (n=>{ var s=""; // as long as we haven"t reached four, just keep going through the loop while(n!=4){ s+=n+" is "; n=n>=0?l(n):(l(-n)+8) // this handles negatives by only passing positive values to l and then just adding 8 onto the length for negatives s+=n+" and "; } // finally just say that "4 is the magic number" console.log(s+"4 is the magic number"); })(999999) 

Antwort

Python 641-150 = 501 Bytes

Zumindest ist es nicht länger als Java! Es basiert auf dies außer Verwendung von Zeichenfolgen.

BEARBEITEN : I. Ich habe 0 vergessen und muss sagen, dass " 5 4 " ist. Springe nicht zu " 4 ist die magische Zahl " – die der Punktzahl etwas hinzugefügt hat.

w={0:"zero",1:"one",2:"two",3:"three",4:"four",5:"five",6:"six",7:"seven",8:"eight",9:"nine",10:"ten",11:"eleven",12:"twelve",13:"thirteen",14:"fourteen",15:"fifteen",16:"sixteen",17:"seventeen",18:"eighteen",19:"nineteen",20:"twenty",30:"thirty",40:"forty",50:"fifty",60:"sixty",70:"seventy",80:"eighty",90:"ninety"} s="" def i(n): global s e="" o=n%10 t=n%100 h=n/100%10 th=n/1000 if th: e+=i(th) e+="thousand" if h: e+=w[h] e+="hundred" if t: if t<20 or o==0: e+=w[t] else: e+=w[t-o] e+=w[o] if len(e)==4:s+="4 is the magic number";print s else: s+="%d is %d and "%(n,len(e));i(len(e)) In=input() i(In) 

Probieren Sie es hier aus!

Kommentare

  • Sie haben ' muss den Namen nicht anzeigen, oder?
  • Dies ist falsch. i(5) druckt 4 is the magic number anstelle von 5 is 4 and 4 is the magic number.

Antwort

PHP, 168 – 30 = 138 Byte

function m($i){$e=strlen(preg_replace("/[^a-z-]/","",(new NumberFormatter("en",5))->format($i)));echo($i==$e?"":"$i is $e and "),($e==4?"4 is the magic number":m($e));} 

Antwort

Moo, 182 176 / 192 188 Bytes – 30 = 146/158

188-Byte-Version:

u=$string_utils;s="";i=args[1];while(i-4)j=u:english_number(i);s=s+j+(s?" and "+j|"")+" is ";i=length(u:strip_chars(j,"- "}));endwhile;return s+(s?"four and "|"")+"four is the magic number" 

176 Byte implementierungsabhängig Version:

s="";i=args[1];while(i-4)j=#20:english_number(i);s=s+j+(s?" and "+j|"")+" is ";i=length(#20:strip_chars(j," -"));endwhile;return s+(s?"four and "|"")+"four is the magic number" 

Beide sind Funktionen.

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.