2020. 2. 16. 01:25ㆍPython/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]
|
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], the output should be |
[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 |
[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
딕셔너리로 풀어보려 했으나 중복 삭제로 인한 실패..
'Python > CodeSignal Algorithm' 카테고리의 다른 글
[Python] CodeSignal 문제 풀이 (16~18) (0) | 2020.03.12 |
---|---|
[Python] CodeSignal 문제 풀이 (13~15) (0) | 2020.03.12 |
[Python] CodeSignal 문제 풀이 (10~12) (0) | 2020.02.29 |
[Python] CodeSignal 문제 풀이 (4~6) (0) | 2020.02.16 |
[Python] CodeSignal 문제 풀이 (1~3) (0) | 2020.02.05 |