- Aggregated Pivot Table
- Bit Shifting
- Bitwise XOR
- Calculate Tick Size
- Collapsing Dictionaries
- Graphs - Breadth First Search
- Graphs - Depth First Search
- Graphs - Detecting Cycles
- Greatest Common Divisor
- Identity Matrix
- Least Common Multiple
- Log Parser
- Nanosleep
- One Hot Encoding
- Parent Child ID Mapping
- Slippage
- Split Training and Testing Sets
- Stratified Sampling
- Symbol Column Update
- Table Indexing
- Word Count
Shuffle
Question: Define a function 'shuffle' that randomly reorders the items of the given list or table.
Example
q)shuffle `a`b`c`d`e`f`g
`b`e`d`a`g`c`f
q)show t:([]s:`AAPL`MSFT`IBM`TSLA;p:100 200 300 400)
s p
--------
AAPL 100
MSFT 200
IBM 300
TSLA 400
q)shuffle t
s p
--------
AAPL 100
TSLA 400
IBM 300
MSFT 200
Solution
######## solution.q ########
/ Before V3.3
shuffle:{neg[count x]?x}
/ V3.3+
if[.z.K >= 3.3;shuffle:0N?];
Explanation: For kdb+ versions 3.3 and above, the '?' operator with '0N' as the left hand operand will natively permute a list. For versions below 3.3, use '?' to randomly deal items from the list. The negative left operand will deal items without duplication, and we deal however many items there are in the list.