일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 | 29 |
30 | 31 |
- Git
- programmers
- ubuntu
- 프로그래머스 파이썬
- 편스토랑
- AI 경진대회
- 파이썬
- github
- gs25
- 우분투
- Baekjoon
- Real or Not? NLP with Disaster Tweets
- 더현대서울 맛집
- leetcode
- 편스토랑 우승상품
- 백준
- 자연어처리
- SW Expert Academy
- Kaggle
- dacon
- ChatGPT
- Docker
- 데이콘
- hackerrank
- 금융문자분석경진대회
- 맥북
- 캐치카페
- PYTHON
- 프로그래머스
- 코로나19
- Today
- Total
솜씨좋은장씨
[leetCode] 1880. Check if Word Equals Summation of Two Words (Python) 본문
[leetCode] 1880. Check if Word Equals Summation of Two Words (Python)
솜씨좋은장씨 2022. 2. 4. 14:04코딩 1일 1문제! 오늘의 문제는 leetCode의 1880번 Check if Word Equals Summation of Two Words 입니다.
Check if Word Equals Summation of Two Words - LeetCode
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
👨🏻💻 문제 풀이
단어 3개가 주어지고 앞의 두개의 단어를 숫자로 변환한 다음 더한 값이
마지막 세번째 단어를 숫자로 변환한 값과 같은지 다른지를 확인하는 문제입니다.
단어를 숫자로 변환할때는 알파벳 하나하나를 숫자로 바꾸어줍니다.
'a' -> 0 / 'b' -> 1 / 'c' -> 2
바꾸는 규칙은 위처럼 a는 0 b는 1 c는 2 와 같이 a는 0 ~ z는 25까지 변환합니다.
이 변환 과정에서는 ord를 활용하여 단어 -> 숫자 변환 함수를 하나 만들어 주었습니다.
class Solution:
@staticmethod
def convert_word_to_num(Word):
return int("".join([str(ord(word) - ord('a')) for word in list(Word)]))
이렇게 만든 함수를
def isSumEqual(self, firstWord: str, secondWord: str, targetWord: str) -> bool:
answer = False
firstWord_num = self.convert_word_to_num(firstWord)
secondWord_num = self.convert_word_to_num(secondWord)
targetWord_num = self.convert_word_to_num(targetWord)
if firstWord_num + secondWord_num == targetWord_num:
answer = True
return answer
입력받은 세개의 단어를 숫자로 변환하는데 활용했고 첫번째 단어와 두번째 단어를 숫자로 바꾼값을 더한 값이
세번째 단어와 같아지면 True 그렇지 않을 경우에는 False가 정답으로 되도록 하였습니다.
전체 코드는 아래를 참고해주세요.
👨🏻💻 코드 ( Solution )
class Solution:
@staticmethod
def convert_word_to_num(Word):
return int("".join([str(ord(word) - ord('a')) for word in list(Word)]))
def isSumEqual(self, firstWord: str, secondWord: str, targetWord: str) -> bool:
answer = False
firstWord_num = self.convert_word_to_num(firstWord)
secondWord_num = self.convert_word_to_num(secondWord)
targetWord_num = self.convert_word_to_num(targetWord)
if firstWord_num + secondWord_num == targetWord_num:
answer = True
return answer
GitHub - SOMJANG/CODINGTEST_PRACTICE: 1일 1문제 since 2020.02.07
1일 1문제 since 2020.02.07. Contribute to SOMJANG/CODINGTEST_PRACTICE development by creating an account on GitHub.
github.com
'Programming > 코딩 1일 1문제' 카테고리의 다른 글
[leetCode] 1844. Replace All Digits with Characters (Python) (0) | 2022.02.06 |
---|---|
[leetCode] 1748. Sum of Unique Elements (Python) (0) | 2022.02.05 |
[leetCode] 1859. Sorting the Sentence (Python) (0) | 2022.02.03 |
[leetCode] 2119. A Number After a Double Reversal (Python) (0) | 2022.02.02 |
[leetCode] 2057. Smallest Index With Equal Value (Python) (0) | 2022.02.01 |