3 ms·
Indeed, their example is terrible, because it's also the kind of 'toy' problem that just doesn't have good real world uses too. How often do you want to revers
by xnorswap 5y ago
Indeed, their example is terrible, because it's also the kind of 'toy' problem that just doesn't have good real world uses too.
How often do you want to reverse a string.
I think your solution is fine and isn't "bad code" at all. Certainly a real explanation warrants talking about the IEnumerable interface.
I'd also consider the simpler (and ever so slightly better performing):
var buffer = "1234simple".ToArray();
Array.Reverse(buffer);
new string(buffer);
edit: The following is dramatically (x10) even better performing:
var buffer = "1234simple".ToCharArray();
Array.Reverse(buffer);
new string(buffer);
But again with the caveat of that not respecting unicode sequences.
But to re-iterate and to add to your criticism, I can't understand the use-case of needing to do this outside of toy problems.
- kingaillas 5y ago> I can't understand the use-case of needing to do this outside of toy problems. Not sure, not my domain, but perhaps bioinformatics has the equivalent of the convolution operation on sequences? EDIT: there is the "reversal" operation. Peeking ahead from the Rosalind problems, I'm not quite up to Reversal Distance (https://rosalind.info/problems/rear/ https://rosalind.info/problems/rear/).
- danbruc 5y agoBut would they represent their data as character strings or would they pack four nucleotides with two bits each into one byte? Or would they be working on the amino acid level or something else all together? And would they really physically reverse potentially very long strings or just use a decreasing index into the original string? I am sure reversing strings has some use cases, maybe even in bioinformatics, but I would agree with the other comment, it seems a pretty niche operation, otherwise there would probably be a String.Reverse() method.