물음표 살인마의 개발블로그

알고리즘 문제/CodeWar

Message Validator

BEstyle 2022. 11. 27. 00:27

DESCRIPTION:

In this kata, you have an input string and you should check whether it is a valid message. To decide that, you need to split the string by the numbers, and then compare the numbers with the number of characters in the following substring.

For example "3hey5hello2hi" should be split into 3, hey, 5, hello, 2, hi and the function should return true, because "hey" is 3 characters, "hello" is 5, and "hi" is 2; as the numbers and the character counts match, the result is true.

Notes:

  • Messages are composed of only letters and digits
  • Numbers may have multiple digits: e.g. "4code13hellocodewars" is a valid message
  • Every number must match the number of character in the following substring, otherwise the message is invalid: e.g. "hello5" and "2hi2" are invalid
  • If the message is an empty string, you should return true

def is_a_valid_message(message):
    print(message)
    nums="1234567890"
    if message=="" :
        return True
    if message[0] not in nums:
        return False
    tleng="0"
    chars=""
    for i in range(len(message)):
        if message[i] in nums:
            if chars=="":
                tleng+=message[i]
            else:
                if int(tleng)!=len(chars):
                    return False
                else:
                    tleng=message[i]
                    chars=""
        else:
            chars+=message[i]
    return int(tleng)==len(chars)

'알고리즘 문제 > CodeWar' 카테고리의 다른 글

Lowest product of 4 consecutive numbers  (0) 2022.11.27
Most Frequent Weekdays  (0) 2022.11.27
Clocky Mc Clock-Face  (0) 2022.11.24
The Office V - Find a Chair  (0) 2022.11.24
Split and then add both sides of an array together.  (0) 2022.11.23