for문을 활용하여 푸는 문제였는데 다른사람의 풀이를 보니 잘 이해가 가지 않았다. 한줄 만에 코드를 다 끝 냈는데 아직 모르는 개념이 많은 것 같다....
내 풀이
1 2 3 4 5 6 7 8 9 10 11 | def solution(arr1, arr2): answer = [] for i in range(len(arr1)): lst = [] for k in range(len(arr2[0])): temp = 0 for j in range(len(arr1[0])): temp += arr1[i][j]*arr2[j][k] lst.append(temp) answer.append(lst) return answer | cs |
zip(*B)로 B의 행들을 접근 할 수 있는게 정말 신기했다.
다른 사람 풀이
1 2 3 | def productMatrix(A, B): return [[sum(a*b for a, b in zip(A_row,B_col)) for B_col in zip(*B)] for A_row in A] | cs |
다른 사람 풀이를 보고 다시 풀어 본 것
1 2 3 4 5 6 7 8 9 10 | def solution2(A,B): ans =[] for A_row in A: lst = [] for B_row in zip(*B): # print(B_row) temp = sum (a*b for a,b in zip(A_row,B_row) ) lst.append(temp) ans.append(lst) return ans | cs |
sum을 구할때 for문을 변수처럼 사용하는 습관을 길러야겠다.
'IT > 알고리즘' 카테고리의 다른 글
프로그래머스 N개의 최소공배수 (0) | 2020.07.04 |
---|---|
프로그래머스 피보나치 수 (python) (0) | 2020.06.24 |
프로그래머스 더 맵게(python) (0) | 2020.06.13 |
프로그래머스 위장 (python) (0) | 2020.06.13 |
프로그래머스 전화번호 목록 (0) | 2020.06.12 |