티스토리 뷰
4.Given an array of integers, find the pair of adjacent elements that has the largest product and return that product
- Example)
-> For inputArray = [3, 6, -2, -5, 7, 3], the output should be adjacentElementsProduct(inputArray) = 21. 7 and 3 produce the largest product.
def adjacentElementsProduct(inputArray):
max_array = []
for i in range(len(inputArray)-1):
max_array.append(inputArray[i]*inputArray[i+1])
return max(max_array)
return max([inputArray[i] * inputArray[i+1] for i in range(len(inputArray)-1)])
5. Below we will define an n-interesting polygon. Your task is to find the area of a polygon for a given n. A 1-interesting polygon is just a square with a side of length 1. An n-interesting polygon is obtained by taking the n - 1-interesting polygon and appending 1-interesting polygons to its rim, side by side. You can see the 1-, 2-, 3- and 4-interesting polygons in the picture below.
- Example)
-> For n = 2, the output should be shapeArea(n) = 5
-> For n = 3, the output should be shapeArea(n) = 13.
def shapeArea(n):
x = 1
for i in range(n):
x += 4*(i)
return x
def shapeArea(n):
return n**2 + (n-1)**2
6. Ratiorg got statues of different sizes as a present from CodeMaster for his birthday, each statue having an non-negative integer size. Since he likes to make things perfect, he wants to arrange them from smallest to largest so that each statue will be bigger than the previous one exactly by 1. He may need some additional statues to be able to accomplish that. Help him figure out the minimum number of additional statues needed.
- Example)
-> For statues = [6, 2, 3, 8], the output should be makeArrayConsecutive2(statues) = 3. Ratiorg needs statues of sizes 4, 5 and 7.
def makeArrayConsecutive2(statues):
return max(statues) - min(statues) + 1 - len(statues)
'Codesignal' 카테고리의 다른 글
code signal - 10. commonCharacterCount (0) | 2020.01.29 |
---|---|
code signal - 9. All Longest Strings (0) | 2020.01.29 |
code signal - 8. matrixElementsSum (0) | 2020.01.29 |
code signal - 7.almostIncreasingSequence (0) | 2020.01.29 |
codesignal 1-3 문제풀이 (0) | 2020.01.09 |
댓글