+ 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
36 Respuestas
+ 7
How do we treat punctuation? If we ignore it, I have this one line solution:
https://code.sololearn.com/cvK1LAv8cneM
+ 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 ;)
+ 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
+ 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
+ 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++.
+ 13
@Pixie I tried making it using dictionaries, but I realised the problem in the end...
Tobi's program is better ;)
+ 12
@Rishabh Yep, use @Burey's solution to sort alphabetically (still not working).
+ 12
@Rishabh My input was
I ate an apple and
+ 12
I done it in Python
https://code.sololearn.com/c73sqdK5F2vQ/?ref=app
+ 10
@Tashi N, @Rishabh - fixed now
+ 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
+ 9
here is JS solution
https://code.sololearn.com/WNAgbp9c2x8s/?ref=app
+ 9
@Tamas Wow. That is awesome *__*
+ 7
it's from Nikolay solution
i just added sorting by length after the alphabetic sort
+ 7
Nice @Gami. 😊
Too bad it wont work with words of same length 😔
+ 7
My solution in Java:
https://code.sololearn.com/cpiC38ei6Bq5/#java
+ 7
https://www.sololearn.com/discuss/387579/?ref=app
Challenge #3
Sorry for deleting this earlier but somethin came up
+ 6
Hell yeah!!!!
I just completed it
Thanks @Tashi, @Burey for your help
Can you check if it is working in all test cases.
+ 6
@richard Well done 😊
+ 6
New challenge
https://www.sololearn.com/Discuss/387024/?ref=app