Brain teaser: How to generate 7 integers with equal probability using a biased coin that has a pr(head) = p? This is a question I found on Glassdoor: How does one generate 7 integers with equal probability using a coin that has a $\mathbb{Pr}(\text{Head}) = p\in(0,1)$?
Basically, you have a coin that may or may not be fair, and this is the only random-number generating process you have, so come up with random number generator that outputs integers from 1 to 7 where the probability of getting each of these integers is 1/7.
Efficiency of the data-generates process matters.
 A: The question is a bit ambiguous, is it asking "generate a random integer equal or less than 7 with equal probability", or is it asking "generate 7 random integers with equal probability?" - but what is the space of integers?!?
I'll assume it's the former, but the same logic I'm applying can be extended to the latter case too, once that problem is cleared up. 
With a biased coin, you can produce a fair coin by following the following procedure: https://en.wikipedia.org/wiki/Fair_coin#Fair_results_from_a_biased_coin
A number 7 or less can be written in binary as three {0,1} digits. So all one needs to do is follow the above procedure three times, and convert the binary number produced back to decimal. 
A: Flip the coin twice. If it lands HH or TT, ignore it and flip it twice again. 
Now, the coin has equal probability of coming up HT or TH. If it comes up HT, call this H1. If it comes up TH, call this T1.
Keep obtaining H1 or T1 until you have three in a row. These three results give you a number based on the table below:
H1 H1 H1 -> 1
H1 H1 T1 -> 2
H1 T1 H1 -> 3
H1 T1 T1 -> 4
T1 H1 H1 -> 5
T1 H1 T1 -> 6
T1 T1 H1 -> 7
T1 T1 T1 -> [Throw out all results so far and repeat]

I argue that this would work perfectly fine, although you would have a lot of wasted throws in the process!
A: Assume that $p \in (0,1)$.
Step 1:. Toss the coin 5 times.  
If the outcome is
$(H, H, H, T, T)$, return $1$ and stop.
$(H, H, T, T, H)$, return $2$ and stop.
$(H, T, T, H, H)$, return $3$ and stop.
$(T, T, H, H, H)$, return $4$ and stop.
$(T, H, H, H, T)$, return $5$ and stop.
$(H, H, T, H, T)$, return $6$ and stop.
$(H, T, H, T, H)$, return $7$ and stop.
Step 2:. If the outcome is none of the above, repeat Step 1.
Note that regardless of the value of $p \in (0,1)$, each of the seven outcomes listed above has probability $q = p^3(1-p)^2$, and the expected number of coin tosses is $\displaystyle \frac{5}{7q}$.  The tosser doesn't need to know the value of $p$ (except that $p\neq 0$ and $p\neq 1$); it is guaranteed that the seven integers are equally likely to be returned by the experiment when it terminates (and it is guaranteed to end with probability $1$).
A: As mentioned in earlier comments, this puzzle relates to John von Neumann's 1951 paper "Various Techniques Used in Connection With Random Digits" published in the research journal of the National Bureau of Standards:

There is a wider literature about such problems that goes under the name of Bernoulli factory problems, that is, given a coin with tail probability $p$, how to simulate a coin with tail probability $f(p)$. If feasible, since some functions $f$ cannot be used as for instance $f(p)=\min\{1,2p\}$. Nacu and Peres (2005) study fast algorithms for solving [solvable] Bernoulli factory problems where fast means exponential decay of the tail of the distribution of the number $N$ of trials.
A: A solution that never wastes flips, which helps a lot for very-biased coins.
The disadvantage of this algorithm (as written, at least) is that it's using arbitrary-precision arithmetic. Practically, you probably want to use this until integer overflow, and only then throw it away and start over.
Also, you need to know what the bias is ... which you might not, say, if it is temperature-dependent like most physical phenomena.

Assuming the chance of heads is, say, 30%.


*

*Start with the range [1, 8).

*Flip your coin. If heads, use the left 30%, so your new range is [1, 3.1). Else, use the right 70%, so your new range is [3.1, 8).

*Repeat until the entire range has the same integer part.



Full code:
#!/usr/bin/env python3
from fractions import Fraction
from collections import Counter
from random import randrange


BIAS = Fraction(3, 10)
STAT_COUNT = 100000


calls = 0
def biased_rand():
    global calls
    calls += 1
    return randrange(BIAS.denominator) < BIAS.numerator


def can_generate_multiple(start, stop):
    if stop.denominator == 1:
        # half-open range
        stop = stop.numerator - 1
    else:
        stop = int(stop)
    start = int(start)
    return start != stop


def unbiased_rand(start, stop):
    if start < 0:
        # negative numbers round wrong
        return start + unbiased_rand(0, stop - start)
    assert isinstance(start, int) and start >= 0
    assert isinstance(stop, int) and stop >= start
    start = Fraction(start)
    stop = Fraction(stop)
    while can_generate_multiple(start, stop):
        if biased_rand():
            old_diff = stop - start
            diff = old_diff * BIAS
            stop = start + diff
        else:
            old_diff = stop - start
            diff = old_diff * (1 - BIAS)
            start = stop - diff
    return int(start)


def stats(f, *args, **kwargs):
    c = Counter()
    for _ in range(STAT_COUNT):
        c[f(*args, **kwargs)] += 1

    print('stats for %s:' % f.__qualname__)
    for k, v in sorted(c.items()):
        percent = v * 100 / STAT_COUNT
        print('  %s: %f%%' % (k, percent))


def main():
    #stats(biased_rand)
    stats(unbiased_rand, 1, 7+1)
    print('used %f calls at bias %s' % (calls/STAT_COUNT, BIAS))


if __name__ == '__main__':
    main()

A: Divide a box into seven equal-area regions, each labeled with an integer. Throw the coin into the box in such a way that it has equal probability of landing in each region.
This is only half in jest -- it's essentially the same procedure as estimating $\pi$ using a physical Monte Carlo procedure, like dropping rice grains onto paper with a circle drawn on it.
This is one of the only answer that works for the case of $p = 1$ or $p=0$.
A: This also only works for $p \neq 1$ and $p \neq 0$.
We first turn the (possibly) unfair coin into a fair coin using the process from NcAdams answer:

Flip the coin twice. If it lands HH or TT, ignore it and flip it twice again. 
Now, the coin has equal probability of coming up HT or TH. If it comes up HT, call this H1. If it comes up TH, call this T1.

Now we use the fair coin to generate a real number between $0$ and $1$ in binary. Let H1$= 1$ and T1 $= 0$. Start with the string 0., flip the coin and append the resulting digit to the at the end of the string. Repeat with the new string. For example, the sequence H1 H1 T1 would give you the number $0.110$.
$1/7$ is a repeating decimal, and with the right-hand side being in base 2 we have that:
$1/7 = 0.001 001 001 ...$
$2/7 = 0.010 010 010 ...$
$3/7 = 0.011 011 011 ...$
$4/7 = 0.100 100 100 ...$
$5/7 = 0.101 101 101 ...$
$6/7 = 0.110 110 110 ...$
Keep flipping the fair coin to generate the decimal digits until the digits of your sequence does not match one of the above sequences, then chose the number $n$ such that your generated number is less than $n/7$ and greater than $(n-1)/7$. Since each generated number is equally likely we have chosen a number between $1$ and $7$ with equal probability.
A: Inspired by AdamO's answer, here is a Python solution that avoids bias:
def roll(p, n):
    remaining = range(1,n+1)
    flips = 0
    while len(remaining) > 1:
        round_winners = [c for c in remaining if random.choices(['H','T'], [p, 1.0-p]) == ['H']]
        flips += len(remaining)
        if len(round_winners) > 0:
            remaining = round_winners
        p = 1.0 - p
    return remaining[0], flips

There are two main changes here: The main one is that if all the number are discarded in a round, repeat the round. Also I flip the choice of whether heads or tails means discard every time. This reduces the number of flips needed in cases where p is close to 0 or 1 by ~70% when p=0.999
A: It appears we are allowed to change the mapping of the outcome of each flip, every time we flip. So, using for convenience the first seven positive integers, we give the following orders:
1st Flip,  map $H \to 1$
2nd Flip,  map $H \to 2$
...
7th flip, map $H \to 7$
8th flip, map $H \to 1$
etc
Repeat, always in batches of 7 flips. Map the $T$ outcomes to nothing.
SOME REMARKS ON EFFICIENCY
Our RNG, index it by $AP$, will generate zero useful outcomes in one 7-flip batch if we get $T$ in all 7 flips. So
$$P_{AP}(\text{no integers generated}) = (1-p)^7$$
As we run $N_b$ 7-flip batches, the total number of useless flips will tend to 
$$\text{Count}_{AP}(\text{useless flips}) \to 7\cdot N_b(1-p)^7$$
Consider now  the RNG of @DilipSarwate. There, we use a binomial $B(p,n=5)$ and 5-flip batches. The seven outcomes that generate an integer each has probability of occuring $p^3(1-p)^2$, so, in a 5-flip batch
$$P_{DS}(\text{no integers generated}) = 1-7\cdot p^3(1-p)^2$$
The count of useless flips will here tend to 
$$\text{Count}_{DS}(\text{useless flips}) \to 5\cdot N_b\cdot [1-7\cdot p^3(1-p)^2]$$
For the $AP$ RNG to tend to produce less useless flips, it must be the case that
$$ \text{Count}_{AP}(\text{useless flips}) < \text{Count}_{DS}(\text{useless flips})$$
$$\implies  7\cdot N_b(1-p)^7 < 5\cdot N_b\cdot [1-7\cdot p^3(1-p)^2]$$
$$\implies  7\cdot (1-p)^7 < 5\cdot [1-7\cdot p^3(1-p)^2]$$
Numerical examination shows that if $p>0.0467$, then the $AP$ RNG generates less useless flips.  
We also find that the number of useless flips is monotonically decreasing in $p$ for the $AP$ RNG, while for the $DS$ RNG it has a minimum at around $p\approx 0.5967$ and then increases again, while in general it stays high. The ratio
$$\frac{\text{Count}_{AP}(\text{useless flips})}{\text{Count}_{DS}(\text{useless flips})}$$
declines pretty quickly. For example it is equal to $0.67$ for$p=0.1$, equal to $0.3$ for $p=0.2$, equal to $0.127$ for $p=0.4$.
