-2

Hi there so I am looking to build this python function with simple things like def, find etc. so far I know how to get the first part of the code.

Given a string such as "HELLODOGMEMEDOGPAPA", I will need to return a list that gives me three things:

Everything before the word dog which i will denote as before_dog The word dog until dog appears again dog_todog Everything after the second time dog appears will be denoted by after_todog The list will be in the form [before_dog,dog_todog,after_todog].

so for example given ("HELLODOGMEMEDOGPAPADD") this will return the list ("HELLO","DOGMEME","DOGPAPADD")

another example would be ("HEYHELLOMANDOGYDOGDADDY") this would return the list ("HEYHELLOMAN","DOGY","DOGDADDY")

but if I have ("HEYHELLODOGDADDY") the output will be ("HEYHELLO","DOGDADDY","")

also if dog never appears ("HEYHELLOYO") then the output will be ("HEYHELLOYO,"","")

This is what I have so far:

def split_list(words):
    # declare the list
    lst = []
    # find the first position
    first_pos=words.find("DOG")
    # find the first_pos
    before_dog = words [0:first_pos]
    lst.append(before_dog)
    return lst
KMAN
  • 39
  • 4

4 Answers4

1

Funny function split_2_dogs() with re.findall() function:

import re

def split_2_dogs(s):
    if s.count('DOG') == 2:   # assuring 2 dogs are "walking" there
        return list(re.findall(r'^(.*)(DOG.*)(DOG.*)$', s)[0])

print(split_2_dogs("HELLODOGMEMEDOGPAPADD"))
print(split_2_dogs("HEYHELLOMANDOGYDOGDADDY"))

The output:

['HELLO', 'DOGMEME', 'DOGPAPADD']
['HEYHELLOMAN', 'DOGY', 'DOGDADDY']

Alternative solution with str.index() and str.rfind() functions:

def split_2_dogs(s):
    if 'DOG' not in s: return [s,'']
    pos1, pos2 = s.index('DOG'), s.rfind('DOG')
    return [s[0:pos1], s[pos1:pos2], s[pos2:]]
RomanPerekhrest
  • 88,541
  • 4
  • 65
  • 105
  • is it possible to do this without any ifs and just slicing or import – KMAN Oct 20 '17 at 16:57
  • hey roman so how would I go about doing it if I wanted the first time dog appeared to always be in the second part of the list even if there is only one time that dog appears in the given string? – KMAN Oct 20 '17 at 17:38
  • @KirusanS, see my **last** update in the second approach – RomanPerekhrest Oct 20 '17 at 18:03
0

Splitting at DOG is the key!! This code will for all the cases that you have mentioned.

from itertools import izip_longest

words = 'HEYHELLODOGDADDY'
words = words.split("DOG")
words = ['DOG'+j if i>0 else j for i,j in enumerate(words)]
# words = ['HEYHELLO', 'DOGDADDY']
ans = ['','','']
# stitch words and ans together
ans = [m+n for m,n in izip_longest(words,ans,fillvalue='')]

print ans

Output :

['HEYHELLO', 'DOGDADDY', '']
Miraj50
  • 4,257
  • 1
  • 21
  • 34
0

This is pretty easy to do using the split function. For example, you can split any string by a delimiter, like dog, as so:

>>> chunks = 'HELLODOGMEMEDOGPAPA'.split('DOG')
>>> print(chunks)
['HELLO', 'MEME', 'PAPA']

You could then use the output of that in a list comprehension, like so:

>>> dog_chunks = chunks[:1] + ["DOG" + chunk for chunk in chunks[1:]]
>>> print(dog_chunks)
['HELLO', 'DOGMEME', 'DOGPAPA']

The only slightly tricky bit is making sure you don't prepend dog to the first string in the list, hence the little bits of slicing.

0

Split the string at 'DOG' and use conditions to get the desired result

s = 'HELLODOGMEMEDOGPAPADD'
l = s.split('DOG')
dl = ['DOG'+i for i in l[1:]]
[l[0]]+dl if l[0] else dl

Output:

['HELLO', 'DOGMEME', 'DOGPAPADD']
Transhuman
  • 3,527
  • 1
  • 9
  • 15