Repository claims modern code, but uses Python 2 (end of life January 1, 2020) https://github.com/Onelinerhub/onelinerhub/blob/main/python/... Copied from https://wiki.python.org/moin/Powerful%20Python%20One-Liners
Incorrect indentation causing unnecessarily large memory usage https://github.com/Onelinerhub/onelinerhub/blob/main/python/...
Undefined behavior for i = 31 https://github.com/Onelinerhub/onelinerhub/blob/main/c/bits_...
Printing of uninitialized memory https://github.com/Onelinerhub/onelinerhub/blob/main/c/set_v...
Undefined behavior https://github.com/Onelinerhub/onelinerhub/blob/main/c/swap_...
I believe the original intention behind using mode="a" was to append to the output file while reading the input files at the same time. This way, there is no need for an ever-growing string array.
But there are still many other issues like using default platform string encoding instead of detecting it properly or at least using utf-8, checking for ".txt" anywhere in the path instead of at the end, and not closing the input files with a context manager like the output file, which suggests that this code is just pierced together from various sources.
A robust solution would require many more lines.
How to reverse the characters in a string
String.Join("",("1234Simple").Select(c=>c).Reverse());
- String.Join("", - concatenate one or more characters in a single list or array, by using empty "" string literal separator
- .Select(c=>c) - select each and every other character in a string into an array of characters
- .Reverse() - reverse each and every one elements in an array or list*
Calling Select(c => c) does nothing and can be omitted. The use of String.Join() feels wrong, it is just used to concatenate the characters and not to join them with some separator in between. Using the String(Char[]) constructor seems more appropriate. new String("1234Simple".Reverse().ToArray())
But that is still bad code, it will rip apart and reverse grapheme clusters, a proper implementation should use System.Globalization.StringInfo. Not sure if it would still fit into one line.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.
How to reverse the words in a phrase
("This tests that").Split(' ').Aggregate((a,b) => b + " " + a);
- .Split(' ') - splits a string by using a space ' ' separator
- .Aggregate - gather every one other element a, in an element list to a target element b
- (a,b) => b + " " + a - arrow function taking input arguments (a,b) used to concatenate b gathered character argument in the list in reverse with " " space separator to the targeted character argument a
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(). String.Join(" ", "This tests that".Split(' ').Reverse())
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. How to retrieve numeric fibonacci list up to 10
List<long> Fib = new List<long>(); Enumerable.Range(0,10).ToList().ForEach(n => (Fib).Add(n <= 1 ? 1 : Fib[n-2] + Fib[n-1]));
- new List(); - create an empty list of large integers
- Enumerable.Range(0,10) - get an enumerable list populated with integers from 0 to 10
- .ToList() - convert the enumerable list to a standard list
- .ForEach - apply a specific arrow function for each and every one item in the list
- n => (Fib).Add - arrow function used to add an item n to enumerable list Fib based on a condition for every one item in the list
- n <= 1 ? 1 : Fib[n-2] + Fib[n-1] - check if n less or equal to 1 then return 1, if n is larger then return addition of elements [n-2] and [n-1] in Fib list for every n
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. Enumerable.Range(0, 10).Select(n => (Int64)(0.5 + Math.Pow((1 + Math.Sqrt(5)) / 2, n) / Math.Sqrt(5)))
Or with using static System.Math even shorter and more readable. Enumerable.Range(0, 10).Select(n => (Int64)(0.5 + Pow((1 + Sqrt(5)) / 2, n) / Sqrt(5)))
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#.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.
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/).