Skip to content

[Algorithm] 내적 #87

Description

@hwangJi-dev

💬 문제

[코딩테스트 연습 - 내적](https://school.programmers.co.kr/learn/courses/30/lessons/70128)


💬 풀이

func solution(_ a:[Int], _ b:[Int])->Int{varresult=0foriin0...a.count -1{
result +=a[i]* b[i]}return result
}

💬 더 나은 방법?

func solution(_ a:[Int], _ b:[Int])->Int{returnzip(a, b).map(*).reduce(0,+)}

💬 알게된 문법

✅ zip

  • 각 쌍의 요소가 sequence1 및 sequence2의 해당 요소인 튜플 쌍의 시퀀스입니다.
letnaturalNumbers=1...Int.max
letzipped=Array(zip(words, naturalNumbers))
// zipped == [("one", 1), ("two", 2), ("three", 3), ("four", 4)]

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions