27
\$\begingroup\$

This idea is loosely based upon @TùxCräftîñg's chat message.

Take a look at the below example sequence:

INVALID0, INVALID1, INVALID2 INVALID3, INVALID4... INVALID9

After INVALID9, it goes on like this:

INVALI0, INVALI1, INVALI2, INVALI3... INVALI9

And after INVALI9, it's like this:

INVAL0,INVAL1, INVAL2, INVAL3... INVAL9

After, INVAL9, it's like this:

INVA0, INVA1, INVA2, INVA3, ... INVA9

Notice how we we kept removing a letter from the word INVALID each time.

You'll keep repeating this until you reach a single letter, that is, the letter I:

I0, I1, I2, I3, I4 ... I9

Now, your task is, to take an input of a word, and produce a sequence from it like the above example. Your code must also work with single letters, and in that case the resulting sequence will be shorter.

You may choose any input and output format that you prefer (with or without a separator, as you wish), but you must specify which one you have chosen.

The sequence needs to be in the exact specified order.

The shortest code, in bytes, that successfully completes this challenge, wins the challenge.

Full sequence in the above example:

INVALID0, INVALID1, INVALID2, INVALID3, INVALID4, INVALID5, INVALID6, INVALID7, INVALID8, INVALID9, INVALI0, INVALI1, INVALI2, INVALI3, INVALI4, INVALI5, INVALI6, INVALI7, INVALI8, INVALI9, INVAL0, INVAL1, INVAL2, INVAL3, INVAL4, INVAL5, INVAL6, INVAL7, INVAL8, INVAL9, INVA0, INVA1, INVA2, INVA3, INVA4, INVA5, INVA6, INVA7, INVA8, INVA9, INV0, INV1, INV2, INV3, INV4, INV5, INV6, INV7, INV8, INV9, IN0, IN1, IN2, IN3, IN4, IN5, IN6, IN7, IN8, IN9, I0, I1, I2, I3, I4, I5, I6, I7, I8, I9

Other examples:

Input: MAYBE (Uppercase and lowercase don't matter)

Output:

MAYBE0, MAYBE1, MAYBE2, MAYBE3, MAYBE4, MAYBE5, MAYBE6, MAYBE7, MAYBE8, MAYBE9, MAYB0, MAYB1, MAYB2, MAYB3, MAYB4, MAYB5, MAYB6, MAYB7, MAYB8, MAYB9, MAY0, MAY1, MAY2, MAY3, MAY4, MAY5, MAY6, MAY7, MAY8, MAY9, MA0, MA1, MA2, MA3, MA4, MA5, MA6, MA7, MA8, MA9, M0, M1, M2, M3, M4, M5, M6, M7, M8, M9


Input: AFTER

Output:

AFTER0, AFTER1, AFTER2, AFTER3, AFTER4, AFTER5, AFTER6, AFTER7, AFTER8, AFTER9, AFTE0, AFTE1, AFTE2, AFTE3, AFTE4, AFTE5, AFTE6, AFTE7, AFTE8, AFTE9, AFT0, AFT1, AFT2, AFT3, AFT4, AFT5, AFT6, AFT7, AFT8, AFT9, AF0, AF1, AF2, AF3, AF4, AF5, AF6, AF7, AF8, AF9, A0, A1, A2, A3, A4, A5, A6, A7, A8, A9


Input: WHAT ARE YOU DOING

WHAT ARE YOU DOING0, WHAT ARE YOU DOING1, WHAT ARE YOU DOING2, WHAT ARE YOU DOING3, WHAT ARE YOU DOING4, WHAT ARE YOU DOING5, WHAT ARE YOU DOING6, WHAT ARE YOU DOING7, WHAT ARE YOU DOING8, WHAT ARE YOU DOING9, WHAT ARE YOU DOIN0, WHAT ARE YOU DOIN1, WHAT ARE YOU DOIN2, WHAT ARE YOU DOIN3, WHAT ARE YOU DOIN4, WHAT ARE YOU DOIN5, WHAT ARE YOU DOIN6, WHAT ARE YOU DOIN7, WHAT ARE YOU DOIN8, WHAT ARE YOU DOIN9, WHAT ARE YOU DOI0, WHAT ARE YOU DOI1, WHAT ARE YOU DOI2, WHAT ARE YOU DOI3, WHAT ARE YOU DOI4, WHAT ARE YOU DOI5, WHAT ARE YOU DOI6, WHAT ARE YOU DOI7, WHAT ARE YOU DOI8, WHAT ARE YOU DOI9, WHAT ARE YOU DO0, WHAT ARE YOU DO1, WHAT ARE YOU DO2, WHAT ARE YOU DO3, WHAT ARE YOU DO4, WHAT ARE YOU DO5, WHAT ARE YOU DO6, WHAT ARE YOU DO7, WHAT ARE YOU DO8, WHAT ARE YOU DO9, WHAT ARE YOU D0, WHAT ARE YOU D1, WHAT ARE YOU D2, WHAT ARE YOU D3, WHAT ARE YOU D4, WHAT ARE YOU D5, WHAT ARE YOU D6, WHAT ARE YOU D7, WHAT ARE YOU D8, WHAT ARE YOU D9, WHAT ARE YOU 0, WHAT ARE YOU 1, WHAT ARE YOU 2, WHAT ARE YOU 3, WHAT ARE YOU 4, WHAT ARE YOU 5, WHAT ARE YOU 6, WHAT ARE YOU 7, WHAT ARE YOU 8, WHAT ARE YOU 9, WHAT ARE YOU0, WHAT ARE YOU1, WHAT ARE YOU2, WHAT ARE YOU3, WHAT ARE YOU4, WHAT ARE YOU5, WHAT ARE YOU6, WHAT ARE YOU7, WHAT ARE YOU8, WHAT ARE YOU9, WHAT ARE YO0, WHAT ARE YO1, WHAT ARE YO2, WHAT ARE YO3, WHAT ARE YO4, WHAT ARE YO5, WHAT ARE YO6, WHAT ARE YO7, WHAT ARE YO8, WHAT ARE YO9, WHAT ARE Y0, WHAT ARE Y1, WHAT ARE Y2, WHAT ARE Y3, WHAT ARE Y4, WHAT ARE Y5, WHAT ARE Y6, WHAT ARE Y7, WHAT ARE Y8, WHAT ARE Y9, WHAT ARE 0, WHAT ARE 1, WHAT ARE 2, WHAT ARE 3, WHAT ARE 4, WHAT ARE 5, WHAT ARE 6, WHAT ARE 7, WHAT ARE 8, WHAT ARE 9, WHAT ARE0, WHAT ARE1, WHAT ARE2, WHAT ARE3, WHAT ARE4, WHAT ARE5, WHAT ARE6, WHAT ARE7, WHAT ARE8, WHAT ARE9, WHAT AR0, WHAT AR1, WHAT AR2, WHAT AR3, WHAT AR4, WHAT AR5, WHAT AR6, WHAT AR7, WHAT AR8, WHAT AR9, WHAT A0, WHAT A1, WHAT A2, WHAT A3, WHAT A4, WHAT A5, WHAT A6, WHAT A7, WHAT A8, WHAT A9, WHAT 0, WHAT 1, WHAT 2, WHAT 3, WHAT 4, WHAT 5, WHAT 6, WHAT 7, WHAT 8, WHAT 9, WHAT0, WHAT1, WHAT2, WHAT3, WHAT4, WHAT5, WHAT6, WHAT7, WHAT8, WHAT9, WHA0, WHA1, WHA2, WHA3, WHA4, WHA5, WHA6, WHA7, WHA8, WHA9, WH0, WH1, WH2, WH3, WH4, WH5, WH6, WH7, WH8, WH9, W0, W1, W2, W3, W4, W5, W6, W7, W8, W9

Leaderboard

var QUESTION_ID=92156,OVERRIDE_USER=58717;function answersUrl(e){return"https://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"https://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 id="answer-list"> <h2>Leaderboard</h2> <table class="answer-list"> <thead> <tr><td></td><td>Author</td><td>Language</td><td>Size</td></tr></thead> <tbody id="answers"> </tbody> </table> </div><div id="language-list"> <h2>Winners by Language</h2> <table class="language-list"> <thead> <tr><td>Language</td><td>User</td><td>Score</td></tr></thead> <tbody id="languages"> </tbody> </table> </div><table style="display: none"> <tbody id="answer-template"> <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 id="language-template"> <tr><td>{{LANGUAGE}}</td><td>{{NAME}}</td><td>{{SIZE}}</td><td><a href="{{LINK}}">Link</a></td></tr></tbody> </table>

\$\endgroup\$
  • 1
    \$\begingroup\$ Could you post the full sequence all at once? Possibly with some more samples? Also, what may the input possibly contain? \$\endgroup\$ – DJMcMayhem Sep 3 '16 at 20:55
  • 1
    \$\begingroup\$ Is a lack of separator (e.g. INVALID0INVALID1INVALID2) a valid output format? \$\endgroup\$ – DLosc Sep 3 '16 at 21:02
  • \$\begingroup\$ @DLosc Yes, it is. \$\endgroup\$ – Buffer Over Read Sep 3 '16 at 21:03
  • 3
    \$\begingroup\$ Just so you know, it is generally discouraged to accept an answer so quickly after posting the challenge. Accepting too early can discourage users from posting new answers. That's not to say you can't keep the accepted answer, but I'd encourage you to wait longer next time. \$\endgroup\$ – DJMcMayhem Sep 4 '16 at 4:21
  • \$\begingroup\$ @DJMcMayhem Okay! \$\endgroup\$ – Buffer Over Read Sep 4 '16 at 16:47

39 Answers 39

1
\$\begingroup\$

Racket 113 bytes

(λ(s)(let((n(string-length s)))(for*/list((i n)(j 10))(string-append(substring s 0(- n i))(number->string j)))))

More readable form:

(define(f s)
  (let ((n (string-length s)))
  (for*/list ((i n)(j 10))
    (string-append
     (substring s 0(- n i))
     (number->string j)))))

Usage:

(f "Invalid")
'("Invalid0"
  "Invalid1"
  "Invalid2"
  "Invalid3"
  "Invalid4"
  "Invalid5"
  "Invalid6"
  "Invalid7"
  "Invalid8"
  "Invalid9"
  "Invali0"
  "Invali1"
  "Invali2"
  "Invali3"
  "Invali4"
  "Invali5"
  "Invali6"
  "Invali7"
  "Invali8"
  "Invali9"
  "Inval0"
  "Inval1"
  "Inval2"
  "Inval3"
  "Inval4"
  "Inval5"
  "Inval6"
  "Inval7"
  "Inval8"
  "Inval9"
  "Inva0"
  "Inva1"
  "Inva2"
  "Inva3"
  "Inva4"
  "Inva5"
  "Inva6"
  "Inva7"
  "Inva8"
  "Inva9"
  "Inv0"
  "Inv1"
  "Inv2"
  "Inv3"
  "Inv4"
  "Inv5"
  "Inv6"
  "Inv7"
  "Inv8"
  "Inv9"
  "In0"
  "In1"
  "In2"
  "In3"
  "In4"
  "In5"
  "In6"
  "In7"
  "In8"
  "In9"
  "I0"
  "I1"
  "I2"
  "I3"
  "I4"
  "I5"
  "I6"
  "I7"
  "I8"
  "I9")
\$\endgroup\$
  • \$\begingroup\$ Do you need all of the newlines? \$\endgroup\$ – NoOneIsHere Sep 4 '16 at 14:33
  • \$\begingroup\$ I mean in the source code. I presume the code is not a list. \$\endgroup\$ – NoOneIsHere Sep 4 '16 at 15:25
  • \$\begingroup\$ I have removed the newlines and reduced the bytes. Thanks. \$\endgroup\$ – rnso Sep 4 '16 at 15:29
1
\$\begingroup\$

sed 107

s/$/HH/;:;s/..$/0/p;s/.$/1/p;s/.$/2/p;s/.$/3/p;s/.$/4/p
s/.$/5/p;s/.$/6/p;s/.$/7/p;s/.$/8/p;s/.$/9/p;/.../b
\$\endgroup\$
1
\$\begingroup\$

Brainf*ck, 217 bytes

>,[>,----------[++++++++++>]<]>++++++++[<++++++>-]>++++++++++[>+>+<<-]>>[<<+>>-]<<[-<<[<]>[.>]<+>>>.<>>[-]+<<[>>-]<[<]>>>[[-]<<++++++++++<<----------<[-]<[[>+<-]<]>>[>]>>>[-]+<<<<<[>>>>>[-]]>[[>]>>>>]<[<<[-]>>[-]]]<<]

Not really a competitive entry, I just wanted to see if I could do it. Not a trivial brainf*ck program! Nested conditionals and all. It prints the output separated by newlines instead of comma, space. Here's my annotated version:

[
    We'll have a string in the first n positions on the tape.
    One after the end of the string, we'll put a 48 '0' and do a loop adding
    1 to it 9 times, then loop back and delete the last letter of the string.
]

>,[>,----------[++++++++++>]<]     
[Read line of input and leave a 0 at the beginning to navigate back.]

>++++++++[<++++++>-]
>++++++++++[>+>+<<-]>>[<<+>>-]<<    we have {string} {48} {0} {10} {10}

Now we should have {string} {48} {0} {9}

[
    -<<[<]
    >[.>]
    <+>>>.<   printed out string and we're at the cell 3 past the end
    >>[-]+   put a 1 in the cell 2 to the right of this
    <<[>>-]  clear that cell if this one is not zero
    <[<]        sets us at the cell 2 after the end of the string (0)

    >>> 
    [         If the cell we flagged is still flagged we counted all the way up
              to 9
        [-]   Clear the flag
        <<++++++++++        Add 10 back to the cell 3 off end of string
        <<----------        subtract 10 to get 48 in the last cell of string
                            again
        <[-]                Clear last cell in string

        <[[>+<-]<]          
        [copy cells into the next one all the way down; entire string has been
         moved up one place now.]


        >>[>]     seek back to end of string
        >>>[-]+<<<<<    set a flag in the cell three after this (unused)
        [>>>>>[-]]      if there is a letter left clear the flag
        >[[>]>>>>]<     this line puts us at the cell with the flag no matter
                        what we did at the last line

        [               If the flag is set we're all out of string
            <<[-]>>[-]  Clear the 3rd cell past the end and the flag; pointer is
                        at the flag cell like it would have been if we never
                        entered the loop
        ]
    ]
    <<                  Puts us at the cell 3 after the end
]

Input is the string followed by optionally a newline (output is prettier) and then EOF.

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

Clojure, 68 bytes

#(for[i(reverse(range(count %)))j(range 10)](str(subs % 0(inc i))j))

Plain boring for, couldn't make it shorter by (iterate butlast %) or others :/

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

R, 72 bytes

Reads input from stdin and returns an R-vector of the desired output:

unname(unlist(lapply(Map(substr,x<-scan(,""),1,nchar(x):1),paste0,0:9)))

Example output for input hello:


 [1] "hello0" "hello1" "hello2" "hello3" "hello4" "hello5" "hello6" "hello7" "hello8" "hello9"
[11] "hell0"  "hell1"  "hell2"  "hell3"  "hell4"  "hell5"  "hell6"  "hell7"  "hell8"  "hell9" 
[21] "hel0"   "hel1"   "hel2"   "hel3"   "hel4"   "hel5"   "hel6"   "hel7"   "hel8"   "hel9"  
[31] "he0"    "he1"    "he2"    "he3"    "he4"    "he5"    "he6"    "he7"    "he8"    "he9"   
[41] "h0"     "h1"     "h2"     "h3"     "h4"     "h5"     "h6"     "h7"     "h8"     "h9" 
\$\endgroup\$
0
\$\begingroup\$

Perl 6, 17 bytes

{m:ex/^.+/X~ ^10}

Try it online!

How it works:

{               }  # A lambda
 m   /   /          # Apply a regex to the lambda argument,
      ^.+           #   matching prefixes of the string,
  :ex               #   with exhaustive matching (i.e. *all* possible prefixes).
             ^10    # Range from 0 to 9.
          X~        # Take the Cartesian product, and concatenate each result.

In case returning a list is not acceptable, this 20 byte version returns a space-separated string:

{~(m:ex/^.+/X~ ^10)}
\$\endgroup\$
0
\$\begingroup\$

Python 3, 76 73 bytes

s=input()
for i in range(len(s),0,-1):
 for j in range(10):print(s[:i],j)

Try it online!

\$\endgroup\$
  • 1
    \$\begingroup\$ You can get rid of the newline and tab in the second for loop. for j in range(10): print(s[:i],j) \$\endgroup\$ – Wheat Wizard Jan 21 '17 at 21:57
0
\$\begingroup\$

Retina, 28 bytes

Thanks to Martin for helping make it valid.

$
0
;{:G`
(?<=.).9
#
}T`#d`d

Try it online

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

Python 2, 84 Bytes

Ah, the old nested merged list generator lambda-roo

c="".join
lambda s:c([c([s[:l]+`i`for i in range(10)])for l in range(len(s),0,-1)])
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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