Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

7번 문제 두 수의 합 3번 풀이 질문 #42

Closed
kmus1232 opened this issue Oct 22, 2020 · 1 comment
Closed

7번 문제 두 수의 합 3번 풀이 질문 #42

kmus1232 opened this issue Oct 22, 2020 · 1 comment

Comments

@kmus1232
Copy link

kmus1232 commented Oct 22, 2020

해답코드중 마지막 두줄
if target - num in nums_map and i != nums_map[target - num]:
return nums.index(num), nums_map[target_num] 에서

저는 코드를 이렇게 작성한 이유는 값이 같은 두 수의 합이 정답이어서 딕셔너리에서 첫번째 숫자의 인덱스가 두번째 숫자의 인덱스로 덮어씌일을 때를 고려해서 짠 코드라고 이해했는데요,
볼드처리한 nums.index(num)은 굳이 index함수를 사용한 이유를 잘 모르겠습니다.

index값이 왼쪽부터 값을 찾아주기 때문에 첫번째 값의 인덱스를 리턴해서 index함수를 사용한 건가요??
그렇다면 그냥 nums.index(num) 대신 i를 사용하면 되지 않나요?
코드를 return i, nums_map[target_num] 로 바꿨을 때도 문제없이 동작해서 index 함수를 사용한 의도가 궁금해서 질문 드립니다.

@likejazz
Copy link
Collaborator

네, 맞습니다. i를 사용해도 충분한 경우였고, 깃허브 코드는 다음과 같이 수정하였습니다.
#39

책 또한 다음 개정판에 반영하도록 하겠습니다.
알려주셔서 감사합니다.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants