PythonCodeFight_Day1

Day_1

https://app.codesignal.com/arcade

Intro.

Q1. add

Write a function that returns the sum of two numbers.

1
2
def add(param1, param2):
return param1 + param2

Q2. centuryFromYear

Given a year, return the century it is in. The first century spans from the year 1 up to and including the year 100, the second - from the year 101 up to and including the year 200, etc.

1
2
3
4
5
6
def centuryFromYear(year):
if year % 100 == 0:
century = int(year / 100)
else:
century = int(year / 100) + 1
return century

Q3. checkPalindrome

Given the string, check if it is a palindrome.

  • palindrome: 앞으로 읽어도, 뒤로 읽어도 같은 문자열

    1
    2
    3
    4
    5
    6
    def checkPalindrome(inputString):
    return (inputString == inputString[::-1])

    # string을 뒤집는 방법
    # s = 'abc'
    # s[::-1] = 'cba'

Q4. adjacentElementsProduct

Given an array of integers, find the pair of adjacent elements that has the largest product and return that product.

1
2
3
4
5
6
7
8
9
def adjacentElementsProduct(inputArray):
arrFront = inputArray[:-1]
arrRear = inputArray[1:]

arrMul = list()
for i in range(len(arrFront)):
arrMul.append(arrFront[i]*arrRear[i])

return max(arrMul)

Q5. shapeArea

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.

1
2
3
4
5
6
7
def shapeArea(n):
area = 0

for i in range(2*n-1,0,-2):
area += i*2

return (area - (2*n-1))