🧑🏻💻/Python(9)
-
중복되는 수
arr = [1,2,3,3,3,4,4] def solution(arr): answer = [] for v in arr: if(not answer) or (v not in list(zip(*answer))[1]): answer.append([1,v]) else: for i in range(len(answer)): if answer[i][1] == v: answer [i][0] += 1 answer = [cnt for cnt, value in answer if cnt > 1] if answer : return answer else : return [-1]
2021.06.24 -
소수 구하기
from itertools import permutations import math def check(n): k = math.sqrt(n) if n < 2: return False for i in range(2, int(k)+1): if n % i == 0: return False return True def solution(numbers): answer = [] for k in range(1, len(numbers)+1): perlist = list(map(''.join, permutations(list(numbers), k))) for i in list(set(perlist)): if check(int(i)): answer.append(int(i)) answer = len(set(answer)) re..
2021.06.24 -
완주하지 못한 선수
def solution(participant, completion): answer ='' participant.sort() completion.sort() for i in range (len(participant)): if participant[i-1] != completion[i-1]: return participant[i-1] return answer
2021.06.24 -
[Python] Dictionary sorted
fruit = {'apple':3, 'grape':10, 'banna':7,'peach':3,'melon':2} sorted1 = sorted(fruit.items()) print(sorted1) sorted1 = sorted(fruit.items(), reverse = True) print(sorted1) def f1(x): return x[0] def f2(x): return x[1] sorted2 = sorted(fruit.items(), key=f1) print("key:", sorted2) sorted2 = sorted(fruit.items(), key=f2) print("value:", sorted2) sorted2 = sorted(fruit.items(), key=f2, reverse = T..
2021.06.24 -
[Python] list sort / sorted
numList = [1, 99, 132, 55, 7] strList = ["red","green","blue","Black","White","Gray"] numList.sort() print(numList) numList.sort(reverse = True) print(numList) strList.sort() # 대문자 우선 정렬후 소문자 정렬 print(strList) strList.sort(key = str.lower) #대소문자 구분없이 정렬 print(strList) strList.sort(key = str.lower, reverse = True) # 대소문자 구분없이 역순정렬 print(strList) #numList.reverse() #print(numList) #strList.reverse..
2021.06.24