Python/Code_Signal(14)
-
Python #Code_Signal_[ Step 16, 17]
Step 16 Two arrays are called similar if one can be obtained from another by swapping at most one pair of elements in one of the arrays. Given two arrays a and b, check whether they are similar. My solution: def areSimilar(a, b): cnt=0 swp=[] for i in range(len(a)): if a[i] not in b: return False elif a[i] != b[i]: cnt+=1 swp.append([a[i],b[i]]) if cnt>2: return False if cnt ==0 or cnt ==2 and s..
2020.02.13 -
Python #Code_Signal_[ Step 14,15]
Step 14 Several people are standing in a row and need to be divided into two teams. The first person goes into team 1, the second goes into team 2, the third goes into team 1again, the fourth into team 2, and so on. You are given an array of positive integers - the weights of the people. Return an array of two integers, where the first element is the total weight of team 1, and the second elemen..
2020.02.07 -
Python #Code_Signal_[ Step 13]
Step 13 Write a function that reverses characters in (possibly nested) parentheses in the input string. Input strings will always be well-formed with matching ()s. My answer: def reverseInParentheses(inputString): for i in range(len(inputString)): if inputString[i] == '(': start = i if inputString[i] == ')': end = i return reverseInParentheses(inputString[:start]+inputString[end-1:start:-1]+inpu..
2020.01.27 -
Python #Code_Signal_[Step 10 - 12]
Step 10 Given two strings, find the number of common characters between them. My answer: def commonCharacterCount(s1, s2): list_s1=list(s1) list_s2=list(s2) abc=list_s2[:] popped=[] for i in range(len(list_s1)): if list_s1[i] in abc: abc.remove(list_s1[i]) return len(list_s2)-len(abc) Best answer: def commonCharacterCount(s1, s2): com = [min(s1.count(i),s2.count(i)) for i in set(s1)] return sum(..
2020.01.13 -
Python #Code_Signal_[Step 8, 9]
Step 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. My answer: def matrixElementsSum(matrix): cnt..
2020.01.13 -
Python #Code_Signal_[Step 7]
Step 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. My answer(hard coding): First try (This solution makes Error when input array's scale is large Error: The test case is too large and is shown truncated)😂 def almostIncreasingSequence(sequence): boool=False value = sequence..
2020.01.09