Weekly Quest #8: Mess with a list
Written by: Francisco Tórtola

Weekly Quest #8: Mess with a list

This article is part of the Weekly Quest series, where each week we present an unique programming challenge designed specifically for the Q language. There are two challenge categories, the shortest code and the fastest execution time, and this week we start a new category, the first submitted valid solution. The chosen winners will have their solutions featured in the following week's Quest. The tests will be executed using Kdb+ 4.1. You can use k operators, but the solution must be in Q, k prompt is not allowed.

The response must be in a comment to this article, not a reply to another comment, it should be an assignable function i.e.

f:{x+1}        

In case two functions have the same performance or length, the winner will be the first one to publish it. You can publish as many as you want, so don't be afraid of giving it a second thought!


Winners of the previous Quest

The winners of the previous Quest are:

- In the category of the shortest code is James Betz with the following function:

f:{max 3(flip(|:)@)\not(=':)2#/x,1b}        

- In the category of the fastest execution is David, KL Hui with the following function:

f:{@[x#(,)@[x#0;i;:;1];i:(0;x-1);:;2#(,)x#1]}        

If you want to try your own, you can look at the last Cue it can help you.


This Week's Quest

The other day, while playing around with lists as usual, I accidentally mixed up all my lists. Now, I have elements of various types in one list, and I'm clueless about how many of each type I have. Can you help me identify the count of each element type?

Your quest: Develop a function that takes a list as input and returns another list indicating the count of each element type, presented in the order of their appearance.

Input:

- list: A list containing elements of multiple primitive data types.

Output:

- type list: A list of integers indicating the count of each type of element.

Example:

Input:

- list:

(`a;2;2020.08.23;3.1;`b;4)        

Output:

- type list: 2 2 1 1

Explanation:

The first type encountered is a symbol with `a, and there are 2 symbols. The next type is a long, with two occurrences. Then, there's a date with one occurrence, followed by a float.

Limits:

  • The list won't be empty.
  • The elements in the list won't be lists.
  • Each element's type is standard; for instance, 2 is not considered an integer but a long.


Write your Q function as a reply to this article.

q)f (`a;2;2020.08.23;3.1;`b;4)
2 2 1 1        

Let your Q expertise shine, and may the most elegant solution prevail!

Happy qoding!


Connect with us on LinkedIn or via email (mailto:info@hablapps.com), and explore our blog for deeper insights into our specialized technologies.

f:{(#:')(.)(=)(@:')x}

Like
Reply

k)f:#:'.=@:'

Mark Street

All Things Technology at Tradefeedr

8mo

Haven't come up with an alternative to the obvious f:{value count each group type each x} ... but leaning on K operators can get the character count lower: f:(.)(#:)@'(=)(@:)@' [edit] Found an alternative that is slower, uses more characters, but has lower memory utilisation: {sum t=\:distinct t:type@'x}

Oscar N.

Software Developer en Habla Computing

8mo

My solution looks like this: f:value count each group type each :: There are most definetly better and/or faster implementations, but I wanted to stick to q :)

To view or add a comment, sign in

More articles by Habla computing

Insights from the community

Others also viewed

Explore topics