
Challenge 3 The RunnerUp
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 runnerup score. You are given n scores. Store them in a list and find the score of the runnerup.
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)
Log in to reply.