[Python] CodeSignal 문제 풀이 (46~48)

2020. 3. 31. 21:34Python/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
electionsWinners(votes, k) = 2.

  • The first candidate got 2 votes. Even if all of the remaining 3 candidates vote for him, he will still have only 5 votes, i.e. the same number as the third candidate, so there will be no winner.
  • The second candidate can win if all the remaining candidates vote for him (3 + 3 = 6 > 5).
  • The third candidate can win even if none of the remaining candidates vote for him. For example, if each of the remaining voters cast their votes for each of his opponents, he will still be the winner (the votes array will thus be [3, 4, 5, 3]).
  • The last candidate can't win no matter what (for the same reason as the first candidate).

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]

  • For inputString = "00-1B-63-84-45-E6", the output should be
    isMAC48Address(inputString) = true;
  • For inputString = "Z1-1B-63-84-45-E6", the output should be
    isMAC48Address(inputString) = false;
  • For inputString = "not a MAC-48 address", the output should be
    isMAC48Address(inputString) = false.

 

[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]

  • For symbol = '0', the output should be
    isDigit(symbol) = true;
  • For symbol = '-', the output should be
    isDigit(symbol) = false.

 

[Solution]

#<My Code>
def isDigit(symbol):
    return True if symbol.isdigit() else False

#<Best Code>
def isDigit(symbol):
    return symbol.isdigit()