अपने शब्दों को तौलना!


10

चुनौती

दो तारों को देखते हुए, प्रत्येक की लंबाई 30 तक होती है, जिसमें मुद्रण योग्य ASCII वर्ण (कोड ) शामिल होते हैं, उन्हें कोड के सबसे कम बाइट्स में एक संतुलन पैमाने पर रखा जाता है! इसमें निम्न चरण शामिल हैं:[32,126]

  1. गणना करें और तारों के वजन की तुलना करें
  2. उचित रूप से झुका हुआ ASCII- कला स्केल चुनें
  3. पैमाने पर दो तार रखें

तार को एक सरणी, दो तर्क या किसी अन्य उचित विधि के रूप में पारित किया जा सकता है।


एक स्ट्रिंग के वजन कि स्ट्रिंग के पात्रों, जहां के वजन की राशि के रूप में परिभाषित किया गया है:

  • रिक्त स्थान का वजन 0 ( ) है
  • लोअरकेस अक्षर का वजन 2 है ( abcdefghijklmnopqrstuvwxyz)
  • अपरकेस अक्षरों में 4 का वजन होता है ( ABCDEFGHIJKLMNOPQRSTUVWXYZ)
  • अन्य सभी प्रतीकों का वजन 3 है ( !"#$%&'()*+,-./0123456789:;<=>?@[\]^_`{|}~)

तराजू इस तरह दिखते हैं:

          । _
          | _- * / \
          | - * / \
       _- * | / \ _
    _- * | / \ _
   / \ / | * ------ *
  / \ / |
 / \ / |
/ \ / |
* ------ * |
    ______ | ______
    _
   / \ * -_ |
  / \ * - |
 / \ | * -_ 
/ \ / | * -_
* ------ * | / \
          | / \ _
          | / \ _
          | / \ _
          | * ------ *
    ______ | ______
          ।
          |
    ______ | ______
   / \ / | / \
  / \ / | / \ _
 / \ / | / \ _
/ \ / | / \ _
* ------ * | * ------ *
          |
          |
    ______ | ______

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


मैं निम्नलिखित सभी उदाहरणों के आधार के रूप में तीसरी ड्राइंग के एक सेगमेंट का उपयोग करूंगा।

पहला स्ट्रिंग बाएं पैन पर और दूसरा स्ट्रिंग दाईं ओर रखा जाना चाहिए।

अपने गैर-अंतरिक्ष वर्णों को डैश के ठीक ऊपर 6x5 क्षेत्र में रखकर पैन पर एक स्ट्रिंग रखें , जैसा कि #यहाँ एस द्वारा चिह्नित है (आप संतुलन पैमाने के ओवरराइटिंग भाग को समाप्त कर सकते हैं - यह ठीक है):

 ###### _
 ######
 ######
 ######
/ ###### \
* ------ *

इन सभी पात्रों को "व्यवस्थित" होना चाहिए। या तो एक -चरित्र के ऊपर या दूसरे वर्ण से स्ट्रिंग:

  गलत गलत सुधार
    ____ ____ ____
 f / \ / \ / \
  l \ / \ / \
 / \ / होव \ / s \
/ oating \ / eri ng \ / ettled \
* ------ * * ------ * * ------ *

इसके अतिरिक्त, पूरे स्टैक को जितना संभव हो उतना सपाट होना चाहिए, जिसका अर्थ है कि छह 1-चौड़ा स्तंभों में, सबसे ऊंची और सबसे छोटी की ऊंचाई 1 से अधिक नहीं होनी चाहिए:

    गलत गलत सुधार
[सबसे लंबा: 5] [सबसे लंबा: 4] [सबसे लंबा: 5] [सबसे लंबा: 2]
[सबसे छोटा: 0] [सबसे छोटा: 2] [सबसे छोटा: ४] [सबसे छोटा: २]
      5__5_ ____ 5_5__ ____
     45445 और / 445454 / \
     45445 $% और $ @ 445454 / \
    / 45445 और% @% $ और 445454% और $ @% &
   / 45445 \ / & $ @ $ &% \ / 445454 \ / $ @ $% $ $ $
   * ------ * * ------ * * ------ * * ------ *

पात्रों का सटीक क्रम / व्यवस्था मायने नहीं रखती है। निम्नलिखित स्ट्रिंग के लिए सभी मान्य व्यवस्थाएं हैं "अपने शब्दों का वजन!"

    ____ ____ ____ ____
   / \ / \ / \ / \
 डी एस! \ / उल्लू ओए \ u! Wd \
 अवरोर वियहोस योविग्र इगियो
/ वजनी \ / egyrr! / / Wrhd! S \ / rrsuwy \
* ------ * * ------ * * ------ * * ------ *

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

INPUT: "CODE GOLF", "कोडिंग चुनौतियां"
वजन: 32, 32
उदाहरण के लिए:
          । 
          |
    ______ | ______
   / \ / | / \
  / \ / | nge \ रों
 / OO \ _ | challe
/ CFGLED \ | / कोडिंग \
* ------ * | * ------ *
          |
          |
    ______ | ______ 
INPUT: "", "$"
वजन: 0, 3
उदाहरण के लिए:
    _
   / \ * -_ |
  / \ * - |
 / \ | * -_ 
/ \ / | * -_
* ------ * | / \
          | / \ _
          | / \ _
          | / $ \
          | * ------ *
    ______ | ______
INPUT: "आप जानते हैं कि वे क्यों कहते हैं!", "There_always_a_relevant_nk"
वजन: 75, 65
उदाहरण के लिए:
          । tr_a_s
          | _hekx_y
          | - * इल्कट्टा
       _- * | revanw
    _- * | / E's_al \
  टी / \ | * ------ *
 AUYOHY |
 A - HWYK |
/ OTSMEW \ |
* ------ * |
    ______ | ______

1
क्या अनुगामी व्हाट्सएप स्वीकार्य है?
Hiatsu

@ हाइट्स हां, यह ठीक है।
नकारात्मक सात

2
मुझे यह चुनौती पसंद है, मैं वास्तव में करता हूं। हालाँकि 2 बिंदु हैं जो मुझे इसे देने से रोकते हैं +1। सबसे पहले: अगर रिक्त स्थान कुछ भी "वजन" नहीं करते हैं और कलाकृति में शामिल नहीं होते हैं तो उन्हें क्यों शामिल करें? पहले उन्हें छानना अनावश्यक अनावश्यक है। दूसरी बात: यह मुझे "2-इन -1" / गिरगिट चुनौती की तरह लगता है - चुनौती 1: यह निर्धारित करें कि कौन सा स्ट्रिंग "भारी" है, चुनौती 2: कुछ ASCII- कला उत्पन्न करें।
झबरा

@ शैगी मुझे लगता है कि यह एक उचित मूल्यांकन है। इस पोस्टिंग ने मुझे सिर्फ चीजों को सरल रखने के लिए सिखाया है।
नकारात्मक सात

जवाबों:


7

चारकोल , 110 बाइट्स

UMθ⪫⪪ι ω≔⁰ηFθ≦⁻ΣEι⁻⁺³№ακ№βκηP-×⁷_↑χ.¶¶≔³ζ¿η«≔∨›⁰η⁵ζM±⁶±²_F⁴⁺¶*-§_|_ι¿›⁰η‖»P-⁺|×⁶_J±⁴±ζFθ«←⁶↑*↗⁴↓↘⁴←↖*←⪪ι⁶J⁹⁻ζ⁶

इसे ऑनलाइन आज़माएं! लिंक कोड के वर्बोज़ संस्करण के लिए है। दो तारों के एक सरणी के रूप में इनपुट लेता है। स्पष्टीकरण:

UMθ⪫⪪ι ω

दोनों तारों से रिक्त स्थान निकालें।

≔⁰η

भार संतुलन मान लें।

Fθ

दोनों तारों पर लूप।

≦⁻ΣEι⁻⁺³№ακ№βκη

स्ट्रिंग के वजन से चल रहे अंतर को घटाएं।

P-×⁷_↑χ.¶¶

बैलेंस स्केल के आधार को प्रिंट करें।

≔³ζ

मान लें कि दोनों पैन जमीन से 3 हैं।

¿η«

यदि वजन संतुलन नहीं था ...

≔∨›⁰η⁵ζ

... बाएँ पैन की ऊँचाई की गणना करें ...

M±⁶±²_F⁴⁺¶*-§_|_ι

... झुक झुक कर दाईं ओर झुकें ...

¿›⁰η‖»

... और प्रतिबिंबित करें कि बाएं पैन भारी था।

P-⁺|×⁶_

अन्यथा एक स्तर संतुलन बनाएं।

J±⁴±ζ

पहले पैमाने पर पैन के लिए कूदो।

Fθ«

इनपुट्स पर लूप करें।

←⁶↑*↗⁴↓↘⁴←↖*

स्केल पैन को ड्रा करें।

←⪪ι⁶

इनपुट को लंबाई 6 के सबस्ट्रिंग में स्लाइस करें और उन्हें उल्टा प्रिंट करें ताकि वे पैन को ऊपर की ओर भरें।

J⁹⁻ζ⁶

दूसरे पैमाने पर पैन के लिए कूदो।


6

पायथन 2 , 1101 1071 855 837 बाइट्स

स्ट्रिंग संपीड़न के साथ -216 बाइट्स

-18 बाइट्स कुछ पुनरावृत्ति को कम करके

from zlib import decompress as Z
from base64 import b64decode as D
r=range(6)
j="".join
w=lambda a:0if not a else(2+2*(a[0]<'[')if a[0].isalpha()else 3)+w(a[1:])
t=Z(D('eJxT0FKIV1BQ0AWT8SAIJsAcXTCppQAGumBSSx8MYsBAC0kCAiCySAIKEJW4ZHGpxA8AejMemQ=='))
p=lambda k,l,m:j(map(j,[(t[2*l+m::6][:30-len(k)]+k)[i::6]for i in r]))
def A(a,b):
 e=cmp(w(j(a.split())),w(j(b.split())))+1;return Z(D('eJxVUUGuhTAI3HOKWTdBW/U2SHoQ4O6ftvrMb0hLZJgZAYABFZB5KxD4zrZtNJOJMaHWIIoa0D6Ao+jrWRiHEI7kMcQg9VLBCo9O3dCbdanepOvZQztF9rRH2xUlwISehIZ96HltLFqu1IMF2p1QH/S+1Ge7CT5blIVOxqUWFudjqHPSwhitjPbzf7uZ1HaIaG2hShFTfU7Eca6J7MBr1K+3/YbRVLd2VlE5oilp7EG/gV7+DPQuSAsZPm7PZE9HBY2G+ctS/QzR+whSGlPAGz4mkkl5Sf18SMvkyL9iF6aLd2WLUm/KDVzvJu93k2tLZXlwetgLmFH4MzcKCaJnqX1Fz3iOf4//Pi7EwP4BHmyJpg=='))[e::3].format(*map(lambda c:[p(j(([a,b]*3)[c].split()),e,c)[i::5]for i in r],r))

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

के रूप में चलाएँ A(string_one, string_two

w स्ट्रिंग के वजन की पुनरावृत्ति की गणना करता है।

t सभी छह सम्भावित पैमानों का संपीड़ित और परस्पर पाठ है, जो बहुत अच्छी तरह से संपीड़ित होता है।

p स्ट्रिंग (रिक्त स्थान हटाए जाने के साथ), स्ट्रिंग का वजन और उस स्ट्रिंग के संतुलन का पक्ष लेता है, और वर्णों का 5x6 ब्लॉक बनाता है।

Aस्ट्रिंग्स लेता है, और उनके ब्लॉक बनाता है p। निचले भाग में विशालकाय स्ट्रिंग तीन स्वरूपित स्ट्रिंग्स है जो इंटरलेस्ड और कंप्रेस्ड है।


3

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

वर्णों के 2 सरणियों के एक सरणी के रूप में इनपुट लेता है। चरित्र द्वारा उत्पादन चरित्र खींचता है।

S=>(s=Math.sign(~(g=i=>(S[i]=S[i].filter(c=>c>' '?i+=/[a-z]/gi.test(c)?c>{}?2:4:3:0),i))(0)+g(1))+1,g=x=>y>10?'':(X=(r=x>9)?20-x:x,Y=(r?2-s:s)*2+y,S[+r][X>0&&X<7&&47-Y*6+X]||`. /\\|-_*
`[~X?x-10?y>9?X>3?6:1:[x+y*3-17,2*y-4,x+~y*3][s]/2|X<4?Y<5|Y>8?Y-9|X>7?1:X%7?5:7:~X+Y?X+Y-8?1:2^r:3^r:[7-x%3,6,5+x%3][s]:y&&4:8])+g(x<21?x+1:!++y))(y=0)

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

कैसे?

हम एक पहले सहायक फ़ंक्शन को परिभाषित करते हैं जो इनपुट स्ट्रिंग से रिक्त स्थान को निकालता है S[i] और उसका वजन लौटाता है:

g = i => (                   // i = string index
  S[i] = S[i].filter(c =>    // for each character c in S[i]:
    c > ' ' ?                //   if c is not a space:
      i +=                   //     update i:
        /[a-z]/gi.test(c) ?  //       if c is a letter:
          c > {} ?           //         if c is in lower case:
            2                //           add 2 to i
          :                  //         else:
            4                //           add 4 to i
        :                    //       else (not a letter):
          3                  //         add 3 to i
    :                        //   else (a space):
      0                      //     remove c from S[i]
  ), i                       // end of filter(); return i
)                            //

NB: क्योंकि हम फिर से उपयोग करते हैं i वजन की गणना करने के लिए, यह एक के लिए बंद है S[1]

हम गणना करते हैं s, जो बराबर है 0 अगर S[0] भारी है, 2 अगर S[1] भारी है, या 1 यदि दोनों तारों का वजन समान है:

s = Math.sign(~g(0) + g(1)) + 1

अब हम दूसरे हेल्पर फ़ंक्शन को आउटपुट आकर्षित करने के लिए आमंत्रित करते हैं:

g = x =>                     // given x:
  y > 10 ?                   //   if we've reached the last row:
    ''                       //     stop recursion
  :                          //   else:
    ( X = (r = x > 9) ?      //     r = true if we're on the right side
        20 - x               //       X = 20 - x on the right side
      :                      //     or:
        x,                   //       X = x on the left side
      Y = (r ? 2 - s : s)    //     Y is the position of the scale tray
          * 2 + y,           //     according to s and the current side
      S[+r][                 //     we try to extract a character from S[0] or S[1]:
        X > 0 && X < 7 &&    //       provided that we're located above the tray
        47 - Y * 6 + X       //       and using an index based on (X, Y)
      ] ||                   //     if this character doesn't exist,
      `. /\\|-_*\n`[INDEX]   //     we need to draw the balance instead
    ) +                      //     (see the next part)
    g(x < 21 ? x + 1 : !++y) //     append the result of a recursive call

कहाँ INDEXकी गणना इस प्रकार है:

~X ?                         // if this is not the last character of the current row:
  x - 10 ?                   //   if this is not the central column:
    y > 9 ?                  //     if this is the last row:
      X > 3 ? 6 : 1          //       draw the base ('_' or a space)
    :                        //     else:
      [ x + y * 3 - 17,      //       attempt to draw the beam:
        2 * y - 4,           //         using an equation depending on s
        x + ~y * 3           //         whose result must be -1, 0 or 1
      ][s] / 2 | X < 4 ?     //       if it's invalid or X is less than 4:
        Y < 5 | Y > 8 ?      //         if we're not over the chains:
          Y - 9 | X > 7 ?    //           if we're not over the pan:
            1                //             draw a space
          :                  //           else:
            X % 7 ? 5 : 7    //             draw the pan ('-' or '*')
        :                    //         else:
          ~X + Y ?           //           if this is not an interior chain:
            X + Y - 8 ?      //             if this is not an exterior chain:
              1              //               draw a space
            :                //             else:
              2 ^ r          //               draw the exterior chain ('/' or '\')
          :                  //           else:
            3 ^ r            //             draw the interior chain ('/' or '\')
      :                      //       else:
        [ 7 - x % 3,         //         draw the beam, using either '_' -> '-' -> '*'
          6,                 //         or just '_'
          5 + x % 3          //         or '*' -> '-' -> '_'
        ][s]                 //         depending on s
  :                          //   else:
    y && 4                   //     draw the central pillar ('|' or '.')
:                            // else:
  8                          //   append a line feed

1

जावा 10, 1043 993 988 983 बाइट्स

(a,b)->{var r=new char[11][21];for(var A:r)java.util.Arrays.fill(A,' ');a=a.replace(" ","");b=b.replace(" ","");int A=s(a),B=s(b),j,c,i=3;for(;++i<17;r[3][i]=A==B?'_':32)r[10][i]=95;for(i=11;i-->1;)r[i][10]=i>0?'|':46;if(A==B){r[8][0]=r[8][7]=r[8][13]=r[8][20]=42;for(i=0;++i<20;)if(i<8|i>13)r[8][i]=45;for(i=8;i-->4;r[i][7-i]=r[i][20-i]=47)r[i][i]=r[i][i+13]=92;A=B=8;}else{r[5][i=A<B?0:13]=r[5][i+7]=r[9][13-i]=r[9][20-i]=42;for(i=5;i-->1;r[i][A>B?18-i*3:2+i*3]=42)r[i][A>B?17-i*3:3+i*3]=45;for(i=0;++i<20;)r[i>13?A>B?5:9:A>B?9:5][i>13|i<7?i:1]=45;for(i=9;i-->1;r[i][i>4?A>B?8-i:21-i:A>B?17-i:4-i]=47)r[i][i>4?A>B?i-1:i+12:A>B?i+16:i+3]=92;A=(A>B?r[i=0][16]=r[1][13]=r[3][7]=r[4][4]=95:(r[0][i=4]=r[1][7]=r[3][13]=r[4][16]=95));A=9-i;B=5+i;}c(r,a,A,7);c(r,b,B,20);return r;};int s(String s){int r=0;for(int i:s.getBytes())r+=i>64&i<91?4:i>96&i<123?2:3;return r;}void c(char[][]r,String s,int p,int q){for(int c=0,i=p,j;i-->p-5;)for(j=q;j-->q-6&c<s.length();)r[i][j]=s.charAt(c++);}

-5 बाइट्स @ceilingcat की बदौलत

इनपुट दो स्ट्रिंग्स हैं, जिसके परिणामस्वरूप एक चरित्र-मैट्रिक्स होगा।

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

स्पष्टीकरण:

// Method with two String parameters and character-matrix return-type:
(a,b)->{
  // Result matrix, with 11 rows and 21 columns:
  var r=new char[11][21];
  // Initially fill the entire matrix with spaces:
  for(var A:r)java.util.Arrays.fill(A,' ');
  // Remove all spaces from the input-Strings:          
  a=a.replace(" ","");b=b.replace(" ","");
  // Call a separated method to calculate the scores of both input-Strings:
  int A=s(a),B=s(b),

  // Fill the cells for the base with '_',
  // and also fill the cells for the balance-bar with '_' when the scores are equal:
  j,c,i=3;for(;++i<17;r[3][i]=A==B?'_':32)r[10][i]=95;
  // Fill the cells for the stand with '|':
  for(i=11;i-->1;)r[i][10]=i>0?'|'
  // And the top of it with '.':
  :46;

  // If the scores are equal:
  if(A==B){
    // Fill the four appropriate cells for the sides of the scales with '*':
    r[8][0]=r[8][7]=r[8][13]=r[8][20]=42;
    // Fill the appropriate cells for the scales themselves with '-':
    for(i=0;++i<20;)if(i<8|i>13)r[8][i]=45;
    // Fill the appropriate cells of the robes with '/' and '\':
    for(i=8;i-->4;r[i][7-i]=r[i][20-i]=47)r[i][i]=r[i][i+13]=92;
    // Set A and B both to 8 to use later on:
    A=B=8;}
  // If the scores aren't equal:
  else{
    // Fill the four appropriate cells for the sides of the scales with '*':
    r[5][i=A<B?0:13]=r[5][i+7]=r[9][13-i]=r[9][20-i]=42;
    // Fill the appropriate four cells of the balance-bar with '-':
    for(i=5;i-->1;r[i][A>B?18-i*3:2+i*3]=42)r[i][A>B?17-i*3:3+i*3]=45;
    // Fill the appropriate cells of the scales with '-':
    for(i=0;++i<20;)r[i>13?A>B?5:9:A>B?9:5][i>13|i<7?i:1]=45;
    // Fill the appropriate cells of the robes with '/' and '\':
    for(i=9;i-->1;r[i][i>4?A>B?8-i:21-i:A>B?17-i:4-i]=47)r[i][i>4?A>B?i-1:i+12:A>B?i+16:i+3]=92;
    // Fill the four appropriate cells of the balance-bar with '_',
    // and set A and B to 9 and 5 depending on which score is higher:
    A=(A>B?r[i=0][16]=r[1][13]=r[3][7]=r[4][4]=95:(r[0][i=4]=r[1][7]=r[3][13]=r[4][16]=95));A=9-i;B=5+i;}
  // Call a separated method to fill the cells above the scales with the input-characters:
  c(r,a,A,7);c(r,b,B,20);
  // And finally return the resulting character-matrix:
  return r;};

// Separated method to calculate the score of the given String:
int s(String s){
  // Initially start the score-sum at 0:
  int r=0;
  // Loop over the characters of the given String:
  for(int i:s.getBytes())
    // Increase the sum by:
    r+=
      // 4 for uppercase letters:
      i>64&i<91?4
      // 2 for lowercase letters:
      :i>96&i<123?2
      // 3 for any other character:
      :3;
  // And return the resulting sum:
  return r;}

// Separated method to draw the strings on top of the scales:
void c(char[][]r,String s,int p,int q){
  // Keep a counter so we know when we're done drawing the given String:
  for(int c=0,
  // Loop over the appropriate rows bottom to top:
  i=p,j;i-->p-5;)
    // Inner loop over the appropriate cells of this row left to right,
    for(j=q;j-->q-6
    // as long as we're not done yet with the input-String:
        &c<s.length();)
      // And fill that appropriate cell with the next character in line of the given String:
      r[i][j]=s.charAt(c++);}
हमारी साइट का प्रयोग करके, आप स्वीकार करते हैं कि आपने हमारी Cookie Policy और निजता नीति को पढ़ और समझा लिया है।
Licensed under cc by-sa 3.0 with attribution required.