Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

PLEASE CODE THIS ACCORDING TO THE PROBLEM STATEMENT AND THE TEMPLATE AND INSTRUCTIONS Template file for problem A: # -*- coding: utf-8 -*- #

PLEASE CODE THIS ACCORDING TO THE PROBLEM STATEMENT AND THE TEMPLATE AND INSTRUCTIONS

image text in transcribed

image text in transcribed

Template file for problem A:

# -*- coding: utf-8 -*- """ # Programming Assignment 5, Problem A, CSE30-02, Winter 2023 ## README * Please **carefully** follow the instructions given in this template code * Only edit within the allowed sections as instructed by the comments; you are risking your own assignment grade if you make edits outside those sections or deviate from the instructions * The testing script provided at the end only performs some basic tests; it DOES NOT imply anything about your assignment grade * You may add your own tesing code in the main function while working on your assignment * **However**, you will need to remove your own testing code, and re-run the original testing script right before you submit the code, to ensure your code is free from syntax errors * If you are using the Colab notebook version, after you finished your work, please download this notebook as **python** file (not notebook file) * on menu bar (Colab's menu bar, NOT your browser's menu bar), go to File -> Download -> Download .py """ #===== Import section begins # In this assignment, No external libraries are allowed # DO NOT ADD any import statements # keep this section as is #===== Import section ends #===== Existing classes section begins # These class(es) are provided as part of the assignment # DO NOT change or add anything in this section # keep this section as is class BinarySearchTree: def __init__(self, val: int, left: 'Union[BinarySearchTree, None]'=None, right: 'Union[BinarySearchTree, None]'=None) -> None: ''' Constructor for a node of a BST - Parameters: - val: the value stored in the respective node - left: the left branch of the tree. defaults to None if no branch is given - right: the right branch of the tree. defaults to None if no branch is given - Returns: - None ''' self.val = val self.left = left self.right = right def __str__(self): """ Str getter function which prints the BST in a String form for visual aid - Parameters: -None - Returns:

- str representation of BST """ return '{} [{}] [{}]'.format(self.val, self.left, self.right) pass #===== Existing classes section ends #===== Helper classes/functions section begins # You may add your own classes or functions within this section # **class** and **function** only! # any statement that is not encapsulated inside a class or function # may result in 0 grade #===== Helper classes/functions section ends #===== Problem A function begins # follow the instruction below # DO NOT change the function signature! def get_closest_numbers(tree: BinarySearchTree, f: float, p: int) -> 'List[int]': #please use a function with this naming convention for consistent function calls when grading ''' return the p-closest values to f - Parameters: - tree: a given BST in the form of a BinarySearchTree class object - root: the root node of the BST - f: the float which is the target value - p: the amount of closest values to f to return - Returns: - List: list of p values within the BST which are closest to f ''' #===== Your implementation begins here p_closest=[] #variables do not have to keep this naming convention, but should return a list with the corresponding output return p_closest #===== Your implementation ends here #===== Problem A function ends #===== Testing scripts main function section begins # follow the instruction below # DO NOT add any statement outside of main() function def main() -> None: # you may add your own testing code within this function while you're # working on your assignment; # however, please remember to remove them, and re-run this testing script # right before you submit your work, in order to ensure your code is # free from syntax error #Below is code for constructing BST trees based on the examples provided in canvas #Example 1 tree myTree1=BinarySearchTree(val=5) myTree1.right=BinarySearchTree(val=6) myTree1.left=BinarySearchTree(val=3) myTree1.left.right=BinarySearchTree(val=4) myTree1.left.left=BinarySearchTree(val=2)

#Example 2 tree myTree2=BinarySearchTree(val=3) #Example 3 tree myTree3=BinarySearchTree(val=8) myTree3.left=BinarySearchTree(val=3) myTree3.left.left=BinarySearchTree(val=1) myTree3.left.right=BinarySearchTree(val=6) myTree3.left.right.left=BinarySearchTree(val=4) myTree3.left.right.right=BinarySearchTree(val=7) myTree3.right=BinarySearchTree(val=10) myTree3.right.right=BinarySearchTree(val=14) myTree3.right.right.left=BinarySearchTree(val=13) f_1 = 4.82 p_1 = 2 output1=get_closest_numbers(myTree1, f_1, p_1) expectedOutput1 = [4, 5] print('Input Tree: ', myTree1) print('Input F: ', f_1) print('Input P: ', p_1) print('Expected result: ', expectedOutput1) print('Your result: ', output1) if output1 is not None and sorted(output1)==expectedOutput1: print("Test 1 passed") else: print("Test 1 incorrect output") print() f_2 = 1.2 p_2 = 1 output2=get_closest_numbers(myTree2, f_2, p_2) expectedOutput2 = [3] print('Input Tree: ', myTree2) print('Input F: ', f_2) print('Input P: ', p_2) print('Expected result: ', expectedOutput2) print('Your result: ', output2) if output2 is not None and sorted(output2)==expectedOutput2: print("Test 2 passed") else: print("Test 2 incorrect output") print() f_3 = 99.0 p_3 = 4 output3=get_closest_numbers(myTree3, f_3, p_3) expectedOutput3 = [8, 10, 13, 14] print('Input Tree: ', myTree3) print('Input F: ', f_3) print('Input P: ', p_3) print('Expected result: ', expectedOutput3) print('Your result: ', output3) if output3 is not None and sorted(output3)==expectedOutput3: print("Test 3 passed") else: print("Test 3 incorrect output") print()

if __name__ == '__main__': main() #===== Testing scripts main function section ends

image text in transcribed

image text in transcribed

Template file for problem B:

# -*- coding: utf-8 -*- """ # Programming Assignment 5, Problem B, CSE30-02, Winter 2023 ## README * Please **carefully** follow the instructions given in this template code * Only edit within the allowed sections as instructed by the comments; you are risking your own assignment grade if you make edits outside those sections or deviate from the instructions * The testing script provided at the end only performs some basic tests; **it DOES NOT imply anything about your assignment grade** * You may add your own tesing code in the main function while working on your assignment * **However**, you will need to remove your own testing code, and re-run the original testing script right before you submit the code, to ensure your code is free from syntax errors * If you are using the Colab notebook version, after you finished your work, please download this notebook as **python** file (not notebook file) * on menu bar (Colab's menu bar, NOT your browser's menu bar), go to File -> Download -> Download .py """ #===== Import section begins import collections import heapq # In this assignment, ONLY given external libraries are allowed # DO NOT ADD any import statements # keep this section as is #===== Import section ends #===== Provided Global variables section begins # These global variable(s) are provided as part of the assignment # DO NOT ADD any other global variables # keep this section as is STOPWORDS = [ 'i','me','my','myself','we','our','ours','ourselves','you','your','yours', 'yourself','yourselves','he','him','his','himself','she','her','hers', 'herself','it','its','itself','they','them','their','theirs','themselves', 'what','which','who','whom','this','that','these','those','am','is','are', 'was','were','be','been','being','have','has','had','having','do','does', 'did','doing','a','an','the','and','but','if','or','because','as','until', 'while','of','at','by','for','with','about','against','between','into', 'through','during','before','after','above','below','to','from','up','down', 'in','out','on','off','over','under','again','further','then','once','here', 'there','when','where','why','how','all','any','both','each','few','more', 'most','other','some','such','no','nor','not','only','own','same','so', 'than','too','very','s','t','can','will','just','don','should','now' ] #===== Provided Global variables section ends #===== Helper classes/functions section begins # You may add your own classes or functions within this section # **class** and **function** only! # any statement that is not encapsulated inside a class or function

# may result in 0 grade #===== Helper classes/functions section ends #===== Problem B function begins # follow the instruction below # DO NOT change the function signature! def getTopWords(words: 'List[str]', K: int) -> 'List[str]': ''' - Parameters: - words: a list of words - K: The number of words to return (K most frequent words) - Returns: - List[str]: list of words sorted by the frequency from highest to lowest ''' #===== Your implementation begins here #===== Your implementation ends here pass #===== Problem B function ends #===== Testing scripts main function section begins # follow the instruction below # DO NOT add any statement outside of main() function def main() -> None: # you may add your own testing code within this function while you're # working on your assignment; # however, please remember to remove them, and re-run this testing script # right before you submit your work, in order to ensure your code is # free from syntax error input_1 = [ 'Cat', 'bat','and', 'a', 'Rat','are', 'singing', 'A', 'bat', 'is', 'not', 'the', 'black', 'caT', 'An', 'elephant', 'a', 'Rat', 'and', 'a', 'cat', 'are', 'walking', 'Bat', 'is', 'black', 'but', 'the', 'cat', 'is', 'white', 'And', 'dog', 'is' 'brown' ] k_1 = 2 res_1 = ['cat','bat'] input_2 = [ 'The','weather','is','sunny','in','SC','The','weather','is','cloudy', 'the','weather' ] k_2 = 2 res_2 = ['weather','cloudy'] print('Test case 1') out_res = getTopWords(input_1, k_1) print('Input words: ', input_1) print('Input K: ', k_1) print('Expected result: ', res_1) print('Your result: ', out_res) type_match = type(res_1) == type(out_res) type_match_str = '==' if type_match else '!=' print('Return Type: ', 'type(Expected result)', type_match_str, 'type(Your

result)') print() print('Test case 2') out_res = getTopWords(input_2, k_2) print('Input words: ', input_2) print('Input K: ', k_2) print('Expected result: ', res_2) print('Your result: ', out_res) type_match = type(res_2) == type(out_res) type_match_str = '==' if type_match else '!=' print('Return Type: ', 'type(Expected result)', type_match_str, 'type(Your result)') print() if __name__ == '__main__': main() #===== Testing scripts main function section ends

Problem Statement You need to implement a function that takes three arguments: - tree: the root of BST, (all the elements in the tree are integers) - fi a float value F - p: an integer P The function should return the P closest values to the value F in the BST. You may return the answer in any order. You are guaranteed to have only one unique set of P values in the BST that are closest to F. Below is an example for the BST class, which is also included in the template files under Programming Assignment 5 in the files section class Binarysearchtree: def _init_(self, val: int, left: 'Union[BinarySearchTree, None]'=None, right: 'Union[BinarySearchTree, None]'=None) > None: self.val = vai self. .eft = left self. right = right def str (self): "this is a helper function which returns the BST as a string return ' \{\}[{}[{}] '. format(self.val, self.left, self.right) def get_closest_numbers(tree: BinarysearchTree, f: float, p: int) -> 'List[int]': p_closest_numbers =[] \# your code return p_closest_numbers Examples: Example 1: Inputs: Root node 5, F: 4.82, P=2 Output: [5,4] Example 2: Inputs: root node 3,F=1.20,P=1 Output: [3] Example 3: Inputs: root node 8,F=99.90,P=4 Output: [8,10,13,14] You may assume: - There is only one unique set of P values in the BST that are closest to F - All the elements in the tree are integers - Do not include an eval function, print statements, or any of your own testing lines in un-commented form, as this may flag the autograder as an error - Please make sure that your function returns the appropriate values instead of simply printing them or storing them somewhere in the memory; we need to be able to call your function and get a usable return value from it - EXTERNAL LIBRARIES SHOULD NOT BE USED Point Allocation (6 points): - 6 points for passing all the test cases - Test cases will be very similar to the above examples; if your code takes the inputs listed above and outputs the correct output, without errors, it should be sufficient for this assignment - Documentation is necessary ( you should always document your code, even if it's not required!) Problem Statement Given a List of English words and an integer K, return the K most frequent words. Meanwhile, you need to convert all the words to lowercase, and ignore the stop words (Stop words are given in the template code). - Examples - Example 1 - input_1: - ['Cat', 'bat', 'and', 'a', 'Rat', 'are', 'singing', 'A', 'bat', 'is', 'not', 'the', 'black', 'caT', 'An', 'elephant', 'a', 'Rat', 'and', 'a', 'cat', 'are', 'walking', 'Bat', 'is', 'black', 'but', 'the', 'cat', 'is', 'white', 'And', 'dog', 'is' 'brown'] - output (when K=2 ): - ['cat', 'bat'] - Example 2 - input_2: - ['The', 'weather', 'is', 'sunny', 'in', 'SC', 'The', 'weather', 'is', 'cloudy', 'the', 'weather'] - output (when K=2 ): - ['weather', 'cloudy'] - Notes: - If two words have the same frequency, the word with the lower alphabetical order comes first - You may assume: - All words in the list are in string type - K is always a positive integer - K is smaller or equal to the number of unique words given in the input - So you don't need to worry about K being larger than the number of items in your result list - ONLY GIVEN EXTERNAL LIBRARIES ARE ALLOWED - Point Allocation (6 points): - 6 points for passing all the test cases - Documentation is necessary ( you should always document your code, even if it's not required! )

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Professional Microsoft SQL Server 2014 Administration

Authors: Adam Jorgensen, Bradley Ball

1st Edition

111885926X, 9781118859261

More Books

Students also viewed these Databases questions

Question

How many Tables Will Base HCMSs typically have? Why?

Answered: 1 week ago

Question

What is the process of normalization?

Answered: 1 week ago