[Python] CodeSignal 문제 풀이 (7~9)

2020. 2. 16. 01:25Python/CodeSignal Algorithm

7. Given a sequence of integers as an array, determine whether it is possible to obtain a strictly increasing sequence by removing no more than one element from the array.

Note: sequence a0, a1, ..., an is considered to be a strictly increasing if a0 < a1 < ... < an. Sequence containing only one element is also considered to be strictly increasing.

 

[Exam]

  • For sequence = [1, 3, 2, 1], the output should be
    almostIncreasingSequence(sequence) = false.

    There is no one element in this array that can be removed in order to get a strictly increasing sequence.

  • For sequence = [1, 3, 2], the output should be
    almostIncreasingSequence(sequence) = true.

    You can remove 3 from the array to get the strictly increasing sequence [1, 2]. Alternately, you can remove 2 to get the strictly increasing sequence [1, 3].

so difficult... i'll try it.

 

[Sol]

def almostIncreasingSequence(sequence):

    standard = sequence[0]

    count = 0

    for i in range(0, len(sequence)-1):

        if (standard < sequence[i]): standard = sequence[i] 

        if(standard > sequence[i + 1] or sequence[i] > sequence[i+1]):

            count = count + 1

        print(standard, sequence[i + 1], count, sep='   ')

        #같은 숫자 제거

        for j in range(i + 1, len(sequence)):

            if(sequence[i] == sequence[j]):

                count = count + 1

    print(count)

    if(count <= 1): 

        return True

    else:

        return False

 

8. After becoming famous, the CodeBots decided to move into a new building together. Each of the rooms has a different cost, and some of them are free, but there's a rumour that all the free rooms are haunted! Since the CodeBots are quite superstitious, they refuse to stay in any of the free rooms, or any of the rooms below any of the free rooms.

Given matrix, a rectangular matrix of integers, where each value represents the cost of the room, your task is to return the total sum of all rooms that are suitable for the CodeBots (ie: add up all the values that don't appear below a 0).

 

[Exam]

matrix = [[0, 1, 1, 2], 
          [0, 5, 0, 0], 
          [2, 0, 3, 3]]

the output should be
matrixElementsSum(matrix) = 9.

 

[Sol]

def matrixElementsSum(matrix):
    arr = [1, 1, 1, 1, 1]
    sum = 0
    for i in range(len(matrix)):
        for j in range(len(matrix[i])):
            if (matrix[i][j] == 0):
                arr[j] = 0
            if (arr[j] != 0):
                sum = sum + matrix[i][j]
    return sum

 

 

9. Given an array of strings, return another array containing all of its longest strings.

 

[Exam]

For inputArray = ["aba", "aa", "ad", "vcd", "aba"],

the output should be
allLongestStrings(inputArray) = ["aba", "vcd", "aba"].

 

[Sol]

# def allLongestStrings(inputArray):
#     d = dict()
#     for i in range(len(inputArray)):
#         d[inputArray[i]] = len(inputArray[i])
#     arr = []
#     for key, values in d.items():
#         if(values == max(d.values())):
#             arr.append(key)
#     return arr

def allLongestStrings(inputArray):
    arr = []
    maxLen = max(len(inputArray[i]) for i in range(len(inputArray)))
    for i in range(len(inputArray)):
        if(maxLen == len(inputArray[i])):
            arr.append(inputArray[i])
    return arr

딕셔너리로 풀어보려 했으나 중복 삭제로 인한 실패..