-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathyour_own_split.py
More file actions
56 lines (46 loc) · 1.45 KB
/
Copy pathyour_own_split.py
File metadata and controls
56 lines (46 loc) · 1.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
'''
Your task is to write your own function, which behaves almost exactly like the original split() method, i.e.:
- it should accept exactly one argument - a string;
- it should return a list of words created from the string, divided in the places where the string contains whitespaces;
- if the string is empty, the function should return an empty list;
its name should be mysplit()
Use the given template. Test your code carefully.
def mysplit(strng):
#
# put your code here
#
print(mysplit("To be or not to be, that is the question"))
print(mysplit("To be or not to be,that is the question"))
print(mysplit(" "))
print(mysplit(" abc "))
print(mysplit(""))
Expected Output:
['To', 'be', 'or', 'not', 'to', 'be,', 'that', 'is', 'the', 'question']
['To', 'be', 'or', 'not', 'to', 'be,that', 'is', 'the', 'question']
[]
['abc']
[]
'''
###############################################################
#python code
def mysplit(strng):
val = strng.strip()
a = []
if len(val) == 0 or val.isspace():
return a
word = ""
for i in val:
if not i.isspace():
word += i
else:
a.append(word)
word = ''
else:
a.append(word)
return a
print(mysplit("To be or not to be, that is the question"))
print(mysplit("To be or not to be,that is the question"))
print(mysplit(" "))
print(mysplit(" abc "))
print(mysplit(""))
print(mysplit("hello there, my name is Samip Shah and this is my own split function."))