16
3
EDIT: If you're using Lisp, I have given some guidelines at the bottom in counting bytes.
Objective: Make the shortest function that splits a string at non-digits and returns an array consisting of only digits in each string, without the use of any regular expressions. Leading zeroes are to be included in each string.
Current Standings (separated in categories):
- C/C++/C#/Java: 68 (C) ....
- GolfScript/APL/J: 13 (APL)
- All others: 17 (Bash, uses
tr
), 24 (Ruby)
Rules:
(I apologize for the lengthiness)
- The format must be as a function with a single string argument. Up to two additional arguments may be added if necessary for the proper return of the array (e.g. sh/csh/DOS Batch needs an extra variable reference to return, etc.).
- The primary function declaration doesn't count, and nor does importing other standard libraries. `#include`s, `import`s, and `using`s don't count. Everything else does. This does include `#define`s and helper functions. Sorry for the confusion. Refer to this as a helpful guide as to what does/does not count (written in C-style syntax)
// doesn't count toward total, may be omitted unless // non-obvious, like half of Java's standard library. #include <stdio.h> import some.builtin.Class // doesn't count, see above #define printf p // counts towards total /* Any other preprocessor directives, etc. count. */ int i = 0; // counts someFunction(); // counts char[][] myMainSplitFunction(char[][] array) { // doesn't count // Everything in here counts return returnArray; // Even this counts. } // doesn't count /* Everything in here counts, including the declaration */ char[][] someHelperFunction(char[] string) { // stuff } // even this counts
- The output must be a string array or similar (Array lists in Java and similar are acceptable). Examples of accepted output:
String[]
,char[][]
,Array
,List
, andArray
(object). - The array must contain only contain variable-length string primitives or string objects. No empty strings should be present in the return, with the exception below. Note: the strings are to contain a string of consecutive matches, such as the example input and output below.
- If there are no matches, then the function body should return
null
, an empty array/list, or an array/list containing an empty string. - No external libraries allowed.
- DOS line endings count as one byte, not two (already covered in meta, but needs emphasized)
- And the biggest rule here: no regular expressions allowed.
This is a code-golf question, so smallest size wins. Good luck!
And here are some example inputs and outputs (with C-style escapes):
Input: "abc123def456" Output: ["123", "456"] Input: "aitew034snk582:3c" Output: ["034", "582", "3"] Input: "as5493tax54\\430-52@g9.fc" Output: ["5493", "54", "430", "52", "9"] Input: "sasprs]tore\"re\\forz" Output: null, [], [""], or similar
Please put how many bytes used by your answers, and as always, happy golfing!
Guidelines for Lisp
Here's what does and doesn't count in Lisp dialects:
;;; Option 1 (defun extract-strings (a b) ; Doesn't count (stuff) ;;; Everything in here counts ) ; Doesn't count ;;; Option 2 (defun extract-strings (string &aux (start 0) (end 0)) ; Doesn't count (stuff) ;;; Everything in here counts ) ; Doesn't count.All other lambdas fully count towards the byte count.
Wasn't this asked before? – Ismael Miguel – 2014-02-23T06:02:28.203
1Yes, but I re-asked it on Meta and made substantial edits to it before posting it again here. Because of this, it shouldn't be classified as a duplicate (the other related one should be closed if not already). – Isiah Meadows – 2014-02-23T06:06:23.420
@IsmaelMiguel It's been deleted.
– Justin – 2014-02-23T06:37:28.2232Shouldn't your "golf" be posted as an answer? – MrWhite – 2014-02-23T10:57:55.813
4Sorry, but -1 for disallowing GolfScript. All languages should be allowed. – Doorknob – 2014-02-23T18:09:30.650
1
@Doorknob That's true, but I also understand the OP's feelings. People should have a chance to compete even if they don't speak GolfScript, J, or APL (and I'm guilty of perusing the latter in these competitions.) Can you give a look at my proposal in the thread he linked to?
– Tobia – 2014-02-23T19:45:43.887I did edit the rules a little because I missed the other two (J i intentionally overlooked, but I forgot about APL). I am going to have two separate scoring levels for those three and any others. – Isiah Meadows – 2014-02-24T15:54:40.950
Comment added by request of @maf-soft: the c# 66 solution was invalid: it returns all digits as char-array – Glenn Randers-Pehrson – 2014-10-21T16:46:55.893