3
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ "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)? \$\endgroup\$ – Bakuriu Sep 15 '13 at 12:27
  • \$\begingroup\$ 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. \$\endgroup\$ – Peter Taylor Sep 15 '13 at 12:39
  • \$\begingroup\$ @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 \$\endgroup\$ – Doorknob Sep 15 '13 at 13:17
  • \$\begingroup\$ 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. \$\endgroup\$ – Peter Taylor Sep 15 '13 at 15:03
  • \$\begingroup\$ @PeterTaylor Meh, okay then, edited \$\endgroup\$ – Doorknob Sep 15 '13 at 15:05
7
\$\begingroup\$

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".

Adjanced - 13

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

Python 3, 48 + 59 = 107

Anywhere – 48

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

Adjacent – 59

s,*L=input().split()
print(list(zip(L,L[1:])).count((s,s)))
\$\endgroup\$
2
\$\begingroup\$

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

Adjacent: 45

  • -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])
\$\endgroup\$
2
\$\begingroup\$

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

\$\endgroup\$
  • \$\begingroup\$ You can replace "[[1]]" by "〚1〛".:) \$\endgroup\$ – chyanog Sep 20 '13 at 16:30
  • \$\begingroup\$ 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.) \$\endgroup\$ – DavidC Sep 20 '13 at 18:23
1
\$\begingroup\$

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

Adjacent - 65 58

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

l,*a stolen from daniero :P

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

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.

Adjacent: 44 characters

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).

\$\endgroup\$
0
\$\begingroup\$

K, 20 + 44 = 64

Anywhere: 20

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

Adjacent: 44

{+/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
\$\endgroup\$
0
\$\begingroup\$

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.

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

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.