2020. 3. 31. 21:34ㆍPython/CodeSignal Algorithm
46. Elections are in progress!
Given an array of the numbers of votes given to each of the candidates so far, and an integer k equal to the number of voters who haven't cast their vote yet, find the number of candidates who still have a chance to win the election.
The winner of the election must secure strictly more votes than any other candidate. If two or more candidates receive the same (maximum) number of votes, assume there is no winner at all.
[Example]
For votes = [2, 3, 5, 2] and k = 3, the output should be
Thus, only 2 candidates can win (the second and the third), which is the answer. |
[Solution]
#<My Code>
def electionsWinners(votes, k):
v = votes
m = max(v)
cnt = 0
if k == 0 and v.count(m) == 1:
return 1
for i in v:
if i + k > m:
cnt += 1
return cnt
#<Best Code>
def electionsWinners(v, k):
m = max(v)
return int(v.count(m) == 1) if k == 0 else len([n for n in v if m < n + k])
47. A media access control address (MAC address) is a unique identifier assigned to network interfaces for communications on the physical network segment.
The standard (IEEE 802) format for printing MAC-48 addresses in human-friendly form is six groups of two hexadecimal digits (0 to 9 or A to F), separated by hyphens (e.g. 01-23-45-67-89-AB).
Your task is to check by given string inputString whether it corresponds to MAC-48 address or not.
[Example]
|
[Solution]
#<My Code>
import re
def isMAC48Address(inputString):
s = inputString
r = r'(([a-fA-F0-9]{2}-){5})[a-fA-F0-9]{2}'
matchString = re.match(r, s)
if matchString == None: return False
if matchString.group() == s: return True
else: return False
#<Best Code>
def isMAC48Address(s):
return bool(re.match(('^' + '[\dA-F]{2}-' * 6)[:-1] + '$', s))
48. Determine if the given character is a digit or not.
[Example]
|
[Solution]
#<My Code>
def isDigit(symbol):
return True if symbol.isdigit() else False
#<Best Code>
def isDigit(symbol):
return symbol.isdigit()
'Python > CodeSignal Algorithm' 카테고리의 다른 글
[Python] CodeSignal 문제 풀이 (52~54) (0) | 2020.04.14 |
---|---|
[Python] CodeSignal 문제 풀이 (49~51) (0) | 2020.04.14 |
[Python] CodeSignal 문제 풀이 (43~45) (0) | 2020.03.31 |
[Python] CodeSignal 문제 풀이 (40~42) (0) | 2020.03.31 |
[Python] CodeSignal 문제 풀이 (37~39) (0) | 2020.03.31 |