목록프로그래밍 (31)
CHIqueen
def isLucky(n): return sum(int(i) for i in str(n)[:len(str(n))//2]) == sum(int(i) for i in str(n)[len(str(n))//2:]) str로 만들어준다음에 슬라이싱으로 반 잘라 합이 같은지 비교했다.
from collections import Counter def commonCharacterCount(s1, s2): common_letters = Counter(s1) & Counter(s2) return sum(common_letters.values()) 딱히 Counter말고 떠오르는게 없어서 Counter를 썼다. def commonCharacterCount(s1, s2): com = [min(s1.count(i),s2.count(i)) for i in set(s1)] return sum(com) 다른 풀이를 보면 set으로 중복을 없앤 다음 파이썬 str 메서드중 count를 이용해 갯수를세 가장 최소를 구했다.
def allLongestStrings(inputArray): maxd = 0 for i in inputArray: maxd = max(maxd,len(i)) return [i for i in inputArray if len(i) == maxd] 나머지 풀이들을 봤는데 max함수에 key를 len으로만 줘서 반복문 돌릴필요가 없었던것이 신기했다. def allLongestStrings(inputArray): return [i for i in inputArray if len(i) == len(max(inputArray, key=len))]
After becoming famous, the CodeBots decided to move into a new building together. Each of the rooms has a different cost, and some of them are free, but there's a rumour that all the free rooms are haunted! Since the CodeBots are quite superstitious, they refuse to stay in any of the free rooms, or any of the rooms below any of the free rooms. Given matrix, a rectangular matrix of integers, wher..
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. func shapeA..
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 numb..
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): return max([inputArray[i] * inputArray[i+1] for i in range(len(inputArray)-1)]) 그냥 다 곱한다음에 ma..
Given the string, check if it is a palindrome. Example For inputString = "aabaa", the output should be checkPalindrome(inputString) = true; For inputString = "abac", the output should be checkPalindrome(inputString) = false; For inputString = "a", the output should be checkPalindrome(inputString) = true. def checkPalindrome(inputString): return inputString==inputString[::-1]
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. Example For year = 1905, the output should be centuryFromYear(year) = 20; For year = 1700, the output should be centuryFromYear(year) = 17. def centuryFromYear(year): return (year + 99) // 100