+ 23

Code challenge : Sort Words in a string (My code included)

I'm longer than you! (By: Pixie) Create a function sortWords() that accepts a string and sorts the words in it based on their respective lengths in an ascending order. If there are two words of the same lengths, sort them alphabetically. Look at the examples below for more details. e.g. sortWords("I just ate an apple") // "I an ate just apple" e.g. sortWords("I have four apples and five pineapples") // "I and five four have apples pineapples" https://code.sololearn.com/c6Ptx0bCbGF0/?ref=app

12th May 2017, 5:50 PM
Pixie
Pixie - avatar
36 Antworten
+ 7
How do we treat punctuation? If we ignore it, I have this one line solution: https://code.sololearn.com/cvK1LAv8cneM
12th May 2017, 8:55 PM
Tob
Tob - avatar
+ 14
@Nikolay It sorts alphabetically first and then by length. I think it should first sort by length and then alphabetically ascending. (I hope I won't get killed here for criticising your code ^^ ) @Rishabh Same issue ;)
12th May 2017, 9:31 PM
Tashi N
Tashi N - avatar
+ 14
@Rishabh I'm not good at c++ :) I know it only from university, but I found some approaches at SO: 1. For splitting a string using vector, see http://stackoverflow.com/questions/236129/split-a-string-in-c (last code example) 2. Use std::sort (sorts alphabetically ascending) 3. Sort by length: http://stackoverflow.com/questions/18831470/sorting-a-string-vector-based-on-the-string-size
12th May 2017, 10:24 PM
Tashi N
Tashi N - avatar
+ 13
Okay, I didn't write a method but I think that doesn't matter (it's quite short)... So here's mine (Java) https://code.sololearn.com/c5HDbgl4wynO/?ref=app
12th May 2017, 8:58 PM
Tashi N
Tashi N - avatar
+ 13
@Rishabh Burey described the method: 1. Parse the string to an array using regular expression (or something else) 2. Sort the array alphabetically ascending (default for Java and Javascript, so calling sort() is enough). 3. Sort the array by the length of the strings That's what I do with Java, too. Just translate it to c++.
12th May 2017, 10:01 PM
Tashi N
Tashi N - avatar
+ 13
@Pixie I tried making it using dictionaries, but I realised the problem in the end... Tobi's program is better ;)
13th May 2017, 10:25 AM
Gami
Gami - avatar
+ 12
@Rishabh Yep, use @Burey's solution to sort alphabetically (still not working).
12th May 2017, 9:50 PM
Tashi N
Tashi N - avatar
+ 12
@Rishabh My input was I ate an apple and
12th May 2017, 9:52 PM
Tashi N
Tashi N - avatar
13th May 2017, 9:36 AM
Gami
Gami - avatar
+ 10
@Tashi N, @Rishabh - fixed now
12th May 2017, 10:28 PM
Nikolay Nachev
Nikolay Nachev - avatar
+ 10
@Tobi @Tashi @Rishabh @Nikolay Well done guys, all the codes work for "The legend of Zelda Ocarina of Time" 😂 It's an honour having you here @Nikolay
13th May 2017, 1:47 AM
Pixie
Pixie - avatar
12th May 2017, 9:26 PM
Nikolay Nachev
Nikolay Nachev - avatar
+ 9
@Tamas Wow. That is awesome *__*
13th May 2017, 2:24 PM
Pixie
Pixie - avatar
+ 7
it's from Nikolay solution i just added sorting by length after the alphabetic sort
12th May 2017, 9:53 PM
Burey
Burey - avatar
+ 7
Nice @Gami. 😊 Too bad it wont work with words of same length 😔
13th May 2017, 10:00 AM
Pixie
Pixie - avatar
13th May 2017, 2:22 PM
Tamás Barta
Tamás Barta - avatar
+ 7
https://www.sololearn.com/discuss/387579/?ref=app Challenge #3 Sorry for deleting this earlier but somethin came up
13th May 2017, 5:43 PM
Pixie
Pixie - avatar
+ 6
Hell yeah!!!! I just completed it Thanks @Tashi, @Burey for your help Can you check if it is working in all test cases.
12th May 2017, 10:48 PM
Rishabh Agrawal
Rishabh Agrawal - avatar
+ 6
@richard Well done 😊
13th May 2017, 8:43 AM
Pixie
Pixie - avatar
13th May 2017, 2:03 PM
Pixie
Pixie - avatar