본문 바로가기

Programming/Python

[python] codeSignal 문제풀이 (4~6)

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.

 

1
2
def adjacentElementsProduct(inputArray):
    return max([inputArray[i] * inputArray[i+1for 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.

 

1
2
3
#best code
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.

 

 

1
2
3
def makeArrayConsecutive2(statues):
    return max(statues)-min(statues)-len(statues)+1