본문 바로가기

분류 전체보기443

[HackerRank] Count Luck def countLuck(matrix, k): row = len(matrix) col = len(matrix[0]) nx = [-1,1,0,0] ny = [0,0,-1,1] start_x, start_y = 0, 0 count = 0 graph = [] for i in range(row): graph.append(list(matrix[i])) for i in range(row): for j in range(col): if graph[i][j] == 'M': start_x, start_y = i, j # Write your code here def dfs(area, x, y): # visited[x][y] = True if area[x][y] == '*': return True direct_count = .. 2023. 5. 19.
[HackerRank] Caesar Cipher def caesarCipher(s, k): # Write your code here length = len(s) str_list = list(s) k %= 26 for i in range(length): char_ord = ord(str_list[i]) if char_ord >= ord('A') and char_ord ord('Z'): diff = ord('Z') - char_ord index = (ord('A')-1) + (k-diff) str_list[i] = chr(index) else: str_list[i] = chr(char_ord+k) elif char_ord >= ord('a') and char_ord ord('z'): diff = ord('z') - char_ord index = (ord(.. 2023. 5. 19.
[HackerRank] Two Characters def alternate(s): # Write your code here result_list = [] def gen_comb(arr,n): result = [] if n == 0: return [[]] for i in range(len(arr)): element = arr[i] rest_arr = arr[i+1:] for c in gen_comb(rest_arr,n-1): result.append([element]+c) return result def validate(arr): length = len(arr) if length >=2: first, second = arr[0],arr[1] for j in range(length): if j%2 == 0 and arr[j] != first: return .. 2023. 5. 19.
[HackerRank] The Power Sum # 정답 코드 def powerSum(X, N): # Write your code here def helper(target,power,num): if target == 0: return 1 if target math.sqrt(target): return 0 # 현재 수를 포함하는 경우와 포함하지 않는 경우의 수를 재귀적으로 계산 include = helper(target-num**power,power,num+1) exclude = helper(target,power,num+1) return include + exclude return helper(X,N,1) 이번 문제는 주어진 X값을 N제곱수로 만들 수 있는 경우의 수를 구하는 문제이다. 처음에 이 문제를 풀이했을 때는 X보다 작.. 2023. 5. 18.
[JS] filter(), map() 함수 정리 1. Array.prototype.filter() const words = ['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present']; const result = words.filter(word => word.length > 6); console.log(result); // Expected output: Array ["exuberant", "destruction", "present"] filter() 메서드는 주어진 함수의 테스트(조건문)를 통과하는 모든 요소를 모아 새로운 배열로 반환합니다. filter는 해석 그대로 필터링, 즉 배열의 요소들을 걸러주는 역할을 하는 함수입니다. 주로 특정 조건을 만족하는 새로운 배열을 필요로 할 때 사용하는.. 2023. 5. 17.
[React] 리액트로 디바운스(Debounce) 구현하기 이전에 Javascript Deep Dive 41장에서 타이머에 관한 내용을 다룬 적이 있다.( https://woohyun-king.tistory.com/136 ) [JS Deep Dive] 41. 타이머 41.1) 호출 스케줄링 함수를 명시적으로 호출하면 함수가 즉시 실행된다. 만약에 함수를 명시적으로 호출하지 않고 일정 시간이 경과된 후에 호출되도록 함수 호출을 "예약"하기 위해서는 Timer 함 woohyun-king.tistory.com 디바운스와 스로틀은 모두 짧은 시간 간격으로 연속으로 발생하는 이벤트 핸들러의 과도한 호출을 방지하기 위한 프로그래밍 기법이다. 그 중에 디바운스는 짧은 시간 간격으로 이벤트가 연속으로 발생하면 이벤트 핸들러를 호출하지 않다가, 일정 시간이 경과한 후에 이벤트 .. 2023. 5. 17.