Expand a C array

36

5

In the C programming language, arrays are defined like this:

int foo[] = {4, 8, 15, 16, 23, 42};      //Foo implicitly has a size of 6

The size of the array is inferred from the initializing elements, which in this case is 6. You can also write a C array this way, explicitly sizing it then defining each element in order:

int foo[6];        //Give the array an explicit size of 6
foo[0] = 4;
foo[1] = 8;
foo[2] = 15;
foo[3] = 16;
foo[4] = 23;
foo[5] = 42;

The challenge

You must write a program or function that expands arrays from the first way to the second. Since you are writing a program to make code longer, and you love irony, you must make your code as short as possible.

The input will be a string representing the original array, and the output will be the expanded array-definition. You can safely assume that the input will always look like this:

<type> <array_name>[] = {<int>, <int>, <int> ... };

"Type" and "array_name" will made up entirely of alphabet characters and underscores _. The elements of the list will always be a number in the range -2,147,483,648 to 2,147,483,647. Inputs in any other format do not need to be handled.

The whitespace in your output must exactly match the whitespace in the test output, although a trailing newline is allowed.

Test IO:

#in
short array[] = {4, 3, 2, 1};

#out
short array[4];
array[0] = 4;
array[1] = 3;
array[2] = 2;
array[3] = 1;


#in
spam EGGS[] = {42};

#out
spam EGGS[1];
EGGS[0] = 42;


#in
terrible_long_type_name awful_array_name[] = {7, -8, 1337, 0, 13};

#out
terrible_long_type_name awful_array_name[5];
awful_array_name[0] = 7;
awful_array_name[1] = -8;
awful_array_name[2] = 1337;
awful_array_name[3] = 0;
awful_array_name[4] = 13;

Submissions in any language are encouraged, but bonus points if you can do it in C.

Leaderboard:

Here is a leaderboard showing the top answers:

var QUESTION_ID=77857,OVERRIDE_USER=31716;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>

James

Posted 2016-04-15T18:38:25.010

Reputation: 54 537

2Are the spaces between the array index, the equals sign, and the values required in the output? For example, would foo[0]=1; be acceptable? – Mego – 2016-04-15T18:42:40.927

@Mego That would not be acceptable. The whitespace is required. I'll edit that in. – James – 2016-04-15T18:45:27.337

Trailing newline allowed? – Luis Mendo – 2016-04-15T19:06:38.797

Are functions allowed? – Mego – 2016-04-15T20:28:48.763

Is it okay to return the output instead of printing it? (in case of functions) – vaultah – 2016-04-15T20:37:01.063

@vaultah By default that is assumed to be okay.

– James – 2016-04-15T20:40:55.683

Is there a maximum number of elements that need to be handled? In particular, the current examples all have single-digit sizes, but that doesn't seem like it was intentional. – DLosc – 2016-04-16T02:20:26.503

It's worth mentioning that -2147483648 is an invalid literal, so given the imput format, the valid range should be -2147483647 to 2147483647. – MooseBoys – 2016-04-16T18:38:20.477

Can the type name contain uppercase letters? – 12Me21 – 2018-03-08T16:56:54.823

Answers

12

Pyth, 44 bytes

++Khcz\]lJ:z"-?\d+"1"];"VJs[ecKd~hZ"] = "N\;

Test suite

Regular expression and string chopping. Not particularly clever.

Explanation:

++Khcz\]lJ:z"-?\d+"1"];"VJs[ecKd~hZ"] = "N\;
                                                Implicit: z = input()
    cz\]                                        Chop z on ']'
   h                                            Take string before the ']'
  K                                             Store it in K
 +                                              Add to that
         :z"-?\d+"1                             Find all numbers in the input
        J                                       Store them in J
       l                                        Take its length.
+                  "];"                         Add on "];" and print.
                       VJ                       For N in J:
                         s[                     Print the following, concatenated:
                            cKd                 Chop K on spaces.
                           e                    Take the last piece (array name)
                               ~hZ              The current interation number
                                  "] = "        That string
                                        N       The number from the input
                                         \;     And the trailing semicolon.

isaacg

Posted 2016-04-15T18:38:25.010

Reputation: 39 268

This answer is the thorn in my side. I thought I would be able to win it in vim, but for the life of me, I can't get the last 2-3 bytes off. =D Nice answer! – James – 2016-04-16T15:06:22.837

28

Vim, 54, 52, 49 47 keystrokes


2wa0<esc>qqYp<c-a>6ldf @qq@q$dT]dd:%norm dwf{xwC;<CR>gg"0P

Explanation:

2wa0<esc>                     'Move 2 words forward, and insert a 0.
         qq                   'Start recording in register Q
           Yp                 'Duplicate the line
             <c-a>6l          'Increment the next number then move 6 spaces right
                    df        'Delete until the next space
                       @qq@q  'Recursively call this macro

Now our buffer looks like this:

int foo[0] = {4, 8, 15, 16, 23, 42};
int foo[1] = {8, 15, 16, 23, 42};
int foo[2] = {15, 16, 23, 42};
int foo[3] = {16, 23, 42};
int foo[4] = {23, 42};
int foo[5] = {42};
int foo[6] = {42};

and our cursor is on the last line.

Second half:

$                           'Move to the end of the line
 dT]                        'Delete back until we hit a ']'
    dd                      'Delete this whole line.
      :%norm         <CR>   'Apply the following keystrokes to every line:
             dw             'Delete a word (in this case "int")
               f{x          '(f)ind the next '{', then delete it.
                  wC;       'Move a word, then (C)hange to the end of this line, 
                            'and enter a ';'

Now everything looks good, we just need to add the original array-declaration. So we do:

gg        'Move to line one
  "0P     'Print buffer '0' behind us. Buffer '0' always holds the last deleted line,
          'Which in this case is "int foo[6];"

James

Posted 2016-04-15T18:38:25.010

Reputation: 54 537

3Whenever I read a vim-golf, I realise all the coding I do (mostly keyboard commands in my miscellaneous GUI editors) looks much like this, and my mind kind of bends for a minute (Vim is just turing complete (and cooler)) :P – cat – 2016-04-15T20:33:16.140

I can't get this to work - when I type the first "@q" (of "@qq@q"), the macro runs then, and apparently runs further than it should, getting things like int foo[6] = { and ending with int foo[12 (cursor on the "2") – LordAro – 2016-04-16T12:08:28.727

@LordAro I probably should have mentioned that. That's because there's already a macro in q, and that runs while you're recording, messing it up. I explained how to get around that here: http://codegolf.stackexchange.com/a/74663/31716

– James – 2016-04-16T15:01:03.783

Hmm. OK, it now doesn't run the macro "early", but i still get the above output (runs to 12, instead of stopping at 6, which is missing anything beyond '{') – LordAro – 2016-04-16T17:15:37.267

1@LordAro Oh, duh, I know what's causing that. I changed df<space> to dW to save a byte, but I forgot that df<space> will break out of the macro on line 6, but dW doesn't. I'll rollback a revision. Thanks for pointing that out! – James – 2016-04-16T17:24:05.780

That's got it :) – LordAro – 2016-04-16T17:31:39.957

1While this isn't quite the shortest answer, it's by far the most impressive. – isaacg – 2016-04-26T20:17:28.907

@isaacg Personally, I like this one more.

– James – 2016-04-26T20:26:57.890

As written, this 47 has 2 problems. First, if a line has a negative number, the wC; in your :%norm deletes the number and leaves the sign. Second, you're "0Ping at the end, even though you need to paste from your dd, so you need "1P instead. – udioica – 2016-10-11T16:31:43.763

10

Retina, 108 104 100 69 bytes

Byte count assumes ISO 8859-1 encoding.

].+{((\S+ ?)+)
$#2];$1
+`((\w+\[).+;(\S+ )*)(-?\d+).+
$1¶$2$#3] = $4;

Beat this, PowerShell...

Code explanation

First line: ].+{((\S+ ?)+)

First, we need to keep the type, array name, and opening bracket (it saves a byte), so we don't match them. So we match the closing bracket, any number of characters, and an opening curly brace: ].+{. Then we match the number list. Shortest I've been able to find so far is this: ((\S+ ?)+). We match any number of non-space characters (this includes numbers, possible negative sign, and possible comma), followed by a space, that may or may not be there: \S+ ?. This group of characters is then repeated as many times as needed: (\S+ ?)+ and put into the large capturing group. Note that we don't match the closing curly brace or semicolon. Third line explanation tells why.

Second line: $#2];$1

Since we only matched a part of input, the unmatched parts will still be there. So we put the length of the list after the unmatched opening bracket: $#2. The replace modifier # helps us with that, as it gives us the number of matches a particular capturing group made. In this case capturing group 2. Then we put a closing bracket and a semicolon, and finally our whole list.

With input short array[] = {4, 3, 2, 1};, the internal representation after this replacing is:

short array[4];4, 3, 2, 1};

(note the closing curly brace and semicolon)

Third line: +`((\w+[).+;(\S+ )*)(-?\d+).+

This is a looped section. That means it runs until no stage in the loop makes a change to the input. First we match the array name, followed by an opening bracket: (\w+\[). Then an arbitrary number of any characters and a semicolon: .+;. Then we match the list again, but this time only numbers and the comma after each number, which have a space following them: (\S+ )*. Then we capture the last number in the list: (-?\d+) and any remaining characters behind it: .+.

Fourth line: $1¶$2$#3] = $4;

We then replace it with the array name and list followed by a newline: $1¶. Next, we put the array name, followed by the length of previously matched list, without the last element (essentially list.length - 1): $2$#3. Followed by a closing bracket and assigment operator with spaces, and this followed by the last element of our number list: ] = $4;

After first replacing, the internal representation looks like this:

short array[4];4, 3, 2, 
array[3] = 1;

Note that the closing curly brace and the semicolon disappeared, thanks to the .+ at the end of third line. After three more replacings, the internal representation looks like this:

short array[4];
array[0] = 4;
array[1] = 3;
array[2] = 2;
array[3] = 1;

Since there's nothing more to match by third line, fourth doesn't replace anything and the string is returned.

TL;DR: First we change up the int list format a bit. Then we take the last element of the list and the name, and put them after the array initialization. We do this until the int list is empty. Then we give the changed code back.

Try it online!

daavko

Posted 2016-04-15T18:38:25.010

Reputation: 824

Someone beat me to it....:( – CalculatorFeline – 2016-04-15T21:01:19.740

M!\`` andG`` are similar, but not quite the same. Be careful. – CalculatorFeline – 2016-04-15T21:05:46.530

The third line explanation confuses me. The first item is the only one without a space behind it, not the last. – CalculatorFeline – 2016-04-17T03:30:43.670

@CatsAreFluffy I tried changing up the wording a bit just now. What I meant was a space following a number, not preceding it. I guess I didn't fully realise the meaning of "behind". I really shouldn't write code explanations at 2 am. – daavko – 2016-04-17T03:51:16.390

@daavko "Behind" normally means "after" i.e. "following", in colloquial English. You were fine. – Fund Monica's Lawsuit – 2016-04-18T11:14:23.333

Ooof ... I don't think PowerShell can get down to that. Have a +1 – AdmBorkBork – 2016-04-18T12:47:00.347

9

V, 37 Bytes

2Eé0òYp6ldf ò$dT]ddÎdwf{xwC;
gg"1P

V is a 2D, string based golfing language that I wrote, designed off of vim. This works as of commit 17.

Explanation:

This is pretty much a direct translation of my vim answer, albeit significantly shorter.

2E                               "Move to the end of 2 words forward.
  é0                             "Insert a single '0'
    ò       ò                    "Recursively do:
     Yp6ldf                      "Yank, paste, move 6 right, delete until space.
             $dT]                "Move to the end of line, delete backwards until ']'
                 dd              "Delete this line
                   Î             "Apply the following to every line:
                    dwf{xwC;<\n> "Delete word, move to '{' and delete it, Change to end of line, and enter ';'

Then we just have:

gg"1P     "Move to line 1, and paste buffer '1' behind us.

Since this unicode madness can be hard to enter, you can create the file with this reversible hexdump:

00000000: 3245 e930 f259 7001 366c 6466 20f2 2464  2E.0.Yp.6ldf .$d
00000010: 545d 6464 ce64 7766 7b78 7743 3b0d 6767  T]dd.dwf{xwC;.gg
00000020: 2231 500a                                "1P.

This can be run by installing V and typing:

python main.py c_array.v --f=file_with_original_text.txt

James

Posted 2016-04-15T18:38:25.010

Reputation: 54 537

1Designed off of vim. 2 notes: 1. most people says from not off of, and 2. why didn't this exist. +1 – Rɪᴋᴇʀ – 2016-04-19T14:33:34.367

8

C, 215 bytes, 196 bytes

19 bytes saved thanks to @tucuxi!

Golfed:

char i[99],o[999],b[99],z[99];t,x,n,c;main(){gets(i);sscanf(i,"%s %[^[]s",b,z);while(sscanf(i+t,"%*[^0-9]%d%n",&x,&n)==1)sprintf(o,"%s[%d] = %d;\n",z,c++,x),t+=n;printf("%s %s[%d];\n%s",b,z,c,o);}

Ungolfed:

/*
 *  Global strings:
 *   i: input string
 *   o: output string
 *   b: input array type
 *   z: input array name
*/
char i[ 99 ], o[ 999 ], b[ 99 ], z[ 99 ];

/* Global ints initialized to zeros */
t, x, n, c;

main()
{
    /* Grab input string from stdin, store into i */
    gets( i );

    /* Grab the <type> <array_name> and store into b and z */
    sscanf( i, "%s %[^[]s", b, z );

    /* Grab only the int values and concatenate to output string */
    while( sscanf( i + t, "%*[^0-9]%d%n", &x, &n ) == 1 )
    {
        /* Format the string and store into a */
        sprintf( o, "%s[%d] = %d;\n", z, c++, x );

        /* Get the current location of the pointer */
        t += n;
    }

    /* Print the <type> <array_name>[<size>]; and output string */
    printf( "%s %s[%d];\n%s", b, z, c, o );
}

Link:

http://ideone.com/h81XbI

Explanation:

To get the <type> <array_name>, the sscanf() format string is this:

%s          A string delimited by a space
    %[^[]   The character set that contains anything but a `[` symbol
         s  A string of that character set

To extract the int values from the string int foo[] = {4, 8, 15, 16, 23, 42};, I essentially tokenize the string with this function:

while( sscanf( i + t, "%*[^0-9]%d%n", &x, &n ) == 1 )

where:

  • i is the input string (a char*)
  • t is the pointer location offset of i
  • x is the actual int parsed from the string
  • n is the total characters consumed, including the found digit

The sscanf() format string means this:

%*            Ignore the following, which is..
  [^0-9]      ..anything that isn't a digit
        %d    Read and store the digit found
          %n  Store the number of characters consumed

If you visualize the input string as a char array:

int foo[] = {4, 8, 15, 16, 23, 42};
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
00000000001111111111222222222233333
01234567890123456789012345678901234

with the int 4 being located at index 13, 8 at index 16, and so on, this is what the result of each run in the loop looks like:

Run 1)  String: "int foo[] = {4, 8, 15, 16, 23, 42};"
        Starting string pointer: str[ 0 ]
        Num chars consumed until after found digit: 14
        Digit that was found: 4
        Ending string pointer: str[ 14 ]

Run 2)  String: ", 8, 15, 16, 23, 42};"
        Starting string pointer: str[ 14 ]
        Num chars consumed until after found digit: 3
        Digit that was found: 8
        Ending string pointer: str[ 17 ]

Run 3)  String: ", 15, 16, 23, 42};"
        Starting string pointer: str[ 17 ]
        Num chars consumed until after found digit: 4
        Digit that was found: 15
        Ending string pointer: str[ 21 ]

Run 4)  String: ", 16, 23, 42};"
        Starting string pointer: str[ 21 ]
        Num chars consumed until after found digit: 4
        Digit that was found: 16
        Ending string pointer: str[ 25 ]

Run 5)  String: ", 23, 42};"
        Starting string pointer: str[ 25 ]
        Num chars consumed until after found digit: 4
        Digit that was found: 23
        Ending string pointer: str[ 29 ]

Run 6)  String: ", 42};"
        Starting string pointer: str[ 29 ]
        Num chars consumed until after found digit: 4
        Digit that was found: 42
        Ending string pointer: str[ 33 ]

homersimpson

Posted 2016-04-15T18:38:25.010

Reputation: 281

1you can avoid using strcat by concatenating o inside sprintf, via %s. This should shave around 7 chars. – tucuxi – 2016-04-18T10:45:52.447

@tucuxi Ah, good catch. Thanks! – homersimpson – 2016-04-18T18:53:07.213

7

C, 195 180 bytes

195-byte original:

golfed:

char*a,*b,*c,*d;j;main(i){scanf("%ms %m[^]]%m[^;]",&a,&b,&c);
for(d=c;*d++;i+=*d==44);printf("%s %s%d];\n",a,b,i);
for(d=strtok(c,"] =,{}");j<i;j++,d=strtok(0," ,}"))printf("%s%d] = %s;\n",b,j,d);}

ungolfed:

char*a,*b,*c,*d;
j;
main(i){
    scanf("%ms %m[^]]%m[^;]",&a,&b,&c); // m-modifier does its own mallocs
    for(d=c;*d++;i+=*d==44);            // count commas
    printf("%s %s%d];\n",a,b,i);        // first line
    for(d=strtok(c,"] =,{}");j<i;j++,d=strtok(0," ,}"))
        printf("%s%d] = %s;\n",b,j,d);  // each array value
}

The two shortcuts are using the m modifier to to get scanf's %s to allocate its own memory (saves declaring char arrays), and using strtok (which is also available by default, without includes) to do the number-parsing part.


180-byte update:

char*a,*b,*c,e[999];i;main(){scanf("%ms %m[^]]%m[^}]",&a,&b,&c);
for(c=strtok(c,"] =,{}");sprintf(e,"%s%s%d] = %s;\n",e,b,i++,c),
c=strtok(0," ,"););printf("%s %s%d];\n%s",a,b,i,e);}

ungolfed:

char*a,*b,*c,e[999];
i;
main(){
    scanf("%ms %m[^]]%m[^}]",&a,&b,&c);
    for(c=strtok(c,"] =,{}");sprintf(e,"%s%s%d] = %s;\n",e,b,i++,c),c=strtok(0," ,"););
    printf("%s %s%d];\n%s",a,b,i,e);
}

Uses bnf679's idea of appending to a string to avoid having to count commas.

tucuxi

Posted 2016-04-15T18:38:25.010

Reputation: 583

6

Python 3.6 (pre-release), 133

m,p=str.split,print;y,u=m(input(),'[');t,n=m(y);i=m(u[5:-2],', ')
l=len(i);p(t,n+f'[{l}];')
for x in range(l):p(n+f'[{x}] = {i[x]};')

Makes heavy use of f-strings.

Ungolfed version:

y, u = input().split('[')
t, n = y.split()
i = u[5:-2].split(', ')
l = len(i)
print(t, n + f'[{l}];')
for x in range(l):
    print(n + f'[{x}] = {i[x]};')

vaultah

Posted 2016-04-15T18:38:25.010

Reputation: 1 254

1Woah, I forgot about f-strings. Those are going to be super useful for golf! – Morgan Thrapp – 2016-04-15T20:27:13.623

I think you can save a byte by using a list comprehension instead of the normal loop – someonewithpc – 2016-04-16T00:30:44.853

@someonewithpc: nope, it would actually add 1 extra byte – vaultah – 2016-04-16T06:35:29.067

5

Ruby, 127 110 108 99 88 bytes

Anonymous function with a single argument as input. Full program, reads the input from STDIN. (If you pipe a file in, the trailing newline is optional.) Returns Prints the output string.

Took @TimmyD bragging about their solution beating all other non-esolangs as a challenge, and finally overcome the (at the time of writing) 114 byte Powershell solution they had posted. Cᴏɴᴏʀ O'Bʀɪᴇɴ's trick with splitting on ] and splicing the second half to get the numbers helped.

I need to use the splat operator more. It's so useful!

Borrowed a trick from @Neil's JavaScript ES6 answer to save more bytes by scanning for words instead of using gsub and split..

t,n,*l=gets.scan /-?\w+/;i=-1
puts t+" #{n}[#{l.size}];",l.map{|e|n+"[#{i+=1}] = #{e};"}

Value Ink

Posted 2016-04-15T18:38:25.010

Reputation: 10 608

AFAICT the task is to write a full program. – vaultah – 2016-04-15T20:20:19.510

@vaultah The default for code golf is a program or function – Mego – 2016-04-15T20:27:05.723

@Mego: the OP said "You must write a program" – vaultah – 2016-04-15T20:27:51.733

@vaultah normally I would say that code golf allows me to use a function, but a full program saved me 2 bytes, so why not? – Value Ink – 2016-04-15T20:28:02.893

Ooof ... I don't think PowerShell can get down to that. Have a +1 – AdmBorkBork – 2016-04-18T12:48:39.393

4

Pyth - 53 50 46 45 44 bytes

2 bytes saved thanks to @FryAmTheEggman.

+Jhcz\[+`]lKcPecz\{d\;j.es[ecJd`]kd\=dPb\;)K

Test Suite.

Maltysen

Posted 2016-04-15T18:38:25.010

Reputation: 25 023

4

05AB1E, 52 50 47 bytes

Code:

… = ¡`¦¨¨ð-',¡©gr¨s«„];«,®v¹ð¡¦¬s\¨N"] = "y';J,

Uses CP-1252 encoding. Try it online!.

Adnan

Posted 2016-04-15T18:38:25.010

Reputation: 41 965

1It's gotten to the point where I skip all the other answers just looking for your 05AB1E answers. The language absolutely fascinates me. – WorseDoughnut – 2016-04-18T19:41:08.773

1@WorseDoughnut Thank you! That is the nicest thing someone has ever said to me about 05AB1E :)! – Adnan – 2016-04-18T20:16:32.483

4

PowerShell v2+, 114 105 bytes

$a,$b,$c,$d=-split$args-replace'\[]';"$a $b[$(($d=-join$d|iex|iex).length)];";$d|%{"$b[$(($i++))] = $_;"}

Takes input string $args and -replaces the square bracket with nothing, then performs a -split on whitespace. We store the first bit into $a, the second bit into $b, the = into $c, and the array elements into $d. For the example below, this stores foo into $a and bar into $b, and all of the array into $d.

We then output our first line with "$a ..." and in the middle transform $d from a an array of strings of form {1,,2,,...100}; to a regular int array by -joining it together into one string, then running it through iex twice (similar to eval). We store that resultant array back in $d before calling the .length method to populate the appropriate number in between the [] in the output line.

We then send $d through a loop with |%{...}. Each iteration we output "$b..." with a counter variable $i encapsulated in brackets and the current value $_. The $i variable starts uninitialized (equivalent to $null) but the ++ will cast it to an int before the output, so it will start output at 0, all before incrementing $i for the next loop iteration.

All output lines are left on the pipeline, and output to the terminal is implicit at program termination.

Example

PS C:\Tools\Scripts\golfing> .\expand-a-c-array.ps1 "foo bar[] = {1, 2, 3, -99, 100};"
foo bar[5];
bar[0] = 1;
bar[1] = 2;
bar[2] = 3;
bar[3] = -99;
bar[4] = 100;

AdmBorkBork

Posted 2016-04-15T18:38:25.010

Reputation: 41 581

Whew! I managed to get my Ruby answer golfed past yours by taking your comment about beating the other non-esolangs as a challenge! Good work, though, +1. – Value Ink – 2016-04-15T20:19:25.313

@KevinLau Thanks! Back at ya with a 105 now. ;-) – AdmBorkBork – 2016-04-15T20:34:32.590

I'll call your 105, and raise you a 99! :D – Value Ink – 2016-04-15T21:08:54.460

No longer closing in on beating Retina. – CalculatorFeline – 2016-04-17T03:33:26.043

4

JavaScript (ES6), 100 bytes

(s,[t,n,...m]=s.match(/-?\w+/g))=>t+` ${n}[${m.length}];`+m.map((v,i)=>`
${n}[${i}] = ${v};`).join``

Since only the words are important, this works by just matching all the words in the original string, plus leading minus signs, then building the result. (I originally thought I was going to use replace but that turned out to be a red herring.)

Neil

Posted 2016-04-15T18:38:25.010

Reputation: 95 035

[t,n,...m] almost a mystic vision – edc65 – 2016-04-16T00:01:08.087

4

Pip, 48 47 bytes

qR`(\S+)(. = ).(.+)}`{[b#Yd^k']';.n.b.,#y.c.y]}

Takes input from stdin and prints to stdout.

Explanation

Tl;dr: Does a regex replacement, using capture groups and a callback function to construct the result.

The q special variable reads a line of input. The regex is (\S+)(. = ).(.+)}, which matches everything except the type (including trailing space) and the final semicolon. Using the first example from the question, the capturing groups get foo[, ] = , and 4, 8, 15, 16, 23, 42.

The replacement is the return value of the unnamed function {[b#Yd^k']';.n.b.,#y.c.y]}, which is called with the whole match plus the capturing groups as arguments. Thus, within the function, b gets capture group 1, c gets group 2, and d gets group 3.

We construct a list, the first three items of which will be "foo[", 6, and "]". To get the 6, we split d on the built-in variable k = ", ", Yank the resulting list of integers into the y variable for future use, and take the length (#). '] is a character literal.

What remains is to construct a series of strings of the form ";\nfoo[i] = x". To do so, we concatenate the following: ';, n (a built-in for newline), b (1st capture group), ,#y (equivalent to Python range(len(y))), c (2nd capture group), and y. Concatenation works itemwise on lists and ranges, so the result is a list of strings. Putting it all together, the return value of the function will be a list such as this:

["foo[" 6 "]"
 [";" n "foo[" 0 "] = " 4]
 [";" n "foo[" 1 "] = " 8]
 [";" n "foo[" 2 "] = " 15]
 [";" n "foo[" 3 "] = " 16]
 [";" n "foo[" 4 "] = " 23]
 [";" n "foo[" 5 "] = " 42]
]

Since this list is being used in a string Replacement, it is implicitly cast to a string. The default list-to-string conversion in Pip is concatenating all the elements:

"foo[6];
foo[0] = 4;
foo[1] = 5;
foo[2] = 15;
foo[3] = 16;
foo[4] = 23;
foo[5] = 42"

Finally, the result (including the type and the final semicolon, which weren't matched by the regex and thus remain unchanged) is auto-printed.

DLosc

Posted 2016-04-15T18:38:25.010

Reputation: 21 213

4

Perl 5.10, 73 72 68 66 + 1 (for -n switch) = 67 bytes

perl -nE '($t,$n,@a)=/[-[\w]+/g;say"$t $n".@a."];";say$n,$i++,"] = $_;"for@a'

This is a nice challenge for Perl and the shortest among general-purpose languages so far. Equivalent to

($t, $n, @a) = /[-[\w]+/g;
say "$t $n" . @a . "];";
say $n, $i++, "] = $_;" for @a;

nwellnhof

Posted 2016-04-15T18:38:25.010

Reputation: 10 037

3

C,278 280 bytes

golfed:

x,e,l,d;char *m,*r,*a;char i[999];c(x){return isdigit(x)||x==45;}main(){gets(i);m=r=&i;while(*r++!=32);a=r;while(*++r!=93);l=r-a;d=r-m;for(;*r++;*r==44?e++:1);printf("%.*s%d];\n",d,m,e+1);r=&i;while(*r++){if(c(*r)){m=r;while(c(*++r));printf("%.*s%d] = %.*s;\n",l,a,x++,r-m,m);}}}

ungolfed:

/* global ints
 * x = generic counter
 * e = number of elements
 * l = length of the array type
 * d = array defination upto the first '['
 */
x,e,l,d;
/* global pointers
 * m = memory pointer
 * r = memory reference / index
 * a = pointer to the start of the array type string
 */
char *m,*r,*a;
/* data storage for stdin */
char i[999];
c(x){return isdigit(x)||x=='-';}
main(){
    gets(i);
    m=r=&i;
    while(*r++!=32);                // skip first space
    a=r;
    while(*++r!=93);                // skip to ']'
    l=r-a;
    d=r-m;
    for(;*r++;*r==44?e++:1);        // count elements
    printf("%.*s%d];\n",d,m,e+1);   // print array define
    r=&i;
    while(*r++) {                   // print elements
        if(c(*r)) {                 // is char a - or a digit?
            m=r;
            while(c(*++r));         // count -/digit chars
            printf("%.*s%d] = %.*s;\n",l,a,x++,r-m,m);
        }
    }
}

While working on this someones posted a shorter version using sscanf for the parsing rather than using data pointers... nice one!

UPDATE: Spotted missing spaces around the equals in the element printing, IDE online link: http://ideone.com/KrgRt0 . Note this implementation does support negative numbers...

bnf679

Posted 2016-04-15T18:38:25.010

Reputation: 51

2

Julia, 154 134 101 bytes

f(s,c=matchall(r"-?\w+",s),n=endof(c)-2)=c[]" "c[2]"[$n];
"join([c[2]"[$i] = "c[i+3]";
"for i=0:n-1])

This is a function that accepts a string and returns a string with a single trailing newline.

Ungolfed:

function f(s, c = matchall(r"-?\w+", s), n = endof(c) - 2)
    c[] " " c[2] "[$n];\n" join([c[2] "[$i] = " x[i+3] ";\n" for i = 0:n-1])
end

We define c to be an array of matches of the input on the regular expression -?\w+. It caputres the type, array name, then each value. We store n as the length of c - 2, which is the number of values. The output is constructed as the type, name and length string interpolated, combined with each definition line separated by newlines. For whatever reason, c[] is the same as c[1].

Saved 32 bytes with help from Dennis!

Alex A.

Posted 2016-04-15T18:38:25.010

Reputation: 23 761

2

JavaScript ES6, 134 132 130 129 bytes

Saved 1 byte thanks to Neil.

x=>(m=x.match(/(\w+) (\w+).+{(.+)}/),m[1]+` `+(q=m[2])+`[${q.length-1}];
`+m[3].split`, `.map((t,i)=>q+`[${i}] = ${t};`).join`
`)

Conor O'Brien

Posted 2016-04-15T18:38:25.010

Reputation: 36 228

Shouldn't \[${i}] = `+t+";"be`[${i}] = ${t};``? – Neil – 2016-04-15T21:13:03.903

@Neil Thanks, saved a byte! – Conor O'Brien – 2016-04-15T21:15:44.923

2

Awk, 101 bytes

{FS="[^[:alnum:]_-]+";printf"%s %s[%d];\n",$1,$2,NF-3;for(i=3;i<NF;i++)printf$2"[%d] = %d;\n",i-3,$i}

More readably:

{
FS="[^[:alnum:]_-]+"
printf "%s %s[%d];\n", $1, $2, NF - 3
for (i=3; i < NF; i++)
    printf $2"[%d] = %d;\n", i-3, $i
}
  • I set the field separator to everything except alphabets, digits, the underscore and -. So, the fields would be the type name, the variable name, and the numbers.
  • The number of fields will be 1 (for the type) + 1 (for the name) + N (numbers) + 1 (an empty field after the trailing };). So, the size of the array is NF - 3.
  • Then it's just printing a special line for the declaration, and looping over the numbers.
  • I should assign FS either when invoking awk (using -F) or in a BEGIN block. In the interests of brevity, ….

muru

Posted 2016-04-15T18:38:25.010

Reputation: 331

1Actually, FS must be assigned either in BEGIN or using -F otherwise it won't be used to split the first line, and since there is only 1 line of input... – Robert Benson – 2017-05-08T21:00:49.077

@RobertBenson you're right, so the command would be awk '-F[^[:alnum:]_-]+' '{printf"%s %s[%d];\n",$1,$2,NF-3;for(i=3;i<NF;i++)printf$2"[%d] = %d;\n",i-3,$i}', which is 102 bytes not counting awk itself. Hmmm. Do I get to exclude the quotes? – muru – 2017-05-11T05:14:42.600

Yes, you can exclude quotes. You sometimes list it as C+O bytes where C and O represent the bytes from code and options respectively. Of course I generally just use a BEGIN block, so I don't have to think about it. :p – Robert Benson – 2017-05-11T17:40:47.947

2

D, 197, 188 bytes

import std.array,std.stdio;void main(){string t,n,e;readf("%s %s] = {%s}",&t,&n,&e);auto v=e.replace(",","").split;writeln(t,' ',n,v.length,"];");foreach(i,c;v)writeln(n,i,"] = ",c,";");}

or ungolfed:

import std.array, std.stdio;

void main() {
    string type, nameAndBracket, elems;
    readf("%s %s] = {%s}", &type, &nameAndBracket, &elems);

    // remove all commas before splitting the string into substrings
    auto vector = elems.replace(",","").split();

    // writeln is shorter than fln by 1 char when filled in
    writeln(type, ' ', nameAndBracket, vector.length, "];");

    // print each element being assigned
    foreach(index, content; vector)
        writeln(nameAndBraket, index, "] = ", content, ";");
}

Ben Perlin

Posted 2016-04-15T18:38:25.010

Reputation: 69

I don't know D, but could you read the opening square bracket as part of the name? That would save you having to write a square bracket separately later. – DLosc – 2016-04-17T21:09:23.533

2

bash, 133 129 bytes

read -a l
d="${l[@]:0:2}"
e=("${l[@]:3}")
echo "${d%?}${#e[@]}];"
for i in "${!e[@]}"
{
echo "${l[0]}[$i] = ${e[$i]//[!0-9]/};"
}

First attempt, sure its posible to get it shorter.

bnf679

Posted 2016-04-15T18:38:25.010

Reputation: 51

1

Python 2, 159 bytes

s=input().split()
t,n,v=s[0],s[1][:-2],''.join(s[3:])
a=v[1:-2].split(',')
print'%s %s[%d];'%(t,n,len(a))
for i in range(len(a)):print'%s[%d] = %s;'%(n,i,a[i])

Try it online

Thanks Kevin Lau for some golfing suggestions

Mego

Posted 2016-04-15T18:38:25.010

Reputation: 32 998

1

Python 3, 116 bytes

t,v,_,*l=input().split();v=v[:-1]+'%s]'
print(t,v%len(l)+';');i=0
for x in l:print(v%i,'= %s;'%x.strip('{,};'));i+=1

Splits the input into the type, name, and the list of numbers. After printing the array declaring, prints the elements by manually enumerating through the numbers, removing excess punctuation that attached to the first and last one.

A different approach in Python 2 came out to 122 bytes:

a,b=input()[:-2].split('] = {')
l=eval(b+',')
print a+`len(l)`+"];"
for y in enumerate(l):print a.split()[1]+'%s] = %s;'%y

The idea is to eval the list of numbers as a tuple, with a comma at the end so that a single number is recognized as a type. The enumerated list of numbers provides tuples to string-format in.

xnor

Posted 2016-04-15T18:38:25.010

Reputation: 115 687

1

PHP, 143 bytes

Golfed

<?$t=count($n=explode(' ',preg_replace('/[^\s\w]/','',$argv[1])))-3;echo"$n[0] {$n[1]}[$t];";for($i=2;$t>$j=++$i-3;)echo$n[1]."[$j] = $n[$i];";

Ungolfed

<?  
$t = count(                                  // Get the number of elements for our array...
    $n = explode(' ',                            // After split the input on whitespace...
    preg_replace('/[^\s\w]/','',$argv[1])))-3;  // After removing all special characters.
echo "$n[0] {$n[1]}[$t];";                     // First line is type, name, and count.
for($i=2;                                        // Loop through array elements
    $t > $j = ++$i-3;)                         // Assign j to be the actual index for our new array
    echo $n[1]."[$j] = $n[$i];";                // Print each line

Input is taken through command line argument. Sample:

C:\(filepath)>php Expand.php "int foo[] = {4,8,15,16,23,42};"

Output:

int foo[6];foo[0] = 4;foo[1] = 8;foo[2] = 15;foo[3] = 16;foo[4] = 23;foo[5] = 42;

Xanderhall

Posted 2016-04-15T18:38:25.010

Reputation: 1 236

0

Clojure, 115 bytes

#(let[[t n & v](re-seq #"-?\w+"%)](apply str t" "n\[(count v)"];\n"(map(fn[i v](str n"["i"] = "v";\n"))(range)v))))

I wasn't able to nicely merge awful_array_name[5]; and awful_array_name[0] = 7; parts so that they'd re-use code :/

NikoNyrh

Posted 2016-04-15T18:38:25.010

Reputation: 2 361

0

MATL, 68 64 58 bytes

'\w+'XX2:H#)XKxXIZc'['KnV'];'v!K"I2X)'['X@qV'] = '@g';'6$h

This isn't C, but it does use C-like sprintf function Nah, that was wasting 4 bytes.

Try it online!

          % Take input implicitly
'\w+'XX   % Find substrings that match regex '\w+'. Gives a cell array
2:H#)     % Split into a subarray with the first two substrings (type and name), and 
          % another with the rest (numbers that form the array)
XKx       % Copy the latter (numbers) into clipboard K. Delete it
XI        % Copy the former (type and name) into clipboard I
Zc        % Join the first two substrings with a space
'['       % Push this string
K         % Paste array of numbers
nV        % Get its length. Convert to string
'];'      % Push this string
v!        % Concatenate all strings up to now. Gives first line of the output
K"        % For each number in the array
  I2X)    %   Get name of array as a string
  '['     %   Push this string
  X@qV    %   Current iteration index, starting at 0, as a string
  '] = '  %   Push this string
  @g      %   Current number of the array, as a string
  ';'     %   Push this string
  5$h     %   Concatenate top 6 strings. This is a line of the output
          % Implicity end for each
          % Implicitly display

Luis Mendo

Posted 2016-04-15T18:38:25.010

Reputation: 87 464