Third day - Simple challenge - List

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 8)

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

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

wsum

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

for this specific case of x mod 2 on ints, i think this is cheaper

isOdd:{0<signum*0W*x}

nice

0<0W*

seems to be enough for positives

and

0<0W*abs@

for handling negatives too

Agreed…

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

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

Agreed…

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

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




<9132980E-6FD7-4274-A5B6-E74933E55B61@gmail.com>
<cah-1qk1dwoi0rh2rbwer9hmdqxrsk7tihr1fnqdcawv2p3_jrg>
To: “[kdb+] [kdb+]”
In-Reply-To: <cah-1qk1dwoi0rh2rbwer9hmdqxrsk7tihr1fnqdcawv2p3_jrg>
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}
</cah-1qk1dwoi0rh2rbwer9hmdqxrsk7tihr1fnqdcawv2p3_jrg></cah-1qk1dwoi0rh2rbwer9hmdqxrsk7tihr1fnqdcawv2p3_jrg>

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

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

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<signum0Wx} 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.)

:)

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

mod should ideally be a primitive.