Input:
A list of integers (which will never contain a zero)
Output:
A list of the same size with counts based on the following:
- If the current item is negative: Look at all items before this item, and count how many times the digits occurred in those other numbers
- If the current item is positive instead: Look at all items after this item, and count how many times the digit occurred in those other numbers
There is one twist: If the size of the list is even we only count every number once (even if it matches multiple digits), and if the size is odd we count every digit of the numbers for each digit of the current item (duplicated digits are counted multiple times).
Let's give some examples to clarify this a bit:
Example with even list:
Input: [4, 10, 42, -10, -942, 8374, 728, -200]
Output: [3, 2, 4, 1, 2, 1, 1, 5 ]
Size of the list is even, so we only count each number once.
4
: It's positive, so we look forward. There are three numbers containing the digit4
(42
,-942
,8374
). So we start with a3
.10
: It's positive, so we look forward. There are two numbers containing either the digit1
and/or0
(-10
,-200
). So the second output is2
.42
: Again positive, so forward. There are four numbers containing either the digit4
and/or2
(-942
,8374
,728
,-200
). So the third output is4
.-10
: This time it's negative, so we look backwards. There is only one number containing the digit1
and/or0
(we ignore the minus sign) (10
). So the fourth output is1
.- etc.
Example with odd list:
Input: [382, -82, -8, 381, 228, 28, 100, -28, -2]
Output: [13, 2, 2, 4, 8, 3, 0, 11, 6 ]
Size of the list is odd, so we count every digit.
382
: It's positive, so we look forward. There is one3
in the other numbers (381
), six8
's in the other numbers (-82, -8, 381, 228, 28, -28
), and six2
's in the other numbers (-82, 228, 28, -28, 2
). So we start with a13
.-82
: It's negative, so backwards. There is one3
in the other number (382
), and one8
in the other number (382
). So the second output is2
.- ...
228
: It's positive, so forward. There are three2
's in the other numbers (28
,-28
,-2
), and another three2
's, and two8
's in the other numbers (28
,-28
). So this output is8
.- etc.
Challenge rules:
- You can assume the input will never contain
0
as item, since it's neither positive nor negative. - You can assume the input-list will always contain at least two items.
- I/O is flexible. Input/output can be array/list of integers, delimited string, digit/character-matrix, etc.
- If the first number in the list is a negative number, or the last number in the list is a positive number, it will be 0 in the resulting list.
- With odd lists, numbers containing the same digit multiple times are counted multiple times, like the
228
in the odd example above resulting in8
(3+3+2) instead of5
(3+2).
General rules:
- This is code-golf, so shortest answer in bytes wins.
Don't let code-golf languages discourage you from posting answers with non-codegolfing languages. Try to come up with an as short as possible answer for 'any' programming language. - Standard rules apply for your answer, so you are allowed to use STDIN/STDOUT, functions/method with the proper parameters and return-type, full programs. Your call.
- Default Loopholes are forbidden.
- If possible, please add a link with a test for your code.
- Also, please add an explanation if necessary.
Test cases:
Input: [4, 10, 42, -10, -942, 8374, 728, -200]
Output: [3, 2, 4, 1, 2, 1, 1, 5 ]
Input: [382, -82, -8, 381, 228, 28, 100, -28, -2]
Output: [13, 2, 2, 4, 8, 3, 0, 11, 6 ]
Input: [10, -11, 12, -13, 14, -15, 16, -17, 18, -19]
Output: [9, 1, 7, 3, 5, 5, 3, 7, 1, 9 ]
Input: [10, -11, 12, -13, 14, -15, 16, -17, 18, -19, 20]
Output: [11, 2, 8, 4, 5, 6, 3, 8, 1, 10, 0 ]
Input: [88, 492, -938, 2747, 828, 84710, -29, -90, -37791]
Output: [8, 9, 3, 9, 3, 4, 5, 4, 12 ]
Input: [-1, 11, 11, 1]
Output: [0, 2, 1, 0]
Input: [1, 11, 11, -1]
Output: [3, 2, 1, 3 ]
Input: [-1, 11, 1]
Output: [0, 2, 0]
Input: [1, 11, -1]
Output: [3, 2, 3 ]