Here is the solution to the Missing K problem that only requires us to go through the list once.

The idea is this: if we have the numbers $L = [1, 2, 3, k-1, k+1, ..., n]$, then their sum is $\text{sum(L)} \sum_{i=1}^n i -k = \frac{n(n+1)}{2} - k$.

We can find $k$ by computing $k = \sum_{i=1}^n i - \text{sum(L)}$

In [1]:
def missing_k(L):
    n = len(L) + 1
    s_full = n*(n+1)/2
    s_partial = sum(L)
        
    return s_full - s_partial

A small note: Python has the built-in function function sum() which computes the sum of (amonth other things) lists.

In [2]:
sum([1, 4, 3, 2])
Out[2]:
10

However, nothing is stopping us from writing our own sum function:

In [3]:
def sum(L):
    s = 0
    for e in L:
        s += e
    
    return s