import collections


def find_consensus(sequences, threshold=0.7):
    """
    Given aligned sequences, return a string where each position i is the
    consensus value at position i if at least threshold fraction of strings
    have the same character that position i, and "." otherwise.
    """

    if len(sequences) == 0:
        return ""

    num_needed = len(sequences) * threshold
    max_len = max(len(s) for s in sequences)
    sequences = [s.ljust(max_len, ".") for s in sequences]
    result = []
    for i in range(max_len):
        chars = collections.Counter(s[i] for s in sequences)
        (char, count) = chars.most_common()[0]
        if char == "." and count >= num_needed: # most strings have terminated
            break
        result.append(char if count >= num_needed else ".")
    return "".join(result).strip(".")