19
\$\begingroup\$

Your challenge: Write a function that takes a string s, a character c, and finds the length of the longest run of c in s. The length of the run will be l.

Rules:

  • If s is of length 0 or c is empty, l should be 0.
  • If there are no instances of c in s, l should be 0.
  • Standard loopholes and Standard I/O Rules apply.
  • No matter where in s the run of cs is located, l should be the same.
  • Any printable ASCII characters can appear in s and c.

Test cases:

s,c --> l
"Hello, World!",'l'  -->  2
"Foobar",'o'         -->  2
"abcdef",'e'         -->  1
"three   spaces",' ' -->  3
"xxx xxxx xx",'x'    -->  4
"xxxx xx xxx",'x'    -->  4
"",'a'               -->  0
"anything",''        -->  0

Winner:

As with the shortest answer in each language wins.

\$\endgroup\$
  • \$\begingroup\$ Related. Sandbox post. \$\endgroup\$ – MD XF Jun 8 '17 at 18:01
  • \$\begingroup\$ Could you include the edge cases of empty s and a c that isn't contained in a non-empty s in your test cases? \$\endgroup\$ – Martin Ender Jun 8 '17 at 18:22
  • \$\begingroup\$ What range of characters can appear in s/c? \$\endgroup\$ – Martin Ender Jun 8 '17 at 18:23
  • 6
    \$\begingroup\$ c can be empty? In many languages, a character is just an integer with special semantics, and you can't really have an empty integer either. \$\endgroup\$ – Martin Ender Jun 8 '17 at 18:40
  • 14
    \$\begingroup\$ That doesn't really make sense to me. Your test cases suggest that we have to support it. If we don't have to support it, then specifying its required output doesn't make sense, because I can always just say that it's not supported if my solution would do something else in that case. \$\endgroup\$ – Martin Ender Jun 8 '17 at 18:45

31 Answers 31

0
\$\begingroup\$

Clojure, 56 bytes

#(apply max(reductions(fn[r c](if(= c %2)(inc r)0))0 %))
\$\endgroup\$

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.