Esempio n. 1
0
    def time_trial(n):
        a = []
        for i in range(0, n):
            a.append(random.randint(-MAXIMUM_INTEGER, MAXIMUM_INTEGER))

        stopwatch = Stopwatch()
        ThreeSum.count(a)
        return stopwatch.elapsed_time()
Esempio n. 2
0
File: linear.py Progetto: dky/cb
from stopwatch import Stopwatch

# Linear O(N)
# N Runtime
# 2 2
# 4 4
# 6 6


def linear_run_time(N):
    total_operations = 0
    i = 1
    while i < N:
        total_operations += 1
        i += 1
    return total_operations


if __name__ == "__main__":
    input_sizes = [100, 200, 400, 800, 1600, 3200, 6400]
    for input_size in input_sizes:
        timer = Stopwatch()
        linear_run_time(input_size)
        # Relationship you should see is if you double your input the run time
        # is also doubled.
        print(input_size, timer.elapsed_time())
Esempio n. 3
0
    @staticmethod
    def get_all_solutions(a):
        solutions = []
        n = len(a)
        for i in range(0, n):
            for j in range(i + 1, n):
                if a[i] + a[j] == 0:
                    solutions.append((a[i], a[j]))

        return solutions

    @staticmethod
    def print_all(a):
        solutions = TwoSum.get_all_solutions(a)
        for solution in solutions:
            print(solution)

    @staticmethod
    def count(a):
        solutions = TwoSum.get_all_solutions(a)
        return len(solutions)


if __name__ == "__main__":
    MIN_INPUT = -100
    MAX_INPUT = 100
    sample_input = [i for i in range(MIN_INPUT, MAX_INPUT)]
    stopwatch = Stopwatch()
    TwoSum.get_all_solutions(sample_input)
    print(stopwatch.elapsed_time())
Esempio n. 4
0
from stopwatch import Stopwatch
import random


def approach_one(our_list, target):
    for i in range(0, len(our_list)):
        number = our_list[i]
        if number == target:
            return i
    return -1


# Choose a random integer and see how it grows.
if __name__ == "__main__":
    input_sizes = [10000, 20000, 40000, 80000, 160000]
    num_samples = 100

    for input_size in input_sizes:
        timer = Stopwatch()
        # input_list = [i for i in range(0, input_sizes)]
        input_list = range(0, input_size)
        total_runtime = 0

        for i in range(0, num_samples):
            target = random.randint(0, input_size)
            approach_one(input_list, target)
            total_runtime += timer.elapsed_time()
        print(input_size, timer.elapsed_time())
Esempio n. 5
0
from stopwatch import Stopwatch
import random


def approach_one(our_list, target):
    for i in range(0, len(our_list)):
        number = our_list[i]
        if number == target:
            return i
    return -1


# Choose a random integer and see how it grows.
if __name__ == "__main__":
    input_sizes = [10000, 20000, 40000, 80000, 160000]
    for input_size in input_sizes:
        timer = Stopwatch()
        # input_list = [i for i in range(0, input_sizes)]
        input_list = range(0, input_size)
        target = random.randint(0, input_size)

        total_operations = approach_one(input_list, target)
        print(input_size, total_operations, timer.elapsed_time())
Esempio n. 6
0
        mid = lo + (hi - lo) // 2

        #if element is in the middle
        if target == our_list[mid]:
            return mid

        #if element is smaller than mid, then take the left half and search it
        elif target < mid:
            return approach_two(our_list, target, lo, mid - 1)

        #if element is > mid, search right subarray
        elif target > mid:
            return approach_two(our_list, target, mid + 1, hi)

    else:
        return -1


if __name__ == "__main__":
    input_sizes = [5, 10, 20, 40]
    num_samples = 100
    for x in input_sizes:
        input_size = 2**x
        timer = Stopwatch()
        input_list = range(0, input_size)
        total_runtime = 0
        for i in range(0, num_samples):
            target = random.randint(0, input_size)
            approach_two(input_list, target, 0, input_size - 1)
            total_runtime += timer.elapsed_time()
        print(input_size, timer.elapsed_time() / num_samples)
Esempio n. 7
0
        # If element is smaller than mid, then it
        # can only be present in left subarray
        elif our_list[mid] > target:
            return approach_two(our_list, lo, mid - 1, target)

        # Else the element can only be present
        # in right subarray
        else:
            return approach_two(our_list, mid + 1, hi, target)

    else:
        # Element is not present in the array
        return -1


if __name__ == "__main__":
    input_sizes = [5, 10, 20, 40]
    num_samples = 100
    for x in input_sizes:
        input_size = 2**x
        timer = Stopwatch()
        input_list = range(0, input_size)
        total_runtime = 0
        for i in range(0, num_samples):
            target = random.randint(0, input_size)
            approach_two(input_list, 0, input_size - 1, target)
            total_runtime += timer.elapsed_time()
        print("2 ^ " + str(x) + " : " +
              str(timer.elapsed_time() / num_samples))