Digit Occurrences

12

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 ]

Kevin Cruijssen

Posted 2018-04-11T12:10:48.033

Reputation: 67 575

Answers

3

05AB1E, 30 bytes

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

Try it online!

Emigna

Posted 2018-04-11T12:10:48.033

Reputation: 50 798

δ¢, never seen double-vector used well, nice one. – Magic Octopus Urn – 2018-04-11T15:53:51.280

5

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!

TFeld

Posted 2018-04-11T12:10:48.033

Reputation: 19 246

4

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

Olivier Grégoire

Posted 2018-04-11T12:10:48.033

Reputation: 10 647

[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.) – Kevin Cruijssen – 2018-04-11T13:49:12.450

@KevinCruijssen Should be fixed now. – Olivier Grégoire – 2018-04-11T14:06:02.617

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. ;) – Kevin Cruijssen – 2018-04-11T14:16:22.657

@KevinCruijssen It's very likely golfable: I made the strict minimum in it given the complexity of the answer. Plus I have to leave now – Olivier Grégoire – 2018-04-11T14:18:40.817

1I 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. – Kevin Cruijssen – 2018-04-11T14:24:58.240

1You can save 2 if you declare for example t=10 and replace all those 10 for t, although it will be less understandable – Java Gonzar – 2018-04-12T08:46:38.600

@JavaGonzar Thank you! Though I only saved a byte. The goal here is not to be understandable but to make it run in as few bytes as possible, so yeah, any byte is a win even if it's not legible. – Olivier Grégoire – 2018-04-12T08:57:36.480

Forgot you had to add a colon, happy to help anyway :) – Java Gonzar – 2018-04-12T11:20:06.307

@ceilingcat the result isn't correct. – Olivier Grégoire – 2019-11-18T22:30:28.693

Suggest b-=~l%2 instead of b+=1-l%2 – ceilingcat – 2019-11-20T17:04:04.327

3

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 ⊍.

nwellnhof

Posted 2018-04-11T12:10:48.033

Reputation: 10 037

2

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!

Xcali

Posted 2018-04-11T12:10:48.033

Reputation: 7 671

1

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!

DanielIndie

Posted 2018-04-11T12:10:48.033

Reputation: 1 220

This looks like JS6, not anything Node-y. – Not that Charles – 2018-04-11T16:41:56.577

this is tio generated string. it has either Node Babel or SpiderMonkey JS. it still works on node so its fine – DanielIndie – 2018-04-11T16:47:52.103

1

Jelly, 43 42 bytes

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

Try it online!

dylnan

Posted 2018-04-11T12:10:48.033

Reputation: 4 993

1

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!

Asone Tuhid

Posted 2018-04-11T12:10:48.033

Reputation: 1 944