[Python] CodeSignal 문제 풀이 (33~35)

2020. 3. 21. 17:21Python/CodeSignal Algorithm

 

 

33. Given an array of equal-length strings, you'd like to know if it's possible to rearrange the order of the elements in such a way that each consecutive pair of strings differ by exactly one character. Return true if it's possible, and false if not.

Note: You're only rearranging the order of the strings, not the order of the letters within the strings!

 

[Example]

  • For inputArray = ["aba", "bbb", "bab"], the output should be
    stringsRearrangement(inputArray) = false.

    There are 6 possible arrangements for these strings:

    • ["aba", "bbb", "bab"]
    • ["aba", "bab", "bbb"]
    • ["bbb", "aba", "bab"]
    • ["bbb", "bab", "aba"]
    • ["bab", "bbb", "aba"]
    • ["bab", "aba", "bbb"]

    None of these satisfy the condition of consecutive strings differing by 1 character, so the answer is false.

  • For inputArray = ["ab", "bb", "aa"], the output should be
    stringsRearrangement(inputArray) = true.

    It's possible to arrange these strings in a way that each consecutive pair of strings differ by 1 character (eg: "aa", "ab", "bb" or "bb", "ab", "aa"), so return true.

 

[Solution]

#<My Code>
def stringsRearrangement(inputArray):


#<Best Code>

 

 

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):
    s = inputString

    for i in range(len(s)):
        if(s[i].isdigit()): return s[i]

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