12
\$\begingroup\$

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 digit 4 (42, -942, 8374). So we start with a 3.
  • 10: It's positive, so we look forward. There are two numbers containing either the digit 1 and/or 0 (-10, -200). So the second output is 2.
  • 42: Again positive, so forward. There are four numbers containing either the digit 4 and/or 2 (-942, 8374, 728, -200). So the third output is 4.
  • -10: This time it's negative, so we look backwards. There is only one number containing the digit 1 and/or 0 (we ignore the minus sign) (10). So the fourth output is 1.
  • 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 one 3 in the other numbers (381), six 8's in the other numbers (-82, -8, 381, 228, 28, -28), and six 2's in the other numbers (-82, 228, 28, -28, 2). So we start with a 13.
  • -82: It's negative, so backwards. There is one 3 in the other number (382), and one 8 in the other number (382). So the second output is 2.
  • ...
  • 228: It's positive, so forward. There are three 2's in the other numbers (28, -28, -2), and another three 2's, and two 8's in the other numbers (28, -28). So this output is 8.
  • 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 in 8 (3+3+2) instead of 5 (3+2).

General rules:

  • This is , 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 ]
\$\endgroup\$
0

8 Answers 8

5
\$\begingroup\$

Python 2, 149 148 121 116 111 107 bytes

lambda l:[sum([any,sum][len(l)%2](map(`n`.count,`abs(v)`))for n in l[:i:2*(v<0)-1])for i,v in enumerate(l)]

Try it online!

\$\endgroup\$
4
\$\begingroup\$

Java (JDK 10), 204 bytes

a->{int l=a.length,r[]=new int[l],i=0,j,x,y,b,s,t=10;for(;i<l;i++)for(j=i+(s=a[i]>0?1:-1);0<=j&j<l;j+=s)for(b=0,x=a[i];x!=0;x/=t)for(y=a[j];b<1&y!=0;y/=t)if(x%t==-y%t|x%t==y%t){r[i]++;b+=1-l%2;}return r;}

Try it online!

Credits

\$\endgroup\$
10
  • \$\begingroup\$ [1,11,-1] should return [3,2,3]. It's an odd list, so all digits count. First 1: Look forward, three 1s in total: 11,-1. Second 11: Look forward for each digit: one 1 + one 1. Third -1: Look backward, three 1s in total: -1,11. (With odd lists you should look at each digit, even the same. I'll clarify this in the challenge, but the odd example with number 228 clarifies this a bit.) \$\endgroup\$ Apr 11, 2018 at 13:49
  • \$\begingroup\$ @KevinCruijssen Should be fixed now. \$\endgroup\$ Apr 11, 2018 at 14:06
  • \$\begingroup\$ It indeed does. I was already afraid the initial explanation might have been a bit too unclear when I posted it.. Now I'll see if I can golf anything of your answer. ;) \$\endgroup\$ Apr 11, 2018 at 14:16
  • 1
    \$\begingroup\$ I don't have a lot of time either, but one thing you can golf is adding a new variable ,t and changing i+(a[i]>0?1:-1) to i+(t=a[i]>0?1:-1), and then simply use j+=t instead of j+=a[i]>0?1:-1. \$\endgroup\$ Apr 11, 2018 at 14:24
  • 1
    \$\begingroup\$ You can save 2 if you declare for example t=10 and replace all those 10 for t, although it will be less understandable \$\endgroup\$ Apr 12, 2018 at 8:46
3
\$\begingroup\$

05AB1E, 30 bytes

vy0›iNƒ¦}ëN£}€Sþyδ¢IgÈiĀ€Ù}OOˆ

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ δ¢, never seen double-vector used well, nice one. \$\endgroup\$ Apr 11, 2018 at 15:53
3
\$\begingroup\$

Perl 6, 100 85 bytes

{.kv.map:{sum map (?*,+*)[$_%2],.[grep (*-$^i)*$^v>0,^$_].map:{.comb⊍$v.abs.comb}}}

Try it online!

Uses the baggy multiplication operator ⊍.

\$\endgroup\$
0
2
\$\begingroup\$

Perl 5 -n, 92 bytes

($t=$&>0?$':$`)&say$q=y/ //%2?@a=$t=~/\b\d*[$&]+\d*\b/g:map$t=~/$_/g,$&=~/\d/g while/-?\d+/g

Try it online!

\$\endgroup\$
1
\$\begingroup\$

JavaScript (Node.js),164,158,140 139 bytes

a=>a.map((x,i)=>a.slice(x<0?0:i+1,x<0?i:l).map(b=>c+=[...b+""].map(X=>s+=X>=0&&(x+"").split(X).length-1,s=0)&&l%2?s:+!!s,c=0)|c,l=a.length)

Try it online!

\$\endgroup\$
2
  • \$\begingroup\$ This looks like JS6, not anything Node-y. \$\endgroup\$ Apr 11, 2018 at 16:41
  • \$\begingroup\$ this is tio generated string. it has either Node Babel or SpiderMonkey JS. it still works on node so its fine \$\endgroup\$ Apr 11, 2018 at 16:47
1
\$\begingroup\$

Ruby, 126 bytes

->l{k=l.map{|i|i.abs.digits};c=-1;k.map{|n|x=k[l[c+=1]<0?0...c:c+1..-1];x.sum{|j|x=n.sum{|d|j.count d};l.size%2<1?x<1?0:1:x}}}

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Jelly, 43 42 bytes

>0ị"ḊÐƤżṖƤƊAṾ€€FċЀ¥e€€³LḂ©¤?"AṾ$€SṀ€S$®?€

Try it online!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.