50
\$\begingroup\$

Task

Given a String as input, your task is to output 42 only if the input String happens to be exactly the following :

abbcccddddeeeeeffffffggggggghhhhhhhhiiiiiiiiijjjjjjjjjjkkkkkkkkkkkllllllllllllmmmmmmmmmmmmmnnnnnnnnnnnnnnoooooooooooooooppppppppppppppppqqqqqqqqqqqqqqqqqrrrrrrrrrrrrrrrrrrsssssssssssssssssssttttttttttttttttttttuuuuuuuuuuuuuuuuuuuuuvvvvvvvvvvvvvvvvvvvvvvwwwwwwwwwwwwwwwwwwwwwwwxxxxxxxxxxxxxxxxxxxxxxxxyyyyyyyyyyyyyyyyyyyyyyyyyzzzzzzzzzzzzzzzzzzzzzzzzzz

It may output any other value, produce an error or not output at all, if the input does not equal the aforementioned String.


Winning Criterion

This is , so the shortest code in bytes wins!

\$\endgroup\$
10
  • 1
    \$\begingroup\$ Many of the solutions provided here are wrong because they print 42 when the string is longer than the desired string and the prefix matches with the desired string. \$\endgroup\$
    – fR0DDY
    Mar 10, 2011 at 10:30
  • \$\begingroup\$ @froddy: What if the only characters? following the string (is|are) a line break? My usual input mechanism doesn't care whether the input is terminated by a line break or not but yield the same in both cases, for example. \$\endgroup\$
    – Joey
    Mar 10, 2011 at 13:22
  • \$\begingroup\$ @fR0DDY : There was no clear definition on how the rest of the input should be handled, so there's no 'wrong' here. \$\endgroup\$
    – PatrickvL
    Mar 10, 2011 at 15:46
  • 4
    \$\begingroup\$ @PatrickvL It does mention 'only' if the input is the given string. So abbcccddddeeeee...zzabc does not satisfy that i suppose and i can see some programs giving yes on that input. \$\endgroup\$
    – fR0DDY
    Mar 10, 2011 at 15:56
  • 2
    \$\begingroup\$ @fR0DDY : Let me put it another way : There's no specification on how input is delimited, so that's open to interpretation. There's also no mention of character encoding (I guess most of us assume the default of their environment - ANSI, UTF8 and UTF16LE will be the most popular ones). Also no mention how the input is presented - is it entered via the standard input, via a command-line parameter? So you see - having all this freedom gives way to some interpretation that you would mark as 'incorrect', while others would judge it 'compliant'. NOFI, but this is daily practise for some of us. \$\endgroup\$
    – PatrickvL
    Mar 10, 2011 at 16:10

80 Answers 80

1 2
3
0
\$\begingroup\$

Haskell, 45 51 bytes

f x|x==zip[1..]['a'..'z']>>=uncurry replicate->"42"

zip [1..] ['a'..'z'] produces the list [(1,'a'), (2,'b'), ...].

uncurry replicate has the type (Int, a) -> [a], and uncurry replicate (n, v) produces a list of n copies of v.

>>= applies uncurry replicate to the zipped list, and concatenates the results.

Finally, that string compared to the argument and returns "42" if it is equal, or crashes.

(edited b/c I'm dumb and didn't notice the return requirement)

\$\endgroup\$
1
  • \$\begingroup\$ Your code does not compile. You need to change -> to = (f is a function, not a lambda) and put the >>= expression into parenthesis (otherwise its parsed as (x==zip[1..]['a'..'c'])>>=(uncurry replicate)). \$\endgroup\$
    – Laikoni
    Feb 27, 2017 at 8:26
0
\$\begingroup\$

Pylongolf2, 21 bytes

con1968701440=?42~¿

Explanations:

con1968701440=?42~¿
c                   read input
 on                 hash it and then convert into integer
   1968701440       push the (aaa.zz) thing (but hashed) into the stack
             =      compare them
              ?     if true,
               42~  output 42
                  ¿ end if statement.

That last inverted question mark messed me up by 2 bytes :|

\$\endgroup\$
1
  • \$\begingroup\$ This is incorrect if more than one string can have that hash. \$\endgroup\$
    – user62131
    Feb 12, 2017 at 21:09
0
\$\begingroup\$

SmileBASIC, 58 bytes

DEF F S
FOR I=1TO 26C$=C$+CHR$(I+64)*I
NEXT?42OR C$!=S
END

I had to make it a function since the maximum INPUT length is 128 characters.

\$\endgroup\$
0
\$\begingroup\$

R, 74 bytes

g=function(x){if(x==paste(rep(rep(letters[1:26]),(1:26)),collapse=""))42}
\$\endgroup\$
0
\$\begingroup\$

Axiom, 141 bytes

f(x:String):NNI==(c:=96;i:=0;k:=1;repeat(c:=c+1;i:=i+1;for j in 1..i repeat(k>#x or ord(x.k)~=c=>return 0;k:=k+1);c=122=>break);k=#x+1=>42;0)

ungolf

--It would return 42 if the string in input it is as string above this codegolf question 
--it would return 0 in other cases; suppose Axiom ord() return 97..122 for a..z (ascii)
ff(x:String):NNI==
   c:=96;i:=0;k:=1
   repeat
      c:=c+1; i:=i+1           -- c='a'=97 i=1 k=1
      for j in 1..i repeat
               k>#x or ord(x.k)~=c=>return 0
               k:=k+1
      c=122=>break -- c==122 is 'z'
   k=#x+1=>42
   0
\$\endgroup\$
0
\$\begingroup\$

Java 8, 103 101 87 bytes

s->{String r="";for(char c=96,i;++c<123;)for(i=96;i++<c;)r+=c;return r.equals(s)?42:0;}

Returns 0 for everything that doesn't match the expected input.

Explanation:

Try it here.

s->{                       // Method with String parameter and integer return-type
  String r="";             //  String starting empty
  for(char c=96,i;++c<123;)//  Loop (1) over the lowercase alphabet:
    for(i=96;i++<c;)       //   Inner loop (2) `c-i` amount of times:
      r+=c;                //    Append that many of the current letter to the String
                           //   End of inner loop (2) (implicit / single-line body)
                           //  End of loop (1) (implicit / single-line body)
  return r.equals(s)?      //  If the input equals the String:
    42                     //   Return 42
   :                       //  Else:
    0;                     //   Return 0
}                          // End of method
\$\endgroup\$
1
  • 1
    \$\begingroup\$ I think you can remove the braces around that for-loop \$\endgroup\$
    – user41805
    Feb 13, 2017 at 15:58
0
\$\begingroup\$

JavaScript, 86 Bytes

c="";for(x=0;x<26;)c+=String.fromCharCode(x+97).repeat(++x);alert(prompt("")==c?42:0);

White spaced:

c="";
for(x=0;x<26;)
    c+=String.fromCharCode(x+97).repeat(++x);
alert(prompt("")==c?42:0);

I couldn't indent by 4 spaces for some reason. Not sure if using alert is allowed.

Edit: Fixed indent

\$\endgroup\$
0
\$\begingroup\$

Python 3, 58 bytes

lambda s:[42][''.join([i*chr(i+96)for i in range(27)])!=s]

Try it online!

Six years late to the party?

golfed 4 bytes off when I realized I could've used != instead of 1- ==

\$\endgroup\$
0
\$\begingroup\$

Jelly, 9 bytes

ØaxJ¤⁼×42

Try it online!

There is already a 13 byte Jelly answer, but the user had their account deleted, and it felt wrong to just edit in this code.

How it works

ØaxJ¤⁼×42 - Main link. Argument: s (string)   e.g. "abc"
Øa  ¤     - Yield the lowercase alphabet           "abc...xyz"   
   J      - Yield range(length)                    [1 2 ... 25 26]
  x       - Repeat (vectorising).                  "abbccc..."
     ⁼    - Equal to the input? Call the A         0
      ×42 - Multiply 42 by A                       0
\$\endgroup\$
0
\$\begingroup\$

Pyth - 21 Bytes

V26=Y+Y*@GNhN;IqsYz42  

Explanation:

V26=Y+Y*@GNhN;IqsYz42  z is initialised to input and Y to empty list
V26                    For N from 0 to 25
   =Y                  Set Y to
      Y                Y
     +                 Plus
        @GN            Item N of the alphabet
      *                Times
           hN          N plus one
             ;         End For
              I        If
                sY     Concatenate all elements of Y       
               q       Equals
                  z    z  
                   42  Print 42   
\$\endgroup\$
0
\$\begingroup\$

Javascript (ES6) 84 bytes

s=>[...Array(26)].map((_,i)=>String.fromCharCode(97+i).repeat(i+1)).join("")==s?42:0

I don't think an explanation is needed, but here's one anyway:

s=> // input
    [...Array(26)] // preparing alphabet array
    .map((_,i)=>String.fromCharCode(97+i).repeat(i+1)) // using the current index to get the corresponding letter and repeat it as many times as it's 1-index
    .join("")==s?42:0 // Join the array into a string and check if equal to input.
\$\endgroup\$
0
\$\begingroup\$

APL NARS 14 chars

{⍵≡⎕a/⍨⍳26:42}

it return 42 for the request string, nothing if something else.

  f←{⍵≡⎕a/⍨⍳26:42}
  w←(⍳26)/⎕a
  f 2
  f w
42
  f 'str'
  
\$\endgroup\$
0
\$\begingroup\$

Japt, 13 12 11 bytes

Outputs 42 or false.

;¶CËpEÄé#*

Try it online

\$\endgroup\$
0
\$\begingroup\$

AWK, 65 bytes

{for(;i<26;)for(j=++i;j--;)L=L sprintf("%c",i+96);$0=L==$0?42:0}1

Try it online!

Maybe I should develop a golfing version of AWK, but until then, here is my AWK solution :)

\$\endgroup\$
0
\$\begingroup\$

K (oK), 14 bytes

Solution:

42*(&!27)~-96+

Try it online!

Explanation:

Returns 42 if the input matches the "abbccc..." otherwise 0.

42*(&!27)~-96+ / the solution
          -96+ / subtract 96 from the input (vectored)
         ~     / matches?
   (&!27)      / where (&) til (!) 27. Generates 1 2 2 3 3 3 4 4 4 4... sequence
42*            / multiply result (0 false or 1 true) by 42
\$\endgroup\$
0
\$\begingroup\$

Python 2, 69 bytes

x=""
for i in range(26):x+=chr(97+i)*(i+1)
if raw_input()==x:print 42

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Julia 0.6, 55 bytes

s->join("$c"^i for (i,c) in enumerate('a':'z'))==s?42:0

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Lua, 80 bytes

c=""for i=1,26 do c=c..string.char(i+96):rep(i)end print(c~=io.read()and""or 42)

Try it online!

Brief explanation: creates the string and then compares it with the input. Output is an empty string or 42.

\$\endgroup\$
0
\$\begingroup\$

Wren, 72 bytes

Generate the range 'a'..'z'. Multiply the string-converted value by the value - 96. Join the list. Check whether the input is equal to this string.

Fn.new{|A|A==(1..26).map{|x|String.fromCodePoint(x+96)*(x)}.join()?42:0}

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Thunno 2, 9 bytes

Ạdż×J=42×

Attempt This Online!

Explanation

Ạdż×J=42×  # Implicit input
Ạd         # Lowercase alphabet, split into a list of characters
  ż×       # Each multiplied by their 0-based index
    J=     # Equals the input string?
      42×  # Multiply by 42
           # Implicit output
\$\endgroup\$
1 2
3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.