-
Notifications
You must be signed in to change notification settings - Fork 0
/
change_sentence.rb
49 lines (40 loc) · 966 Bytes
/
change_sentence.rb
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
# Inplace reverse a sentence
# You given a sentence of english words and spaces between them.
# Nothing crazy:
# 1) no double spaces
# 2) no empty words
# 3) no spaces at the ends of a sentence
# void inplace_reverse(char* arr, int length) {
# // your solution
# }
# Example:
# input "I wish you a merry Christmas"
# output "Christmas merry a you wish I"
# Constrains: O(1) additional memory
def inplace_reverse(string, length)
revert_word(string, 0 , length-1)
i = 0
while i < length
j = i
while (string[i] != " " and i < length) do
puts i
i += 1
end
puts string[j..i-1]
revert_word(string, j, i-1)
puts "#{j} #{i-1} "
i += 1
end
puts string
end
#memory keep 1
def revert_word(word, start, last)
puts word
j = 0
(start..((last + start)/2)).each do |i|
word[i], word[last-j] = word[last-j], word[i]
j += 1
end
end
a = "I wish you a merry Christmas"
inplace_reverse(a, a.size)