본문으로 바로가기

HackerRank Python Solution / Mutations

category Develop/hackerrank.com 2022. 1. 17. 02:31

 

 

Mutations

 def mutate_string(string, position, character):

    s = string
    l = list(string)
    l[position] = character
    return ''.join(l)

if __name__ == '__main__':

 

 

해설

>>> string = "abracadabra"
>>> l = list(string)
>>> l
['a', 'b', 'r', 'a', 'c', 'a', 'd', 'a', 'b', 'r', 'a']
>>> l[5] = 'k'
>>> l
['a', 'b', 'r', 'a', 'c', 'k', 'd', 'a', 'b', 'r', 'a']
''.join(l)
>>> 'abrackdabra'

지금까지 배운 문자열, 리스트, 인덱싱, 조인 등의 활용법이다

 

indexing

https://bohemihan.tistory.com/entry/HackerRank-Python-Solution-Lists 

 

HackerRank Python Solution / Lists

Lists if __name__ == '__main__': N = int(input()) result = [] for i in range(0, N): tmp_str = input() tmp_str_list = tmp_str.strip().split(" ") cmd = tmp_str_list[0] if cmd == "print": print(resul..

bohemihan.tistory.com



join

>>> words = ['this', 'is', 'a', 'string']
>>> "-".join(words)
'this-is-a-string'

join함수는 객체(iterable) 내의 문자사이에 '삽입할것'을 넣어주는 기능을 한다.

사용방법
삽입할것.join(iterable) 

파라미터
· join 함수는 객체(iterable) 자리에 List, Tuple, Dictionary, Set 등을 받는다.