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

Apparently-Modifying Strings

daco2020 2022. 4. 26. 00:21
반응형

Description:

For every string, after every occurrence of 'and' and/or 'but', insert the substring 'apparently' directly after the occurrence(s).

If input does not contain 'and' or 'but', return the same string. If a blank string, return ''.

If substring 'apparently' is already directly after an 'and' and/or 'but', do not add another. (Do not add duplicates).

Examples:

Input 1

'It was great and I've never been on live television before but sometimes I don't watch this.'

Output 1

'It was great and apparently I've never been on live television before but apparently sometimes I don't watch this.'

Input 2

'but apparently'

Output 2

'but apparently' 

(no changes because 'apparently' is already directly after 'but' and there should not be a duplicate.)

An occurrence of 'and' and/or 'but' only counts when it is at least one space separated. For example 'andd' and 'bbut' do not count as occurrences, whereas 'b but' and 'and d' does count.

 

 

Solution:

1. Divide 'string' based on space.
2. Check if there are 'and' and 'but' among the divided words.
3. Check that the next word is not 'apparently'.
4. If the condition is met, add the word 'apparently'.
5. If not, move on to the next word.

 

def apparently(string):
    arr = string.split(" ")
    return " ".join([
        [v, v+" apparently"][v in ["and","but"] and arr[i+1:i+2] != ["apparently"]
        ] for i,v in enumerate(arr)])

 

 

 

 

반응형

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

Mumbling  (0) 2022.04.27
Two to One  (0) 2022.04.26
The Office I - Outed  (0) 2022.04.24
Shortest Word  (0) 2022.04.23
Vowel Count  (0) 2022.04.22