코드로 우주평화

Remove First and Last Character Part Two 본문

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

Remove First and Last Character Part Two

daco2020 2022. 4. 10. 22:45
반응형

Description:

This is a spin off of my first kata.

You are given a string containing a sequence of character sequences separated by commas.

Write a function which returns a new string containing the same character sequences except the first and the last ones but this time separated by spaces.

If the input string is empty or the removal of the first and last items would cause the resulting string to be empty, return an empty value (represented as a generic value NULL in the examples below).

Examples

"1,2,3"      =>  "2"
"1,2,3,4"    =>  "2 3"
"1,2,3,4,5"  =>  "2 3 4"

""     =>  NULL
"1"    =>  NULL
"1,2"  =>  NULL

 

 

Solution:

1. Split the string into an array and exclude elements on the edges.
2. It returns with spaces between elements.
3. If there is no value to return, it returns None.

 

def array(string):
  return " ".join(string.split(",")[1:-1]) or None

 

 

 

반응형

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

Isograms  (0) 2022.04.12
Student's Final Grade  (0) 2022.04.11
Total amount of points  (0) 2022.04.09
Transportation on vacation  (0) 2022.04.08
Beginner - Lost Without a Map  (0) 2022.04.07