Description:
You get an array of arrays.
If you sort the arrays by their length, you will see, that their length-values are consecutive.
But one array is missing!
You have to write a method, that return the length of the missing array.
Example:
[[1, 2], [4, 5, 1, 1], [1], [5, 6, 7, 8, 9]] --> 3
If the array of arrays is null/nil or empty, the method should return 0.
When an array in the array is null or empty, the method should return 0 too!
There will always be a missing element and its length will be always between the given arrays.
Have fun coding it and please don't forget to vote and rank this kata! :-)
I have created other katas. Have a look if you like coding and challenges.
Solution:
1. If there is a false value in the array, 0 is returned.
2. Count the number of elements in the list in the array and put them in the 'arr' array.
3. Sort the 'arr' array, find an empty number and return it.
def get_length_of_missing_array(array):
arr = array and all(array) and sorted(list(map(len, array)))
return [v-1 for i, v in enumerate(arr) if arr[0] != v-i][0] if arr else 0
The all() function returns True only if all elements in the array are True.
The any() function returns True if at least one element in the array is True.
'나는 이렇게 학습한다 > Algorithm & SQL' 카테고리의 다른 글
Beginner - Lost Without a Map (0) | 2022.04.07 |
---|---|
Calculate average (0) | 2022.04.07 |
How Green Is My Valley? (0) | 2022.04.04 |
Regexp Basics - is it a letter? (0) | 2022.04.03 |
Delete occurrences of an element if it occurs more than n times (0) | 2022.04.02 |