prove

py

School

Brigham Young University, Idaho *

*We aren’t endorsed by this school

Course

251

Subject

Computer Science

Date

Feb 20, 2024

Type

py

Pages

4

Uploaded by ChiefOxide13308

Report
""" Course: CSE 251 Lesson: L07 Prove File: prove.py Author: <Add name here> Purpose: Process Task Files. Instructions: See Canvas for the full instructions for this assignment. You will need to complete the TODO comment below before submitting this file: TODO: Add your comments here on the pool sizes that you used for your assignment and why they were the best choices. """ from datetime import datetime, timedelta import requests import multiprocessing as mp from matplotlib.pylab import plt import numpy as np import glob import math # Include cse 251 common Python files - Dont change from cse251 import * # Constants - Don't change TYPE_PRIME = 'prime' TYPE_WORD = 'word' TYPE_UPPER = 'upper' TYPE_SUM = 'sum' TYPE_NAME = 'name' # TODO: Change the pool sizes and explain your reasoning in the header comment PRIME_POOL_SIZE = 1 WORD_POOL_SIZE = 1 UPPER_POOL_SIZE = 1 SUM_POOL_SIZE = 1 NAME_POOL_SIZE = 1 # Global lists to collect the task results result_primes = [] result_words = [] result_upper = [] result_sums = [] result_names = [] def is_prime(n: int): """Primality test using 6k+-1 optimization. From: https://en.wikipedia.org/wiki/Primality_test """ if n <= 3: return n > 1
if n % 2 == 0 or n % 3 == 0: return False i = 5 while i ** 2 <= n: if n % i == 0 or n % (i + 2) == 0: return False i += 6 return True def task_prime(value): """ Use the is_prime() above Add the following to the global list: {value} is prime - or - {value} is not prime """ pass def task_word(word): """ search in file 'words.txt' Add the following to the global list: {word} Found - or - {word} not found ***** """ pass def task_upper(text): """ Add the following to the global list: {text} ==> uppercase version of {text} """ pass def task_sum(start_value, end_value): """ Add the following to the global list: sum of {start_value:,} to {end_value:,} = {total:,} """ pass def task_name(url): """ use requests module Add the following to the global list: {url} has name <name> - or - {url} had an error receiving the information """ pass
def main(): log = Log(show_terminal=True) log.start_timer() # TODO Create process pools # TODO change the following to start the pools count = 0 task_files = glob.glob("tasks/*.task") for filename in task_files: # print() # print(filename) task = load_json_file(filename) print(task) count += 1 task_type = task['task'] if task_type == TYPE_PRIME: task_prime(task['value']) elif task_type == TYPE_WORD: task_word(task['word']) elif task_type == TYPE_UPPER: task_upper(task['text']) elif task_type == TYPE_SUM: task_sum(task['start'], task['end']) elif task_type == TYPE_NAME: task_name(task['url']) else: log.write(f'Error: unknown task type {task_type}') # TODO wait on the pools # DO NOT change any code below this line! #--------------------------------------------------------------------------- def log_list(lst, log): for item in lst: log.write(item) log.write(' ') log.write('-' * 80) log.write(f'Primes: {len(result_primes)}') log_list(result_primes, log) log.write('-' * 80) log.write(f'Words: {len(result_words)}') log_list(result_words, log) log.write('-' * 80) log.write(f'Uppercase: {len(result_upper)}') log_list(result_upper, log) log.write('-' * 80) log.write(f'Sums: {len(result_sums)}') log_list(result_sums, log) log.write('-' * 80) log.write(f'Names: {len(result_names)}') log_list(result_names, log)
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
log.write(f'Number of Primes tasks: {len(result_primes)}') log.write(f'Number of Words tasks: {len(result_words)}') log.write(f'Number of Uppercase tasks: {len(result_upper)}') log.write(f'Number of Sums tasks: {len(result_sums)}') log.write(f'Number of Names tasks: {len(result_names)}') log.stop_timer(f'Total time to process {count} tasks') if __name__ == '__main__': main()