Esempio n. 1
0
def fraud_detect_merge(first_booth_voters, second_booth_voters):
    """This function takes a VoterList from two voting booths and returns a new
    VoterList, the voters who cast a vote in both booths, and an integer, the
    number of VoterName comparisons the function made. Both VoterLists are in
    lexicographical order."""

    fraud_list = VoterList()
    count = 0
    pos1 = 0
    pos2 = 0
    first_len = len(first_booth_voters)
    second_len = len(second_booth_voters)
    while pos1 < first_len and pos2 < second_len:
        first = first_booth_voters[pos1]
        second = second_booth_voters[pos2]
        count += 1
        if first == second:
            fraud_list.append(second)
            pos1 += 1
            pos2 += 1
        else:
            count += 1
            if first < second:
                pos1 += 1
            else:
                pos2 += 1
    return fraud_list, count
def fraud_detect_bin(first_booth_voters, second_booth_voters):
    """This function takes a VoterList from two voting booths and returns a new
    VoterList, the voters who cast a vote in both booths, and an integer, the
    number of VoterName comparisons the function made. The second VoterList is
    in lexicographical order."""

    fraud_list = VoterList()
    count = 0
    for voter in first_booth_voters:
        lowerBound = 0
        upperBound = len(second_booth_voters)
        mid = (lowerBound + upperBound) // 2
        while lowerBound < upperBound:

            count += 1
            if second_booth_voters[mid] < voter:
                lowerBound = mid + 1
            else:
                upperBound = mid
            mid = (lowerBound + upperBound) // 2

        if mid < len(second_booth_voters):
            count += 1
            if second_booth_voters[mid] == voter:
                fraud_list.append(voter)
    return fraud_list, count
Esempio n. 3
0
def fraud_detect_seq(first_booth_voters, second_booth_voters):
    """This function takes a VoterList from two voting booths and returns a new
    VoterList, the voters who cast a vote in both booths, and an integer, the
    number of VoterName comparisons the function made. The VoterLists are
    un-ordered."""

    fraud_list = VoterList()
    count = 0
    for voter1 in first_booth_voters:
        for voter2 in second_booth_voters:
            count += 1
            if voter1 == voter2:
                fraud_list.append(voter1)
                break
    return fraud_list, count
Esempio n. 4
0
def fraud_detect_hash(first_booth_voters, second_booth_voters):
    """This function takes a VoterList from two voting booths and returns a new
    VoterList, the voters who cast a vote in both booths, and an integer, the
    number of VoterName comparisons the function made. This function should use
    a VoterHashTable initialised to an appropreate size to represent a hash
    table, use the defualt hash function for VoterNames as a hash function and
    use chaining to resolve collisions."""

    fraud_list = VoterList()
    count = 0
    table_cap = len(second_booth_voters) * 2
    voter_hash = VoterHashTable(table_cap)
    for voter in first_booth_voters:
        voter_index = hash(voter) % table_cap
        voter_hash[voter_index].append(voter)
    for voters in second_booth_voters:
        index = hash(voters) % table_cap
        for voter in voter_hash[index]:
            count += 1
            if voters == voter:
                fraud_list.append(voters)
                break
    return fraud_list, count
 def internal_comparisons_test(self, test_file_location, fraud_detect):
     first_booth_voters, second_booth_voters, fraud_voters = read_test_data(
         test_file_location)
     fraud_found, comparisons = fraud_detect(first_booth_voters,
                                             second_booth_voters)
     self.assertEqual(comparisons, VoterList.get_comparisons())
 def setUp(self):
     VoterList.reset_comparisons()
     VoterHashTable.reset_hashes()
     VoterHashTable.reset_memory_used()
 def setUp(self):
     """This runs before each test case"""
     VoterList.reset_comparisons()
def read_test_data(filename):
    """Reads a test data file and returns a triple containg first booth voters,
    second booth voters and voters comitting fraud by voting in both booths."""
    first_booth_voters = VoterList()
    second_booth_voters = VoterList()
    fraud_voters = VoterList()
    with open(filename) as test_data_file:
        #Read first booth voter names
        current_line = _get_next_line(test_data_file)
        first_booth_voters_size = int(current_line)
        for _ in range(first_booth_voters_size):
            current_line = _get_next_line(test_data_file)
            first_booth_voters.append(VoterName(current_line))

        #Read second booth voter names
        current_line = _get_next_line(test_data_file)
        second_booth_voters_size = int(current_line)
        for _ in range(second_booth_voters_size):
            current_line = _get_next_line(test_data_file)
            second_booth_voters.append(VoterName(current_line))

        #Read voter names that commited fraud
        current_line = _get_next_line(test_data_file)
        fraud_voters_size = int(current_line)
        for _ in range(fraud_voters_size):
            current_line = _get_next_line(test_data_file)
            fraud_voters.append(VoterName(current_line))

    return first_booth_voters, second_booth_voters, fraud_voters