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 Re: Challenge 3 The Runner-Up

  • 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