cancel
Showing results for
Did you mean:

## Third day - Simple challenge - List

New Contributor
Good night!
Third day with q / kdb +.

I took some time after work to dedicate myself to my studies.
I am still at the beginning, but today I decided to study based on a challenge.

Simple challenge:
Given a list, add the even numbers and add the odd numbers.

Notice the (beginner's) reasoning I used:
q) l:(1 4 6 9 3 5 2 7 😎
q) 5 mod 2
1
q) 4 mod 2
0
q)l mod 2
1 0 0 1 1 1 0 1 0
q)where l mod 2
0 3 4 5 7
q)where 0= l mod 2
1 2 6 8
q)l[where l mod 2]
1 9 3 5 7
q)l[where 0= l mod 2]
4 6 2 8
q)sum l[where l mod 2]
25
q)sum l[where 0= l mod 2]
20

Then, I thought as follows:
q)group l mod 2
1| 0 3 4 5 7
0| 1 2 6 8
q) l[group l mod 2]
1| 1 9 3 5 7
0| 4 6 2 8
q) sum each l[group l mod 2]
1| 25
0| 20
q)value sum each l[group l mod 2]
25 20

How to do it more optimally?

Best regards,
Geraldo
12 REPLIES 12
New Contributor II
Not necessarily more optimal, but an introduction to boolean multiplication:

q)sum l*l mod 2
25
q)sum l*not l mod 2
20

New Contributor II
wsum

q)l wsum l mod 2
25f
q)l wsum not l mod 2
20

New Contributor II
for this specific case of x mod 2 on ints, i think this is cheaper
isOdd:{0<signum[x]*0W*x}

New Contributor
nice

0<0W*

seems to be enough for positives

and
0<0W*abs@

for handling negatives too

New Contributor II
Agreed...

There is another way (can be faster if there is a better way to get least significant bit)

isOdd: (last vs[0b]@)'

New Contributor II
Agreed...

There is another way (can be faster if there is a better way to get least significant bit)

isOdd: (last vs[0b]@)'

New Contributor

<9132980E-6FD7-4274-A5B6-E74933E55B61@gmail.com>

To: "[kdb+] [kdb+]"
Message-Id:
X-Mailer: Apple Mail (2.3608.120.23.2.4)

another way which generalizes to other divisors
(and probably easier to understand is)

{x<>2*x div 2}
Valued Contributor
You are trying to work your vector muscle; maybe an iterator as well?

{sum each x where each 1 not\x mod 2}til 9

or taking any of the isOdd alternatives

sum each x where each 1 not\isOdd x

1 f\x is the answer to: How many Zen monks does it take to change a light bulb? (Two: one to change it, and one not to change it.)

Stephen Taylor | Librarian | Kx | +44 7713 400852 | stephen@kx.com

New Contributor
Thank you!

I really liked all the solutions.
For those starting out, like me, this solution is the easiest to understand:

q){sum each x where each 1 not \ x mod 2} (2 3 5 8 9)
17 10

Best Regards
Geraldo

Em quinta-feira, 15 de outubro de 2020 às 16:25:21 UTC-3, ste...@kx.com escreveu:
You are trying to work your vector muscle; maybe an iterator as well?

{sum each x where each 1 not\x mod 2}til 9

or taking any of the isOdd alternatives

sum each x where each 1 not\isOdd x

1 f\x is the answer to: How many Zen monks does it take to change a light bulb? (Two: one to change it, and one not to change it.)

Stephen Taylor | Librarian | Kx | +44 7713 400852 | ste...@kx.com

New Contributor II
mod should ideally be a primitive.

New Contributor
Haha .. multiplying infinities.

Similarly to Ajay's I tried

q)\t r1:{x mod 2} til 1000000
38
q)r1
0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0..
q)\t r2:{last 2 vs x} til 1000000
523
q)r1~r2
1b

It's interesting that you run into these omissions (like no fast binary logical operators) during these experiments but rarely during actual work.

One other idea:
q)\t r1:{(~':)floor x%2} til 1000000
28
q)\t r2:{0<signum[x]*0W*x} til 1000000
36
q)r1~r2
1b

>> 1 f\x is the answer to: How many Zen monks does it take to change a light bulb? (Two: one to change it, and one not to change it.)
🙂

New Contributor
Guys, good afternoon!

I'm speechless.
Thank you all so much for the tips and patience with my simple questions.

Thank you very much for the solutions presented.
I just got home and saw the amount of help I received.
It's been a great learning experience for me.

I will take each solution and analyze each detail.

Terry Lynch, Ajay Rathore, Charles Skelton, Attila and Stephen Taylor. Thank you!

Best Regards.
Geraldo.

Em quinta-feira, 15 de outubro de 2020 às 04:48:28 UTC-3, TerryLynch escreveu:
Not necessarily more optimal, but an introduction to boolean multiplication:

q)sum l*l mod 2
25
q)sum l*not l mod 2
20