Notice
Recent Posts
Recent Comments
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- Baekjoon
- ChatGPT
- 프로그래머스
- 자연어처리
- 금융문자분석경진대회
- 더현대서울 맛집
- SW Expert Academy
- 캐치카페
- 데이콘
- Kaggle
- dacon
- Docker
- hackerrank
- 코로나19
- PYTHON
- 편스토랑
- 우분투
- 프로그래머스 파이썬
- programmers
- ubuntu
- gs25
- Git
- 파이썬
- Real or Not? NLP with Disaster Tweets
- 맥북
- AI 경진대회
- leetcode
- 백준
- 편스토랑 우승상품
- github
Archives
- Today
- Total
솜씨좋은장씨
[leetCode] 1556. Thousand Separator (Python) 본문
728x90
반응형
Given an integer n, add a dot (".") as the thousands separator and return it in string format.
Example 1:
Input: n = 987
Output: "987"
Example 2:
Input: n = 1234
Output: "1.234"
Example 3:
Input: n = 123456789
Output: "123.456.789"
Example 4:
Input: n = 0
Output: "0"
Constraints:
- 0 <= n < 2^31
Solution
class Solution:
def thousandSeparator(self, n: int) -> str:
if len(str(n)) < 4:
return str(n)
else:
len_str = len(str(n))
loop_num = len_str // 3
mod_num_list = []
for i in range(loop_num):
n_mod_1000 = n % 1000
if n_mod_1000 < 100:
mod_num_list.append('0' + str(n_mod_1000))
else:
mod_num_list.append(str(n_mod_1000))
n = n // 1000
if n != 0:
mod_num_list.append(str(n))
mod_num_list_reverse = mod_num_list[::-1]
answer = '.'.join(mod_num_list_reverse)
return answer
'Programming > 코딩 1일 1문제' 카테고리의 다른 글
[leetCode] 977. Squares of a Sorted Array (Python) (0) | 2020.09.01 |
---|---|
[leetCode] 1281. Subtract the Product and Sum of Digits of an Integer (Python) (0) | 2020.08.30 |
[leetCode] 709. To Lower Case (Python) (0) | 2020.08.15 |
[leetCode] 263. Ugly Number (Python) (1) | 2020.08.14 |
[leetCode] 665. Non-decreasing Array (Python) (0) | 2020.08.13 |
Comments