खाने की जिद

Oct 16 2020

परिचय

संख्या नरभक्षण का परिणाम ज्ञात करें।

Of में से ६ क्यों डरा हुआ था? बन गया ase 8९।

आपके प्रोग्राम को 8एक नंबर पर ऑपरेशन (नीचे परिभाषित) लागू करने का परिणाम बार-बार खोजना होगा ।

संबंधित , सैंडबॉक्स

चुनौती

आपका इनपुट एकल धनात्मक पूर्णांक होगा, n।

आपको 8पूर्णांक में प्रत्येक को खोजने की आवश्यकता है , और इसे निम्न स्थितियों के आधार पर +या इसके लिए विकल्प दें -।

  • 8स्ट्रिंग की शुरुआत या अंत में सभी को हटा दें ।
  • यदि कई 8एस हैं, तो उन्हें एक ही में संक्षिप्त करें 8।
  • \ _$a\$और \ _$b\$8क्रमशः बायीं और दाईं ओर विखंडू हैं। चूंकि 8एक ऑपरेटर है, इसलिए संख्याओं को सीमांकित किया जाता है 8।
  • अगर \$ a ≥ b,\$ 8बन जाता है +।
  • और, यह बन जाता है -।

फिर बाएं से दाएं की अभिव्यक्ति का मूल्यांकन करें, और पूर्ण मूल्य लें।

यदि परिणाम में कोई 8s है, तो उपरोक्त चरणों को तब तक दोहराएं जब तक कि कोई भी न हो।

उदाहरण के लिए, 12384568789बन जाता है:

123 456 7 9
123 < 456: 123 - 456 7 9
456 ≥ 7  : 123 - 456 + 7 9
7 < 9    : 123 - 456 + 7 - 9 = -335
= abs(-335)
= 335

अत: 335 अंतिम उत्तर है।

उदाहरण इनपुट और आउटपुट

789 → 2
180 → 1
42312348 → 4231234
2389687 → 66
12384568789 → 335
13749547874394873104972349 → 7309154
808 → 0
7800482 → 13
14458883442 → 1997
88888 → indeterminate
388182 → 2
08182 → 3

यहाँ एक सरणी के रूप में टेस्टकेस हैं:

[789,180,42312348,2389687,12384568789,13749547874394873104972349,808,7800482,14458883442,88888]

स्कोरिंग क्राइटेरिया

यह कोड-गोल्फ है । प्रत्येक भाषा में सबसे छोटा उत्तर जीतता है।

जवाब

8 KevinCruijssen Oct 16 2020 at 17:29

05AB1E , 18 14 बाइट्स

Δ8¡þDü@1š·<*OÄ

इसे ऑनलाइन आज़माएं या सभी परीक्षण मामलों को सत्यापित करें ।

स्पष्टीकरण:

Δ               # Loop until it no longer changes:
 8¡             #  Split the integer on 8s
                #  (which will use the implicit input-integer in the first iteration)
   þ            #  Remove all empty strings by only leaving digits
    D           #  Duplicate this list
     ü          #  For each overlapping pair [a,b]:
      @         #   Check if a>=b (1 if truthy; 0 if falsey)
       1š       #  Prepend a 1 to this list
         ·      #  Double each value
          <     #  Decrease each by 1 (0 has become -1; 1 is still 1)
           *    #  Multiply the values at the same positions in the two lists
            O   #  Sum the list
             Ä  #  And take the absolute value of this sum
                # (after which the result is output implicitly)
7 UnrelatedString Oct 16 2020 at 13:57

जेली , 20 18 बाइट्स

IŻṠo-×
ṣ8LƇḌÇSAµÐL

इसे ऑनलाइन आज़माएं!

5 या 6 अन्य तरीकों के माध्यम से चला गया, इससे पहले कि मैं काम करने के लिए मिल सकता है केवल एक ही निकला ... और फिर मैंने अपने दूसरे दृष्टिकोण पर एक अलग स्पिन की कोशिश की और यह 2 बाइट्स छोटा है।

पुराना संस्करण: ṣ8LƇḌ<Ɲị⁾_+ż@ƲFVAµÐL

IŻṠo-×         Monadic helper link:
  Ṡ            signs of
I              deltas
 Ż             with prepended 0,
   o-          replace all 0s with -1s,
     ×         pairwise multiply with argument.

ṣ8LƇḌÇSAµÐL    Main link:
ṣ8             split (implicitly converted digit list) on 8s,
  LƇ           remove empty slices,
    Ḍ          convert digit lists from decimal,
     Ç         apply helper link,
      S        sum,
       A       abs.
        µÐL    Loop that whole thing until it doesn't change.
7 Arnauld Oct 16 2020 at 14:09

जावास्क्रिप्ट (ईएस 6), 76 बाइट्स

f=n=>n.replace(p=/[^8]+/g,x=>t-=p<(p=+x)?x:-x,t=0)&&n-(t=t<0?-t:t)?f(t+''):t

इसे ऑनलाइन आज़माएं!

टिप्पणी की

f = n =>               // f is a recursive function taking n as a string
  n.replace(           // we look for ...
    p = /[^8]+/g,      // ... all groups of consecutive non-eight digits
    x =>               // for each group x:
      t -=             //   update t:
        p < (p = +x) ? //     if the previous value is less than x:
          x            //       subtract x from t
        :              //     else:
          -x,          //       add x to t
    t = 0              //   start with t = 0
  ) &&                 // end of replace()
  n - (                // if n is not equal to t,
    t = t < 0 ? -t : t // where t is first updated to its absolute value:
  ) ?                  //
    f(t + '')          //   do a recursive call with t (coerced back to a string)
  :                    // else:
    t                  //   success: return t
5 DominicvanEssen Oct 17 2020 at 16:32

हस्क , 27 26 25 बाइट्स

संपादित करें: -1 का उपयोग करके बाइट Sपुनर्व्यवस्था का एक सा द्वारा अधिक बाइट पुनरावृत्ति समारोह तर्क को Combinator, और इन-लाइन सहायक समारोह चलती है, और फिर -1 उपयोग करने के लिए सक्षम होने के लिए oके बजाय Combinator()

ω(aΣSz*o:1Ẋȯ`^_1¬<mdfIx8d

इसे ऑनलाइन आज़माएं!

एक थोड़ा के लिए अलग दृष्टिकोण असंबंधित स्ट्रिंग की भूसी जवाब , भी 27 बाइट्स ।

मैंने आयोजित किया था, इसे थोड़ा सा पोस्ट करने के लिए, किसी भी नए हुस्कर को एक साफ स्लेट देने के लिए जिससे इस चुनौती को आज़माया जा सके ... लेकिन अब हस्केटिशन खुला है ...

कैसे? *

mdfIx8d                 # helper function: splits input on 8s
      d                 # get digits of input
    x8                  # split on 8  
  f                     # remove elements that are falsy for
   I                    # the identity function (so, remove empty elements)
m                       # map over each element of the list 
 d                      # combining digits as decimal number
 
ωλaΣz*₁¹m`^_1ΘẊo¬<₁     # main program
ω                       # repeatedly apply function until results are constant
 λ                      # lambda function taking one argument:
  a                     # get the absolute value of
   Σ                    # the sum of all elements of
    z*                  # element-wise multiplication of
      ₁¹                # (1) helper function applied to input
                        #     (so: the input split on 8s)
        m               # (2) map this function to all elements of
                  ₁     #     helper function applied to input
             Θ          #     (with 0 prepended at the start)                 
         `^_1           #     minus one to the power of
              Ẋ         #     the results for each pair of elements
               o        #     combine 2 functions
                ¬       #     NOT
                 <      #     second is greater than first    
4 DominicvanEssen Oct 16 2020 at 16:45

आर , 89 122 बाइट्स

संपादित करें: +33 बाइट्स को पुनरावर्ती फ़ंक्शन में बदलने के लिए जब मुझे एहसास हुआ कि इसे 8अपने स्वयं के आउटपुट पर ऑपरेशन को दोहराना चाहिए जब तक कि अधिक 8एस न हों। दोहे!

f=function(s)`if`(grepl(8,a<-abs(sum(c(1,-sign(diff(n<-sapply((n=el(strsplit(s,8)))[n>-1],as.double))))*n))),f(c(a,'')),a)

इसे ऑनलाइन आज़माएं!

तर्क nको एक स्ट्रिंग के रूप में स्वीकार करता है। nकोई गैर-8 अंक / वर्ण वाले त्रुटियों के लिए ।

4 Lyxal Oct 16 2020 at 13:18

पायथन 3 , 158 148 145 138 बाइट्स

x=input()
while'8'in x:
 y=[int(f)for f in x.split('8')if f];x=str(sum([-1,1][y[i-1]<y[i]]*y[i]for i in range(len(y))))
print(abs(int(x)))

इसे ऑनलाइन आज़माएं!

-10 बाइट्स @ pavi2410 की बदौलत और मेरे लिए एक बेवकूफ नहीं होने और गलती से गोल्फ सुझाव से एक अतिरिक्त जगह छोड़ने के लिए

और एक और -3 @ pavi2410 के लिए धन्यवाद

और एक अन्य -7 @ pavi2410 के लिए धन्यवाद

4 Noodle9 Oct 16 2020 at 21:54

सी (जीसीसी) , 149 \$\cdots\$ 139 137 बाइट्स

सेविंग के लिए धन्यवाद 2 बाइट्स बचाए !!! AZTECCO
के लिए 8 बाइट्स धन्यवाद बचाया !!!

R;t=10;m;s;p;c;f(n){R=n;for(s=p=0;n;){for(m=1,c=0;n&&n%t-8;n/=t,m*=t)c+=n%t*m;s+=c>p?p:-p;for(p=c;n%t==8;)n/=t;}p=abs(s+c);R=p-R?f(p):p;}

इसे ऑनलाइन आज़माएं!

स्पष्टीकरण (कुछ गोल्फ से पहले)

t=10;                                 // golf by storing 10 in t
g(n){                                 // helper function takes int n  
     for(     ;n;){                   // loops until n is zero  
         s=p=0                        // init sum s and previous p to 0  
       for(                           // inner loop calculates next rh value  
           m=1,                       // init multiplier m to 1  
               c=0;                   // and current c to 0   
                   n&&                // loop until n is 0   
                      n%t-8;          // or most rh digit is 8     
                            n/=t,     // knock most rh digit off n      
                               m*=t)  // and bump m by 10 each loop  
         c+=n%t*m;                    // add next digit to current 
                                      // building up number after  
                                      // most right-hand 8  
      s+=c>p?p:-p;                    // after that's done update sum s  
      for(                            // loop to strip off all rh 8's  
          p=c;                        // also make previous = current  
              n%t==8;)                // loop until most rh digit isn't an 8  
                      n/=t;           // knock most rh 8 off n   
        }                             //   
 p=abs(s+c);                          // return abs value of sum with   
                                      // positive most lh value  
}                                     //  
f(n){                                 // recursive main function  
     x=g(n);                          // calc first iteration
            x=x-g(x)?                 // is it different to next iteration?  
                     f(x):            // if so iterate  
                          x;          // else return value   
}                                     //  
4 Xcali Oct 18 2020 at 06:24

पर्ल 5 -p , 59 बाइट्स

y/8/ /;s/\d+/$&<$'?"$&-":"$&+"/ge;$_=abs eval$_.0;/8/&&redo

इसे ऑनलाइन आज़माएं!

4 xash Oct 18 2020 at 07:32

जे , 53 बाइट्स

|@('8'+/@(*_1x^0,2</\])@(,(*@#;._1#".;._1)~[=,)":)^:_

इसे ऑनलाइन आज़माएं!

यह काम किस प्रकार करता है

  • f^:_: जब तक रिजल्ट नहीं बदलेगा, तब तक f।
  • '8'…(,g[=,)":: इनपुट को एक स्ट्रिंग में परिवर्तित करें, ,चरित्र को प्रीपेंड करें 8, और gइसे बाएं तर्क के रूप में निष्पादित करें, और 8सही तर्क के रूप में पात्रों का एक सा मुखौटा ।
  • (…".;._1)~: स्ट्रिंग को समूहों में विभाजित करें u;._1, जो कि बिट मास्क में 1 से शुरू होता है और उन्हें वापस संख्या ".(छोड़कर 8) में परिवर्तित करता है ।
  • *@#;._1#: क्योंकि हम खाली समूहों (हो सकता है 888), ले #, केवल उन जिसकी लंबाई के #रोजर *1. है (वहाँ एक अच्छा तरीका हो सकता है।)
  • 2</\]: <प्रत्येक समूह की उसके साथ तुलना करें , जिसके परिणामस्वरूप एक बिट-मास्क होता है।
  • *_1x^0,: बिट मास्क, और गणना करने के लिए एक 0 पहले जोड़ें x*(-1)^yप्रत्येक समूह के लिए xके साथ yतुलना करने का परिणाम जा रहा है। तो हम प्राप्त करते हैं 3 1 2 -> 3 1 _2।
  • |@…+/: +/परिणाम का योग करें और निरपेक्ष मान लें |।
3 user Oct 16 2020 at 22:53

स्काला , 94 90 बाइट्स

i=>"[^8]+".r.findAllIn(""+i).map(_.toInt).scanRight(0){(a,b)=>if(a<b.abs)a else-a}.sum.abs

इसे ऑनलाइन आज़माएं!

जावा की शापित splitविधि का उपयोग नहीं करके -4 बाइट्स ।

i=>                           //The input
  "[^8]+".r                   //Regex for operands
  .findAllIn(""+i)            //Get all separate numbers in the input
  .map(_.toInt)               //Turn them into integers
  .scanRight(0){(a,b)=>       //Starting at the right,
    if(a<b.abs)a else-a}      //Replace a with -a if a >= b.abs (this is different from the challenge, but it works because we call abs again later)
  .sum                        //Sum them up
  .abs                        //Take the absolute value
3 UnrelatedString Oct 17 2020 at 07:21

भूसी , 27 बाइट्स

ω(aΣSz*(Ẋȯ`^_1±>Ṡ:←)mdfIx8d

इसे ऑनलाइन आज़माएं!

यह मेरा पहला हस्क उत्तर नहीं है, लेकिन यह मेरे द्वारा किए गए जेली उत्तरों में से एक का एक बहुत ही भद्दा प्रत्यक्ष अनुवाद है, इसलिए यह कम से कम किसी के लिए झपट्टा मारने और इनाम अर्जित करने के लिए सेवा करने योग्य शुरुआती बिंदु के रूप में काम कर सकता है।

ω(                             Iterate until periodic:
  a                            absolute value of
   Σ                           sum of
     z*                        zipwith multiplication on:
                          d    decimal digits
                        x8     split on 8s
                      fI       with empty slices removed
                    md         and converted back from decimal,
    S                          and the result of that
       (        Ṡ:←)           with its first element duplicated
        Ẋ                      and with neighboring pairs mapped to
          `^_1                 -1 to the power of
         ȯ    ±>               if the first is less than the second.
3 PetrFiedler Oct 17 2020 at 09:39

बैश , 177 बाइट्स

A=($(sed s/8/\ /g<<<$1));B=;while [ ${#A[@]} -ge 2 ];do [ ${A[0]} -ge ${A[1]} ]&&C=+||C=-;B=$B${A[0]}$C;A=(${A[@]:1});done;R=$(bc<<<$B$A|tr -d -);grep -cq 8 <<<$R&&f $R||echo $R

इसे वर्तमान कार्य निर्देशिका में फ़ंक्शन fया फ़ाइल के रूप में संग्रहीत किया जाना है f।

इसे ऑनलाइन आज़माएं!

3 ZsoltSzilagy Oct 18 2020 at 04:35

PHP, 290 बाइट्स

(अनुमान है कि आपको php में गोल्फ नहीं करना चाहिए। :))

<?php $n=$_SERVER["argv"][1];while(false!==strpos($n,'8')){$n=abs(array_reduce(str_split(preg_replace('|8+|','8',trim($n,'8')).'8'),function($c,$i){if($i!=8){$c[1]*=10;$c[1]+=$i;}else{$c[2]+=$c[1]*((!isset($c[0])||$c[0]>=$c[1])?1:-1);$c[0]=$c[1];$c[1]=0;}return$c;},[null,0,0])[2]);}echo$n;

इसे ऑनलाइन आज़माएं

अधूरा:

<?php

$n = $_SERVER["argv"][1]; $f=function($c, $i){
    if($i!=8) { $c[1]*=10;
        $c[1]+=$i;
    } else {
        $c[2] += $c[1] * ( (!isset($c[0]) || $c[0]>=$c[1])?1:-1); $c[0]=$c[1]; $c[1]=0;
    }
    return $c; }; while(false!==strpos($n,'8')) {
    $n = trim($n, '8');
    $n = preg_replace('|8+|', '8', $n);
    $a = str_split($n.'8');
    $n = abs(array_reduce($a, $f, [null, 0, 0])[2]); } echo $n;

स्पष्टीकरण:

मैं हर अंक पर चलने के लिए array_reduce का उपयोग करता हूं, और 3 डेटापॉइंट्स पर ले जाने के लिए एक सरणी का उपयोग करता हूं: चर $a, $बी और $sum, though they are unnamed as the elements of $सी।

यदि वर्तमान अंक एक गैर -8 है, तो मैं इसे "मेरे लिए" जोड़ देता हूं$b", otherwise I first compare $b से $a, add/subtract $b से $sum, and move the content of $ख $ क।

2 GalenIvanov Oct 16 2020 at 17:47

लाल , 142 बाइट्स

func[n][while[find to""n"8"][b: to[]load form split to""n"8"forall b[if b/2[b/1:
reduce[b/1 pick[- +]b/1 < b/2]]]n: absolute do form b]to 1 n]

इसे ऑनलाइन आज़माएं!

2 2014MELO03 Oct 16 2020 at 22:53

GolfScript , 32 बाइट्स

.,{8`%(~:x\{~.x\:x<2*(*-}/abs`}*

इसे ऑनलाइन आज़माएं!

हर बार एल्गोरिथ्म निष्पादित होने पर, संख्या या तो समान रहती है या कम से कम 1 अंक छोटा हो जाता है। इसका मतलब है कि हम परीक्षण के बजाय हर बाइट के लिए एक बार एल्गोरिथ्म को निष्पादित कर सकते हैं यदि इसमें 8 है।

.,                                 # Get the number of bytes in the input
  {                           }*   # Execute this block that many times
   8`%                             # Split the string around the 8s and remove the empty strings
                                   # This simultaneously executes the first 3 steps
      (~                           # Get the first number
        :x                         # Store it in the variable x
          \{~           }/         # For each number left in the array
             .x                    # Copy it and push x
               \:x                 # Store the current number in x
                  <                # Compare the two numbers
                   2*(             # 1 if previous<current and -1 if previous>=curret
                      *-           # Multiply and subtract
                          abs`     # Parse the absolute value to a string
2 AZTECCO Oct 17 2020 at 21:26

जाप , 37 बाइट्स

@=q8 f;=äÈn ¨Y?Y:-Y}Ug)x a s ,Uø8}f U

इसे अजमाएं

  • इनपुट को एक स्ट्रिंग के रूप में लेता है, इसे आसानी से एक नंबर के रूप में इनपुट लेने के लिए संशोधित किया जा सकता है लेकिन यह बड़ी संख्या के साथ काम नहीं करेगा

'' ''

@=q8 f;=äÈn ¨Y?Y:-Y}Ug)x a s ,Uø8}f U
@....}f    - return first number which return false when passed through @...
=q8 f;     - input splitted on 8 and empties removed
=äÈ...}Ug) - pass each consecutive values through È , but prepend first value before so that they are >=
n ¨Y?Y:-Y  - 1st to number compared to 2nd: return 2nd (negated eventually)
x a s      - reduce->abs->to string

The above is assigned to U while the funxtion returns..
Uø8        - contains 8? => repeat
Finally we return U

'' ''

2 GalenIvanov Oct 17 2020 at 03:26

के (ओके) , 60 43 38 बाइट्स

-22 बाइट्स ट्राव्स के लिए धन्यवाद

{a|-a:+/{x*1-2*>':x}.:'(~^.)#"8"\$:x}/

इसे ऑनलाइन आज़माएं!

1 Neil Oct 16 2020 at 16:38

रेटिना 0.8.2 , 68 बाइट्स

{`8+
-
^-|-$ \d+ $*
(?=-(1*))(?<=\1)-
+
O`\D1*
\+

1>`-

(1+)-\1

1

इसे ऑनलाइन आज़माएं! लिंक में छोटे परीक्षण के मामले शामिल हैं (रेटिना को यूरीरी में घटाव करना पड़ता है, जो बड़े मामलों के लिए बहुत धीमा है)। स्पष्टीकरण:

{`

तब तक दोहराएं जब तक कोई 8शेष न हो।

8+
-

8S के प्रत्येक रन को a में बदलें -।

^-|-$

प्रमुख और अनुगामी -s को हटाएँ ।

\d+
$*

शेष संख्याओं को एकात्मक में बदलें।

(?=-(1*))(?<=\1)-
+

प्रत्येक -को +तब तक बदलें जब तक कि निम्नलिखित संख्या अधिक न हो।

O`\D1*

आरंभ में जोड़े जाने वाली संख्याओं को और अंत में घटाए जाने वाली संख्याओं को क्रमबद्ध करें।

\+

एक साथ जोड़े जाने वाले सभी नंबरों को जोड़ें।

1>`-

एक साथ घटाए जाने के लिए सभी संख्याओं को जोड़ें।

(1+)-\1
   

अंतर का पूर्ण मूल्य लें।

1

इसे दशमलव में बदलें।