The Trim algorithm, shown below, sorts all peptides in Leaderboard according to their scores, resulting in a sorted Leaderboard. Trim then retains the top $$N$$ scoring peptides including ties, and removes all other peptides from Leaderboard.

Trim(Leaderboard, N, Spectrum, AminoAcid, AminoAcidMass)
    for j ← 1 to |Leaderboard|
        Peptidej-th peptide in Leaderboard
        LinearScores(j) ← ScoreL(Peptide, Spectrum)
    sort Leaderboard according to the decreasing order of scores in LinearScores
    sort LinearScores in decreasing order
    for jN + 1 to |Leaderboard|
        if LinearScores(j) < LinearScores(N)
            remove all peptides starting from the j-th peptide from Leaderboard
            return Leaderboard
    return Leaderboard

Assignment

Write a function trim that takes three arguments: i) an integer $$N \in \mathbb{N}_0$$, ii) a linear spectrum $$s$$ and iii) a collection of peptides (a list, tuple or set). The function must return a set containing the top $$N$$ peptides from Leaderboard scored against spectrum $$s$$. Remember to use the linear score (ScoreL).

Example

>>> spectrum = (0, 71, 87, 101, 113, 158, 184, 188, 259, 271, 372)
>>> peptides = ('LAST', 'ALST', 'TLLT', 'TQAS')
>>> trim(2, spectrum, peptides)
{'ALST', 'LAST'}

>>> spectrum = (0, 113, 113, 129, 131, 137, 137, 156, 244, 250, 266, 269, 293, 373, 379, 397, 406, 406, 510, 510, 535, 543, 623, 666, 672, 779, 803, 916)
>>> peptides = ('HLMHREI', 'RLIHHME', 'MELHHRI', 'LMEHIRH', 'RHLHIEM', 'IRHEHML')
>>> trim(2, spectrum, peptides)
{'IRHEHML', 'MELHHRI', 'LMEHIRH'}