चार मैजिक नंबर है


26

मैंने इस चुनौती को पोस्ट करने से पहले सैंडबॉक्स की जांच नहीं की - ऐसा लगता है कि यह चुनौती Cʀɪᴇɴ O'Bʀɪᴇɴ द्वारा प्रस्तावित की गई थी ।

पूर्णांक इनपुट को देखते हुए, एक प्रोग्राम लिखें जो "चार एक जादू नंबर है" पहेली को प्रिंट करता है

  • चार मैजिक नंबर है
  • पांच चार है और चार मैजिक नंबर है
  • छह तीन है और तीन पांच है और पांच चार है और चार मैजिक नंबर है
  • ग्यारह साढ़े छह है और तीन तीन है और पांच पांच है और पांच चार है और चार जादू की संख्या है
  • पाँच सौ ग्यारह हैं और ग्यारह छह हैं और छह तीन है और तीन पाँच है और पाँच चार है और चार जादू की संख्या है

यदि आप पहले से ही पहेली को जानते हैं, या यह जानने के लिए उत्सुक हैं कि यह जानने के लिए उत्सुक हैं कि पहेली क्या है, तो यहाँ एक स्पष्टीकरण है

अगली संख्या पिछली संख्या के अक्षरों की संख्या है। इसलिए, उदाहरण के लिए, पाँच में चार अक्षर हैं, इसलिए अगली संख्या चार है

छह में तीन अक्षर होते हैं, इसलिए अगली संख्या 3 होती है , और तीन में पाँच अक्षर होते हैं, इसलिए अगली संख्या 5 होती है , और पाँच में चार अक्षर होते हैं, इसलिए अगली संख्या 4

होती है, क्योंकि चार पर पहेली खत्म होती है क्योंकि चार में चार अक्षर होते हैं , और चार चार है और चार चार है और चार चार है ... (चार जादू की संख्या है)

परीक्षण के मामलों

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

नियम

  • इनपुट या तो STDINकिसी फ़ंक्शन से तर्क के रूप में लिया जा सकता है
  • इनपुट 0 और 999,999 के बीच एक पॉजिटिव नंबर होगा
  • इनपुट में केवल नंबर होंगे (यह regex का अनुसरण करेगा ^[0-9]+$)
  • इनपुट को पूर्णांक या स्ट्रिंग के रूप में लिया जा सकता है
  • जब एक शब्द स्ट्रिंग में परिवर्तित हो जाता है, तो रिक्त स्थान और हाइफ़न को गिनती में शामिल नहीं किया जाना चाहिए (100 [एक सौ] 10 वर्ण हैं, न कि 11. 1,742 [एक हजार सात सौ चालीस -31] 31 वर्ण हैं, 36 नहीं)
  • जब एक स्ट्रिंग में परिवर्तित किया जाता है, तो 100 को एक सौ, एक सौ या सौ नहीं होना चाहिए, 1000 को एक हजार होना चाहिए, न कि एक हजार या हजार।
  • जब एक स्ट्रिंग के लिए परिवर्तित कर 142 एक सौ चालीस दो, नहीं एक सौ होना चाहिए और चालीस दो
  • उत्पादन होता है केस-संवेदी , और प्रारूप का पालन करना चाहिए " एन है कश्मीर और कश्मीर है एम और एम है ... और चार जादुई संख्या है" (जब तक इनपुट 4, है जो मामले में उत्पादन बस "होना चाहिए चार जादू नंबर है ")
  • जब तक आपका प्रोग्राम हमेशा संगत रहता है तब तक आउटपुट अक्षरों के बजाय संख्याओं का उपयोग कर सकता है ("5 है 4 और मैजिक नंबर है" के बजाय "फाइव इज़ फोर इज फोर मैजिक नंबर है")
  • आउटपुट या तो किसी फ़ंक्शन का रिटर्न मान हो सकता है, या प्रिंट कर सकता है STDOUT
  • मानक खामियां लागू होती हैं
  • यह , इसलिए बाइट्स में सबसे छोटा कार्यक्रम जीत जाता है। सौभाग्य!

बोनस

-30 बाइट्स यदि प्रोग्राम तब काम करता है जब इनपुट -999,999 और 999,999 के बीच होता है।

नकारात्मक संख्याएं, जब शब्दों में परिवर्तित होती हैं, तो बस उनके सामने "नकारात्मक" होता है। उदाहरण के लिए -4"नेगेटिव फोर" है, नेगेटिव फोर बारह है और बारह साढ़े छह है और साढ़े तीन है और पांच पांच है और पांच चार है और चार मैजिक नंबर है

-150 बाइट्स यदि प्रोग्राम संख्या के स्ट्रिंग प्रतिनिधित्व को उत्पन्न करने के लिए किसी भी अंतर्निहित कार्यों का उपयोग नहीं करता है

लीडरबोर्ड

यह एक स्टैक स्निपेट है जो लीडरबोर्ड और भाषा द्वारा विजेताओं के अवलोकन दोनों को उत्पन्न करता है।

अपने उत्तर को सुनिश्चित करने के लिए, कृपया निम्नलिखित मार्काडाउन टेम्पलेट का उपयोग करके अपना उत्तर शीर्षक से शुरू करें

## Language Name, N bytes

जहाँ N आकार में है, बाइट्स में, आपके सबमिशन का

यदि आप अपने हेडर में कई संख्याओं को शामिल करना चाहते हैं (उदाहरण के लिए, पुराने स्कोर के माध्यम से, या बाइट काउंट में झंडे सहित), तो बस यह सुनिश्चित कर लें कि वास्तविक शीर्षक आपके हेडर में अंतिम संख्या है।

## Language Name, <s>K</s> X + 2 = N bytes


क्या अधिकतम संभव इनपुट है?
आर्कटिकस

7
भविष्य में, कृपया सैंडबॉक्स को यह देखने के लिए भी देखें कि क्या आपके विचार में कोई और था
एलेंडिया स्टट्रान

@ El'endiaStarman ठीक है, मैंने उस पोस्ट को लिंक करने के लिए चुनौती के शीर्ष पर कुछ पाठ जोड़ा
जोजोड्मो

फंक्शन बोनस में आपका बनाया -150 से -200 बाइट्स की तरह होना चाहिए।
तन्मथ

1
मैं बस इसे बाहर फेंकने जा रहा हूं - यहां तक ​​कि कन्वर्टर्स के नाम के लिए सबसे पागलपन से अनुकूलित संख्या मुश्किल से अधिकांश भाषाओं के लिए 150 बाइट्स से भी कम है, क्योंकि यह खड़ा है -150 एक बोनस की तुलना में अधिक जाल है।
रिक्सी

जवाबों:


9

बैश + आम उपयोगिताओं (bsd- गेम सहित), 123 - 30 = 93 बाइट्स

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

सौभाग्य से bsd- गेम numberउपयोगिता से आउटपुट लगभग वही है जो हमें चाहिए। 8 वीं बुलेट बिंदु के अनुसार आउटपुट संख्या सभी संख्यात्मक रूप से लिखी जाती है और शब्दों में नहीं:

$ ./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
$ 

6

सी, 263 261 बाइट्स - 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");}

कोल कैमरन के जवाब से प्रेरित । मुझे लगा कि मैक्रो परिभाषा के बिना मैं बेहतर कर सकता हूं। हालाँकि मैं अंततः कामयाब रहा, लेकिन इसे हासिल करने में कुछ कमी लगी!

इसके लिए लगातार अक्षरों के साथ एक मेजबान वर्ण सेट की आवश्यकता होती है (इसलिए ASCII ठीक है, लेकिन EBCDIC काम नहीं करेगा)। यह लुकअप टेबल की जोड़ी के लिए है। मैंने jशून्य वर्ण के रूप में चुना , और दो लुक्स की आवश्यकता का लाभ उठाया, इसलिए मैं अपने शून्य को दोनों से घटाने के बजाय एक दूसरे से घटा सकता हूं।

टिप्पणी संस्करण:

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");
}

के f(n/1000,8)साथ बदलकर लाखों का समर्थन करने के लिए एक स्पष्ट विस्तार है f(n/1000000,7)+f(n/1000%1000,8)

परीक्षण उत्पादन

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

4

गणितज्ञ, 156 - 30 = 126 बाइट्स

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

मुझे बस आश्चर्य है कि यह तार का उपयोग करता है और हास्यास्पद रूप से लंबा नहीं है


4

स्विफ्ट 2 , 408 419 - 30 = 389 बाइट्स

मैं 176 बाइट्स से छुटकारा पा सकता हूं यदि यह स्विफ्ट नियमित अभिव्यक्तियों (हाइफ़न और रिक्त स्थान को हटाने) के साथ क्रिया नहीं थी * Apple पर चमक *

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"}

इसे यहां पर Rapidstub.com पर परखा जा सकता है

मैं लूप के लिए थोड़ा भाग गया, और यह पता चला कि 1000030 और 999999 के बीच की संख्या सबसे लंबे स्ट्रिंग परिणाम है, जिसमें 6 पुनरावृत्तियों हैं, और है

100,003 है 23 और 23 है 11 और 11 है 6 और 6 है 3 और 3 है 5 और 5 है 4 और 4 जादुई संख्या है

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"
}

7
NSStringCompareOptions.RegularExpressionSearchऔर मुझे लगा कि JS की String.fromCharCodeक्रिया थी। : P
ETHproductions

4
पायथन और रूबी string.replace:। स्विफ्ट:String.stringByReplacingOccurrencesOfString
बिल्ली

4

हास्केल, 285 - 180 = 105 बाइट्स

वास्तव में, संख्या प्रदर्शित करने के लिए कोई बिल्ट-इन नहीं है। मैं अभी भी स्कोर से असंतुष्ट था। टिप्पणी करने के लिए स्वतंत्र महसूस करें। मैं आगे प्रयोग करूंगा, यद्यपि। फिर भी स्कोर स्विफ्ट के स्कोर से बेहतर है

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)

प्रयोग

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"

स्पष्टीकरण।

mहालांकि, यह पर्याप्त cनहीं है। cसंख्या का अंग्रेजी नाम वर्ण की संख्या गिनने का कार्य है।

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.

1
अरे हाँ? खैर, स्विफ्ट है ... उह ... एक उच्च स्कोर ... (मैं
कॉमबैक्स

4

सी, 268 - 180 = 88 बाइट्स

#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");}

इसे यहाँ आज़माएँ ।

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");
}

3

जावा, 800 - 150 = 650 बाइट्स

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)));}}

डी-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)));
   }
}

मुझे पता है कि यह एक वर्ष से अधिक समय हो गया है, लेकिन आप उस त्रैमासिक असाइनमेंट में कुछ कोष्ठक को हटा सकते हैं और साथ ही साथ बदल ==0सकते हैं <1। तो: 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 बाइट्स )
केविन क्रूज़सेन

3

क्यूसी, 265 - 30 - 150 = 85 बाइट्स

(✵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"+

परीक्षण सूट

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"+

यदि आप संख्या की लंबाई प्राप्त करने के लिए बिल्ट-इन फ़ंक्शन का उपयोग नहीं कर रहे हैं, तो आप वास्तव में अपने स्कोर से एक और 150 घटा सकते हैं
जोजोडमो

2

जावास्क्रिप्ट, 382 - 150 - 30 = 202 बाइट्स

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");})()

इनपुट को तत्काल-इनवॉइस फंक्शन एक्सप्रेशन के पैरामीटर के रूप में दिया गया है।

टेस्ट इनपुट:

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

डी-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)

1

पायथन 641-150 = 501 बाइट्स

कम से कम यह जावा की तुलना में अधिक लंबा नहीं है! यह स्ट्रिंग्स का उपयोग करने के अलावा इस पर आधारित है ।

संपादित करें : मैं 0 के बारे में भूल गया था और मुझे यह कहने की ज़रूरत है कि "5 4 है", "4 यह जादू नंबर है" को छोड़ दें - जिसने स्कोर में थोड़ा सा जोड़ा।

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)

यहाँ कोशिश करो!


आपको नाम प्रदर्शित करने की आवश्यकता नहीं है, है ना?
अकांका

यह गलत है। i(5)के 4 is the magic numberबजाय प्रिंट 5 is 4 and 4 is the magic number
mbomb007 19

1

मू, 182 176/ 192 188 बाइट्स - 30 = 146/158

188 बाइट संस्करण:

u=$string_utils;s="";i=args[0];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 बाइट कार्यान्वयन-निर्भर संस्करण:

s="";i=args[0];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"

दोनों ही कार्य हैं।


1

PHP, 168 - 30 = 138 बाइट्स

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));}
हमारी साइट का प्रयोग करके, आप स्वीकार करते हैं कि आपने हमारी Cookie Policy और निजता नीति को पढ़ और समझा लिया है।
Licensed under cc by-sa 3.0 with attribution required.