KX Community

Find answers, ask questions, and connect with our KX Community around the world.
KX Community Guidelines

Home Forums kdb+ Challenge 3 The Runner-Up

  • Challenge 3 The Runner-Up

    Posted by megan_mcp on February 27, 2023 at 12:00 am

    Thanks again to everyone who takes the time to interact / comment on these challenges, it really does benefit my knowledge of the q language!

    My next challenge is a simpler one, but I know theres lots of different ways to solve it.

    Given the participants’ score sheet for your University Sports Day, you are required to find the runner-up score. You are given n scores. Store them in a list and find the score of the runner-up.

    Here’s my approach:

     

    q)list1: (4;7;9;7;2;8;9) 
    q)list1: asc list1 
    q)list1 `s#
    2 4 7 7 8 9 9 
    q)list2:list1 except max list1 
    q)runner_up:max list2 
    q)runner_up 
    8

     

    Also a solution in python:

    n = 5

    arr = [2,3,6,6,5]

    outmax = min(arr)

    for i in range(len(arr)): if outmax < arr[i] and arr[i] != max(arr): outmax = arr[i]

    print(outmax)

     

     

     

    megan_mcp replied 2 months, 2 weeks ago 3 Members · 4 Replies
  • 4 Replies
  • mnolan

    Member
    January 3, 2023 at 12:00 am

    Indexing into the descending sorted list of distinct scores works pretty well ok

     

    q)f:{desc[distinct x]1}
    q)l:4 7 9 7 2 8 9
    q)f[l]
    8

     

    EDIT: type and scale can matter here when looking for the best option. A version of the original suggestion outperforms mine in a lot of cases

    q)f1:{max x except max x}  // original suggested solution
    q)f2:{desc[distinct x]1}

    q)l:4 7 9 7 2 8 9
    q)t:10000 f1 l
    9
    q)t:10000 f2 l
    16

    q)longs:100000?100  //scale up
    q)t:100 f1 longs
    38
    q)t:100 f2 longs  //lots of duplicates gives the “distinct” solution an advantage
    19

    q)floats:100000?100f  // change type to float
    q)t:100 f1 floats
    392
    q)t:100 f2 floats  //advantage disappears when there are few duplicate entries
    707

     

  • megan_mcp

    Administrator
    February 3, 2023 at 12:00 am

    does this take the second distinct number in the list? If so, thanks! I really like that approach

  • SJT

    Member
    March 19, 2024 at 10:55 am

     

    q)list1:4 7 9 7 2 8 9 
    q)ru:@[;1] desc distinct:: /runner-up 
    q)ru list1 
    8

     

    Using general list notation (items separated by semicolons and embraced by parentheses) suggests a list is general; better to write a vector as a vector literal.

    Composition ru is a sequence of three unaries. In evaluation order: select the distinct items; sort descending; select second item.

  • SJT

    Member
    March 19, 2024 at 10:55 am

    Nicely timed question! Im just up to the chapter on composition in the book Im writing, Vector Programming in Q, and I need to settle a few questions about it myself. For example, is there ever a reason to prefer Compose to a train suffixed by @ or ::?

    Nick Psaris commented recently that he favours :: because it composes directly rather than forming a projection. (I doubt the evaluation overhead is significant, but I get the point.)

Log in to reply.