Python(11)
-
Python #Code_Signal_[ Step 25]
Step 25 Given an array of integers, replace all the occurrences of elemToReplace with substitutionElem. My solution : def arrayReplace(inputArray, elemToReplace, substitutionElem): a=[] for i in inputArray: if i == elemToReplace: a.append(substitutionElem) else: a.append(i) return a Best solution : def arrayReplace(i, e, s): return [x if x!=e else s for x in i] Note This task require to replace ..
2020.03.20 -
Python #Code_Signal_[ Step 24]
Step 24 In the popular Minesweeper game you have a board with some mines and those cells that don't contain a mine have a number in it that indicates the total number of mines in the neighboring cells. Starting off with some arrangement of mines we want to create a Minesweepergame setup. My solution : Best solution : def minesweeper(matrix): r = [] for i in range(len(matrix)): r.append([]) for j..
2020.03.17 -
Python #Code_Signal_[ Step 21, 22]
Step 21 An IP address is a numerical label assigned to each device (e.g., computer, printer) participating in a computer network that uses the Internet Protocol for communication. There are two versions of the Internet protocol, and thus two versions of addresses. One of them is the IPv4 address. Given a string, find out if it satisfies the IPv4 address naming rules. a.b is named network part an..
2020.02.17 -
Python #Code_Signal_[ Step 18 - 20]
Step 18 Given a string, find out if its characters can be rearranged to form a palindrome. ( PALINDROME : A palindrome is a string that reads the same left-to-right and right-to-left ) My solution: def palindromeRearranging(inputString): cntString=[] done=[] for i in inputString: if i in done: continue if inputString.count(i)%2==1: cntString.append(inputString.count(i)) done.append(i) if len(cnt..
2020.02.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