Live data from Hacker News

Help us collect modern Python NumPy code solutions

github.com

1–10 of 41 posts

Re: Help us collect modern Python NumPy code solutions

#2
For almost all issues, you can simply search for site:stackoverflow.com + the title of the issue and get a larger variety of solutions ranked by upvotes, so this repository seems strictly worse.

Is there any advantage that this repository brings over just searching for the question on StackOverflow?

Re: Help us collect modern Python NumPy code solutions

#3

For almost all issues, you can simply search for site:stackoverflow.com + the title of the issue and get a larger variety of solutions ranked by upvotes, so this repository seems strictly worse. Is there any advantage that this repository brings over just searching for the question on StackOverflow?

I didn’t review all of the code, but the examples I saw seemed to have some basic explanations with them. Additionally, they have samples for a lot of different technologies. A curated repo seems much nicer than scoring several StackOverflow answers. Depends on the code quality, of course.

Re: Help us collect modern Python NumPy code solutions

#6
post #3

For almost all issues, you can simply search for site:stackoverflow.com + the title of the issue and get a larger variety of solutions ranked by upvotes, so this repository seems strictly worse. Is there any advantage that this repository brings over just searching for the question on StackOverflow?

I didn’t review all of the code, but the examples I saw seemed to have some basic explanations with them. Additionally, they have samples for a lot of different technologies. A curated repo seems much nicer than scoring several StackOverflow answers. Depends on the code quality, of course.

I looked at the C# ones - the code is not great and neither are the explanations, both show a lack of experience in C# and .NET.

  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.Reverse() paired with String.Join() is the better solution as it is much easier to unterstand than using IEnumerable.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 Fib = new List(); Enumerable.Range(0,10).ToList().ForEach(n => (Fib).Add(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 
This is really abusing LINQ to express a for loop - create an IEnumerbale, materialize it as a List only to be able to use List.ForEach() which is - for better or worse - not available for IEnumerable 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#.

Re: Help us collect modern Python NumPy code solutions

#7
Just had a quick look at a few of the python examples:

- it might be neat, but advocating for `eval(input())` [0] might not be the safest solution for this problem, especially without explaining the dangers of `eval` (assuming this site is partially aimed at beginners?)

- for an article titled 'how to terminate a script', the suggested method (`quit()`) [1] is specifically described in the official python docs [2] as code that "should not be used in programs".

[0]: https://onelinerhub.com/python/calculator [1]: https://onelinerhub.com/python/how-to-terminate-script [2]: https://docs.python.org/3/library/constants.html#constants-a...

Re: Help us collect modern Python NumPy code solutions

#8

For almost all issues, you can simply search for site:stackoverflow.com + the title of the issue and get a larger variety of solutions ranked by upvotes, so this repository seems strictly worse. Is there any advantage that this repository brings over just searching for the question on StackOverflow?

The README explains that they want a single curated answer, not a list of 15 answers with 25 comments. This does seem worthwhile, at least for some types of questions.

Re: Help us collect modern Python NumPy code solutions

#9
post #4

#write a function that zips numpy arrays import numpy as np def zip_arrays(arrays): return np.array([np.concatenate(arr) for arr in zip(*arrays)])

>>> arrays = [np.arange(1000)]*10

>>> zip_arrays(arrays)

Traceback (most recent call last):

  File "", line 1, in 

  File "", line 2, in zip_arrays2

  File "", line 2, in 

  File "", line 180, in concatenate
ValueError: zero-dimensional arrays cannot be concatenated

Try this:

def zip_arrays(arrays):

    return np.concatenate([array[:l,...,None] for l in [min(array.shape[0] for array in arrays)] for array in arrays], axis=-1)
Or if we are trying to be more tidy instead of a one liner

def zip_arrays(arrays):

    l = min(a.shape[0] for a in arrays)

    arrays = [a[:l, ..., None] for a in arrays]

    return np.concatenate(arrays)

Re: Help us collect modern Python NumPy code solutions

#10
I learned J before NumPy and I'm glad I did, because it's much easier to see and learn the algorithms when they're short symbols.

Compare

    np.sqrt(sum(np.square(x)))
versus

    %: +/ *: x
So if you can translate one of the array languages to NumPy, you can tap into the wealth of "idioms" collected over the years.

For example: J Phrases[0] (organized by category), BQNcrate[1] and APLcart[2] (searchable).

[0]: https://www.jsoftware.com/help/phrases/sums_means.htm [1]: https://mlochbaum.github.io/bqncrate [2]: https://aplcart.info/

Post reply on HN