Having trouble with special characters in a string, super hard coding challenge problem

Doctor Reality :

I have to check the validity of a string with the following rules:

There are 3 special characters: $ # ^

  • They cannot be next to each other in a string (ignoring spaces) except with the number/pound/hash character (#).

    • The # character can appear after another special character but only one.
  • There must be a number before and after a special character, except with the # character if there is a number after it. However if there are two, there must be a number before the two #s.

    • "#5" is valid
    • "##5" is invalid
    • "5##5" is valid
  • If there are only special characters in the string, it is automatically invalid.

  • If there is only ONE number in the string it is valid. If there are multiple numbers separated by spaces, the string is no longer valid.
    • e.g. "25 40" is invalid since 25 and 40 are seperated by a space
    • "25" is valid

You can assume you will get a string with only numbers and/or these special characters.

You can assume you will not get any zeroes in the string such as 0#0 or hanging zeroes like: 0.000, 00004 or 4.000

Treat all numbers the same, so .4 is a number and it works the same as 0.4 or 5 or 3.2 or 9231, etc.

You cannot use exec() or eval()

You can assume numbers are not in scientific notation or any other special number, only ones are shown here. No negative numbers.

Examples of valid strings:

  • "#5 #5 #5"
  • "5 ^5.321 $#2.6"
  • "5^#4.5"
  • "#5#5#5"
  • "56.2"

Examples of invalid strings:

  • "5 6" (reason: separated by a space)
  • "5^^5" (reason: two special characters next to each other)
  • "5^^^##5" (reason: way too many special characters next to each other)
  • "^5" (reason: no number before "^")
  • "^5&" (reason: no number before "^" AND no number after "&")

Here's my attempt:

def checkvalid(string1):
    string1 = string1.replace(" ", "")
    checkStatements = ["$$", "#$", "###", "^^", "#^"]
    checkOut = [x for x in checkStatements if x not in string1]
    if len(checkOut) != len(checkStatements):
        return False
    return True

It works for some of them, which is quite obvious since it only checks for specific patterns but for others such as:

  • print(checkvalid("5##5 5"))

  • print(checkvalid("5$$$"))

  • print(checkvalid("$$$"))

  • print(checkvalid("$5"))

My code returns "True" on all of them despite that these are all invalid so my code should return False. I am having trouble accounting for the fact that operators can't be next to each other and checking for a number before or after a special character.

Błotosmętek :

I'm not completely sure if this is correct answer, because the problem is slightly unclear. But check this:

import re
def checkvalid(string1):
    if re.search(r'[\d.]\s+[\d.]', string1): return False # two numbers separated by spaces
    string1 = string1.replace(' ', '')
    return (bool(re.search(r'\d', string1))               # must contain numbers
        and not re.search(r'\.\d*\.', string1)            # a number can't contain two dots
        and not re.search(r'[$^]{2}|#[$^]|[$^]#[$^#]', string1) # obviously
        and not re.search(r'^[$^]|[$^]$', string1)        # special at the beginning or end
        and not re.search(r'^##|##$', string1)            # two hashes at the beginning or end
    )

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=173924&siteId=1