55
5
We define whitespace to be any of the three characters, tab (0x09), newline (0x0A) or space (0x20).
For this challenge, you're to write two programs or functions in the same programming language, which perform the following tasks:
Count the whitespace characters in a given string. For instance, the input
123 -_- abc def
would return 7 (provided there is no trailing newline).
Split a given string at consecutive runs of whitespace. If the string starts or ends with whitespace, no empty strings should be returned at the ends. For instance, the same input
123 -_- abc def
would return
["123", "-_-", "abc", "def"]
.
In either case you may take input via STDIN, command-line argument or function argument a return the result or print it STDOUT. For the second program, if you choose to print to STDOUT, please print each string on its own line, without surrounding quotes.
For both programs you may assume that the input contains only printable ASCII (0x20 to 0x7E) and whitespace.
Now here is the catch:
- If all whitespace is removed from both programs/functions, the resulting strings have to be identical. That is, your two submissions may only differ in the number and placement of whitespace characters.
- Neither program/function may contain any string or regex literals (character literals are fine, provided your language has a designated character type).
- Neither program/function may contain any comments.
- You must not read the program's source code, directly or indirectly.
This is code golf. Your score is the sum of the sizes of both solutions (in bytes). Lowest score wins.
Leaderboards
The following Stack Snippet generates both a regular leaderboard and an overview of winners by language. So even if your language of choice doesn't let you win the entire challenge, why not try to snatch a spot on the second list? I'd be very interested to see how people tackle this challenge in a variety of languages!
To make sure that your answer shows up, please start your answer with a headline, using the following Markdown template:
# Language Name, N bytes
where N
is the total size of your submission. If you improve your score, you can keep old scores in the headline, by striking them through. For instance:
# Ruby, <s>104</s> <s>101</s> 96 bytes
You may also include the individual counts before the total count, e.g.
# Python 2, 35 + 41 = 76 bytes
The last number that is not struck through will be used by the snippet.
<script src='https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js'></script><script>site = 'meta.codegolf',postID = 5314,isAnswer = true,QUESTION_ID = 42253;jQuery(function(){var u='https://api.stackexchange.com/2.2/';if(isAnswer)u+='answers/'+postID+'?order=asc&sort=creation&site='+site+'&filter=!GeEyUcJFJeRCD';else u+='questions/'+postID+'?order=asc&sort=creation&site='+site+'&filter=!GeEyUcJFJO6t)';jQuery.get(u,function(b){function d(s){return jQuery('<textarea>').html(s).text()};function r(l){return new RegExp('<pre class="snippet-code-'+l+'\\b[^>]*><code>([\\s\\S]*?)<\\/code><\/pre>')};b=b.items[0].body;var j=r('js').exec(b),c=r('css').exec(b),h=r('html').exec(b);if(c!==null)jQuery('head').append(jQuery('<style>').text(d(c[1])));if (h!==null)jQuery('body').append(d(h[1]));if(j!==null)jQuery('body').append(jQuery('<script>').text(d(j[1])))})})</script>
1So what in Bash count as string literals? – jimmy23013 – 2014-12-22T11:08:31.043
@user23013 As far as I'm aware, string literals in Bash can only include whitespace if they are surrounded in quotes, right? If so, then it would be "in the spirit of the law" to disallow only quoted string literals. (The idea behind disallowing string literals is to avoid some simple solutions, where you just include a space in a string and switch the behaviour depending on the length of the string.) – Martin Ender – 2014-12-22T11:18:58.817
The snippet is not listing the rankings :( – sergiol – 2017-07-21T23:40:39.593
26That leaderboard is cool! – Digital Trauma – 2014-12-09T19:56:51.113
5Sounds like any Whitespace answer would comply with rule 1. ;) – nyuszika7h – 2014-12-10T14:31:32.583
1@nyuszika7h Indeed it would, but it won't be particularly short anyway. – Martin Ender – 2014-12-10T14:32:32.563
7Significant whitespace, so not whitespace with benefits or anything... – corsiKa – 2014-12-10T19:23:08.053