-
Notifications
You must be signed in to change notification settings - Fork 0
/
SentenseReversal.py
176 lines (110 loc) · 3.49 KB
/
SentenseReversal.py
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# -*- coding: utf-8 -*-
"""
Created on Thu Jul 26 01:05:45 2018
@author: sanooj
"""
"""
Given a string of words
reverse all words
input: "This is the best"
putput: "best the is this"
all trailing an leading white space shoudld be removed
example
in: "Hi John, are you ready to go?"
out: "go? to ready you are John, hi"
"""
class WordOrder(object):
def __init__(self,word: str, order: int):
self.word = word
self.order = order
def __repr__ (self):
return (
"%s"
%(self.word)
)
def reverse_a_string_of_words(string_of_words: str):
#remove trailing and leading white space
#string_of_words = \
#string_of_words.lstrip().rstrip()
#enumerate through word
words = []
index = 0
array_of_words = \
string_of_words.split(" ")
print("array_of_words: %s" %(array_of_words))
for word in array_of_words:
word_order = \
WordOrder(
word,
index
)
if len(word_order.word) != 0:
words.append(word_order)
index+=1
print("_words: %s" %(words))
# reversing
words_reversed = []
index = len(words) - 1
for word in words:
if len(words[index].word) != 0:
words_reversed.append(words[index])
index-=1
print(words_reversed)
#reverse_a_string_of_words(" the very best ")
atr = " Hi John, are you ready to go?"
reverse_a_string_of_words(atr)
def reverse_word_preferred(s):
words = []
length = len(s)
spaces = [' ']
i = 0
while i < length:
# check if char is space
if s[i] not in spaces:
# start counting the word
word_begin = i
# find the end of the word
# lopp till another space is found
while i < length and s[i] not in spaces:
#kep looping
i += 1
# space found
# word ended
words.append(s[word_begin:i])
# keep looping
i += 1
return " ".join(reversed(words))
print("reverse_word_preferred")
print(reverse_word_preferred(atr))
def reverse_word_preferred_for_loop(string_of_words: str):
words = []
length = len(string_of_words)
spaces = [' ','']
# index to detect end of string
i = 0
# each word in the string
word_substring = ""
print("length %s" %(length))
for char in string_of_words:
i += 1
# check if it is space
if char not in spaces:
# not space
# add to word array
word_substring += char
print(word_substring)
print(i)
# last word
if i == length:
words.append(word_substring)
# go till next space
else:
# add word to dict
if len(word_substring) > 0:
words.append(word_substring)
# reset substring
word_substring = ""
print(words)
print(" ".join(reversed(words)))
print("reverse_word_preferred_for_loop")
reverse_word_preferred_for_loop(atr)