2020. 3. 31. 21:30ㆍPython/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]
|
[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]
|
[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>
'Python > CodeSignal Algorithm' 카테고리의 다른 글
[Python] CodeSignal 문제 풀이 (49~51) (0) | 2020.04.14 |
---|---|
[Python] CodeSignal 문제 풀이 (46~48) (0) | 2020.03.31 |
[Python] CodeSignal 문제 풀이 (40~42) (0) | 2020.03.31 |
[Python] CodeSignal 문제 풀이 (37~39) (0) | 2020.03.31 |
[Python] CodeSignal 문제 풀이 (34~36) (0) | 2020.03.31 |