본문 바로가기

Python345

Equal Sides Of An Array Description: You are going to be given an array of integers. Your job is to take that array and find an index N where the sum of the integers to the left of N is equal to the sum of the integers to the right of N. If there is no index that would make this happen, return -1. For example: Let's say you are given the array {1,2,3,4,3,2,1}: Your function will return the index 3, because at the 3rd p.. 2022. 3. 5.
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.
Sum of Numbers Description: Given two integers a and b, which can be positive or negative, find the sum of all the integers between and including them and return it. If the two numbers are equal return a or b. Note: a and b are not ordered! Examples (a, b) --> output (explanation) (1, 0) --> 1 (1 + 0 = 1) (1, 2) --> 3 (1 + 2 = 3) (0, 1) --> 1 (0 + 1 = 1) (1, 1) --> 1 (1 since both are same) (-1, 0) --> -1 (-1 .. 2022. 3. 3.
Grasshopper - Summation Summation Write a program that finds the summation of every number from 1 to num. The number will always be a positive integer greater than 0. For example: summation(2) -> 3 1 + 2 summation(8) -> 36 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 Solution 1. Repeat the number incrementing by 1 as much as 'num'. 2. Add up all the calculated numbers and return them. JS code :: // js :: for var summation = function .. 2022. 3. 3.
Counting Duplicates Description: Count the number of Duplicates Write a function that will return the count of distinct case-insensitive alphabetic characters and numeric digits that occur more than once in the input string. The input string can be assumed to contain only alphabets (both uppercase and lowercase) and numeric digits. Example "abcde" -> 0 # no characters repeats more than once "aabbcde" -> 2 # 'a' and.. 2022. 3. 1.