7 kyu
Alphabetize a list by the nth character
753 of 1,996edm2410
Loading description...
Lists
Strings
Sorting
Arrays
Fundamentals
View
This comment has been reported as {{ abuseKindText }}.
Show
This comment has been hidden. You can view it now .
This comment can not be viewed.
- |
- Reply
- Edit
- View Solution
- Expand 1 Reply Expand {{ comments?.length }} replies
- Collapse
- Spoiler
- Remove
- Remove comment & replies
- Report
{{ fetchSolutionsError }}
-
-
Your rendered github-flavored markdown will appear here.
-
Label this discussion...
-
No Label
Keep the comment unlabeled if none of the below applies.
-
Issue
Use the issue label when reporting problems with the kata.
Be sure to explain the problem clearly and include the steps to reproduce. -
Suggestion
Use the suggestion label if you have feedback on how this kata can be improved.
-
Question
Use the question label if you have questions and/or need help solving the kata.
Don't forget to mention the language you're using, and mark as having spoiler if you include your solution.
-
No Label
- Cancel
Commenting is not allowed on this discussion
You cannot view this solution
There is no solution to show
Please sign in or sign up to leave a comment.
JS fork π€
mocha/chai
assertions +lodash
for randomnessIssue with problem statement
There are inconsistencies between the problem description and the test cases.
The problem statement says, "The letters should be compared case-insensitive," but the tests only pass if the sorting is case-sensitive.
The the following sentence in the problem statement is redundant: "If both letters are the same, order them normally (lexicographically), again, case-insensitive". Lexicographical ordering already handles ties without needing a separate case-insensitivity clause.
Please clarify whether the sorting should be case-sensitive or case-insensitive.
Not worth spending time cause the description is awful.
python new test framework is required. updated in this fork
Approved
doesn't match case-insensitive spec
There is an error in the description. It says that letters should be compared case insensitive, but the tests can only succeed if they are case-sensitive. The lexicographic sorting is also unnecessary if the letters match.
Thanks ilya for the input and I was able to solve the Kata by following your instructions. I unnecessary spent time on the Kata because of its incorrect description.
Whoever wants to solve this kata just follow this part of the description "Write a function that accepts two parameters, i) a string (containing a list of words) and ii) an integer (n). The function should alphabetize the list based on the nth letter of each word." and ignore anything further.
Could some one help on "how to deal with multiple words separated by spaces". Should space be considered or not.Any further explanation to under this Kata is very much appreciated. I see many people solved the Kata in many different languages, so I must be missing something. I sorted by character at the index and then the word lexicographically, but test are failing. I followed the step to make the comparision case insensitive as well.
Compare words (groups of words) separated by a comma. Also, when comparing, you must take the case of letters into account, even though the description says otherwise. The lexicographic sorting is also unnecessary if the letters match.
OP solved it, closing
Misleading. Its not just words, it's words separated by spaces, then the comma. And some of the words are less than n in length.
JS: Node v14 should be used along with its appropiate assertion tools (Mocha + Chai). Refer to this and this
Description should mention how to deal with multiple words separated by spaces.
This is great but a 7? Really?
First part doesn't hold for ruby where case does matter, second also doesn't because ties are broken by position in original array.
This comment has been hidden.
(Python) I can't understand the logic when sorting "multiple words" words. First problem is that description says nothing about what is actually considerated a word here... I guess words are things separated by commas... So why :
"Ernst von Eisenach, Oskar von Reuenthal, Anton Ferner, Ernst von Eisenach, Hildegard von Mariendorf, Karl Gustav Kempf, Adalbert von Fahrenheit, Karl Robert Steinmetz, August Samuel Wahlen, Theodor von Lucke, Siegfried Kircheis, Ulrich Kesler, Helmut Rennenkampf, Emil von Secla, Arthur von Streit, Paul von Oberstein, Reinhard von Lohengramm, Wolfgang Mittermeyer"
should be sorted as
"'Ernst von Eisenach, Oskar von Reuenthal, Ernst von Eisenach, Hildegard von Mariendorf, Siegfried Kircheis, Wolfgang Mittermeyer, Paul von Oberstein, Emil von Secla, Karl Gustav Kempf, August Samuel Wahlen, Anton Ferner, Helmut Rennenkampf, Arthur von Streit, Theodor von Lucke, Karl Robert Steinmetz, Ulrich Kesler, Adalbert von Fahrenheit, Reinhard von Lohengramm"
with n = 10?
10th letter of first three "sorted" "words" are 'i', 'e' and 'i' respectively. How could that be sorted? Unless it's actually meant not 10th letter but 10th character (in that case please call things by their names). If it is so in those three words the 10th character is a space (how should we sort between letters and spaces?), the description says " If both letters are the same, order them normally (lexicographically)". How would "Ernst..., Oskar..., Ernst..." be sorted alphabetically?
This comment has been hidden.
Python: the first argument should be named
s
orstrng
, notlist_
... x)Fixed.
I think the unit test has a bug. There is no requirement on sorting two words with the same sorting character. But in unit test, it is ordered. Thus on random test, simply order by n char may fail.
Not only that, the order is not consistent.
Some random tests require the (n+1)th char to be sorted also:
But some require the opposite:
and whichever you pick, the other one appears eventually.
Perhaps we can randomly choose the correct algorithm and hope to get lucky?
When the nth characters are the same, the test compares the whole words alphabetically.
Revised ordering condition
This comment has been hidden.
How are you supposed to handle multiple words having a character at "n"? In another comment, it is mentioned that such corner cases aren't tested, yet my results are failing because my ordering of multiple words with the same 'n'th character does not match the expected ordering. The expected ordering of these 'duplicates' does not seem to be based on insertion order, alphabetical order starting from the first letter, or alphabetical order starting from the nth letter. It seems really ambiguous to leave out the instructions for how to handle this corner case and then test that corner case.
Where do I enter the solution?
Use more descriptive parameter names in the beginning code template. list_ and n could be more descriptive and the underscore suffix is unconventional for Ruby. Suggestion: words and sort_char_position as clearer param names. Good for participants to see good naming practices in the provided code.
Hello everyone, I'm new here and this is my first Kata. Can someone please explain to me what is going on. I just finished up the code and was excited when all of the tests passed then this happened.
Basic tests Test Passed: Value == wording, words, wordy, wordz Test Passed: Value == ha, he, hi, ho Test Passed: Value == a, ba, cat, wax, yellow, zephyr Test Passed: Value == today, are, doing, hello, you, how Test Passed: Value == wording, words, wordy, wordz Test Passed: Value == ha, he, hi, ho Test Passed: Value == a, ba, cat, wax, yellow, zephyr Test Passed: Value == today, are, doing, hello, you, how Random tests Testing for "August Samuel Wahlen, Paul von Oberstein, Emil von Secla, Karl Robert Steinmetz, Fritz Josef Bittenfeld, Ernst von Eisenach, Arthur von Streit", 10 TypeError: Object function { var i = this.length, j, k; for { j = Math.floor(Math.random() * this.length); k = this[--i];this[i] = this[j];this[j] = k; } return this; } has no method 'substring' at sortIt at Object.Test.it at Object.Test.describe
8 Passed 0 Failed 40 Errors Process took 81ms to complete
I'm really confused. None of this code is even anything I wrote. How do I get a typeError for something I never put in there. I'm sure there is a simple explanation I hope that someone can explain to me whats happening. Thank You
I also believe the Random tests can at least sometimes be incorrect.
The test instructions mention a primary sort by the n-th letter, but has no further sorting instructions. A comment below suggests a secondary sort of "insertion order", where "insertion order" is assumed [by me] to be 'left to right'.
However, the "Random tests" don't appear to always follow the this primary-secondary sort order.
I believe some of the random test cases are incorrect:
Testing for "Arthur von Streit, August Samuel Wahlen, Siegfried Kircheis, Karl Robert Steinmetz, Reinhard von Lohengramm, Helmut Rennenkampf, Ernst von Eisenach, Fritz Josef Bittenfeld", 2
It should work for random inputs too - Expected: Karl Robert Steinmetz, Reinhard von Lohengramm, Helmut Rennenkampf, Siegfried Kircheis, Arthur von Streit, Ernst von Eisenach, Fritz Josef Bittenfeld, August Samuel Wahlen, instead got: Karl Robert Steinmetz, Reinhard von Lohengramm, Helmut Rennenkampf, Siegfried Kircheis, Fritz Josef Bittenfeld, Ernst von Eisenach, Arthur von Streit, August Samuel Wahlen
Compare the Expected (top) vs mine (bottom)
K[a]rl Robert Steinmetz, R[e]inhard von Lohengramm, H[e]lmut Rennenkampf, S[i]egfried Kircheis, A[r]thur von Streit, E[r]nst von Eisenach, F[r]itz Josef Bittenfeld, A[u]gust Samuel Wahlen
K[a]rl Robert Steinmetz, R[e]inhard von Lohengramm, H[e]lmut Rennenkampf, S[i]egfried Kircheis, F[r]itz Josef Bittenfeld, E[r]nst von Eisenach, A[r]thur von Streit, A[u]gust Samuel Wahlen
Specifically,
A[rt]hur von Streit, E[rn]st von Eisenach, F[ri]tz Josef Bittenfeld
F[ri]tz Josef Bittenfeld, E[rn]st von Eisenach, A[rt]hur von Streit
Clearly, the correct aphabetical order, starting from the 2nd letter is [ri, rn, rt] and NOT [rt, rn, ri].
This example test case is sorted alphabetically by second letter until there are ties. Then ties are sorted by insertion order.
This needs to be made clear if it is the intended behavior.
The duplicate handling is still confusing me.
Notice in this test case, that "Reinhard" is the first name to appear with an "i" in 3rd position.
Here's what is expected. Notice that Reinhard is now in the middle of several other names with "i" in the 3d position. This order seems arbitrary. By insertion order, Reinhard should be first in the "i" group; by alphabetical order, Ne[id]hardt should come before Re[in]hardt, but doesn't. Did I misread something??
Here's what I got:
This comment has been hidden.
Revised ordering condition
Kata upvoted, greenlit, took the liberty of adding a few extra cases myself and translated it into both Ruby and Python, if you wish to approve them :)
[For the sake of my own curiosity, I was wondering why you would do a kata in JS and call the array "list", as it is the norm in Python: could you tell me why you used that term, if there was a particular reason?]
So, what about the corner cases? For example, if some words don't have enough letters, how should they be sorted?
per the current directions "The length of all words provided in the list will be >= n." the Kata was written to be a 7/8 level difficulty, so corner cases such as the one you mention or e.g. multiple words having the same letter at the nth position are not tested
Add some example test cases and I'd say it's good to go.
added
No example test cases :/.
added
Tells me that my answer is wrong, whereas echos that the expected and actual are the same.
In that case, there may be some eroneous whitespace in there?
Thanks, there was :)