[Python] CodeSignal 문제 풀이 (34~36)

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

 

 

 

34. Given array of integers, remove each kth element from it.

 

[Example]

For inputArray = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] and k = 3, the output should be
extractEachKth(inputArray, k) = [1, 2, 4, 5, 7, 8, 10].

 

[Solution]

#<My Code>
def extractEachKth(inputArray, k):
    a = inputArray

    del a[k-1::k]
    return a
    
#<Best Code>
def extractEachKth(inputArray, k):
    del inputArray[k-1::k]
    return inputArray

 

 

35. Find the leftmost digit that occurs in a given string.

 

[Example]

  • For inputString = "var_1__Int", the output should be
    firstDigit(inputString) = '1';
  • For inputString = "q2q-q", the output should be
    firstDigit(inputString) = '2';
  • For inputString = "0ss", the output should be
    firstDigit(inputString) = '0'.

 

[Solution]

#<My Code>
def firstDigit(inputString):
    for i in inputString:
        if i.isdigit(): return i

 

 

36. Given a string, find the number of different characters in it.

 

[Example]

For s = "cabca", the output should be
differentSymbolsNaive(s) = 3.

There are 3 different characters a, b and c.

 

[Solution]

#<My Code>
def differentSymbolsNaive(s):
    return len(set(s))