2020. 2. 16. 00:13ㆍPython/CodeSignal Algorithm
4. Given an array of integers, find the pair of adjacent elements that has the largest product and return that product.
For inputArray = [3, 6, -2, -5, 7, 3], the output should be 7 and 3 produce the largest product. |
def adjacentElementsProduct(inputArray):
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.
|
def shapeArea(n):
polygon = 1
for i in range(1, n):
polygon = polygon + 4 * i
return polygon
<Best>
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.
For statues = [6, 2, 3, 8], the output should be Ratiorg needs statues of sizes 4, 5 and 7. |
def makeArrayConsecutive2(statues):
maxNum = max(statues)
minNum = min(statues)
statues2 = list(range(minNum, maxNum))
return len(set(statues2) - set(statues))
'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 문제 풀이 (7~9) (0) | 2020.02.16 |
[Python] CodeSignal 문제 풀이 (1~3) (0) | 2020.02.05 |