8
\$\begingroup\$

What is the shortest C# statement that will convert a string representation of a number to an int[] that contains each digit?

string number = "1234";
number.Array.ConvertAll(line.ToCharArray(), c => Int32.Parse(c.ToString()));
number.ToCharArray().Select(i => Convert.ToInt32(i.ToString())).ToArray();
number.Select(i => Int32.Parse(i.ToString())).ToArray();
number.Select(i => i - '0').ToArray();

Can anyone beat my last approach?

\$\endgroup\$
  • \$\begingroup\$ You might want to not specify the language, as this is CodeGolf. \$\endgroup\$ – phase Jul 17 '15 at 6:02
  • 1
    \$\begingroup\$ @phase you have to be kidding me. the language is in the title and the tag. \$\endgroup\$ – JJS Jul 17 '15 at 6:04
  • 2
    \$\begingroup\$ @phase's comment refers to the fact that 99% of the "questions" on this site are actually programming contests. However, the community has decided that language-specific "tips" questions like this one are also on topic. \$\endgroup\$ – Dennis Jul 17 '15 at 6:10
  • \$\begingroup\$ @Dennis thx for the advice. first post on this site, would normally post on stackoverflow, but that community considers code-golf off topic \$\endgroup\$ – JJS Jul 17 '15 at 6:12
  • \$\begingroup\$ @Dennis I was just about to search meta for that! I do think this would be a nice golf question, if it hasn't been asked already. \$\endgroup\$ – phase Jul 17 '15 at 6:14
10
\$\begingroup\$

28 bytes

You can make your last approach a bit shorter by replacing the '0' by 48. You can also strip all unnecessary spaces and use 1-char variable names (for example, number -> n):

n.Select(i=>i-48).ToArray();
\$\endgroup\$
  • \$\begingroup\$ Fantastic - really nails the fact that a Char handles arithmatic operators as a Short stackoverflow.com/a/1516639/26877 \$\endgroup\$ – JJS Jul 17 '15 at 14:20
  • \$\begingroup\$ The subtraction could also be replaced by &15 for the same character count. \$\endgroup\$ – Peter Taylor Jul 17 '15 at 16:29
3
\$\begingroup\$

C# without LINQ, 97 86 82 bytes

var n=Console.ReadLine();int i=n.Length;var o=new int[i];while(i>0)o[--i]=n[i]-48;

I've been hanging in the shadows for a while and wanted to try this out. I'm no golfer, or even that experienced with C#, but I wanted to see how small I could get it on my own, without using LINQ. It's nothing special, but at least I had my fun.

I wasn't planning on posting this at first, since it's much longer than any good LINQ solution and doesn't really belong here, but I'm kinda curious if there is a shorter way to do it without LINQ.

Edit 1: Instead of int.Parse it now substracts 48 from the char's ASCII code. Well, that's already much better. 2: Now with var instead of string and int[].

\$\endgroup\$
  • \$\begingroup\$ Using var instead of string and int[] also saves 4. \$\endgroup\$ – Peter Taylor Jul 17 '15 at 19:40
3
\$\begingroup\$

76 bytes

number.ToCharArray().Select(var=>Convert.ToInt32(var.ToString())).ToArray();
\$\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.