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

Reverse words

daco2020 2022. 3. 28. 11:53
반응형

Description:

Complete the function that accepts a string parameter, and reverses each word in the string. All spaces in the string should be retained.

Examples

"This is an example!" ==> "sihT si na !elpmaxe"
"double  spaces"      ==> "elbuod  secaps"

 

Solution:

1. Separate text based on space.
2. Turn over the separated text and put it in an array.
3. Convert the text in the array to a string with spaces.

 

def reverse_words(text):
  return " ".join([i[::-1] for i in text.split(" ")])

 

 

 

반응형

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

Find the odd int  (0) 2022.03.30
Sum of the first nth term of Series  (0) 2022.03.29
Invert values  (0) 2022.03.27
How many pages in a book?  (0) 2022.03.26
Is n divisible by x and y?  (0) 2022.03.25