Re:Help us collect modern Python NumPy code solutions
by xnorswap on Monday March 28, 2022 @08:41AM
(#30829892)
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.
Re:Help us collect modern Python NumPy code solutions
by kingaillas on Monday March 28, 2022 @09:01AM
(#30830086)
> 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/).
Re:Help us collect modern Python NumPy code solutions
by danbruc on Monday March 28, 2022 @09:17AM
(#30830216)
But 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.
Also the explanation are not very accurate and show a lack of understanding of the .NET collection type architecture and how array, list and enumeration types fit together.
Here I would say IEnumerable<T>.Reverse() paired with String.Join() is the better solution as it is much easier to unterstand than using IEnumerable<T>.Aggregate(). Now nobody has to figure out that (a, b) are the accumulated result and the current list element in this order which is even with the explanation not to obvious - better variable names could really help here. This is really abusing LINQ to express a for loop - create an IEnumerbale<T>, materialize it as a List<T> only to be able to use List<T>.ForEach() which is - for better or worse - not available for IEnumerable<T> and then manipulate a different list created with a second statement in the same line. If you desperately want a one line solution for Fibonacci numbers, maybe consider using a closed-form expression, the recursive definition just doesn't work well in C# and a single line. This will however run into precision issues for big enough numbers while working with integers will work fine until Int64 overflows. Or with using static System.Math even shorter and more readable. The explanation has again issues with the collection types, Enumerable.Range(0, 10) generates 10 values starting from 0, i.e. 0 to 9, but maybe they wanted to say from 0 (inclusive) to 10 (exclusive). Also arrow functions are called lambda functions or lambda expressions in C#.