Significant Whitespace
Python, 54 + 56 = 110 bytes
Counter:
m=lambda x:sum(y.isspace()for y in x)
+1
0<9or x.split()
Splitter:
m=lambda x:sum(y.isspace()for y in x)+10<9or x.split()
For the counter, we use the fact that Python is okay with having just an expression on a line. It's necessary to split up +1
and 0<9or x.split()
to stop a NameError
from being thrown, as 0<9
being True
prevents x.split()
from being evaluated due to short circuiting.
For the splitter, since the number of whitespace is always nonnegative, sum(y.isspace()for y in x)+10<9
is always False
and the splitting function comes into play.
Alternative 1, 59 + 60 = 119 bytes
Counter:
m=lambda x:[x.split(),sum(y.isspace()for y in x)][min([1])]
Splitter:
m=lambda x:[x.split(),sum(y.isspace()for y in x)][m in([1])]
The results of both counting and splitting are stored in a two-element list. The list is indexed by either min([1])
, returning the minimum of the one-element list containing 1
, or m in([1])
, which returns False
(equivalent to 0
) as m
is not contained in [1]
.
Alternative 2, 67 + 69 = 136 bytes
Counter:
ted=1;s=lambda x:[x.split(),sum(y.isspace()for y in x)][not s or ted]
Splitter:
ted=1;s=lambda x:[x.split(),sum(y.isspace()for y in x)][not sorted]
Like above, the results of both counting and splitting are stored in a two-element list. sorted
is a built-in function which is a truthy value, so not sorted
returns False
(equivalent to 0
). For not s or ted
, since s
is a function and also truthy, not s
is False
and ted = 1
is returned.
Alternative 3, 59 + 60 = 119 bytes
Counter:
def f(s):a=s.split();a1=len(s)-len((s*0).join(a));return a1
Splitter:
def f(s):a=s.split();a1=len(s)-len((s*0).join(a));return a
1
This is a function where splitter's result is stored in the variable a
, and counter's result is stored in the variable a1
. Like before, Python is fine with having just an expression on a line, in this case 1
. Splitting up a1
determines what to return from the function.
Pyth, 16 + 15 = 31 bytes
Try it here.
Counter:
L@,cb)sm!cd)b1 0
Splitter:
L@,cb)sm!cd)b10
These each define a function, y
, which takes a string input to solve the desired task.
Thanks to @FryAmTheEggman for the idea of using Pyth's feature of modular indexing into lists to shave a character.
Test cases:
L@,cb)sm!cd)b1 0y++"abc def"b"gh ij k"
L@,cb)sm!cd)b10y++"abc def"b"gh ij k"
Explanation:
L define a function, y, which takes one input, b.
@ Index into
, 2-tuple of
cb) b.split() (solution to splitter)
s sum over (solution to counter)
m map, with input d, to
!cd) logical negation of d.split() (empty list is falsy)
b over b.
Index is either:
10
1
Indexing is modulo the length of the list in Pyth.
0 In one case, a 0 with a leading space is outside the function.
Leading space suppresses print, so the 0 is invisible.
Java 8, 239 + 240 = 479
Count the whitespace (returns Integer)
Object f(String s){String r=new String(new char[]{92,'s'}),e=new String();int intx=0;intx=1;return intx>0?s.chars().filter(c->c==9|c==10|c==32).count():java.util.Arrays.stream(s.split(r)).map(t->t.replaceAll(r,e)).filter(t->t.length()>0);}
Split on the whitespace (returns Stream<String>)
Object f(String s){String r=new String(new char[]{92,'s'}),e=new String();int intx=0;int x=1;return intx>0?s.chars().filter(c->c==9|c==10|c==32).count():java.util.Arrays.stream(s.split(r)).map(t->t.replaceAll(r,e)).filter(t->t.length()>0);}
Explanation:
Object f(String s){
String r=new String(new char[]{92,'s'}),e=new String(); // init regex'es
int intx=0; // critical variable
intx=1; // change intx to 1
OR
int x=1; // new, unused variable
return intx>0 ? // test variable to decide what to do
s.chars().filter(c->c==9|c==10|c==32).count() :
java.util.Arrays.stream(s.split(r)).map(t->t.replaceAll(r,e)).filter(t->t.length()>0);
}