코드로 우주평화

Dashatize it 본문

나는 이렇게 학습한다/Algorithm & SQL

Dashatize it

daco2020 2022. 3. 22. 11:17
반응형

Description:

Given a variable n,

If n is an integer, Return a string with dash'-'marks before and after each odd integer, but do not begin or end the string with a dash mark. If n is negative, then the negative sign should be removed.

If n is not an integer, return an empty value.

Ex:

dashatize(274) -> '2-7-4'
dashatize(6815) -> '68-1-5'

 

Solution:

1. If 'n' is not a number, 'None' is returned.
2. If 'n' is a number, convert it to a positive number and then to a string.
3. Repeat the string, and if it is an odd number, put '-' on both sides, and if it is an even number, put it in the array.
4. Combine the arrays and change '--' to '-'.
5. If there is a '-' at the end, it is deleted and returned.

 

def dashatize(n):
    if type(n) != int: return 'None'
    temp_arr = [f"-{i}-" if int(i)&1 else i for i in str(abs(n))] 
    result = ''.join(temp_arr).replace('--','-').strip('-')
    return result

 

 

 

 

반응형

'나는 이렇게 학습한다 > Algorithm & SQL' 카테고리의 다른 글

Find the divisors!  (0) 2022.03.24
Sum of Digits / Digital Root  (0) 2022.03.23
Data Reverse  (0) 2022.03.21
Persistent Bugger  (0) 2022.03.20
자료구조 _ Hash table  (0) 2022.03.20