Coding Test Study


Intro 문제

1번

def add(param1, param2):
  return param1 + param2


2번

def centuryFromYear(year):
    cent = year // 100
    if year % 100 == 0:
        return cent
    else:
        return cent + 1


3번

def checkPalindrome(inputString):
    if len(inputString) == 1:
        return True
    elif len(inputString) != 1:
        mid = len(inputString) // 2
        for i in range(mid):
            if inputString[i] != inputString[-1-i]:
                return False
        return True


4번

def adjacentElementsProduct(inputArray):
    max_num = inputArray[0] * inputArray[1]
    return_idx1, return_idx2 = 0, 1
    for i in range(1, len(inputArray)-1):
        if inputArray[i] * inputArray[i+1] > max_num:
            max_num = inputArray[i] * inputArray[i+1]
            return_idx1, return_idx2 = i, i+1
        elif inputArray[i-1] * inputArray[i] > max_num:
            max_num = inputArray[i-1] * inputArray[i]
            return_idx1, return_idx2 = i-1, i
    return inputArray[return_idx1] * inputArray[return_idx2]


5번 (20.06.05)

def shapeArea(n):
    sums = 0
    for i in range(1, n+1):
        sums+=i
    return sums*4 - (n*4-1)


6번 (20.06.16)

def makeArrayConsecutive2(statues):
    return_num = 0
    statues.sort()
    for i in range(len(statues)-1):
        if statues[i+1] - statues[i] != 1:
            return_num += statues[i+1] - statues[i] - 1
    return return_num


7번 (20.06.26)

def almostIncreasingSequence(sequence):
    cnt = 0
    i = 1
    first = sequence[0]
    while i <= len(sequence):
        if sequence[i-1] >= sequence[i]:
            cnt += 1
            if sequence[i-1] != first:
                if sequence[i-2] >= sequence[i]:
                    sequence.pop(i)
                else:
                    sequence.pop(i-1)
            else:
                sequence.pop(i-1)
            if sorted(set(sequence)) == sequence:
                return True
            else:
                return False
        i += 1
    if cnt < 1:
        return True
    else:
        return False


8번(20.06.28)

def matrixElementsSum(matrix):
    tran_mat = [ list(x) for x in zip(*matrix) ]
    sum_num = 0
    for lists in tran_mat:
        nums = 0
        for i in lists:
            if i != 0:
                nums += i
            else:
                break
        sum_num += nums
    return sum_num


9번(20.07.19)

def allLongestStrings(inputArray):
    max_len = 0
    for i in inputArray:
        if len(i) > max_len:
            max_len = len(i)
    return_array = []
    for j in inputArray:
        if len(j) == max_len:
            return_array.append(j)
    return return_array


10번(20.08.03)

def commonCharacterCount(s1, s2):
    s1_dic = {}; s2_dic = {}
    for i in set(s1):
        s1_dic[i] = 0
    for i in set(s2):
        s2_dic[i] = 0
    for j in s1:
        s1_dic[j] = s1_dic[j] + 1
    for j in s2:
        s2_dic[j] = s2_dic[j] + 1
    
    return_sum = 0
    for i in s1_dic.keys():
        for j in s2_dic.keys():
            if i == j:
                if s1_dic[i] < s2_dic[j]:
                    return_sum += s1_dic[i]
                elif s1_dic[i] >= s2_dic[j]:
                    return_sum += s2_dic[j]
    return return_sum


11번(20.08.10)

def isLucky(n):
    left = str(n)[:len(str(n))//2]
    right = str(n)[len(str(n))//2:]
    left_sum = sum([int(i) for i in left])
    right_sum = sum([int(i) for i in right])
    if left_sum == right_sum:
        return True
    else:
        return False


12번(20.08.25)

def sortByHeight(a):
    sort_list = []
    for i in a:
        if i != -1:
            sort_list.append(i)
    sort_list = sorted(sort_list)
    return_list = []
    for j in a:
        if j == -1:
            return_list.append(j)
        else:
            return_list.append(sort_list[0])
            sort_list.pop(0)
    return return_list


13번(20.09.07)

def reverseInParentheses(inputString):
    left = 0
    right = 0
    for i in range(len(inputString)):
        if inputString[i] == "(":
            left = i
        if inputString[i] == ")":
            right = i
            
            sequence = list(inputString)
            a = sequence[left: right + 1]
            a = a[::-1]
            for j in range(len(a)):
                sequence[left + j] = a[j]
            
            result = ""
            for k in range(len(inputString)):
                if k != left and k != right:
                    result += sequence[k]
            inputString = result
            return reverseInParentheses(inputString)
    return inputString


May 6, 2020 에 작성
Tags: python