본문 바로가기

dictionary7

Make a function that does arithmetic! Given two numbers and an arithmetic operator (the name of it, as a string), return the result of the two numbers having that operator used on them. a and b will both be positive integers, and a will always be the first number in the operation, and b always the second. The four operators are "add", "subtract", "divide", "multiply". A few examples:(Input1, Input2, Input3 --> Output) 5, 2, "add" --.. 2022. 7. 31.
Total amount of points Description: Our football team finished the championship. The result of each match look like "x:y". Results of all matches are recorded in the collection. For example: ["3:1", "2:2", "0:1", ...] Write a function that takes such collection and counts the points of our team in the championship. Rules for counting points for each match: if x > y: 3 points if x < y: 0 point if x = y: 1 point Notes: .. 2022. 4. 9.
Transportation on vacation Description: After a hard quarter in the office you decide to get some rest on a vacation. So you will book a flight for you and your girlfriend and try to leave all the mess behind you. You will need a rental car in order for you to get around in your vacation. The manager of the car rental makes you some good offers. Every day you rent the car costs $40. If you rent the car for 7 or more days,.. 2022. 4. 8.
Python _ 리스트 요소 개수 세기(dictionary, collections) 리스트에 어떤 요소의 개수를 파악해야하는 때가 있습니다. (특히 코딩테스트에서 사용할 일이 많습니다) 그래서 오늘은 요소 개수를 세는 방법을 정리해보고자 합니다. dictionary 사용 # 요소를 세고 싶은 리스트 >>> list = [1,2,3,4,5,5,5,5,5,1,1] # 빈 딕셔너리를 생성 >>> dict = {} # 요소가 딕셔너리에 있다면 += 1, 없다면 = 1 >>> for num in list: if num in dict: # dict.get(num)로 대체가능 dict[num] += 1 else: dict[num] = 1 # {요소(키): 개수(값)} 형태의 딕셔너리 생성 >>> dict {1: 3, 2: 1, 3: 1, 4: 1, 5: 5} 기본 딕셔너리를 사용하는 베이직한 방법입.. 2022. 3. 6.
Python _ 딕셔너리 가져오기 메서드 정리 딕셔너리의 get 메서드와 ['키']를 이용해 값을 가져올 수 있습니다. 단 ['키']를 이용해 값을 가져올 경우, 키가 없다면 키 에러를 내뱉으므로 에러를 따로 핸들링 해주어야 합니다. >>> x {'a': 100, 'b': 200, 'c': 300} # x 딕셔너리 키-값 >>> x.get('a') # 'a'키를 가져온다. 100 >>> x.get('d') # 'd', 만약 없는 키를 요청하면 None을 반환한다. >>> >>> type(x.get('d')) >>> x.get('d', 1000) # 없는 키에 두 번째 인자로 기본값을 지정해주면 기본값을 반환한다. 1000 >>> x['a'] # get 뿐만아니라 ['키'] 형태를 이용해 값으르 가져올 수 있다. 100 >>> x['d'] # 단, 이.. 2022. 3. 4.
Python _ 딕셔너리 추가, 삭제 메서드 정리 setdefault 메서드로 딕셔너리에 '키'와 '값' 추가할 수 있다. >>> x = {} >>> x.setdefault('a') # 딕셔너리에 '키'를 추가한다. >>> x {'a': None} # 기본값은 'None'이다. >>> x.setdefault('b', 100) # 두번째 인자에 '값'을 넣을 수도 있다. >>> x {'a': None, 'b': 100} # '키'와 '값'을 확인할 수 있다. update 메서드를 이용해 딕셔너리 '키'와 '값' 수정할 수 있다.(없으면 키-값 추가) >>> x.update(a=50) # '키'와 '값'을 지정하여 넣을 수 있다. >>> x {'a': 50, 'b': 100} >>> x.update(c=200) # 만약 '키'와 '값'이 없다면 추가된다. .. 2022. 3. 4.