Turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

- KX Community
- :
- Discussion Forums
- :
- kdb+ and q
- :
- Re: Third day - Simple challenge - List

Options

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Mute
- Printer Friendly Page

Third day - Simple challenge - List

Options

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content

2020.10.14 04:51 PM

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.

Given a list, add the even numbers and add the odd numbers.

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

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

Best regards,

Geraldo

12 REPLIES 12

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content

2020.10.15 12:48 AM

q)sum l*l mod 2

25

q)sum l*not l mod 2

20

25

q)sum l*not l mod 2

20

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content

2020.10.15 12:55 AM

wsum

q)l wsum l mod 2

25f

q)l wsum not l mod 2

20

25f

q)l wsum not l mod 2

20

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content

2020.10.15 02:26 AM

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

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

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content

2020.10.15 03:28 AM

nice

0<0W*

seems to be enough for positives

and

0<0W*abs@

for handling negatives too

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content

2020.10.15 03:30 AM

Agreed...

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

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

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content

2020.10.15 04:46 AM

Agreed...

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

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

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content

2020.10.15 12:22 PM

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

To: "[kdb+] [kdb+]"

In-Reply-To:

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}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content

2020.10.15 12:25 PM

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

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content

2020.10.15 01:34 PM

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 9or taking any of the isOdd alternativessum each x where each 1 not\isOdd x1 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.)

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content

2020.10.15 01:46 PM

mod should ideally be a primitive.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content

2020.10.15 01:28 PM

Haha .. multiplying infinities.

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

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

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

🙂

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content

2020.10.15 01:24 PM

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:

q)sum l*l mod 2

25

q)sum l*not l mod 2

20

Main Office Contacts

**EMEA**

Tel: +44 (0)28 3025 2242

**AMERICAS**

Tel: +1 (212) 447 6700

**APAC**

Tel: +61 (0)2 9236 5700

Useful Information

Popular Links

Follow Us

KX. All Rights Reserved.

KX and kdb+ are registered trademarks of KX Systems, Inc., a subsidiary of FD Technologies plc.