How many pairs?

Inspired by this question

Your challenge is very simple: take input in the form of lookupString string1 string2 ... stringN and output how many pairs of strings there are.

But there's a catch! There are two ways of interpreting "pairs." The first way is combinations. For example,

       1      2     3      4
orange orange apple orange orange


(the first orange is the lookup string) has 3 pairs of oranges, since there is #1 and #3, #3 and #4, and #1 and #4.

The second way of interpreting it is adjacent pairs. For example, the same input would have a result of 1 with this interpretation, since there's only one pair in which both oranges are next to each other. (#3 and #4)

You must write two programs, one with the first interpretation and another with the second. Your score is the sum of the length of the two programs. This is . Shortest score wins.

• "Your score is the length of the two programs combined". combined in which way? Sum of the lengths? L_1 ^ L_2? L_1 * L_2? max(L_1, L_2)? – Bakuriu Sep 15 '13 at 12:27
• This question could probably be a lot clearer with rewording to avoid the word "pair". I see six pairs according to the first metric and two pairs according to the second metric. – Peter Taylor Sep 15 '13 at 12:39
• @Peter The first pear is the search string. Also, it's intentionally confusing :P Why did you think I chose "pear" as my example? :D – Doorknob Sep 15 '13 at 13:17
• All questions on this site should have a clear specification of what constitutes a correct submission.. As things stand you're asking people to read your mind. – Peter Taylor Sep 15 '13 at 15:03
• @PeterTaylor Meh, okay then, edited – Doorknob Sep 15 '13 at 15:05

J, 24 characters

Anywhere - 14 11

2!+/}.{.=;:


is 2 choose (2!) sum (+/) of the beheading (}.) of the first row (head) ({.) of the self-classification (=) of the words (;:) of its argument

beheading means "drop the first element and return the rest".

+/2*/\}.{.=;:


is the sum (+/) of products (ANDs) within each two-element prefix (2*/\) of the beheading (}.) of the head ({.) of the self-classification (=) of the words (;:) of its argument

ex:

   s=.'pear pear apple pear pear'
+/#\2}.#~{.=;:s
3
+/2*/\}.{.=;:s
1

• For the first program, the obvious solution (IMO) is shorter 2!+/}.{.=;: at 11 chars – Volatility Sep 15 '13 at 9:03
• @Volatility thanks - I knew I was something missing but I couldn't find it. – John Dvorak Sep 15 '13 at 12:06

Python 3, 48 + 59 = 107

Anywhere – 48

s,*L=input().split()
c=L.count(s)
print(c*~-c/2)


s,*L=input().split()
print(list(zip(L,L[1:])).count((s,s)))


Ruby, 38 + 45 = 83

Anywhere: 38

• -2 chars: Two statements was shorter than one.
l,*a=gets.split
p (n=a.count(l))*~-n/2


• -2 chars: Borrowed x==[l,l] from Doorknob.
• -2 chars: find_all => select
• -12 chars: select => count
l,*a=gets.split
p a.each_cons(2).count([l,l])


Mathematica 33+42 = 75 31+40 = 71

Program 1

Binomial[Count[Rest@#, #〚1〛, 2] &


Example

Binomial[Count[Rest@#, #〚1〛, 2] &[{"orange", "orange", "apple", "orange", "orange"}]


3

Program 2

Count[Partition[Rest@d, 2, 1], {e = #〚1〛, e}] &


Example

Count[Partition[Rest@d,2,1],{e=#〚1〛,e}] &[{"orange","orange","apple","orange", "orange"}]


1

• You can replace "[[1]]" by "〚1〛".:) – chyanog Sep 20 '13 at 16:30
• Thanks. It took me a bit to figure out just how to do that. (Paste double brackets first as regular text then copy and paste into the code.) – DavidC Sep 20 '13 at 18:23

Ruby, 46 + 58 = 104

Posting my own solution first as I always do:

Anywhere - 49 46

l,*a=gets.split;p ((1..a.count(l)).inject:*)/2


l,*a=gets.split;c=0;a.each_cons(2){|x|c+=1if x==[l,l]};p c


l,*a stolen from daniero :P

• Sure your first program is correct? Your own example input gives you 6, while you state the answer is 3. – daniero Sep 15 '13 at 18:28
• @daniero Whoops, right. Edited. – Doorknob Sep 15 '13 at 19:02

R : 82

Anywhere: 38 characters

a=scan(,"")
choose(sum(a[-1]==a[1]),2)


sum(a[-1]==a[1]) returns the number of corresponding item and choose computes the corresponding binomial coefficient.

a=scan(,"")
sum(diff(which(a[-1]==a[1]))==1)


which(a[-1]==a[1]) returns the indices of the matching items, diff computes the difference between those indices and sum(...==1) returns the number of differences being 1 (i. e. the number of pairs of adjacent matching items).

K, 20 + 44 = 64

Anywhere: 20

{-1++/a~\:*a:" "\:x}


{+/min'(*h)~/:/:(2*!-_-(#a)%2)_a:1_h:" "\:x}


.

k){-1++/a~\:*a:" "\:x}"orange orange apple orange orange"
3
k){+/min'(*h)~/:/:(2*!-_-(#a)%2)_a:1_h:" "\:x}"orange orange apple orange orange"
1


JavaScript - 166 chars

F=function(A){return A.match(RegExp(A.split(' ')[0], 'g')).length-1}

G=function(A){C=0;R=A.split(' ').splice(1);for(i=0;i<R.length-1;i++)R[i]==R[i+1]?C++:0;return C}


Edited to be separate functions.

• You have to include the S/F/R declaration characters in both snippets... – Doorknob Sep 17 '13 at 2:41