[Python] CodeSignal 문제 풀이 (43~45)

2020. 3. 31. 21:30Python/CodeSignal Algorithm

 

 

 

43. A string is said to be beautiful if each letter in the string appears at most as many times as the previous letter in the alphabet within the string; ie: b occurs no more times than a; c occurs no more times than b; etc.

Given a string, check whether it is beautiful.

 

[Example]

  • For inputString = "bbbaacdafe", the output should be isBeautifulString(inputString) = true.

    This string contains 3 as, 3 bs, 1 c, 1 d, 1 e, and 1 f (and 0 of every other letter), so since there aren't any letters that appear more frequently than the previous letter, this string qualifies as beautiful.

  • For inputString = "aabbb", the output should be isBeautifulString(inputString) = false.

    Since there are more bs than as, this string is not beautiful.

  • For inputString = "bbc", the output should be isBeautifulString(inputString) = false.

    Although there are more bs than cs, this string is not beautiful because there are no as, so therefore there are more bs than as.

 

[Solution]

#<My Code>
def isBeautifulString(inputString):
    s = inputString
    a = sorted(list(set(inputString)))
    n = s.count(a[0])
    check = "abcdefghijklmnopqrstuvwxyz"

    for i in range(len(a)):
        if a[i] != check[i]: return False

        if i == 0:
            n = s.count(a[0])

        if n < s.count(a[i]): return False
        else: n = s.count(a[i])
    return True

#<Best Code>
def isBeautifulString(inputString):
    r = [inputString.count(i) for i in string.ascii_lowercase]
    return r[::-1] == sorted(r)

 

 

44. An email address such as "John.Smith@example.com" is made up of a local part ("John.Smith"), an "@" symbol, then a domain part ("example.com").

The domain name part of an email address may only consist of letters, digits, hyphens and dots. The local part, however, also allows a lot of different special characters. Here you can look at several examples of correct and incorrect email addresses.

Given a valid email address, find its domain part.

 

[Example]

  • For address = "prettyandsimple@example.com", the output should be
    findEmailDomain(address) = "example.com";
  • For address = "fully-qualified-domain@codesignal.com", the output should be
    findEmailDomain(address) = "codesignal.com".

 

[Solution]

#<My Code>
def findEmailDomain(address):
    r = r'@[a-z0-9-]+.[a-z0-9]+'
    
    matchList = re.findall(r, address)
    if matchList:
        return ''.join(matchList)[1:]

#<Best Code>
def findEmailDomain(address):
    a = address.split('@')
    return a[-1] 

 

 

45. Given a string, find the shortest possible string which can be achieved by adding characters to the end of initial string to make it a palindrome.

 

[Example]

For st = "abcdc", the output should be
buildPalindrome(st) = "abcdcba".

 

[Solution] -ing

#<My Code>
def buildPalindrome(st):
    n = int(len(st) / 2)

    while n <= len(st):
        i = 0
        s = st
        l = n
        
        for i in range(l - 1, -1, -1):
            if len(s) % 2 == 1:
                if s[:l] == s[:l:-1]: return s
            else:
                if s[:l] == s[l:]: return s       
            s += s[i]
        n += 1

#<Best Code>