30秒学会 Python 片段 – median

Finds the median of a list of numbers.

Sort the numbers of the list using list.sort() and find the median, which is either the middle element of the list if the list length is odd or the average of the two middle elements if the list length is even.

statistics.median() provides similar functionality to this snippet.

30秒学会 JavaScript 片段 – sortedIndex

Returns the lowest index at which value should be inserted into array in order to maintain its sort order.

Check if the array is sorted in descending order (loosely).
Use Array.prototype.findIndex() to find the appropriate index where the element should be inserted.

30秒学会 JavaScript 片段 – luhnCheck

Implementation of the Luhn Algorithm used to validate a variety of identification numbers, such as credit card numbers, IMEI numbers, National Provider Identifier numbers etc.

Use String.prototype.split(''), Array.prototype.reverse() and Array.prototype.map() in combination with parseInt() to obtain an array of digits.
Use Array.prototype.splice(0,1) to obtain the last digit.
Use Array.prototype.reduce() to implement the Luhn Algorithm.
Return true if sum is divisible by 10, false otherwise.

30秒学会 JavaScript 片段 – averageBy

Returns the average of an array, after mapping each element to a value using the provided function.

Use Array.prototype.map() to map each element to the value returned by fn, Array.prototype.reduce() to add each value to an accumulator, initialized with a value of 0, divide by the length of the array.

30秒学会 C# 片段 – Median

Finds the median of a list of numbers.

Use the params keyword to accept either an array or a variable number of arguments.
Sort the array using Array.sort() and find the median.
Which is either the middle element of the list, if the list length is odd or the average of the two middle elements, if the list length is even.

30秒学会 Dart 片段 – intersection

Returns a list of elements that exist in both lists.

Use Iterable.toSet() to get the unique values in b.
Use Iterable.toSet(), Iterable.where() and Iterable.contains() to keep only the values in a contained in b, Iterable.toList() to return the appropriate result.

30秒学会 JavaScript 片段 – maxBy

Returns the maximum value of an array, after mapping each element to a value using the provided function.

Use Array.prototype.map() to map each element to the value returned by fn, Math.max() to get the maximum value.