This question already has an answer here:

Challenge :

Check :

  • if there are exactly n exclamation marks ! between every pair
  • and whether the sum of each pair equals to 10.

Input :

  • A string of arbitary length.
  • The number of exclamation marks to check for

Output :

if there are exactly n exclamation marks between each pair, return truthy value, otherwise falsy value.

Pair :

Pair is defined as two digits with at least one exclamation mark between them

Note :

  • The string may contain
    • lowercase letters
    • digits (1-9)
    • space
    • and obviously exclamation marks (!)
  • In case no digit pair is found, return a falsy value.
  • The exclamation will not appear at start or end of the string

Examples :

asas6!!4somevbss5!!asa5 , 2                ---> true
sasdasd3!2ssjdn3! sdf!3 , 1                ---> false
asjsj8sj!!!! 2 sjsadasd9!!!!1 , 4          ---> true
sal7!!3 , 2                                ---> true
nodtejsanhf , 2                            ---> true
2is digit no excla 5 , 8                   ---> true
a!1!9!8!2!a  , 1                           ---> true (thanks @ngn)

The last two cases were false imo but as suggested by Adam (who has more experience than me and probably knows better :) ), they were made true

Winning criteria :

this is so shortest code in bytes for each language wins.

Don't forget :

You must check whether the sum of the two numbers equals 10 or not too, along with whether each pair is separated via n exclamation marks

Credit ? :

@Laikoni found a challenge on coderbyte similar to this : Here. O_o ? I didn't know that

Some explanation :

@ngn suggested this case a!!1!2!3a45!6!!a , 1 (return false)

a!!      --> ignore this 
1        --> here we go start 
!        --> sum so far = 1
2        --> sum = 3, and stop

Yep the sum of each pair must be 10 and since the sum of the first pair wasn't 10, no need to go on. :)


marked as duplicate by JayCe, Sriotchilism O'Zaic, mbomb007 code-golf Jun 8 '18 at 15:11

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 3
    \$\begingroup\$ Very closely related. \$\endgroup\$ – AdmBorkBork Jun 7 '18 at 19:45
  • 5
    \$\begingroup\$ I recommend the Sandbox to iron out issues such as these before posting to Main. \$\endgroup\$ – Adám Jun 7 '18 at 19:53
  • 1
    \$\begingroup\$ @AdmBorkBork, if that challenge wasn't (currently) closed, I'd dupehammer this. \$\endgroup\$ – Shaggy Jun 7 '18 at 19:58
  • 1
    \$\begingroup\$ @MuhammadSalman I think the downvotes are due to lack of specification clarity at posting time. The Sandbox would have helped. \$\endgroup\$ – Adám Jun 7 '18 at 20:36
  • 2
    \$\begingroup\$ By the definition currently given ("Pair is defined as two digits with at least one exclamation mark between them") 9!8 is a pair. (Furthermore 1!9!8 is too, somewhat more debatable though). \$\endgroup\$ – Jonathan Allan Jun 7 '18 at 22:23

APL (Dyalog Unicode), 62 48 bytesSBCS

Full program. Prompts stdin for string and then for n. Now handles cases like a!1!9!!8!2!a. Thanks to ngn for saving a bunch of bytes while pointing out failure of some unlisted test cases. Assumes ⎕IO (Index Origin) to be 0, which is default on many systems.


Try it online!

⎕D,'!' concatenate the Digits and an exclamation point.

⍞∩ prompt for string and find the intersection with the above (removes all irrelevant chars)

⊢i← store in i (for input) and yield that

'\d!+\d'⎕S'&' PCRE Search for a digit, a "!" run, a digit

m← store in m (for matches)

 mix the list of strings into a character matrix, padding with spaces

⎕D⍳ find the ɩndex of each character in the Digits (i.e. "0" → 0, "1" → 1,… "!" and " " → 10)

+/ sum each row (i.e. each pair)

10| division remainder when divided by 10 (non-digits do not affect this result)

0= is it 0? (i.e. divisible by ten; max is 18 and min is 2, so no false positives)

(), prepend the following:

≢¨m tally (count the length of) each match

¯2+ less 2 (for the two digits, i.e. length of the "!" run)

⎕= prompt for n; is it equal to the lengths?

∧/ are they all true? (AND reduction)

  • \$\begingroup\$ @MuhammadSalman Now it has an explanation. APL is really not very hard to understand. I'll be happy to teach you in The APL Orchard. \$\endgroup\$ – Adám Jun 7 '18 at 20:34
  • \$\begingroup\$ And I would love to learn. Will check that out \$\endgroup\$ – Muhammad Salman Jun 7 '18 at 20:35
  • \$\begingroup\$ '(\d)!+(\d)'⎕S'\1\2' -> '!'~⍨¨'\d!+\d'⎕S'&' \$\endgroup\$ – ngn Jun 7 '18 at 20:53
  • \$\begingroup\$ +/¨⍎¨¨(⊂'55'), -> +/⎕d⍳↑ (⎕io←0) \$\endgroup\$ – ngn Jun 7 '18 at 20:56
  • \$\begingroup\$ ⍵='!+'⎕S 1⊢i - this condition is wrong, there may be extra '!'s that are not part of pairs \$\endgroup\$ – ngn Jun 7 '18 at 20:59

Python 2, 94 bytes

lambda s,c:all(sum(map(int,r))==10for r in re.findall('(\d).*?%s.*?(\d)'%('!'*c),s))
import re

Try it online!


Not the answer you're looking for? Browse other questions tagged or ask your own question.