hye-log

[프로그래머스]내적(Python) 본문

CodingTest/Programmers

[프로그래머스]내적(Python)

iihye_ 2022. 7. 21. 11:41

0. 문제 링크

https://school.programmers.co.kr/learn/courses/30/lessons/70128

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 

1. 문제 설명

1) 두 배열의 각 요소의 곱의 합 구하기

 

2. 입출력

# input
a = [1, 2, 3, 4]
b = [-3, -1, 0, 2]

# output
answer = 3

 

3. 코드

1) 첫 번째 코드

def solution(a, b):
    # a, b : 내적할 두 배열
    answer = 0
    answer = sum([i*j for (i, j) in zip(a, b)])
    return answer

 

실행 결과

 

4. 알게된 점

1) zip 함수의 사용

a = [1, 2, 3, 4]
b = [-3, -1, 0, 2]

for i in zip(a, b):
    print(i)
    
# (1, -3)
# (2, -1)
# (3, 0)
# (4, 2)

 

5. Github

https://github.com/iihye/Algorithm/blob/main/Programmers/dot_product.py

 

GitHub - iihye/Algorithm

Contribute to iihye/Algorithm development by creating an account on GitHub.

github.com

 

728x90
Comments