@Vishal, Don't confuse printing in reverse with actually modifying the actual string to reverse word order in it :)
On Thu, Jul 7, 2011 at 3:34 PM, Vishal Thanki <vishaltha...@gmail.com> wrote: > @Navneet, it works with multiple spaces between words.. And here is > the two line solution :) > > import sys > print " ".join((sys.argv[1].split())[::-1]) > > > > On Thu, Jul 7, 2011 at 3:12 PM, Navneet Gupta <navneetn...@gmail.com> wrote: >> @Vishal, can you see if your program works well for more than single >> space between words? Not sure how split functions helps. >> >> BTW, Perl also is very strong language for string manipulations. >> (Specially designed for efficient string operations) >> >> On Thu, Jul 7, 2011 at 2:48 PM, Vishal Thanki <vishaltha...@gmail.com> wrote: >>> Off Topic: >>> Sorry for the diversion, but I was just wondering how easy it has >>> become to code in languages other than c. Here is the code i wrote for >>> the above mentioned problem in Python. It takes command line arg as >>> string. something like >>> >>> vishal@ubuntu:~/progs/python\ 02:45:07 PM >$ cat rev.py >>> #!/usr/bin/python >>> import sys >>> strn="" >>> for i in (sys.argv[1].split())[::-1]: >>> strn+=i+" " >>> print strn >>> >>> vishal@ubuntu:~/progs/python\ 02:45:09 PM >$ ./rev.py "This is a new world" >>> world new a is This >>> >>> vishal@ubuntu:~/progs/python\ 02:47:15 PM >$ >>> >>> >>> >>> >>> >>> On Thu, Jul 7, 2011 at 1:19 PM, Piyush Sinha <ecstasy.piy...@gmail.com> >>> wrote: >>>> @Navneet....take a look at the solution below and tell if there is any bug >>>> in it... >>>> >>>> #include <string.h> >>>> >>>> typedef struct revll >>>> { >>>> char s[100]; >>>> struct revll *next; >>>> }revll; >>>> >>>> revll *rev_str(char *a) >>>> { >>>> char temp[100]; >>>> int i,len=strlen(a),j=0; >>>> revll *head,*p; >>>> head=NULL; >>>> for(i=0;i<len;i++) >>>> { >>>> if(a[i]!=' ') >>>> { >>>> temp[j++] = a[i]; >>>> } >>>> else >>>> { >>>> temp[j] = '\0'; >>>> p = (revll *)malloc(sizeof(revll)); >>>> p->next = head; >>>> strcpy(p->s,temp); >>>> head = p; >>>> j=0; >>>> } >>>> } >>>> /*for last word*/ >>>> temp[j] = '\0'; >>>> p = (revll *)malloc(sizeof(revll)); >>>> p->next = head; >>>> strcpy(p->s,temp); >>>> head = p; >>>> return head; >>>> } >>>> >>>> main() >>>> { >>>> char a[100]; >>>> revll *head; >>>> gets(a); >>>> head = rev_str(a); >>>> while(head) >>>> { >>>> printf("%s->",head->s); >>>> head=head->next; >>>> } >>>> system("pause"); >>>> } >>>> >>>> >>>> >>>> On Thu, Jul 7, 2011 at 9:10 AM, Navneet Gupta <navneetn...@gmail.com> >>>> wrote: >>>>> >>>>> @Piyush, could you elaborate your approach with Linked List? >>>>> From what i am getting, even with Linked List, you would need two >>>>> traversals at least. >>>>> >>>>> On Thu, Jul 7, 2011 at 2:07 AM, Piyush Sinha <ecstasy.piy...@gmail.com> >>>>> wrote: >>>>> > Can we do it using linked list if ONE TIME TRAVERSAL is a constraint?? >>>>> > >>>>> > On 7/6/11, Tushar Bindal <tushicom...@gmail.com> wrote: >>>>> >> I read that solution. >>>>> >> But the same doubt as Navneet which I think you also raised i one of >>>>> >> your >>>>> >> posts on that thread >>>>> >> >>>>> >> On Wed, Jul 6, 2011 at 10:34 PM, Navneet Gupta >>>>> >> <navneetn...@gmail.com>wrote: >>>>> >> >>>>> >>> Saurabh, >>>>> >>> >>>>> >>> I understood your solution but wonder if it is purely single traversal >>>>> >>> >>>>> >>> In affect, you have a second traversal when you are popping the >>>>> >>> strings from stack to form the reverse order string. >>>>> >>> >>>>> >>> Though the second activity is less than O(n) i.e. O(#words in string) >>>>> >>> Nice solution, this way we can also get rid of extra spaces easily in >>>>> >>> the actual string if that is also to be done. >>>>> >>> >>>>> >>> On Wed, Jul 6, 2011 at 10:16 PM, saurabh singh <saurab...@gmail.com> >>>>> >>> wrote: >>>>> >>> > I have proposed my solution in one of the previous posts.Check the >>>>> >>> solution >>>>> >>> > there >>>>> >>> > >>>>> >>> > On Wed, Jul 6, 2011 at 10:10 PM, Tushar Bindal >>>>> >>> > <tushicom...@gmail.com> >>>>> >>> > wrote: >>>>> >>> >> >>>>> >>> >> good job >>>>> >>> >> but how can this be done in one traversal as asked on the Adobe >>>>> >>> Interview >>>>> >>> >> Questions thread. >>>>> >>> >> >>>>> >>> >> >>>>> >>> >> >>>>> >>> >> On Wed, Jul 6, 2011 at 9:49 PM, Navneet Gupta >>>>> >>> >> <navneetn...@gmail.com> >>>>> >>> >> wrote: >>>>> >>> >>> >>>>> >>> >>> I think somebody on this thread has asked this question but i am >>>>> >>> >>> not >>>>> >>> >>> able to find that. >>>>> >>> >>> >>>>> >>> >>> Question was if a string is like "my name is ram", then output >>>>> >>> >>> should >>>>> >>> >>> be "ram is name my". >>>>> >>> >>> >>>>> >>> >>> Wrote the code for same, so sharing. >>>>> >>> >>> >>>>> >>> >>> #include<iostream> >>>>> >>> >>> #include<string> >>>>> >>> >>> using namespace std; >>>>> >>> >>> >>>>> >>> >>> void SwapStringChars(string &str, int pos1, int pos2) >>>>> >>> >>> { >>>>> >>> >>> char ch = str[pos1]; >>>>> >>> >>> str[pos1] = str[pos2]; >>>>> >>> >>> str[pos2] = ch; >>>>> >>> >>> } >>>>> >>> >>> >>>>> >>> >>> void reverseString(string &str, int left, int right) >>>>> >>> >>> { >>>>> >>> >>> for(int i = left ; i <= left + (right-left)/2 ; i++) >>>>> >>> >>> SwapStringChars(str, i, right + left -i)); >>>>> >>> >>> } >>>>> >>> >>> >>>>> >>> >>> void reverseWordsInString(string &str) >>>>> >>> >>> { >>>>> >>> >>> char space = ' '; >>>>> >>> >>> int len = str.length(); >>>>> >>> >>> int startIndex = 0, endIndex = 0; >>>>> >>> >>> while(endIndex < len - 1) >>>>> >>> >>> { >>>>> >>> >>> while(str[endIndex] != space && endIndex < >>>>> >>> len)endIndex++; >>>>> >>> >>> reverseString(str, startIndex, endIndex-1); >>>>> >>> >>> startIndex = endIndex; >>>>> >>> >>> while(str[startIndex] == space)startIndex++; >>>>> >>> >>> endIndex = startIndex; >>>>> >>> >>> } >>>>> >>> >>> } >>>>> >>> >>> >>>>> >>> >>> int main() >>>>> >>> >>> { >>>>> >>> >>> string str; >>>>> >>> >>> cout<<"\nEnter enter the string :"; >>>>> >>> >>> getline(cin,str); >>>>> >>> >>> >>>>> >>> >>> //Reverse whole string at once >>>>> >>> >>> reverseString(str, 0, str.length() - 1); >>>>> >>> >>> >>>>> >>> >>> //Reverse Individual words in string >>>>> >>> >>> reverseWordsInString(str); >>>>> >>> >>> cout<<str; >>>>> >>> >>> cin.get(); >>>>> >>> >>> return 0; >>>>> >>> >>> } >>>>> >>> >>> >>>>> >>> >>> -- >>>>> >>> >>> Regards, >>>>> >>> >>> Navneet >>>>> >>> >>> >>>>> >>> >>> -- >>>>> >>> >>> You received this message because you are subscribed to the Google >>>>> >>> Groups >>>>> >>> >>> "Algorithm Geeks" group. >>>>> >>> >>> To post to this group, send email to algogeeks@googlegroups.com. >>>>> >>> >>> To unsubscribe from this group, send email to >>>>> >>> >>> algogeeks+unsubscr...@googlegroups.com. >>>>> >>> >>> For more options, visit this group at >>>>> >>> >>> http://groups.google.com/group/algogeeks?hl=en. >>>>> >>> >>> >>>>> >>> >> >>>>> >>> >> >>>>> >>> >> >>>>> >>> >> -- >>>>> >>> >> Tushar Bindal >>>>> >>> >> Computer Engineering >>>>> >>> >> Delhi College of Engineering >>>>> >>> >> Mob: +919818442705 >>>>> >>> >> E-Mail : tushicom...@gmail.com >>>>> >>> >> Website: www.jugadengg.com >>>>> >>> >> >>>>> >>> >> -- >>>>> >>> >> You received this message because you are subscribed to the Google >>>>> >>> Groups >>>>> >>> >> "Algorithm Geeks" group. >>>>> >>> >> To post to this group, send email to algogeeks@googlegroups.com. >>>>> >>> >> To unsubscribe from this group, send email to >>>>> >>> >> algogeeks+unsubscr...@googlegroups.com. >>>>> >>> >> For more options, visit this group at >>>>> >>> >> http://groups.google.com/group/algogeeks?hl=en. >>>>> >>> > >>>>> >>> > >>>>> >>> > >>>>> >>> > -- >>>>> >>> > Saurabh Singh >>>>> >>> > B.Tech (Computer Science) >>>>> >>> > MNNIT ALLAHABAD >>>>> >>> > >>>>> >>> > >>>>> >>> > -- >>>>> >>> > You received this message because you are subscribed to the Google >>>>> >>> > Groups >>>>> >>> > "Algorithm Geeks" group. >>>>> >>> > To post to this group, send email to algogeeks@googlegroups.com. >>>>> >>> > To unsubscribe from this group, send email to >>>>> >>> > algogeeks+unsubscr...@googlegroups.com. >>>>> >>> > For more options, visit this group at >>>>> >>> > http://groups.google.com/group/algogeeks?hl=en. >>>>> >>> > >>>>> >>> >>>>> >>> >>>>> >>> >>>>> >>> -- >>>>> >>> Regards, >>>>> >>> Navneet >>>>> >>> >>>>> >>> -- >>>>> >>> You received this message because you are subscribed to the Google >>>>> >>> Groups >>>>> >>> "Algorithm Geeks" group. >>>>> >>> To post to this group, send email to algogeeks@googlegroups.com. >>>>> >>> To unsubscribe from this group, send email to >>>>> >>> algogeeks+unsubscr...@googlegroups.com. >>>>> >>> For more options, visit this group at >>>>> >>> http://groups.google.com/group/algogeeks?hl=en. >>>>> >>> >>>>> >>> >>>>> >> >>>>> >> >>>>> >> -- >>>>> >> Tushar Bindal >>>>> >> Computer Engineering >>>>> >> Delhi College of Engineering >>>>> >> Mob: +919818442705 >>>>> >> E-Mail : tushicom...@gmail.com >>>>> >> Website: www.jugadengg.com >>>>> >> >>>>> >> -- >>>>> >> You received this message because you are subscribed to the Google >>>>> >> Groups >>>>> >> "Algorithm Geeks" group. >>>>> >> To post to this group, send email to algogeeks@googlegroups.com. >>>>> >> To unsubscribe from this group, send email to >>>>> >> algogeeks+unsubscr...@googlegroups.com. >>>>> >> For more options, visit this group at >>>>> >> http://groups.google.com/group/algogeeks?hl=en. >>>>> >> >>>>> >> >>>>> > >>>>> > >>>>> > -- >>>>> > *Piyush Sinha* >>>>> > *IIIT, Allahabad* >>>>> > *+91-8792136657* >>>>> > *+91-7483122727* >>>>> > *https://www.facebook.com/profile.php?id=100000655377926 * >>>>> > >>>>> > -- >>>>> > You received this message because you are subscribed to the Google >>>>> > Groups "Algorithm Geeks" group. >>>>> > To post to this group, send email to algogeeks@googlegroups.com. >>>>> > To unsubscribe from this group, send email to >>>>> > algogeeks+unsubscr...@googlegroups.com. >>>>> > For more options, visit this group at >>>>> > http://groups.google.com/group/algogeeks?hl=en. >>>>> > >>>>> > >>>>> >>>>> >>>>> >>>>> -- >>>>> Regards, >>>>> Navneet >>>>> >>>>> -- >>>>> You received this message because you are subscribed to the Google Groups >>>>> "Algorithm Geeks" group. >>>>> To post to this group, send email to algogeeks@googlegroups.com. >>>>> To unsubscribe from this group, send email to >>>>> algogeeks+unsubscr...@googlegroups.com. >>>>> For more options, visit this group at >>>>> http://groups.google.com/group/algogeeks?hl=en. >>>>> >>>> >>>> >>>> >>>> -- >>>> Piyush Sinha >>>> IIIT, Allahabad >>>> +91-8792136657 >>>> +91-7483122727 >>>> https://www.facebook.com/profile.php?id=100000655377926 >>>> >>>> -- >>>> You received this message because you are subscribed to the Google Groups >>>> "Algorithm Geeks" group. >>>> To post to this group, send email to algogeeks@googlegroups.com. >>>> To unsubscribe from this group, send email to >>>> algogeeks+unsubscr...@googlegroups.com. >>>> For more options, visit this group at >>>> http://groups.google.com/group/algogeeks?hl=en. >>>> >>> >>> -- >>> You received this message because you are subscribed to the Google Groups >>> "Algorithm Geeks" group. >>> To post to this group, send email to algogeeks@googlegroups.com. >>> To unsubscribe from this group, send email to >>> algogeeks+unsubscr...@googlegroups.com. >>> For more options, visit this group at >>> http://groups.google.com/group/algogeeks?hl=en. >>> >>> >> >> >> >> -- >> Regards, >> Navneet >> >> -- >> You received this message because you are subscribed to the Google Groups >> "Algorithm Geeks" group. >> To post to this group, send email to algogeeks@googlegroups.com. >> To unsubscribe from this group, send email to >> algogeeks+unsubscr...@googlegroups.com. >> For more options, visit this group at >> http://groups.google.com/group/algogeeks?hl=en. >> >> > > -- > You received this message because you are subscribed to the Google Groups > "Algorithm Geeks" group. > To post to this group, send email to algogeeks@googlegroups.com. > To unsubscribe from this group, send email to > algogeeks+unsubscr...@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/algogeeks?hl=en. > > -- Regards, Navneet -- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.