(This may be Python-specific)
The question states 4 times that the input will be a string, the input/output at run time displays strings, but the input is actually an array and you're expected to leave it as an array.
The only way to discover that you're actually operating on an array is to manually stdout the input value. And once you do that, if you then solve it and return a string, the tests will still fail because you didn't do it in-place.
This isn't just confusing and poorly-written, this is downright incorrect. The question that was asked doesn't line up with the test cases at all.
The only way to discover that you're actually operating on an array is to manually stdout the input value.
Why can't you discover that from the given specification of the parameter? This is what you're told:
class Solution: # @param s, a list of 1 length strings, e.g., s = ['h','e','l','l','o'] # @return nothing def reverseWords(self, s):
if you then solve it and return a string
Why would you do that? You're explicitly told to "return nothing".
@StefanPochmann I must have deleted that comment before reading it. I don't think that changes much about my complaint though -- now the question contradicts both the test cases, and the type signature.
Also, in the question, you aren't told to return nothing. You're told to modify a string, which is impossible to do in-place in Python (strings are immutable). At the end of the question, modifying it in-place without extra space is posed as a bonus question (which is the convention every other question on LeetCode follows -- short addendum questions are posed as optional bonus material).
I can appreciate that the input is probably an array because strings are immutable and this question is really about performing the operation in-place, but if the input is going to be an array then the question should say so, the examples given should depict arrays, and the input/output of the run time should also correctly display it as an array.
@FlorenceMachine It's not a contradiction. Lists of single-character strings are strings. That's not what you'd usually call it in Python, but that's what that parameter specification is for, specializing the general problem text to each language.
And amusingly, you yourself are doing the same thing. Over and over again you're saying "array" even though in Python it's not called "array" but "list", and arrays do exist in Python and are something different. It's somewhat ok, though, since Python's lists are arrays in the more general computer science sense, just like these "strings". But I must wonder why you're not complaining about yourself for that.
@StefanPochmann That's interesting, I don't have experience with lower-level languages so I wasn't aware that strings are just char arrays.
So in Java and Python, even though these languages have string literals like
"abc", these are just objects or interfaces to a char array under the hood? Are there any languages that treat strings differently?
If they're just char arrays under the hood, then why do Python and Java make them immutable? Wouldn't these languages benefit from mutable strings?
Also, if strings are just char arrays, then why does every other question I've seen on LeetCode provide strings instead of char arrays as the input/output? Isn't this confusing/inconsistent?
Also! When I say "string", I've always referred to the string literal (or a string literal stored in a variable). Is there a standard way to distinguish between string literals and char arrays in written english?
@FlorenceMachine Haha, while I did consider asking you whether you think strings don't exist in C just because it doesn't have a data type called "string", I was really not talking about "lower-level" programming languages and their implementations. Instead I was thinking about the rather higher level of computer science / math (Wikipedia, MathWorld). A finite sequence of values from a finite set.
Yes, under the hood Java and Python use "character arrays", at least the standard implementations. See Implementations, apparently that's indeed the typical way.
For example Java (from here):
public final class String ... /** The value is used for character storage. */ private final char value; ....
Immutability has advantages as well. Some maybe useful links:
Why do most other problems use normal Python strings? Because most problems aren't about modifying strings in-place. Not even the first version of this problem! (This here is "Reverse Words in a String II", after all.)
How to distinguish between "strings" and "char arrays" in text? Exactly like that, I'd say.
@StefanPochmann Thanks for the links.
On a side note, when I was in my early teens a long time ago (maybe 15+ years?) I learned one of your methods for blindfold solving the 3x3. And now you're here teaching me about strings. Strange coincidence!
@FlorenceMachine Haha, nice. Must've been a little later, though, I think I started blindcubing in late 2003 and published my first method in 2004. Just took a trip down memory lane, going through some old archives. Good times...
@StefanPochmann 2004 sounds about right, it was a method where you memorized the initial state of the cube as an array of chars (oddly enough) and had one algorithm that swapped them until that memorized string was in the solved state.
I failed the first attempt, succeeded the second, failed the third, succeeded the fourth, then went back to straight 3x3 because blindfolds are hard. I was a sub-20 average solver back when that was considered fast. I wasn't very good blindfolded though, I think my best of the 2 solves was about 32 minutes.
I haven't touched a cube in almost 10 years, I really should buy one.
@FlorenceMachine Ha, array of chars again, yes. If you do get a new cube, you'll probably be amazed how much better they've become. Now they're awesome right out of the box. The top models even have magnets in them for better alignment, though they cost about $40 :-(. But even the $3 speedcubes are really good now.
@StefanPochmann Last time I cubed, you still had to sand down the plastic yourself and apply silicone lubricant and replace the stickers with the PVC (?) kind that wouldn't peel. I've never heard of magnetic cubes before.
Is this a good one?:
Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.