본문 바로가기

set6

[자료구조] 좌석구매 파이썬 (hashable VS. unhashable) unique한 pair를 구하면 될거라 생각해서 2d 리스트를 그대로 set에 넣었지만 TypeError: unhashable type: 'list' unhashable 에러가 발생했다. 문제를 마저 풀기 전에 unhashable과 hashable의 차이점을 알아보자. 출처: realpython.com/lessons/immutable-vs-hashable/ hashable object라는 것은 can't modify 정도로 이해가 가능한데 좀 더 설명이 필요하기 때문에 다른 글을 찾아봤다. 요약: non-duplicate를 요구하는 python object들은 hashable하다. 출처: https://analytics4everything.tistory.com/m/138 2021. 4. 22.
[LeetCode] Ransom Note 파이썬 (dict, set, count) from collections import defaultdict class Solution: def canConstruct(self, ransomNote: str, magazine: str) -> bool: seen = defaultdict(int) for r in ransomNote: seen[r] += 1 for m in magazine: if m in seen: seen[m] -= 1 for v in seen.values(): if v > 0: return False return True "fihjjjjei" "hjibagacbhadfaefdjaeaebgi" defaultdict(, {'f': -1, 'i': 0, 'h': -1, 'j': 2, 'e': -2}) seen 이라는 dict에 0보다 큰.. 2021. 4. 14.
[LeetCode] Intersection of Two Arrays 파이썬 (set, intersection) set 자료형의 intersection 사용 class Solution: def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]: return list(set(nums1).intersection(nums2)) difference 도 차집합 구할 떄 사용 가능 list(set(nums1).difference(nums2)) 2021. 4. 13.
[LeetCode] Swap Salary (update, ENUM, SET, CASE, IF) ENUM data type link ENUM(val1, val2, val3, ...) A string object that can have only one value, chosen from a list of possible values. You can list up to 65535 values in an ENUM list. If a value is inserted that is not in the list, a blank value will be inserted. The values are sorted in the order you enter them UPDATE salary SET sex = IF(sex='m', 'f', 'm'); UPDATE salary SET sex = CASE sex WHEN '.. 2021. 4. 9.
[LeetCode] Longest Common Prefix 파이썬 (zip, list comprehension, set) class Solution: def longestCommonPrefix(self, strs: List[str]) -> str: strs = [list(x) for x in strs] strs = zip(*strs) pre = "" for a in strs: if len(set(a)) == 1: pre += a[0] else: break return pre # output # [['f', 'l', 'o', 'w', 'e', 'r'], ['f', 'l', 'o', 'w'], ['f', 'l', 'i', 'g', 'h', 't']] # ('f', 'f', 'f') # a output # {'f'} # set(a) # 1 # len(set(a)) # ('l', 'l', 'l') # {'l'} # 1 # ('o'.. 2021. 3. 28.
[프로그래머스] 체육복 (python) Python 주의사항은 여벌을 가지고 있는 리스트에서도 도난이 발생할 수 있기 때문에 lost와 reserve 각각에 대해서 중복이 없게 차집합을 만들어 놓아야 한다. (first two lines of codes.) 그리고 여벌 리스트(rsv_pos)를 loop돌면서 도난 당한 리스트(lost_pos)의 앞 or 뒤에 여벌리스트의 요소들이 있는지 체크하면서 도난 리스트를 제거한다. python remove method는 list에서 remove에 주어진 item을 찾아서 삭제하는 메서드이다. 만약, 해당 아이템이 없다면 "ValueError: list.remove(x): x not in list" 이러한 에러 메시지를 보게될 것이다. def solution(n, lost, reserve): rsv_po.. 2021. 1. 18.