Beispiel #1
0
 def test_if_False_is_returned_if_input_format_is_not_correct(self):
     input_file = os.path.join(CURRENT_DIR,
                               "test_input_files/bad_test_input.txt")
     lines = ir.read_input(input_file)
     regex = test_config_data.get("std_input_format")
     with self.assertRaises(ir.InputFormatError):
         ir.validate_format(regex, lines)
Beispiel #2
0
def run(filename, quantum):
    proclist = []
    (ncpu, nprocs) = read_input(filename, proclist)
    total_wait_time = 0
    total_turnaround_time = 0

    for proc in proclist:
        total_turnaround_time += proc.runtime

    CPUs = []
    finish = []
    proclist1 = proclist.copy()
    time = rr(CPUs, ncpu, proclist1, finish, quantum)
    draw_gantt(CPUs, time, proclist)

    print("Whole exeution time: ", time)

    for proc in finish:
        total_wait_time += proc.wait_time
        total_turnaround_time += proc.wait_time

    avg_wait_time = total_wait_time / nprocs
    avg_turnaround_time = total_turnaround_time / nprocs

    print("Average wait time: ", avg_wait_time)
    print("Average turnaround time: ", avg_turnaround_time)
Beispiel #3
0
 def test_if_True_is_returned_if_input_format_is_correct(self):
     input_file = os.path.join(CURRENT_DIR,
                               "test_input_files/test_input.txt")
     lines = ir.read_input(input_file)
     regex = test_config_data.get("std_input_format")
     result = ir.validate_format(regex, lines)
     self.assertTrue(result)
Beispiel #4
0
def main():
    input_file = sys.argv[1]
    data = ir.read_input(input_file)
    messages = ir.validate_format(config_data.get("std_input_format"), data)
    home_kingdom = config_data.get("home_kingdom")
    home_kngdm_obj = Kingdom(home_kingdom)
    home_kngdm_obj.send_secret_msg(messages)
    allies = home_kngdm_obj.form_rule()
    output = op.Outputter(allies)
    output.print_standard_output()
Beispiel #5
0
 def test_if_correct_strings_are_returned_with_secret_message_striped_for_white_spaces_at_ends(
         self):
     input_file = os.path.join(CURRENT_DIR,
                               "test_input_files/test_input.txt")
     expected = [
         "KINGDOMONE SECRETMESSAGEONE", "KINGDOMTWO SECRET MESSAGE TWO",
         "KINGDOMTHREE SECRET MESSAGETHREE"
     ]
     actual = ir.read_input(input_file)
     self.assertEqual(expected, actual)
Beispiel #6
0
def turn_input_to_in(address, out_dir='out/'):
    """
    Main function in utils.py
    """
    file_name = address.split('/')
    name = file_name[-1][:file_name[-1].index('.')]
    # read input from raw data
    voronoi_list = input_reader.read_input(address)

    # store readed voronoi to increase processing speed
    if not os.path.exists(out_dir):
        os.makedirs(out_dir)

    input_reader.store_processed_input(voronoi_list, out_dir + name + '.in')
Beispiel #7
0
    if match == None:
        return False

    min_letter_count = int(match.group(1))
    max_letter_count = int(match.group(2))
    letter = match.group(3)
    password = match.group(4)
    occurences_count = password.count(letter)
    result = min_letter_count <= occurences_count <= max_letter_count

    print(
        "{password:<30} {occurences_count:>3}{letter} {belongs} [{min_letter_count}, {max_letter_count}] {result}"
        .format(
            password=password,
            occurences_count=occurences_count,
            letter=letter,
            belongs=("∈" if result else "∉"),
            min_letter_count=min_letter_count,
            max_letter_count=max_letter_count,
            result=('\033[92mOK\033[0m' if result else '\033[91mKO\033[0m')))
    return result


password_policy_and_password_regex: re.Pattern = re.compile(
    "(\d+)\-(\d+) (\w): (\w+)")
correct_password_count = 0
for line in read_input("input.txt"):
    if password_match(line, password_policy_and_password_regex):
        correct_password_count += 1

print(f"{correct_password_count} passwords are correct")
tree_character:str = '#'

def count_trees_on_slope(horizontal_increment: int, vertical_increment:int) -> int:
    line_index:int = 0
    column_index:int = 0
    trees_count:int = 0

    while line_index < len(lines):
        line:str = lines[line_index]
        if line[column_index % len(line)] == tree_character:
            trees_count += 1
        line_index += vertical_increment
        column_index += horizontal_increment
    return trees_count


lines:List[str] = read_input("input.txt")

trees_count_slope_1_1:int = count_trees_on_slope(1, 1)
trees_count_slope_3_1:int = count_trees_on_slope(3, 1)
trees_count_slope_5_1:int = count_trees_on_slope(5, 1)
trees_count_slope_7_1:int = count_trees_on_slope(7, 1)
trees_count_slope_1_2:int = count_trees_on_slope(1, 2)

print("Trees on slope 1, 1 = " + str(trees_count_slope_1_1))
print("Trees on slope 3, 1 = " + str(trees_count_slope_3_1))
print("Trees on slope 5, 1 = " + str(trees_count_slope_5_1))
print("Trees on slope 7, 1 = " + str(trees_count_slope_7_1))
print("Trees on slope 1, 2 = " + str(trees_count_slope_1_2))

print(f"{trees_count_slope_1_1} x {trees_count_slope_3_1} x {trees_count_slope_5_1} x {trees_count_slope_7_1} x {trees_count_slope_1_2} = {trees_count_slope_1_1 * trees_count_slope_3_1 * trees_count_slope_5_1 * trees_count_slope_7_1 * trees_count_slope_1_2}")
Beispiel #9
0
from typing import List
import os, sys, inspect
current_dir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
parent_dir = os.path.dirname(current_dir)
sys.path.insert(0, parent_dir) 
from input_reader import read_input

def find_two_values_whose_sum_equals(input_values: List[int], result: int) -> (int, int):
    values_count = len(input_values)
    for first_operand_index in range(values_count):
        for second_operand_index in range(first_operand_index, values_count):
            if input_values[first_operand_index] + input_values[second_operand_index] == result:
                return input_values[first_operand_index], input_values[second_operand_index]

def find_three_values_whose_sum_equals(input_values: List[int], result: int) -> (int, int, int):
    values_count = len(input_values)
    for first_operand_index in range(values_count):
        for second_operand_index in range(first_operand_index, values_count):
            for third_operand_index in range(second_operand_index, values_count):
                if input_values[first_operand_index] + input_values[second_operand_index] + input_values[third_operand_index] == result:
                    return input_values[first_operand_index], input_values[second_operand_index], input_values[third_operand_index]

# ==========================================================================================

input_values: List[int] = [int(value) for value in read_input("input.txt")]
value_pair: (int, int) = find_two_values_whose_sum_equals(input_values, 2020)
value_trio: (int, int, int) = find_three_values_whose_sum_equals(input_values, 2020)
print(f"{value_pair[0]} + {value_pair[1]} = {value_pair[0] + value_pair[1]} // {value_pair[0]} x {value_pair[1]} = {value_pair[0] * value_pair[1]}")
print(f"{value_trio[0]} + {value_trio[1]} + {value_trio[2]} = {value_trio[0] + value_trio[1] + value_trio[2]} // {value_trio[0]} x {value_trio[1]} x {value_trio[2]} = {value_trio[0] * value_trio[1] * value_trio[2]}")
Beispiel #10
0
def decode(code: str, lower_bound_code: str, upper_bound_code) -> int:
    code_length: int = len(code)
    lower_bound: int = 0
    upper_bound: int = pow(2, code_length)

    for index in range(code_length):
        if code[index] == upper_bound_code:
            lower_bound = (upper_bound - lower_bound) / 2 + lower_bound
        elif code[index] == lower_bound_code:
            upper_bound = (upper_bound - lower_bound) / 2 + lower_bound

    return lower_bound


def decode_seat(seat_code: str) -> int:
    row_count: int = 7
    column_count: int = 3
    row = decode(seat_code[:row_count], 'F', 'B')
    column = decode(seat_code[row_count:row_count + column_count], 'L', 'R')
    seat = row * 8 + column
    return int(seat)


values: list = [(line, decode_seat(line)) for line in read_input("input.txt")]
values.sort(key=lambda v: v[1], reverse=True)

for index in range(len(values)):
    if (index > 0 and values[index][1] != values[index - 1][1] - 1):
        print(f">>> YOUR SEAT {values[index - 1][1] - 1}")
    print(f"{values[index][0]} -> {values[index][1]}")
Beispiel #11
0
 def test_if_correct_number_of_rows_are_returned_from_input_file(self):
     input_file = os.path.join(CURRENT_DIR,
                               "test_input_files/test_input.txt")
     expected = 3
     actual = len(ir.read_input(input_file))
     self.assertEqual(expected, actual)
Beispiel #12
0
from input_reader import read_input
import os
import logging
import sys

if __name__== '__main__':
    logging.basicConfig(stream=sys.stdout, level=logging.DEBUG)
    for filename in ["me_at_the_zoo","trending_today","videos_worth_spreading","kittens"] :
        logging.info('*'*10+' '+filename+' '+'*'*10)
        infile = os.path.join('input', filename)+'.in'
        outfile = os.path.join('over_video_size', filename)+'.out'
        problem = read_input(infile)
        problem.solution_3()
        output_file = open(outfile, 'w+')
        output_file.write(problem.output_sol())
        output_file.close()