Skip to content

Commit 9f28c49

Browse files
+Reverse words in a given string
1 parent 3fdbb13 commit 9f28c49

File tree

2 files changed

+136
-2
lines changed

2 files changed

+136
-2
lines changed

Scripts/Largest Number formed from an Array.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
#http://www.geeksforgeeks.org/given-an-array-of-numbers-arrange-the-numbers-to-form-the-biggest-number/
22

33
def calc_largest_num(numbers):
4-
#sort the numbers in descending order (not considering their length) (with custom compare function applied)
4+
#sort the numbers in descending order (not considering their length)
5+
#custom compare function applied to the sorting proces
56
sorted_nums = sorted(numbers,key=cmp_to_key(mycmp),reverse=True)
67
return ''.join(sorted_nums)
78

89

910
#Sorting magic happens here
10-
#compare adjacent numbers combined, and then swapped. Whichever is higher, is retained
11+
#compare adjacent numbers combined and then swapped. Whichever is higher, is retained
1112
def mycmp(x,y):
1213
if int(x+y)>int(y+x):
1314
return 1
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
#http://www.geeksforgeeks.org/reverse-words-in-a-given-string/
2+
3+
def reverse_words(string):
4+
#store words in list spilt according to space
5+
list_string = string.split()
6+
#reverse the list
7+
list_string = list_string[::-1]
8+
return ' '.join(list_string)
9+
10+
#main
11+
if __name__=="__main__":
12+
string = input()
13+
reversed_words = reverse_words(string)
14+
15+
print("Reversed words -")
16+
print(reversed_words)
17+
18+
"""
19+
Input Explanation :
20+
- String
21+
22+
Input :
23+
This is easy with python
24+
25+
Output :
26+
Reversed words -
27+
python with easy is This
28+
29+
"""
30+
31+
'''
32+
Examples :
33+
34+
Input : s = "geeks quiz practice code"
35+
Output : s = "code practice quiz geeks"
36+
37+
Input : s = "getting good at coding needs a lot of practice"
38+
Output : s = "practice of lot a needs coding at good getting"
39+
Recommended: Please solve it on “PRACTICE ” first, before moving on to the solution.
40+
Algorithm:
41+
42+
1) Reverse the individual words, we get the below string.
43+
"i ekil siht margorp yrev hcum"
44+
2) Reverse the whole string from start to end and you get the desired output.
45+
"much very program this like i"
46+
#include<stdio.h>
47+
48+
/* function prototype for utility function to
49+
reverse a string from begin to end */
50+
void reverse(char *begin, char *end);
51+
52+
/*Function to reverse words*/
53+
void reverseWords(char *s)
54+
{
55+
char *word_begin = s;
56+
char *temp = s; /* temp is for word boundry */
57+
58+
/*STEP 1 of the above algorithm */
59+
while( *temp )
60+
{
61+
temp++;
62+
if (*temp == '\0')
63+
{
64+
reverse(word_begin, temp-1);
65+
}
66+
else if(*temp == ' ')
67+
{
68+
reverse(word_begin, temp-1);
69+
word_begin = temp+1;
70+
}
71+
} /* End of while */
72+
73+
/*STEP 2 of the above algorithm */
74+
reverse(s, temp-1);
75+
}
76+
77+
/* UTILITY FUNCTIONS */
78+
/*Function to reverse any sequence starting with pointer
79+
begin and ending with pointer end */
80+
void reverse(char *begin, char *end)
81+
{
82+
char temp;
83+
while (begin < end)
84+
{
85+
temp = *begin;
86+
*begin++ = *end;
87+
*end-- = temp;
88+
}
89+
}
90+
91+
/* Driver function to test above functions */
92+
int main()
93+
{
94+
char s[] = "i like this program very much";
95+
char *temp = s;
96+
reverseWords(s);
97+
printf("%s", s);
98+
getchar();
99+
return 0;
100+
}
101+
Run on IDE
102+
Output:
103+
104+
much very program this like i
105+
The above code doesn’t handle the cases when the string starts with space. The following version handles this specific case and doesn’t make unnecessary calls to reverse function in the case of multiple space in between. Thanks to rka143 for providing this version.
106+
107+
void reverseWords(char *s)
108+
{
109+
char *word_begin = NULL;
110+
char *temp = s; /* temp is for word boundry */
111+
112+
/*STEP 1 of the above algorithm */
113+
while( *temp )
114+
{
115+
/*This condition is to make sure that the string start with
116+
valid character (not space) only*/
117+
if (( word_begin == NULL ) && (*temp != ' ') )
118+
{
119+
word_begin=temp;
120+
}
121+
if(word_begin && ((*(temp+1) == ' ') || (*(temp+1) == '\0')))
122+
{
123+
reverse(word_begin, temp);
124+
word_begin = NULL;
125+
}
126+
temp++;
127+
} /* End of while */
128+
129+
/*STEP 2 of the above algorithm */
130+
reverse(s, temp-1);
131+
}
132+
Time Complexity: O(n)
133+
'''

0 commit comments

Comments
 (0)