diff --git a/src/results/results.pickle b/src/results/results.pickle index 0441a808..8233d59c 100644 Binary files a/src/results/results.pickle and b/src/results/results.pickle differ diff --git a/src/students/B05902135.py b/src/students/B05902135.py deleted file mode 100644 index 86fd1278..00000000 --- a/src/students/B05902135.py +++ /dev/null @@ -1,402 +0,0 @@ -import os - -# Define global variables with upper case -SRC_PATH = os.path.dirname(os.path.dirname(os.path.realpath(__file__))) -TEST_DATA_DIR = os.path.join(SRC_PATH, 'test_data') - - -def task_1(dummy=None): - ''' - Task 1: Basic Syntax and Flake8 Checker - - Python uses indentations to separate blocks instead of backets. - Unlike most programming language (like C++), indentations in Python - are required. - - See https://www.python-course.eu/python3_blocks.php for some examples. - - Flake8 (http://flake8.pycqa.org/en/latest/) could help you check these - syntax error. It also regular your coding style. For example, using - two whitespaces as indentation is allowed in Python. However, Flake8 - will tell you it is an error "E111: indentation is not a multiple of four". - This is because when many people work on the same project, it would be - confusing if people are using different identation style. - - Following the coding style in Flake8 is strongly suggested. - - ''' - # Hint: - # Run `python autograder.py -task 1 -student_id ` - # under src/ to see if you pass this task. - # The correct output would be "Hello world" without any - # error. Note that passing this task does NOT mean you pass the - # Flake8 chcker. Please check your style with - # `flake8 src/student/.py` - - # TODO: fix the syntax error for the following code - if True: - sentence = "Hello world" - print(sentence) - - # End of TODO (do not change the code below) - return True - - -def task_2( - input_list: list = [1, 4, 53, 27, 9], - target_index: int = 0, - input_dictionary: dict = {"a": " taiwan", "b": 20, "c": "CSIE"}, - target_key: str = "a" -) -> tuple: - ''' - Task 2: Data Types - - Python has many data types, including Boolean, String, Integer, Float, - List, Dictionary, etc. - - You could use the function type() to see the data type: - - >>> type(5) - - >>> type("hi") - - >>> type(9.2) - - >>> type(["list", "could", "include", "different", "data type", 5, 3.2]) - - >>> type(("you could not change elements", "in a tuple")) - - >>> type({"a": 1, "b":20}) - - >>> type(True) - - >>> - - Try to play with the Python IDE to see different data types by yourself. - - In this task, you are asked to use these datatype. - - Args: - input_list: a list with several items - target_index: target index for the input_list. You need to get the - list element with this index (i.e, 'input_list[target_index]') - input_dictionary: a dictionary with several key-value pairs. - target_key: target key for the input_dictionary You need to get the - value with this key (i.e., input_dictionary[target_key]) - - Returns: - input_list_length_and_sentence: a tuple that contains two elements. - The first one is an integer that indicates the length of input_list - The second one is a string that contains the combination of - input_list[target_index] and input_dictionary[target_key] - - Examples: - Inputs: - input_list = [1, 3, 5, 7, 9] - target_index = 0 - input_dictionary = {"1": "8", "f": "abc", "s": 5.5, "5.5" 900} - target_key = "5.5" - - Returns: - input_list_length_and_sentence = (5, "1900") - - Hints: - * Try to use print() to print out the inputs. - * Use len() to get the length of the list. - * Different data types could not be added. Use str() to convert data - to string. - * Run `python src/autograder.py -task 2 -student_id ` - to see if you pass this task. - * The correct output would be (4, '1 taiwan') - ''' - # TODO: change length and sentence to fit the requirement - length = len(input_list) - sentence = str(input_list[target_index]) + str(input_dictionary[target_key]) - # End of TODO - input_list_length_and_sentence = (length, sentence) - print(input_list_length_and_sentence) - return input_list_length_and_sentence - - -def task_3( - number: int = 1314151677777 -) -> list: - ''' - Task 3: Conditions - - Args: - number: a integer input - - Returns: - prime_factors_below_10: a list of the number's largest factors - below 10 - if the number is negative, return [-1] - if the number is zero, return [0] - - Hints: - * Use % to get the remainder - * Using a loop (introduced in the next task) will make some - conditions simpler - ''' - prime_factors_below_10 = [] - # TODO: fill in the conditions - if number < 0: - prime_factors_below_10 = [-1] - # elif stands for "else if" in Python. - elif number == 0: - prime_factors_below_10 = [0] - else: - if number % 2 == 0: - prime_factors_below_10.append(2) - if number % 3 == 0: - prime_factors_below_10.append(3) - if number % 5 == 0: - prime_factors_below_10.append(5) - if number % 7 == 0: - prime_factors_below_10.append(7) - # End of TODO - print(prime_factors_below_10) - return prime_factors_below_10 - - -def task_4( - numbers: list = [2, 4, 5, 6, 9] -) -> list: - ''' - Task 4: For and While Loop - - Args: - numbers: a list of integers - - Returns: - list_of_stars: a list of stars (*) - For each number n in the list, you need to - append n lines of stars to the list, where - the first line has one star, the last line - has n stars. - - Examples: - input: - [1, 3, 5] - output: - ['*', - '*', - '**', - '***', - '*', - '**', - '***', - '****', - '*****'] - - Hints: - * You could create a string with repetitive substring by * - ''' - list_of_stars = [] - # In Python, the for loop could iterate through a list directly - for number in numbers: - # TODO: change stars to correct length - for i in range(1, number+1): - stars = "*" * i - list_of_stars.append(stars) - # End of TODO - - # This could be done by the while loop - list_of_stars_while = [] - i = 0 - while i < len(numbers): - # TODO: change stars to correct length - j = 1 - while j <= numbers[i]: - stars = "*" * j - j += 1 # This line is equivalant to j = j + 1 - list_of_stars_while.append(stars) - i += 1 - # End of TODO - - print("=====> Output list_of_stars") - for stars in list_of_stars: - print(stars) - print("=====> Output list_of_stars_while") - for stars in list_of_stars_while: - print(stars) - - for ans1, ans2 in zip(list_of_stars, list_of_stars_while): - assert ans1 == ans2 - return list_of_stars - - -def task_5( - input_filename: str = 'task_5_input.txt', - output_filename: str = 'task_5_output.txt' -) -> str: - ''' - Task 5: I/O with files - - Args: - input_filename: input filename - output_filename: output filename - - Returns: - lines: content in the output file without commas - - Hints: - * Use .split(something) to split a string into several substring - * Use fout.write(something) to write text into the output file - - ''' - input_filename = os.path.join(TEST_DATA_DIR, input_filename) - output_filename = os.path.join(TEST_DATA_DIR, output_filename) - # Remove previous output file - if os.path.exists(output_filename): - os.remove(output_filename) - - with open(input_filename, 'r') as fin, open(output_filename, 'w') as fout: - lines = fin.readlines() - print(f"=======> Input file content:") - for line in lines: - print(f"{line}") - # TODO: read the content of the input file, where words are separate by - # commas. Please remove the commas and write words to the output file - fout.write(''.join(line.split(','))) - pass - # End of TODO - - with open(output_filename, 'r') as fin: - lines = fin.readlines() - print(f"=======> Output file content:") - print(lines) - return "".join(lines) - - -def task_6( - matrix: list = [[-0.5, 1], [1, 0.5], [-1, 0.5], [-1, -0.5]], - vector: list = [1, 0.5] -) -> list: - ''' - Task 6: Functions - - Args: - matrix: a list of v1 - vector: v2 - - Returns: - cos_sims: a list of cosine similarity between v1s and v2 - - Hints: - * A good function name should be self-explained - * A good function should be less than 30 lines - * A good function should include comments to explain how to use it - * Cosine similarity of the vector itself will be 0.9999999 instead of 1 - ''' - # You could define function B in function A, but function B could only - # be used in the scope of function A - def dot_product(v1, v2): - assert len(v1) == len(v2) - return sum(a*b for a, b in zip(v1, v2)) - - def norm(vector): - # Note that this function would have some minor error due to the - # approximation of square root - return dot_product(vector, vector) ** 0.5 - - def get_cosine_simialrity(v1, v2): - ''' - Calculate the cosine similarity = v1 * v2 / (|v1| * |v2|) - ''' - # TODO: use the above functions to calculate cosine similarity of - # the two vectors v1 and v2 - cos_sim = dot_product(v1, v2) / (norm(v1) * norm(v2)) - # End of TODO - - return cos_sim - - cos_sims = [] - for v1 in matrix: - cos_sim = get_cosine_simialrity(v1, vector) - print(f"Cosine similarity between {v1} and {vector}: {cos_sim}") - cos_sims.append(cos_sim) - return cos_sims - - -class Student(): - def __init__(self, student_id, time): - self.student_id = student_id - self.time = time - self.words_to_say = "initial value" - - def set_words_to_say(self, words_to_say): - self.words_to_say = words_to_say - - def hello(self): - return ( - f"Hello, {self.student_id}! Time is {self.time}. " - f"I want to say {self.words_to_say}" - ) - - -def task_7( - student_id: str = 'test_id', - time: str = '2018_11_24_0000' -) -> Student: - ''' - Task 7: Class - - Args: - student_id: someone's student ID - time: a certain time - - Returns: - student: an Student object - - Hints: - * Use Student(parameters1, parameters2 ...) to create an object - and assign it to a variable - * Use . to call object function - ''' - # TODO: create a student object with different words to say - student = Student(student_id, time) - student.set_words_to_say('What?') - # End of TODO - - print(student.hello()) - return student - - -def task_8( - img_url: str = 'https://i.imgur.com/B75zq0x.jpg' -) -> object: - ''' - Task 8: Module - - Args: - img_url: address of an image - - Returns: - result_img: an PIL Image - - Hints: - * Make sure you have installed the PIL package - * Take a look at utils.py first - * You could easily find answers with Google - ''' - from urllib import request - result_img = None - - # TODO: download the image from img_url with the request module - # and add your student ID on it with draw_text() in the utils module - # under src/. - - # You are allowed to change the img_url to your own image URL. - - # Display the image: - # result_img.show() - # Note: please comment this line when hand in. - - # If you are running on a server, use - # result.save('test.jpg') - # and copy the file to local or use Jupyter Notebook to render. - - # End of TODO - - return result_img diff --git a/src/students/B06202007.py b/src/students/B06202007.py deleted file mode 100644 index 107138b8..00000000 --- a/src/students/B06202007.py +++ /dev/null @@ -1,413 +0,0 @@ -''' -This is the sample code from the homework. You shold NOT modify this file. -Instead, please copy this file to src/students/.py and -edit it there. -''' -import os - -# Define global variables with upper case -SRC_PATH = os.path.dirname(os.path.dirname(os.path.realpath(__file__))) -TEST_DATA_DIR = os.path.join(SRC_PATH, 'test_data') - - -def task_1(): - ''' - Task 1: Basic Syntax and Flake8 Checker - - Python uses indentations to separate blocks instead of backets. - Unlike most programming language (like C++), indentations in Python - are required. - - See https://www.python-course.eu/python3_blocks.php for some examples. - - Flake8 (http://flake8.pycqa.org/en/latest/) could help you check these - syntax error. It also regular your coding style. For example, using - two whitespaces as indentation is allowed in Python. However, Flake8 - will tell you it is an error "E111: indentation is not a multiple of four". - This is because when many people work on the same project, it would be - confusing if people are using different identation style. - - Following the coding style in Flake8 is strongly suggested. - - ''' - # Hint: - # Run `python src/autograder.py -task 1 -student ` - # to see if you pass this task. - # The correct output would be "Hello world" without any - # error. Note that passing this task does NOT mean you pass the - # Flake8 chcker. Please check your style with - # `flake8 src/student/.py` - - # TODO: fix the syntax error for the following code - if True: - sentence="Hello world" - print (sentence) - - # End of TODO (do not change the code below) - return True - - -def task_2( - input_list: list = [1, 4, 53, 27, 9], - target_index: int = 0, - input_dictionary: dict = {"a": " taiwan", "b": 20, "c": "CSIE"}, - target_key: str = "a" -) -> tuple: - ''' - Task 2: Data Types - - Python has many data types, including Boolean, String, Integer, Float, - List, Dictionary, etc. - - You could use the function type() to see the data type: - - >>> type(5) - - >>> type("hi") - - >>> type(9.2) - - >>> type(["list", "could", "include", "different", "data type", 5, 3.2]) - - >>> type(("you could not change elements", "in a tuple")) - - >>> type({"a": 1, "b":20}) - - >>> type(True) - - >>> - - Try to play with the Python IDE to see different data types by yourself. - - In this task, you are asked to use these datatype. - - Args: - input_list: a list with several items - target_index: target index for the input_list. You need to get the - list element with this index (i.e, 'input_list[target_index]') - input_dictionary: a dictionary with several key-value pairs. - target_key: target key for the input_dictionary You need to get the - value with this key (i.e., input_dictionary[target_key]) - - Returns: - input_list_length_and_sentence: a tuple that contains two elements. - The first one is an integer that indicates the length of input_list - The second one is a string that contains the combination of - input_list[target_index] and input_dictionary[target_key] - - Examples: - Inputs: - input_list = [1, 3, 5, 7, 9] - target_index = 0 - input_dictionary = {"1": "8", "f": "abc", "s": 5.5, "5.5" 900} - target_key = "5.5" - - Returns: - input_list_length_and_sentence = (5, "1900") - - Hints: - * Try to use print() to print out the inputs. - * Use len() to get the length of the list. - * Different data types could not be added. Use str() to convert data - to string. - * Run `python src/autograder.py -task 2 -student_id ` - to see if you pass this task. - * The correct output would be (5, '1 taiwan') - ''' - # TODO: change length and sentence to fit the requirement - length = len(input_list) - sentence = input_list[0] + input_dictionary["a"] - # End of TODO - input_list_length_and_sentence = (length, sentence) - print(input_list_length_and_sentence) - return input_list_length_and_sentence - - -def task_3( - number: int = 1314151677777 -) -> list: - ''' - Task 3: Conditions - - Args: - number: a integer input - - Returns: - prime_factors_below_10: a list of the number's largest factors - below 10 - if the number is negative, return [-1] - if the number is zero, return [0] - - Hints: - * Use % to get the remainder - * Using a loop (introduced in the next task) will make some - conditions simpler - ''' - prime_factors_below_10 = [] - # TODO: fill in the conditions - if number < 0: - prime_factors_below_10 = [-1] - # elif stands for "else if" in Python. - elif number == 0: - prime_factors_below_10 = [0] - else: - if number % 2 == 0: - prime_factors_below_10.append(2) - if number % 3 == 0: - prime_factors_below_10.append(3) - if number % 5 == 0: - prime_factors_below_10.append(5) - if number % 7 == 0: - prime_factors_below_10.append(7) - # End of TODO - return prime_factors_below_10 - - -def task_4( - numbers: list = [2, 4, 5, 6, 9] -) -> list: - ''' - Task 4: For and While Loop - - Args: - numbers: a list of integers - - Returns: - list_of_stars: a list of stars (*) - For each number n in the list, you need to - append n lines of stars to the list, where - the first line has one star, the last line - has n stars. - - Examples: - input: - [1, 3, 5] - output: - ['*', - '*', - '**', - '***', - '*', - '**', - '***', - '****', - '*****'] - - Hints: - * You could create a string with repetitive substring by * - ''' - list_of_stars = [] - # In Python, the for loop could iterate through a list directly - for number in numbers: - # TODO: change stars to correct length - for i in range(1, number+1): - stars = "*" - list_of_stars.append(stars * i) - # End of TODO - - # This could be done by the while loop - list_of_stars_while = [] - i = 0 - while i <= len(numbers): - # TODO: change stars to correct length - j = 1 - while j <= numbers[i]: - stars = "*" - j += 1 # This line is equivalant to j = j + 1 - list_of_stars_while.append(stars * (j-1)) - i += 1 - # End of TODO - - print("=====> Output list_of_stars") - for stars in list_of_stars: - print(stars) - print("=====> Output list_of_stars_while") - for stars in list_of_stars_while: - print(stars) - - for ans1, ans2 in zip(list_of_stars, list_of_stars_while): - assert ans1 == ans2 - return list_of_stars - - -def task_5( - input_filename: str = os.path.join(TEST_DATA_DIR, 'task_5_input.txt'), - output_filename: str = os.path.join(TEST_DATA_DIR, 'task_5_output.txt') -) -> list: - ''' - Task 5: I/O with files - - Args: - input_filename: input filename - output_filename: output filename - - Returns: - lines: content in the output file without commas - - Hints: - * Use .split(something) to split a string into several substring - * Use fout.write(something) to write text into the output file - - ''' - with open(input_filename, 'r') as fin, open(output_filename, 'w') as fout: - lines = fin.readlines() - print(f"=======> Input file content:") - for line in lines: - print(f"{line}") - # TODO: read the content of the input file, where words are separate by - # commas. Please remove the commas and write words to the output file - for i in line: - if i!=",": - fout.write(i) - pass - # End of TODO - - with open(output_filename, 'r') as fin: - lines = fin.readlines() - print(f"=======> Output file content:") - for line in lines: - print(f"{line}") - return lines - - -def task_6( - matrix: list = [[-0.5, 1], [1, 0.5], [-1, 0.5], [-1, -0.5]], - vector: list = [1, 0.5] -) -> list: - ''' - Task 6: Functions - - Args: - matrix: a list of v1 - vector: v2 - - Returns: - cos_sims: a list of cosine similarity between v1s and v2 - - Hints: - * A good function name should be self-explained - * A good function should be less than 30 lines - * A good function should include comments to explain how to use it - * Cosine similarity of the vector itself will be 0.9999999 instead of 1 - ''' - # You could define function B in function A, but function B could only - # be used in the scope of; function A - def dot_product(v1, v2): - assert len(v1) == len(v2) - return sum(a*b for a, b in zip(v1, v2)) - - def norm(vector): - # Note that this function would have some minor error due to the - # approximation of square root - return dot_product(vector, vector) ** 0.5 - - def get_cosine_simialrity(v1, v2): - ''' - Calculate the cosine similarity = v1 * v2 / (|v1| * |v2|) - ''' - # TODO: use the above functions to calculate cosine similarity of - # the two vectors v1 and v2 - cos_sim = dot_product(v1, v2) / (norm(v1) * norm(v2)) - # End of TODO - - return cos_sim - - cos_sims = [] - for v1 in matrix: - cos_sim = get_cosine_simialrity(v1, vector) - print(f"Cosine similarity between {v1} and {vector}: {cos_sim}") - cos_sims.append(cos_sim) - return cos_sims - - -class Student(): - def __init__(self, student_id, time): - self.student_id = student_id - self.time = time - self.words_to_say = "initial value" - - def set_words_to_say(self, words_to_say): - self.words_to_say = words_to_say - - def hello(self): - return ( - f"Hello, {self.student_id}! Time is {self.time}. " - f"I want to say {self.words_to_say}" - ) - - -def task_7( - student_id: str = 'test_id', - time: str = '2018_11_24_0000' -) -> Student: - ''' - Task 7: Class - - Args: - student_id: someone's student ID - time: a certain time - - Returns: - student: an Student object - - Hints: - * Use Student(parameters1, parameters2 ...) to create an object - and assign it to a variable - * Use . to call object function - ''' - # TODO: create a student object with different words to say - student = Student(student_id,time) - student.set_words_to_say('hello') - # End of TODO - - print(student.hello()) - return student - - -def task_8( - img_url: str = 'https://i.imgur.com/B75zq0x.jpg' -) -> object: - ''' - Task 8: Module - - Args: - img_url: address of an image - - Returns: - result_img: an PIL Image - - Hints: - * Make sure you have installed the PIL package - * Take a look at utils.py first - * You could easily find answers with Google - ''' - from urllib import request - result_img = None - - # TODO: download the image from img_url with the request module - from PIL import image - import requests - from io import BytesIO - import sys - sys.path.append("..") - from utils import draw_text - respond = requests.get(img_url) - result_img = Image_open(ByesIO(respond.content)) - draw_text(result_img, 'b06202007') - #result_img.show() - # and add your student ID on it with draw_name() in the utils module - # under src/. - - # You are allowed to change the img_url to your own image URL. - - # Display the image: - # result_img.show() - # Note: please comment this line when hand in. - - # If you are running on a server, use - # result.save('test.jpg') - # and copy the file to local or use Jupyter Notebook to render. - - # End of TODO - - return result_img diff --git a/src/students/B07902019.py b/src/students/B07902019.py deleted file mode 100644 index 650e54c0..00000000 --- a/src/students/B07902019.py +++ /dev/null @@ -1,413 +0,0 @@ -''' -This is the sample code from the homework. You shold NOT modify this file. -Instead, please copy this file to src/students/.py and -edit it there. -''' -import os - -# Define global variables with upper case -SRC_PATH = os.path.dirname(os.path.dirname(os.path.realpath(__file__))) -TEST_DATA_DIR = os.path.join(SRC_PATH, 'test_data') - - -def task_1(dummy=None): - ''' - Task 1: Basic Syntax and Flake8 Checker - - Python uses indentations to separate blocks instead of backets. - Unlike most programming language (like C++), indentations in Python - are required. - - See https://www.python-course.eu/python3_blocks.php for some examples. - - Flake8 (http://flake8.pycqa.org/en/latest/) could help you check these - syntax error. It also regular your coding style. For example, using - two whitespaces as indentation is allowed in Python. However, Flake8 - will tell you it is an error "E111: indentation is not a multiple of four". - This is because when many people work on the same project, it would be - confusing if people are using different identation style. - - Following the coding style in Flake8 is strongly suggested. - - ''' - # Hint: - # Run `python autograder.py -task 1 -student_id ` - # under src/ to see if you pass this task. - # The correct output would be "Hello world" without any - # error. Note that passing this task does NOT mean you pass the - # Flake8 chcker. Please check your style with - # `flake8 src/student/.py` - - # TODO: fix the syntax error for the following code - if True: - sentence = "Hello world" - print(sentence) - - # End of TODO (do not change the code below) - return True - - -def task_2( - input_list: list = [1, 4, 53, 27, 9], - target_index: int = 0, - input_dictionary: dict = {"a": " taiwan", "b": 20, "c": "CSIE"}, - target_key: str = "a" -) -> tuple: - ''' - Task 2: Data Types - - Python has many data types, including Boolean, String, Integer, Float, - List, Dictionary, etc. - - You could use the function type() to see the data type: - - >>> type(5) - - >>> type("hi") - - >>> type(9.2) - - >>> type(["list", "could", "include", "different", "data type", 5, 3.2]) - - >>> type(("you could not change elements", "in a tuple")) - - >>> type({"a": 1, "b":20}) - - >>> type(True) - - >>> - - Try to play with the Python IDE to see different data types by yourself. - - In this task, you are asked to use these datatype. - - Args: - input_list: a list with several items - target_index: target index for the input_list. You need to get the - list element with this index (i.e, 'input_list[target_index]') - input_dictionary: a dictionary with several key-value pairs. - target_key: target key for the input_dictionary You need to get the - value with this key (i.e., input_dictionary[target_key]) - - Returns: - input_list_length_and_sentence: a tuple that contains two elements. - The first one is an integer that indicates the length of input_list - The second one is a string that contains the combination of - input_list[target_index] and input_dictionary[target_key] - - Examples: - Inputs: - input_list = [1, 3, 5, 7, 9] - target_index = 0 - input_dictionary = {"1": "8", "f": "abc", "s": 5.5, "5.5" 900} - target_key = "5.5" - - Returns: - input_list_length_and_sentence = (5, "1900") - - Hints: - * Try to use print() to print out the inputs. - * Use len() to get the length of the list. - * Different data types could not be added. Use str() to convert data - to string. - * Run `python src/autograder.py -task 2 -student_id ` - to see if you pass this task. - * The correct output would be (4, '1 taiwan') - ''' - # TODO: change length and sentence to fit the requirement - length = len(input_list) - sentence = str(input_list[target_index]) + input_dictionary[target_key] - # End of TODO - input_list_length_and_sentence = (length, sentence) - print(input_list_length_and_sentence) - return input_list_length_and_sentence - - -def task_3( - number: int = 1314151677777 -) -> list: - ''' - Task 3: Conditions - - Args: - number: a integer input - - Returns: - prime_factors_below_10: a list of the number's largest factors - below 10 - if the number is negative, return [-1] - if the number is zero, return [0] - - Hints: - * Use % to get the remainder - * Using a loop (introduced in the next task) will make some - conditions simpler - ''' - prime_factors_below_10 = [] - # TODO: fill in the conditions - if number < 0: - prime_factors_below_10 = [-1] - # elif stands for "else if" in Python. - elif number == 0: - prime_factors_below_10 = [0] - else: - if number % 2 == 0: - prime_factors_below_10.append(2) - if number % 3 == 0: - prime_factors_below_10.append(3) - if number % 5 == 0: - prime_factors_below_10.append(5) - if number % 7 == 0: - prime_factors_below_10.append(7) - # End of TODO - print(prime_factors_below_10) - return prime_factors_below_10 - - -def task_4( - numbers: list = [2, 4, 5, 6, 9] -) -> list: - ''' - Task 4: For and While Loop - - Args: - numbers: a list of integers - - Returns: - list_of_stars: a list of stars (*) - For each number n in the list, you need to - append n lines of stars to the list, where - the first line has one star, the last line - has n stars. - - Examples: - input: - [1, 3, 5] - output: - ['*', - '*', - '**', - '***', - '*', - '**', - '***', - '****', - '*****'] - - Hints: - * You could create a string with repetitive substring by * - ''' - list_of_stars = [] - # In Python, the for loop could iterate through a list directly - for number in numbers: - # TODO: change stars to correct length - for i in range(1, number+1): - stars = "*" * i - list_of_stars.append(stars) - # End of TODO - - # This could be done by the while loop - list_of_stars_while = [] - i = 0 - while i < len(numbers): - # TODO: change stars to correct length - j = 1 - while j <= numbers[i]: - stars = "*" * j - j += 1 # This line is equivalant to j = j + 1 - list_of_stars_while.append(stars) - i += 1 - # End of TODO - - print("=====> Output list_of_stars") - for stars in list_of_stars: - print(stars) - print("=====> Output list_of_stars_while") - for stars in list_of_stars_while: - print(stars) - - for ans1, ans2 in zip(list_of_stars, list_of_stars_while): - assert ans1 == ans2 - return list_of_stars - - -def task_5( - input_filename: str = 'task_5_input.txt', - output_filename: str = 'task_5_output.txt' -) -> str: - ''' - Task 5: I/O with files - - Args: - input_filename: input filename - output_filename: output filename - - Returns: - lines: content in the output file without commas - - Hints: - * Use .split(something) to split a string into several substring - * Use fout.write(something) to write text into the output file - - ''' - input_filename = os.path.join(TEST_DATA_DIR, input_filename) - output_filename = os.path.join(TEST_DATA_DIR, output_filename) - # Remove previous output file - if os.path.exists(output_filename): - os.remove(output_filename) - - with open(input_filename, 'r') as fin, open(output_filename, 'w') as fout: - lines = fin.readlines() - print(f"=======> Input file content:") - for line in lines: - print(f"{line}") - # TODO: read the content of the input file, where words are separate by - # commas. Please remove the commas and write words to the output file - for line in lines: - new = line.split(",") - for w in new: - fout.write(w) - # End of TODO - - with open(output_filename, 'r') as fin: - lines = fin.readlines() - print(f"=======> Output file content:") - print(lines) - return "".join(lines) - - -def task_6( - matrix: list = [[-0.5, 1], [1, 0.5], [-1, 0.5], [-1, -0.5]], - vector: list = [1, 0.5] -) -> list: - ''' - Task 6: Functions - - Args: - matrix: a list of v1 - vector: v2 - - Returns: - cos_sims: a list of cosine similarity between v1s and v2 - - Hints: - * A good function name should be self-explained - * A good function should be less than 30 lines - * A good function should include comments to explain how to use it - * Cosine similarity of the vector itself will be 0.9999999 instead of 1 - ''' - # You could define function B in function A, but function B could only - # be used in the scope of function A - def dot_product(v1, v2): - assert len(v1) == len(v2) - return sum(a*b for a, b in zip(v1, v2)) - - def norm(vector): - # Note that this function would have some minor error due to the - # approximation of square root - return dot_product(vector, vector) ** 0.5 - - def get_cosine_simialrity(v1, v2): - ''' - Calculate the cosine similarity = v1 * v2 / (|v1| * |v2|) - ''' - # TODO: use the above functions to calculate cosine similarity of - # the two vectors v1 and v2 - cos_sim = dot_product(v1, v2) / (norm(v1) * norm(v2)) - # End of TODO - - return cos_sim - - cos_sims = [] - for v1 in matrix: - cos_sim = get_cosine_simialrity(v1, vector) - print(f"Cosine similarity between {v1} and {vector}: {cos_sim}") - cos_sims.append(cos_sim) - return cos_sims - - -class Student(): - def __init__(self, student_id, time): - self.student_id = student_id - self.time = time - self.words_to_say = "initial value" - - def set_words_to_say(self, words_to_say): - self.words_to_say = words_to_say - - def hello(self): - return ( - f"Hello, {self.student_id}! Time is {self.time}. " - f"I want to say {self.words_to_say}" - ) - - -def task_7( - student_id: str = 'test_id', - time: str = '2018_11_24_0000' -) -> Student: - ''' - Task 7: Class - - Args: - student_id: someone's student ID - time: a certain time - - Returns: - student: an Student object - - Hints: - * Use Student(parameters1, parameters2 ...) to create an object - and assign it to a variable - * Use . to call object function - ''' - # TODO: create a student object with different words to say - student = Student(student_id, time) - student.set_words_to_say("TheAnswerToLifeTheUniverseAndEverything = 42") - # End of TODO - - print(student.hello()) - return student - - -def task_8( - img_url: str = 'https://i.imgur.com/B75zq0x.jpg' -) -> object: - ''' - Task 8: Module - - Args: - img_url: address of an image - - Returns: - result_img: an PIL Image - - Hints: - * Make sure you have installed the PIL package - * Take a look at utils.py first - * You could easily find answers with Google - ''' - from urllib import request - result_img = None - - # TODO: download the image from img_url with the request module - # and add your student ID on it with draw_text() in the utils module - # under src/. - - # You are allowed to change the img_url to your own image URL. - from PIL import Image - import utils - p = request.urlopen(img_url) - result_img = Image.open(p) - result_img = utils.draw_text(result_img, "B07902019") - # Display the image: - # result_img.show() - # Note: please comment this line when hand in. - - # If you are running on a server, use - # result.save('test.jpg') - # and copy the file to local or use Jupyter Notebook to render. - - # End of TODO - - return result_img diff --git a/src/students/B07902037.py b/src/students/B07902037.py deleted file mode 100644 index dda3726e..00000000 --- a/src/students/B07902037.py +++ /dev/null @@ -1,428 +0,0 @@ -''' -This is the sample code from the homework. You shold NOT modify this file. -Instead, please copy this file to src/students/.py and -edit it there. -''' -import os - -# Define global variables with upper case -SRC_PATH = os.path.dirname(os.path.dirname(os.path.realpath(__file__))) -TEST_DATA_DIR = os.path.join(SRC_PATH, 'test_data') - - -def task_1(dummy=None): - ''' - Task 1: Basic Syntax and Flake8 Checker - - Python uses indentations to separate blocks instead of backets. - Unlike most programming language (like C++), indentations in Python - are required. - - See https://www.python-course.eu/python3_blocks.php for some examples. - - Flake8 (http://flake8.pycqa.org/en/latest/) could help you check these - syntax error. It also regular your coding style. For example, using - two whitespaces as indentation is allowed in Python. However, Flake8 - will tell you it is an error "E111: indentation is not a multiple of four". - This is because when many people work on the same project, it would be - confusing if people are using different identation style. - - Following the coding style in Flake8 is strongly suggested. - - ''' - # Hint: - # Run `python autograder.py -task 1 -student_id ` - # under src/ to see if you pass this task. - # The correct output would be "Hello world" without any - # error. Note that passing this task does NOT mean you pass the - # Flake8 chcker. Please check your style with - # `flake8 src/student/.py` - - # TODO: fix the syntax error for the following code - - dummy = None - if True: - sentence="Hello world" - print (sentence) - - # End of TODO (do not change the code below) - return True - - -def task_2( - input_list: list = [1, 4, 53, 27, 9], - target_index: int = 0, - input_dictionary: dict = {"a": " taiwan", "b": 20, "c": "CSIE"}, - target_key: str = "a" -) -> tuple: - ''' - Task 2: Data Types - - Python has many data types, including Boolean, String, Integer, Float, - List, Dictionary, etc. - - You could use the function type() to see the data type: - - >>> type(5) - - >>> type("hi") - - >>> type(9.2) - - >>> type(["list", "could", "include", "different", "data type", 5, 3.2]) - - >>> type(("you could not change elements", "in a tuple")) - - >>> type({"a": 1, "b":20}) - - >>> type(True) - - >>> - - Try to play with the Python IDE to see different data types by yourself. - - In this task, you are asked to use these datatype. - - Args: - input_list: a list with several items - target_index: target index for the input_list. You need to get the - list element with this index (i.e, 'input_list[target_index]') - input_dictionary: a dictionary with several key-value pairs. - target_key: target key for the input_dictionary You need to get the - value with this key (i.e., input_dictionary[target_key]) - - Returns: - input_list_length_and_sentence: a tuple that contains two elements. - The first one is an integer that indicates the length of input_list - The second one is a string that contains the combination of - input_list[target_index] and input_dictionary[target_key] - - Examples: - Inputs: - input_list = [1, 3, 5, 7, 9] - target_index = 0 - input_dictionary = {"1": "8", "f": "abc", "s": 5.5, "5.5" 900} - target_key = "5.5" - - Returns: - input_list_length_and_sentence = (5, "1900") - - Hints: - * Try to use print() to print out the inputs. - * Use len() to get the length of the list. - * Different data types could not be added. Use str() to convert data - to string. - * Run `python src/autograder.py -task 2 -student_id ` - to see if you pass this task. - * The correct output would be (4, '1 taiwan') - ''' - # TODO: change length and sentence to fit the requirement - length = len(input_list) - sentence = str( input_list[ target_index ] ) + str( input_dictionary[ target_key ] ) - # End of TODO - input_list_length_and_sentence = (length, sentence) - print(input_list_length_and_sentence) - return input_list_length_and_sentence - - -def task_3( - number: int = 1314151677777 -) -> list: - ''' - Task 3: Conditions - - Args: - number: a integer input - - Returns: - prime_factors_below_10: a list of the number's largest factors - below 10 - if the number is negative, return [-1] - if the number is zero, return [0] - - Hints: - * Use % to get the remainder - * Using a loop (introduced in the next task) will make some - conditions simpler - ''' - prime_factors_below_10 = [] - # TODO: fill in the conditions - if number < 0: - prime_factors_below_10 = [-1] - # elif stands for "else if" in Python. - elif number == 0: - prime_factors_below_10 = [0] - else: - if number % 2 == 0: - prime_factors_below_10.append(2) - if number % 3 == 0: - prime_factors_below_10.append(3) - if number % 5 == 0: - prime_factors_below_10.append(5) - if number % 7 == 0: - prime_factors_below_10.append(7) - # End of TODO - print(prime_factors_below_10) - return prime_factors_below_10 - - -def task_4( - numbers: list = [2, 4, 5, 6, 9] -) -> list: - ''' - Task 4: For and While Loop - - Args: - numbers: a list of integers - - Returns: - list_of_stars: a list of stars (*) - For each number n in the list, you need to - append n lines of stars to the list, where - the first line has one star, the last line - has n stars. - - Examples: - input: - [1, 3, 5] - output: - ['*', - '*', - '**', - '***', - '*', - '**', - '***', - '****', - '*****'] - - Hints: - * You could create a string with repetitive substring by * - ''' - list_of_stars = [] - # In Python, the for loop could iterate through a list directly - for number in numbers: - # TODO: change stars to correct length - for i in range(1, number+1): - stars = "*" - stars *= i - list_of_stars.append(stars) - # End of TODO - - # This could be done by the while loop - list_of_stars_while = [] - i = 0 - while i < len(numbers): - # TODO: change stars to correct length - j = 1 - while j <= numbers[i]: - stars = "*" - stars *= j - j += 1 # This line is equivalant to j = j + 1 - list_of_stars_while.append(stars) - i += 1 - # End of TODO - - print("=====> Output list_of_stars") - for stars in list_of_stars: - print(stars) - print("=====> Output list_of_stars_while") - for stars in list_of_stars_while: - print(stars) - - for ans1, ans2 in zip(list_of_stars, list_of_stars_while): - assert ans1 == ans2 - return list_of_stars - - -def task_5( - input_filename: str = 'task_5_input.txt', - output_filename: str = 'task_5_output.txt' -) -> str: - ''' - Task 5: I/O with files - - Args: - input_filename: input filename - output_filename: output filename - - Returns: - lines: content in the output file without commas - - Hints: - * Use .split(something) to split a string into several substring - * Use fout.write(something) to write text into the output file - - ''' - input_filename = os.path.join(TEST_DATA_DIR, input_filename) - output_filename = os.path.join(TEST_DATA_DIR, output_filename) - # Remove previous output file - if os.path.exists(output_filename): - os.remove(output_filename) - - with open(input_filename, 'r') as fin, open(output_filename, 'w') as fout: - lines = fin.readlines() - print(f"=======> Input file content:") - for line in lines: - print(f"{line}") - # TODO: read the content of the input file, where words are separate by - # commas. Please remove the commas and write words to the output file - words = line.split(',') - i = 0 - while i < len(words): - fout.write( words[ i ] ) - i += 1 - # End of TODO - - with open(output_filename, 'r') as fin: - lines = fin.readlines() - print(f"=======> Output file content:") - print(lines) - return "".join(lines) - - -def task_6( - matrix: list = [[-0.5, 1], [1, 0.5], [-1, 0.5], [-1, -0.5]], - vector: list = [1, 0.5] -) -> list: - ''' - Task 6: Functions - - Args: - matrix: a list of v1 - vector: v2 - - Returns: - cos_sims: a list of cosine similarity between v1s and v2 - - Hints: - * A good function name should be self-explained - * A good function should be less than 30 lines - * A good function should include comments to explain how to use it - * Cosine similarity of the vector itself will be 0.9999999 instead of 1 - ''' - # You could define function B in function A, but function B could only - # be used in the scope of function A - def dot_product(v1, v2): - assert len(v1) == len(v2) - return sum(a*b for a, b in zip(v1, v2)) - - def norm(vector): - # Note that this function would have some minor error due to the - # approximation of square root - return dot_product(vector, vector) ** 0.5 - - def get_cosine_simialrity(v1, v2): - ''' - Calculate the cosine similarity = v1 * v2 / (|v1| * |v2|) - ''' - # TODO: use the above functions to calculate cosine similarity of - # the two vectors v1 and v2 - cos_sim = dot_product(v1, v2) / (norm(v1) * norm(v2)) - # End of TODO - - return cos_sim - - cos_sims = [] - for v1 in matrix: - cos_sim = get_cosine_simialrity(v1, vector) - print(f"Cosine similarity between {v1} and {vector}: {cos_sim}") - cos_sims.append(cos_sim) - return cos_sims - - -class Student(): - def __init__(self, student_id, time): - self.student_id = student_id - self.time = time - self.words_to_say = "initial value" - - def set_words_to_say(self, words_to_say): - self.words_to_say = words_to_say - - def hello(self): - return ( - f"Hello, {self.student_id}! Time is {self.time}. " - f"I want to say {self.words_to_say}" - ) - - -def task_7( - student_id: str = 'test_id', - time: str = '2018_11_24_0000' -) -> Student: - ''' - Task 7: Class - - Args: - student_id: someone's student ID - time: a certain time - - Returns: - student: an Student object - - Hints: - * Use Student(parameters1, parameters2 ...) to create an object - and assign it to a variable - * Use . to call object function - ''' - # TODO: create a student object with different words to say - - student = Student(student_id,time) - student.set_words_to_say( "it works." ) - - # End of TODO - - print(student.hello()) - return student - - -def task_8( - img_url: str = 'https://i.imgur.com/B75zq0x.jpg' -) -> object: - ''' - Task 8: Module - - Args: - img_url: address of an image - - Returns: - result_img: an PIL Image - - Hints: - * Make sure you have installed the PIL package - * Take a look at utils.py first - * You could easily find answers with Google - ''' - from urllib import request - result_img = None - - # TODO: download the image from img_url with the request module - # and add your student ID on it with draw_text() in the utils module - # under src/. - from PIL import Image, ImageDraw - from utils import draw_text - - request.urlretrieve(img_url, "image.jpg") - result_img = Image.open("image.jpg") - - text = ImageDraw.Draw(result_img) - text.ink = 0 + 0*256 + 255*256*256 - text.text((5, 5), "B07902037") - - #text.save("text.jpg") - #result_img.paste(text, (0, 0)) - - # You are allowed to change the img_url to your own image URL. - - # Display the image: - # result_img.show() - # Note: please comment this line when hand in. - - # If you are running on a server, use - # result_img.save("testing.jpg") - # and copy the file to local or use Jupyter Notebook to render. - - # End of TODO - - return result_img diff --git a/src/students/B07902065.py b/src/students/B07902065.py deleted file mode 100644 index afd38d2f..00000000 --- a/src/students/B07902065.py +++ /dev/null @@ -1,417 +0,0 @@ -''' -This is the sample code from the homework. You shold NOT modify this file. -Instead, please copy this file to src/students/.py and -edit it there. -''' -import os - -# Define global variables with upper case -SRC_PATH = os.path.dirname(os.path.dirname(os.path.realpath(__file__))) -TEST_DATA_DIR = os.path.join(SRC_PATH, 'test_data') - - -def task_1(dummy=None): - ''' - Task 1: Basic Syntax and Flake8 Checker - - Python uses indentations to separate blocks instead of backets. - Unlike most programming language (like C++), indentations in Python - are required. - - See https://www.python-course.eu/python3_blocks.php for some examples. - - Flake8 (http://flake8.pycqa.org/en/latest/) could help you check these - syntax error. It also regular your coding style. For example, using - two whitespaces as indentation is allowed in Python. However, Flake8 - will tell you it is an error "E111: indentation is not a multiple of four". - This is because when many people work on the same project, it would be - confusing if people are using different identation style. - - Following the coding style in Flake8 is strongly suggested. - - ''' - # Hint: - # Run `python src/autograder.py -task 1 -student ` - # to see if you pass this task. - # The correct output would be "Hello world" without any - # error. Note that passing this task does NOT mean you pass the - # Flake8 chcker. Please check your style with - # `flake8 src/student/.py` - - # TODO: fix the syntax error for the following code - if True: - sentence = "Hello world" - print(sentence) - - # End of TODO (do not change the code below) - return True - - -def task_2( - input_list: list = [1, 4, 53, 27, 9], - target_index: int = 0, - input_dictionary: dict = {"a": " taiwan", "b": 20, "c": "CSIE"}, - target_key: str = "a" -) -> tuple: - ''' - Task 2: Data Types - - Python has many data types, including Boolean, String, Integer, Float, - List, Dictionary, etc. - - You could use the function type() to see the data type: - - >>> type(5) - - >>> type("hi") - - >>> type(9.2) - - >>> type(["list", "could", "include", "different", "data type", 5, 3.2]) - - >>> type(("you could not change elements", "in a tuple")) - - >>> type({"a": 1, "b":20}) - - >>> type(True) - - >>> - - Try to play with the Python IDE to see different data types by yourself. - - In this task, you are asked to use these datatype. - - Args: - input_list: a list with several items - target_index: target index for the input_list. You need to get the - list element with this index (i.e, 'input_list[target_index]') - input_dictionary: a dictionary with several key-value pairs. - target_key: target key for the input_dictionary You need to get the - value with this key (i.e., input_dictionary[target_key]) - - Returns: - input_list_length_and_sentence: a tuple that contains two elements. - The first one is an integer that indicates the length of input_list - The second one is a string that contains the combination of - input_list[target_index] and input_dictionary[target_key] - - Examples: - Inputs: - input_list = [1, 3, 5, 7, 9] - target_index = 0 - input_dictionary = {"1": "8", "f": "abc", "s": 5.5, "5.5" 900} - target_key = "5.5" - - Returns: - input_list_length_and_sentence = (5, "1900") - - Hints: - * Try to use print() to print out the inputs. - * Use len() to get the length of the list. - * Different data types could not be added. Use str() to convert data - to string. - * Run `python src/autograder.py -task 2 -student_id ` - to see if you pass this task. - * The correct output would be (5, '1 taiwan') - ''' - # TODO: change length and sentence to fit the requirement - length = len(list) - sentence = str(list[int]) + dict[str] - # End of TODO - input_list_length_and_sentence = (length, sentence) - print(input_list_length_and_sentence) - return input_list_length_and_sentence - - -def task_3( - number: int = 1314151677777 -) -> list: - ''' - Task 3: Conditions - - Args: - number: a integer input - - Returns: - prime_factors_below_10: a list of the number's largest factors - below 10 - if the number is negative, return [-1] - if the number is zero, return [0] - - Hints: - * Use % to get the remainder - * Using a loop (introduced in the next task) will make some - conditions simpler - ''' - prime_factors_below_10 = [] - # TODO: fill in the conditions - if int < 0: - prime_factors_below_10 = [-1] - # elif stands for "else if" in Python. - elif int == 0: - prime_factors_below_10 = [0] - else: - if int % 2 == 0: - prime_factors_below_10.append(2) - if int % 3 == 0: - prime_factors_below_10.append(3) - if int % 5 == 0: - prime_factors_below_10.append(5) - if int % 7 == 1: - prime_factors_below_10.append(7) - # End of TODO - print(prime_factors_below_10) - return prime_factors_below_10 - - -def task_4( - numbers: list = [2, 4, 5, 6, 9] -) -> list: - ''' - Task 4: For and While Loop - - Args: - numbers: a list of integers - - Returns: - list_of_stars: a list of stars (*) - For each number n in the list, you need to - append n lines of stars to the list, where - the first line has one star, the last line - has n stars. - - Examples: - input: - [1, 3, 5] - output: - ['*', - '*', - '**', - '***', - '*', - '**', - '***', - '****', - '*****'] - - Hints: - * You could create a string with repetitive substring by * - ''' - list_of_stars = [] - # In Python, the for loop could iterate through a list directly - for number in numbers: - # TODO: change stars to correct length - for i in range(1, number+1): - stars = "*" - list_of_stars.append(stars*i) - # End of TODO - - # This could be done by the while loop - list_of_stars_while = [] - i = 0 - while i < len(numbers): - # TODO: change stars to correct length - j = 0 - while j <= numbers[i]: - stars = "*" - j += 1 # This line is equivalant to j = j + 1 - list_of_stars_while.append(stars*j) - i += 1 - # End of TODO - - print("=====> Output list_of_stars") - for stars in list_of_stars: - print(stars) - print("=====> Output list_of_stars_while") - for stars in list_of_stars_while: - print(stars) - - for ans1, ans2 in zip(list_of_stars, list_of_stars_while): - assert ans1 == ans2 - return list_of_stars - - -def task_5( - input_filename: str = 'task_5_input.txt', - output_filename: str = 'task_5_output.txt' -) -> str: - ''' - Task 5: I/O with files - - Args: - input_filename: input filename - output_filename: output filename - - Returns: - lines: content in the output file without commas - - Hints: - * Use .split(something) to split a string into several substring - * Use fout.write(something) to write text into the output file - - ''' - input_filename = os.path.join(TEST_DATA_DIR, input_filename) - output_filename = os.path.join(TEST_DATA_DIR, output_filename) - # Remove previous output file - if os.path.exists(output_filename): - os.remove(output_filename) - - with open(input_filename, 'r') as fin, open(output_filename, 'w') as fout: - lines = fin.readlines() - print(f"=======> Input file content:") - for line in lines: - print(f"{line}") - # TODO: read the content of the input file, where words are separate by - # commas. Please remove the commas and write words to the output file - for line in lines: - splt = line.split(";") - for elmnt in splt: - fout.write(elmnt) - fout.write("\n") - # End of TODO - - with open(output_filename, 'r') as fin: - lines = fin.readlines() - print(f"=======> Output file content:") - print(lines) - return "".join(lines) - - -def task_6( - matrix: list = [[-0.5, 1], [1, 0.5], [-1, 0.5], [-1, -0.5]], - vector: list = [1, 0.5] -) -> list: - ''' - Task 6: Functions - - Args: - matrix: a list of v1 - vector: v2 - - Returns: - cos_sims: a list of cosine similarity between v1s and v2 - - Hints: - * A good function name should be self-explained - * A good function should be less than 30 lines - * A good function should include comments to explain how to use it - * Cosine similarity of the vector itself will be 0.9999999 instead of 1 - ''' - # You could define function B in function A, but function B could only - # be used in the scope of function A - def dot_product(v1, v2): - assert len(v1) == len(v2) - return sum(a*b for a, b in zip(v1, v2)) - - def norm(vector): - # Note that this function would have some minor error due to the - # approximation of square root - return dot_product(vector, vector) ** 0.5 - - def get_cosine_simialrity(v1, v2): - ''' - Calculate the cosine similarity = v1 * v2 / (|v1| * |v2|) - ''' - # TODO: use the above functions to calculate cosine similarity of - # the two vectors v1 and v2 - cos_sim = dot_product(v1,v2) / (norm(v1) * norm(v2)) - # End of TODO - - return cos_sim - - cos_sims = [] - for v1 in matrix: - cos_sim = get_cosine_simialrity(v1, vector) - print(f"Cosine similarity between {v1} and {vector}: {cos_sim}") - cos_sims.append(cos_sim) - return cos_sims - - -class Student(): - def __init__(self, student_id, time): - self.student_id = student_id - self.time = time - self.words_to_say = "initial value" - - def set_words_to_say(self, words_to_say): - self.words_to_say = words_to_say - - def hello(self): - return ( - f"Hello, {self.student_id}! Time is {self.time}. " - f"I want to say {self.words_to_say}" - ) - - -def task_7( - student_id: str = 'test_id', - time: str = '2018_11_24_0000' -) -> Student: - ''' - Task 7: Class - - Args: - student_id: someone's student ID - time: a certain time - - Returns: - student: an Student object - - Hints: - * Use Student(parameters1, parameters2 ...) to create an object - and assign it to a variable - * Use . to call object function - ''' - # TODO: create a student object with different words to say - student = Student(student_id, time) - student.set_words_to_say("some random rambling.") - # End of TODO - - print(student.hello()) - return student - - -def task_8( - img_url: str = 'https://i.imgur.com/B75zq0x.jpg' -) -> object: - ''' - Task 8: Module - - Args: - img_url: address of an image - - Returns: - result_img: an PIL Image - - Hints: - * Make sure you have installed the PIL package - * Take a look at utils.py first - * You could easily find answers with Google - ''' - from urllib import request - result_img = None - - # TODO: download the image from img_url with the request module - from PIL import Image - from utils.py import draw_text - img = request.get(img_url) - - - # and add your student ID on it with draw_name() in the utils module - # under src/. - result_img = draw_text(img, "B07902065", (10, 10), (0.457, 0.730, 0.988)) - - - # You are allowed to change the img_url to your own image URL. - - # Display the image: - # result_img.show() - # Note: please comment this line when hand in - - # If you are running on a server, use - # result.save('test.jpg') - # and copy the file to local or use Jupyter Notebook to render. - - # End of TODO - - return result_img diff --git a/src/students/b04505049.py b/src/students/b04505049.py deleted file mode 100644 index 70c48fef..00000000 --- a/src/students/b04505049.py +++ /dev/null @@ -1,415 +0,0 @@ -''' -This is the sample code from the homework. You shold NOT modify this file. -Instead, please copy this file to src/students/.py and -edit it there. -''' -import os - -# Define global variables with upper case -SRC_PATH = os.path.dirname(os.path.dirname(os.path.realpath(__file__))) -TEST_DATA_DIR = os.path.join(SRC_PATH, 'test_data') - - -def task_1(dummy=None): - ''' - Task 1: Basic Syntax and Flake8 Checker - - Python uses indentations to separate blocks instead of backets. - Unlike most programming language (like C++), indentations in Python - are required. - - See https://www.python-course.eu/python3_blocks.php for some examples. - - Flake8 (http://flake8.pycqa.org/en/latest/) could help you check these - syntax error. It also regular your coding style. For example, using - two whitespaces as indentation is allowed in Python. However, Flake8 - will tell you it is an error "E111: indentation is not a multiple of four". - This is because when many people work on the same project, it would be - confusing if people are using different identation style. - - Following the coding style in Flake8 is strongly suggested. - - ''' - # Hint: - # Run `python src/autograder.py -task 1 -student_id ` - # to see if you pass this task. - # The correct output would be "Hello world" without any - # error. Note that passing this task does NOT mean you pass the - # Flake8 chcker. Please check your style with - # `flake8 src/student/.py` - - # TODO: fix the syntax error for the following code - if True: - sentence = "Hello world" - print(sentence) - - # End of TODO (do not change the code below) - return True - - -def task_2( - input_list: list = [1, 4, 53, 27, 9], - target_index: int = 0, - input_dictionary: dict = {"a": " taiwan", "b": 20, "c": "CSIE"}, - target_key: str = "a" -) -> tuple: - ''' - Task 2: Data Types - - Python has many data types, including Boolean, String, Integer, Float, - List, Dictionary, etc. - - You could use the function type() to see the data type: - - >>> type(5) - - >>> type("hi") - - >>> type(9.2) - - >>> type(["list", "could", "include", "different", "data type", 5, 3.2]) - - >>> type(("you could not change elements", "in a tuple")) - - >>> type({"a": 1, "b":20}) - - >>> type(True) - - >>> - - Try to play with the Python IDE to see different data types by yourself. - - In this task, you are asked to use these datatype. - - Args: - input_list: a list with several items - target_index: target index for the input_list. You need to get the - list element with this index (i.e, 'input_list[target_index]') - input_dictionary: a dictionary with several key-value pairs. - target_key: target key for the input_dictionary You need to get the - value with this key (i.e., input_dictionary[target_key]) - - Returns: - input_list_length_and_sentence: a tuple that contains two elements. - The first one is an integer that indicates the length of input_list - The second one is a string that contains the combination of - input_list[target_index] and input_dictionary[target_key] - - Examples: - Inputs: - input_list = [1, 3, 5, 7, 9] - target_index = 0 - input_dictionary = {"1": "8", "f": "abc", "s": 5.5, "5.5" 900} - target_key = "5.5" - - Returns: - input_list_length_and_sentence = (5, "1900") - - Hints: - * Try to use print() to print out the inputs. - * Use len() to get the length of the list. - * Different data types could not be added. Use str() to convert data - to string. - * Run `python src/autograder.py -task 2 -student_id ` - to see if you pass this task. - * The correct output would be (5, '1 taiwan') - ''' - # TODO: change length and sentence to fit the requirement - - length = len(input_list) - sentence = str(input_list[target_index])+str(input_dictionary[target_key]) - - # End of TODO - input_list_length_and_sentence = (length, sentence) - print(input_list_length_and_sentence) - return input_list_length_and_sentence - - -def task_3( - number: int = 1314151677777 -) -> list: - ''' - Task 3: Conditions - - Args: - number: a integer input - - Returns: - prime_factors_below_10: a list of the number's largest factors - below 10 - if the number is negative, return [-1] - if the number is zero, return [0] - - Hints: - * Use % to get the remainder - * Using a loop (introduced in the next task) will make some - conditions simpler - ''' - prime_factors_below_10 = [] - # TODO: fill in the conditions - if number < 0: - prime_factors_below_10 = [-1] - # elif stands for "else if" in Python. - elif number == 0: - prime_factors_below_10 = [0] - else: - if number % 2 == 0: - prime_factors_below_10.append(2) - if number % 3 == 0: - prime_factors_below_10.append(3) - if number % 5 == 0: - prime_factors_below_10.append(5) - if number % 7 == 0: - prime_factors_below_10.append(7) - # End of TODO - print(prime_factors_below_10) - return prime_factors_below_10 - - -def task_4( - numbers: list = [2, 4, 5, 6, 9] -) -> list: - ''' - Task 4: For and While Loop - - Args: - numbers: a list of integers - - Returns: - list_of_stars: a list of stars (*) - For each number n in the list, you need to - append n lines of stars to the list, where - the first line has one star, the last line - has n stars. - - Examples: - input: - [1, 3, 5] - output: - ['*', - '*', - '**', - '***', - '*', - '**', - '***', - '****', - '*****'] - - Hints: - * You could create a string with repetitive substring by * - ''' - list_of_stars = [] - # In Python, the for loop could iterate through a list directly - for number in numbers: - # TODO: change stars to correct length - for i in range(1, number + 1): - stars = "*" * i - list_of_stars.append(stars) - # End of TODO - - # This could be done by the while loop - list_of_stars_while = [] - i = 0 - while i < len(numbers): - # TODO: change stars to correct length - j = 1 - while j <= numbers[i]: - stars = "*" * j - j += 1 # This line is equivalant to j = j + 1 - list_of_stars_while.append(stars) - i += 1 - # End of TODO - - print("=====> Output list_of_stars") - for stars in list_of_stars: - print(stars) - print("=====> Output list_of_stars_while") - for stars in list_of_stars_while: - print(stars) - - for ans1, ans2 in zip(list_of_stars, list_of_stars_while): - assert ans1 == ans2 - return list_of_stars - - -def task_5( - input_filename: str = 'task_5_input.txt', - output_filename: str = 'task_5_output.txt' -) -> str: - ''' - Task 5: I/O with files - - Args: - input_filename: input filename - output_filename: output filename - - Returns: - lines: content in the output file without commas - - Hints: - * Use .split(something) to split a string into several substring - * Use fout.write(something) to write text into the output file - - ''' - input_filename = os.path.join(TEST_DATA_DIR, input_filename) - output_filename = os.path.join(TEST_DATA_DIR, output_filename) - # Remove previous output file - if os.path.exists(output_filename): - os.remove(output_filename) - - with open(input_filename, 'r') as fin, open(output_filename, 'w') as fout: - lines = fin.readlines() - print(f"=======> Input file content:") - for line in lines: - print(f"{line}") - tempList = line.split(',') - for temp in tempList: - fout.write(temp) - # TODO: read the content of the input file, where words are separate by - # commas. Please remove the commas and write words to the output file - line = line.replace(",","") - fout.write(line) - #output - - # End of TODO - - with open(output_filename, 'r') as fin: - lines = fin.readlines() - print(f"=======> Output file content:") - print(lines) - return "".join(lines) - - -def task_6( - matrix: list = [[-0.5, 1], [1, 0.5], [-1, 0.5], [-1, -0.5]], - vector: list = [1, 0.5] -) -> list: - ''' - Task 6: Functions - - Args: - matrix: a list of v1 - vector: v2 - - Returns: - cos_sims: a list of cosine similarity between v1s and v2 - - Hints: - * A good function name should be self-explained - * A good function should be less than 30 lines - * A good function should include comments to explain how to use it - * Cosine similarity of the vector itself will be 0.9999999 instead of 1 - ''' - # You could define function B in function A, but function B could only - # be used in the scope of function A - def dot_product(v1, v2): - assert len(v1) == len(v2) - return sum(a*b for a, b in zip(v1, v2)) - - def norm(vector): - # Note that this function would have some minor error due to the - # approximation of square root - return dot_product(vector, vector) ** 0.5 - - def get_cosine_simialrity(v1, v2): - ''' - Calculate the cosine similarity = v1 * v2 / (|v1| * |v2|) - ''' - # TODO: use the above functions to calculate cosine similarity of - # the two vectors v1 and v2 - cos_sim = dot_product(v1, v2) / (norm(v1) * norm(v2)) - # End of TODO - - return cos_sim - - cos_sims = [] - for v1 in matrix: - cos_sim = get_cosine_simialrity(v1, vector) - print(f"Cosine similarity between {v1} and {vector}: {cos_sim}") - cos_sims.append(cos_sim) - return cos_sims - - -class Student(): - def __init__(self, student_id, time): - self.student_id = student_id - self.time = time - self.words_to_say = "initial value" - - def set_words_to_say(self, words_to_say): - self.words_to_say = words_to_say - - def hello(self): - return ( - f"Hello, {self.student_id}! Time is {self.time}. " - f"I want to say {self.words_to_say}" - ) - - -def task_7( - student_id: str = 'test_id', - time: str = '2018_11_24_0000' -) -> Student: - ''' - Task 7: Class - - Args: - student_id: someone's student ID - time: a certain time - - Returns: - student: an Student object - - Hints: - * Use Student(parameters1, parameters2 ...) to create an object - and assign it to a variable - * Use . to call object function - ''' - # TODO: create a student object with different words to say - student = Student(student_id, time) - student.set_words_to_say("I just want to guard anyone") - # End of TODO - - print(student.hello()) - return student - - -def task_8( - img_url: str = 'https://imgur.com/t0yCtDw.jpg' -) -> object: - ''' - Task 8: Module - - Args: - img_url: address of an image - - Returns: - result_img: an PIL Image - - Hints: - * Make sure you have installed the PIL package - * Take a look at utils.py first - * You could easily find answers with Google - ''' - from urllib import request - result_img = None - import utils - import io - # TODO: download the image from img_url with the request module - # and add your student ID on it with draw_name() in the utils module - # under src/. - #參考網絡code完成fix - from PIL import Image - Arianrhod=request.urlopen(img_url).read() - result_img = Image.open(io.BytesIO(Arianrhod)) - result_img = utils.draw_text(result_img, "b04505049") - # You are allowed to change the img_url to your own image URL. - # Display the image: - #result_img.show() - # Note: please comment this line when hand in. - # If you are running on a server, use - # result.save('test.jpg') - # and copy the file to local or use Jupyter Notebook to render. - # End of TODO - return result_img diff --git a/src/students/b05202077.py b/src/students/b05202077.py deleted file mode 100644 index c20b7786..00000000 --- a/src/students/b05202077.py +++ /dev/null @@ -1,428 +0,0 @@ -''' -This is the sample code from the homework. You shold NOT modify this file. -Instead, please copy this file to src/students/.py and -edit it there. -''' -import os - -# Define global variables with upper case -SRC_PATH = os.path.dirname(os.path.dirname(os.path.realpath(__file__))) -TEST_DATA_DIR = os.path.join(SRC_PATH, 'test_data') - - -def task_1(dummy=None): - ''' - Task 1: Basic Syntax and Flake8 Checker - - Python uses indentations to separate blocks instead of backets. - Unlike most programming language (like C++), indentations in Python - are required. - - See https://www.python-course.eu/python3_blocks.php for some examples. - - Flake8 (http://flake8.pycqa.org/en/latest/) could help you check these - syntax error. It also regular your coding style. For example, using - two whitespaces as indentation is allowed in Python. However, Flake8 - will tell you it is an error "E111: indentation is not a multiple of four". - This is because when many people work on the same project, it would be - confusing if people are using different identation style. - - Following the coding style in Flake8 is strongly suggested. - - ''' - # Hint: - # Run `python autograder.py -task 1 -student_id ` - # under src/ to see if you pass this task. - # The correct output would be "Hello world" without any - # error. Note that passing this task does NOT mean you pass the - # Flake8 chcker. Please check your style with - # `flake8 src/student/.py` - - # TODO: fix the syntax error for the following code - if True: - sentence="Hello world" - print (sentence) - - # End of TODO (do not change the code below) - return True - - -def task_2( - input_list: list = [1, 4, 53, 27, 9], - target_index: int = 0, - input_dictionary: dict = {"a": " taiwan", "b": 20, "c": "CSIE"}, - target_key: str = "a" -) -> tuple: - ''' - Task 2: Data Types - - Python has many data types, including Boolean, String, Integer, Float, - List, Dictionary, etc. - - You could use the function type() to see the data type: - - >>> type(5) - - >>> type("hi") - - >>> type(9.2) - - >>> type(["list", "could", "include", "different", "data type", 5, 3.2]) - - >>> type(("you could not change elements", "in a tuple")) - - >>> type({"a": 1, "b":20}) - - >>> type(True) - - >>> - - Try to play with the Python IDE to see different data types by yourself. - - In this task, you are asked to use these datatype. - - Args: - input_list: a list with several items - target_index: target index for the input_list. You need to get the - list element with this index (i.e, 'input_list[target_index]') - input_dictionary: a dictionary with several key-value pairs. - target_key: target key for the input_dictionary You need to get the - value with this key (i.e., input_dictionary[target_key]) - - Returns: - input_list_length_and_sentence: a tuple that contains two elements. - The first one is an integer that indicates the length of input_list - The second one is a string that contains the combination of - input_list[target_index] and input_dictionary[target_key] - - Examples: - Inputs: - input_list = [1, 3, 5, 7, 9] - target_index = 0 - input_dictionary = {"1": "8", "f": "abc", "s": 5.5, "5.5": 1900} - target_key = "5.5" - - Returns: - input_list_length_and_sentence = (5, "1900") - - Hints: - * Try to use print() to print out the inputs. - * Use len() to get the length of the list. - * Different data types could not be added. Use str() to convert data - to string. - * Run `python src/autograder.py -task 2 -student_id ` - to see if you pass this task. - * The correct output would be (5, '1 taiwan') - ''' - # TODO: change length and sentence to fit the requirement - length = len(input_list) - sentence = str(input_list[target_index]) + str(input_dictionary["a"]) - # End of TODO - input_list_length_and_sentence = (length, sentence) - print(input_list_length_and_sentence) - return input_list_length_and_sentence - - -def task_3( - number: int = 1314151677777 -) -> list: - ''' - Task 3: Conditions - - Args: - number: a integer input - - Returns: - prime_factors_below_10: a list of the number's largest factors - below 10 - if the number is negative, return [-1] - if the number is zero, return [0] - - Hints: - * Use % to get the remainder - * Using a loop (introduced in the next task) will make some - conditions simpler - ''' - prime_factors_below_10 = [] - # TODO: fill in the conditions - if number < 0 : - prime_factors_below_10 = [-1] - # elif stands for "else if" in Python. - elif number == 0: - prime_factors_below_10 = [0] - else: - if number % 2 == 0: - prime_factors_below_10.append(2) - if number % 3 == 0: - prime_factors_below_10.append(3) - if number % 5 == 0: - prime_factors_below_10.append(5) - if number % 7 == 0: - prime_factors_below_10.append(7) - # End of TODO - print(prime_factors_below_10) - return prime_factors_below_10 - - -def task_4( - numbers: list = [2, 4, 5, 6, 9] -) -> list: - ''' - Task 4: For and While Loop - - Args: - numbers: a list of integers - - Returns: - list_of_stars: a list of stars (*) - For each number n in the list, you need to - append n lines of stars to the list, where - the first line has one star, the last line - has n stars. - - Examples: - input: - [1, 3, 5] - output: - ['*', - '*', - '**', - '***', - '*', - '**', - '***', - '****', - '*****'] - - Hints: - * You could create a string with repetitive substring by * - ''' - list_of_stars = [] - stars = "" - # In Python, the for loop could iterate through a list directly - for number in numbers: - # TODO: change stars to correct length - for i in range(1, number+1): - for j in range(1,i+1): - stars += "*" - list_of_stars.append(stars) - stars = "" - # End of TODO - - # This could be done by the while loop - list_of_stars_while = [] - i = 0; - star = "" - while i < len(numbers): - # TODO: change stars to correct length - j = 1 - while j <= numbers[i]: - stars += "*"# This line is equivalant to j = j + 1 - list_of_stars_while.append(stars) - j += 1; - stars = ""; - i += 1 - # End of TODO - - print("=====> Output list_of_stars") - for stars in list_of_stars: - print(stars) - print("=====> Output list_of_stars_while") - for stars in list_of_stars_while: - print(stars) - - for ans1, ans2 in zip(list_of_stars, list_of_stars_while): - assert ans1 == ans2 - return list_of_stars - - -def task_5( - input_filename: str = 'task_5_input.txt', - output_filename: str = 'task_5_output.txt' -) -> str: - ''' - Task 5: I/O with files - - Args: - input_filename: input filename - output_filename: output filename - - Returns: - lines: content in the output file without commas - - Hints: - * Use .split(something) to split a string into several substring - * Use fout.write(something) to write text into the output file - - ''' - input_filename = os.path.join(TEST_DATA_DIR, input_filename) - output_filename = os.path.join(TEST_DATA_DIR, output_filename) - # Remove previous output file - if os.path.exists(output_filename): - os.remove(output_filename) - - with open(input_filename, 'r') as fin, open(output_filename, 'w') as fout: - lines = fin.readlines() - print(f"=======> Input file content:") - for line in lines: - print(f"{line}") - print(line); - l = line.replace(",","") - fout.write(l); - # TODO: read the content of the input file, where words are separate by - # commas. Please remove the commas and write words to the output file - - pass - # End of TODO - - with open(output_filename, 'r') as fin: - lines = fin.readlines() - print(f"=======> Output file content:") - print(lines) - return "".join(lines) - - -def task_6( - matrix: list = [[-0.5, 1], [1, 0.5], [-1, 0.5], [-1, -0.5]], - vector: list = [1, 0.5] -) -> list: - ''' - Task 6: Functions - - Args: - matrix: a list of v1 - vector: v2 - - Returns: - cos_sims: a list of cosine similarity between v1s and v2 - - Hints: - * A good function name should be self-explained - * A good function should be less than 30 lines - * A good function should include comments to explain how to use it - * Cosine similarity of the vector itself will be 0.9999999 instead of 1 - ''' - # You could define function B in function A, but function B could only - # be used in the scope of function A - def dot_product(v1, v2): - assert len(v1) == len(v2) - return sum(a*b for a, b in zip(v1, v2)) - - def norm(vector): - # Note that this function would have some minor error due to the - # approximation of square root - return dot_product(vector, vector) ** 0.5 - - def get_cosine_simialrity(v1, v2): - ''' - Calculate the cosine similarity = v1 * v2 / (|v1| * |v2|) - ''' - # TODO: use the above functions to calculate cosine similarity of - # the two vectors v1 and v2 - product = dot_product(v1,v2) - Norm_1 = norm(v1) - Norm_2 = norm(v2) - cos_sim = product/(Norm_1 * Norm_2) - # End of TODO - - return cos_sim - - cos_sims = [] - for v1 in matrix: - cos_sim = get_cosine_simialrity(v1, vector) - print(f"Cosine similarity between {v1} and {vector}: {cos_sim}") - cos_sims.append(cos_sim) - return cos_sims - - -class Student(): - def __init__(self, student_id, time): - self.student_id = student_id - self.time = time - self.words_to_say = "initial value" - - def set_words_to_say(self, words_to_say): - self.words_to_say = words_to_say - - def hello(self): - return ( - f"Hello, {self.student_id}! Time is {self.time}. " - f"I want to say {self.words_to_say}" - ) - - -def task_7( - student_id: str = 'test_id', - time: str = '2018_11_24_0000' -) -> Student: - ''' - Task 7: Class - - Args: - student_id: someone's student ID - time: a certain time - - Returns: - student: an Student object - - Hints: - * Use Student(parameters1, parameters2 ...) to create an object - and assign it to a variable - * Use . to call object function - ''' - # TODO: create a student object with different words to say - - - - - student = Student(student_id,time) - # End of TODO - student.set_words_to_say("Task 7 done!") - student.set_words_to_say("Are these boxes?") - print(student.hello()) - return student - - -def task_8( - img_url: str = 'https://i.imgur.com/B75zq0x.jpg' -) -> object: - ''' - Task 8: Module - - Args: - img_url: address of an image - - Returns: - result_img: an PIL Image - - Hints: - * Make sure you have installed the PIL package - * Take a look at utils.py first - * You could easily find answers with Google - ''' - from urllib import request - result_img = None - - # TODO: download the image from img_url with the request module - # and add your student ID on it with draw_name() in the utils module - # under src/. - from PIL import Image - import utils - import io - store = request.urlopen(img_url) - result_img = Image.open(io.BytesIO(store.read())) - result_img = utils.draw_text(result_img, "B05202077") - - # You are allowed to change the img_url to your own image URL. - - # Display the image: - # result_img.show() - # Note: please comment this line when hand in. - - # If you are running on a server, use - # result.save('test.jpg') - # and copy the file to local or use Jupyter Notebook to render. - - # End of TODO - - return result_img diff --git a/src/students/b06902007.py b/src/students/b06902007.py deleted file mode 100644 index e841cd2c..00000000 --- a/src/students/b06902007.py +++ /dev/null @@ -1,417 +0,0 @@ -''' -This is the sample code from the homework. You shold NOT modify this file. -Instead, please copy this file to src/students/.py and -edit it there. -''' -import os - -# Define global variables with upper case -SRC_PATH = os.path.dirname(os.path.dirname(os.path.realpath(__file__))) -TEST_DATA_DIR = os.path.join(SRC_PATH, 'test_data') - - -def task_1(dummy=None): - ''' - Task 1: Basic Syntax and Flake8 Checker - - Python uses indentations to separate blocks instead of backets. - Unlike most programming language (like C++), indentations in Python - are required. - - See https://www.python-course.eu/python3_blocks.php for some examples. - - Flake8 (http://flake8.pycqa.org/en/latest/) could help you check these - syntax error. It also regular your coding style. For example, using - two whitespaces as indentation is allowed in Python. However, Flake8 - will tell you it is an error "E111: indentation is not a multiple of four". - This is because when many people work on the same project, it would be - confusing if people are using different identation style. - - Following the coding style in Flake8 is strongly suggested. - - ''' - # Hint: - # Run `python src/autograder.py -task 1 -student ` - # to see if you pass this task. - # The correct output would be "Hello world" without any - # error. Note that passing this task does NOT mean you pass the - # Flake8 chcker. Please check your style with - # `flake8 src/student/.py` - - # TODO: fix the syntax error for the following code - if True: - sentence = "Hello world" - print(sentence) - - # End of TODO (do not change the code below) - return True - - -def task_2( - input_list: list = [1, 4, 53, 27, 9], - target_index: int = 0, - input_dictionary: dict = {"a": " taiwan", "b": 20, "c": "CSIE"}, - target_key: str = "a" -) -> tuple: - ''' - Task 2: Data Types - - Python has many data types, including Boolean, String, Integer, Float, - List, Dictionary, etc. - - You could use the function type() to see the data type: - - >>> type(5) - - >>> type("hi") - - >>> type(9.2) - - >>> type(["list", "could", "include", "different", "data type", 5, 3.2]) - - >>> type(("you could not change elements", "in a tuple")) - - >>> type({"a": 1, "b":20}) - - >>> type(True) - - >>> - - Try to play with the Python IDE to see different data types by yourself. - - In this task, you are asked to use these datatype. - - Args: - input_list: a list with several items - target_index: target index for the input_list. You need to get the - list element with this index (i.e, 'input_list[target_index]') - input_dictionary: a dictionary with several key-value pairs. - target_key: target key for the input_dictionary You need to get the - value with this key (i.e., input_dictionary[target_key]) - - Returns: - input_list_length_and_sentence: a tuple that contains two elements. - The first one is an integer that indicates the length of input_list - The second one is a string that contains the combination of - input_list[target_index] and input_dictionary[target_key] - - Examples: - Inputs: - input_list = [1, 3, 5, 7, 9] - target_index = 0 - input_dictionary = {"1": "8", "f": "abc", "s": 5.5, "5.5" 900} - target_key = "5.5" - - Returns: - input_list_length_and_sentence = (5, "1900") - - Hints: - * Try to use print() to print out the inputs. - * Use len() to get the length of the list. - * Different data types could not be added. Use str() to convert data - to string. - * Run `python src/autograder.py -task 2 -student_id ` - to see if you pass this task. - * The correct output would be (5, '1 taiwan') - ''' - # TODO: change length and sentence to fit the requirement - length = len(input_list) - sentence = str( - str(input_list[target_index]) + input_dictionary[target_key]) - # End of TODO - input_list_length_and_sentence = (length, sentence) - print(input_list_length_and_sentence) - return input_list_length_and_sentence - - -def task_3( - number: int = 1314151677777 -) -> list: - ''' - Task 3: Conditions - - Args: - number: a integer input - - Returns: - prime_factors_below_10: a list of the number's largest factors - below 10 - if the number is negative, return [-1] - if the number is zero, return [0] - - Hints: - * Use % to get the remainder - * Using a loop (introduced in the next task) will make some - conditions simpler - ''' - prime_factors_below_10 = [] - # TODO: fill in the conditions - if number < 0: - prime_factors_below_10 = [-1] - # elif stands for "else if" in Python. - elif number == 0: - prime_factors_below_10 = [0] - else: - if number % 2 == 0 and number % 3 and number % 5 and number % 7: - prime_factors_below_10.append(2) - if number % 3 == 0 and number % 5 and number % 7: - prime_factors_below_10.append(3) - if number % 5 == 0 and number % 7: - prime_factors_below_10.append(5) - if number % 7 == 0: - prime_factors_below_10.append(7) - # End of TODO - return prime_factors_below_10 - - -def task_4( - numbers: list = [2, 4, 5, 6, 9] -) -> list: - ''' - Task 4: For and While Loop - - Args: - numbers: a list of integers - - Returns: - list_of_stars: a list of stars (*) - For each number n in the list, you need to - append n lines of stars to the list, where - the first line has one star, the last line - has n stars. - - Examples: - input: - [1, 3, 5] - output: - ['*', - '*', - '**', - '***', - '*', - '**', - '***', - '****', - '*****'] - - Hints: - * You could create a string with repetitive substring by * - ''' - list_of_stars = [] - # In Python, the for loop could iterate through a list directly - for number in numbers: - # TODO: change stars to correct length - for i in range(1, number+1): - stars = "*" - list_of_stars.append(stars * i) - # End of TODO - - # This could be done by the while loop - list_of_stars_while = [] - i = 0 - while i < len(numbers): - # TODO: change stars to correct length - j = 1 - while j <= numbers[i]: - stars = "*" - j += 1 # This line is equivalant to j = j + 1 - list_of_stars_while.append(stars * (j - 1)) - i += 1 - # End of TODO - - print("=====> Output list_of_stars") - for stars in list_of_stars: - print(stars) - print("=====> Output list_of_stars_while") - for stars in list_of_stars_while: - print(stars) - - for ans1, ans2 in zip(list_of_stars, list_of_stars_while): - assert ans1 == ans2 - return list_of_stars - - -def task_5( - input_filename: str = 'task_5_input.txt', - output_filename: str = 'task_5_output.txt' -) -> str: - ''' - Task 5: I/O with files - Args: - input_filename: input filename - output_filename: output filename - Returns: - lines: content in the output file without commas - Hints: - * Use .split(something) to split a string into several substring - * Use fout.write(something) to write text into the output file - ''' - input_filename = os.path.join(TEST_DATA_DIR, input_filename) - output_filename = os.path.join(TEST_DATA_DIR, output_filename) - # Remove previous output file - if os.path.exists(output_filename): - os.remove(output_filename) - - with open(input_filename, 'r') as fin, open(output_filename, 'w') as fout: - lines = fin.readlines() - print(f"=======> Input file content:") - for line in lines: - print(f"{line}") - # TODO: read the content of the input file, where words are separate by - # commas. Please remove the commas and write words to the output file - for c in line: - if c != ",": - fout.write(c) - pass - # End of TODO - - with open(output_filename, 'r') as fin: - lines = fin.readlines() - print(f"=======> Output file content:") - print(lines) - return "".join(lines) - - -def task_6( - matrix: list = [[-0.5, 1], [1, 0.5], [-1, 0.5], [-1, -0.5]], - vector: list = [1, 0.5] -) -> list: - ''' - Task 6: Functions - - Args: - matrix: a list of v1 - vector: v2 - - Returns: - cos_sims: a list of cosine similarity between v1s and v2 - - Hints: - * A good function name should be self-explained - * A good function should be less than 30 lines - * A good function should include comments to explain how to use it - * Cosine similarity of the vector itself will be 0.9999999 instead of 1 - ''' - # You could define function B in function A, but function B could only - # be used in the scope of function A - def dot_product(v1, v2): - assert len(v1) == len(v2) - return sum(a*b for a, b in zip(v1, v2)) - - def norm(vector): - # Note that this function would have some minor error due to the - # approximation of square root - return dot_product(vector, vector) ** 0.5 - - def get_cosine_simialrity(v1, v2): - ''' - Calculate the cosine similarity = v1 * v2 / (|v1| * |v2|) - ''' - # TODO: use the above functions to calculate cosine similarity of - # the two vectors v1 and v2 - cos_sim = 0 - cos_sim = dot_product(v1, v2)/(norm(v1) * norm(v2)) - if v1 == v2: - cos_sim = 0.9999999 - # End of TODO - - return cos_sim - - cos_sims = [] - for v1 in matrix: - cos_sim = get_cosine_simialrity(v1, vector) - print(f"Cosine similarity between {v1} and {vector}: {cos_sim}") - cos_sims.append(cos_sim) - return cos_sims - - -class Student(): - def __init__(self, student_id, time): - self.student_id = student_id - self.time = time - self.words_to_say = "initial value" - - def set_words_to_say(self, words_to_say): - self.words_to_say = words_to_say - - def hello(self): - return ( - f"Hello, {self.student_id}! Time is {self.time}. " - f"I want to say {self.words_to_say}" - ) - - -def task_7( - student_id: str = 'test_id', - time: str = '2018_11_24_0000' -) -> Student: - ''' - Task 7: Class - - Args: - student_id: someone's student ID - time: a certain time - - Returns: - student: an Student object - - Hints: - * Use Student(parameters1, parameters2 ...) to create an object - and assign it to a variable - * Use . to call object function - ''' - # TODO: create a student object with different words to say - student = Student(student_id, time) - student.set_words_to_say('I love Python!') - # End of TODO - - print(student.hello()) - return student - - -def task_8( - img_url: str = 'https://i.imgur.com/B75zq0x.jpg' -) -> object: - ''' - Task 8: Module - - Args: - img_url: address of an image - - Returns: - result_img: an PIL Image - - Hints: - * Make sure you have installed the PIL package - * Take a look at utils.py first - * You could easily find answers with Google - ''' - # from urllib import request - result_img = None - - # TODO: download the image from img_url with the request module - # and add your student ID on it with draw_name() in the utils module - # under src/ - from PIL import Image - import requests - from io import BytesIO - import sys - sys.path.append("..") - from utils import draw_text - response = requests.get(img_url) - result_img = Image.open(BytesIO(response.content)) - draw_text(result_img, 'b06902007') - result_img.save('test.jpg') - # You are allowed to change the img_url to your own image URL. - - # Display the image: - # result_img.show() - # Note: please comment this line when hand in. - - # If you are running on a server, use - # result.save('test.jpg') - # and copy the file to local or use Jupyter Notebook to render. - - # End of TODO - - return result_img diff --git a/src/students/b06902079.py b/src/students/b06902079.py deleted file mode 100644 index a43e3823..00000000 --- a/src/students/b06902079.py +++ /dev/null @@ -1,411 +0,0 @@ -''' -This is the sample code from the homework. You shold NOT modify this file. -Instead, please copy this file to src/students/.py and -edit it there. -''' -import os - -# Define global variables with upper case -SRC_PATH = os.path.dirname(os.path.dirname(os.path.realpath(__file__))) -TEST_DATA_DIR = os.path.join(SRC_PATH, 'test_data') - - -def task_1(dummy=None): - ''' - Task 1: Basic Syntax and Flake8 Checker - - Python uses indentations to separate blocks instead of backets. - Unlike most programming language (like C++), indentations in Python - are required. - - See https://www.python-course.eu/python3_blocks.php for some examples. - - Flake8 (http://flake8.pycqa.org/en/latest/) could help you check these - syntax error. It also regular your coding style. For example, using - two whitespaces as indentation is allowed in Python. However, Flake8 - will tell you it is an error "E111: indentation is not a multiple of four". - This is because when many people work on the same project, it would be - confusing if people are using different identation style. - - Following the coding style in Flake8 is strongly suggested. - - ''' - # Hint: - # Run `python src/autograder.py -task 1 -student ` - # to see if you pass this task. - # The correct output would be "Hello world" without any - # error. Note that passing this task does NOT mean you pass the - # Flake8 chcker. Please check your style with - # `flake8 src/student/.py` - - # TODO: fix the syntax error for the following code - if True: - sentence = 'Hello world' - print(sentence) - - # End of TODO (do not change the code below) - return True - - -def task_2( - input_list: list = [1, 4, 53, 27, 9], - target_index: int = 0, - input_dictionary: dict = {"a": " taiwan", "b": 20, "c": "CSIE"}, - target_key: str = "a" -) -> tuple: - ''' - Task 2: Data Types - - Python has many data types, including Boolean, String, Integer, Float, - List, Dictionary, etc. - - You could use the function type() to see the data type: - - >>> type(5) - - >>> type("hi") - - >>> type(9.2) - - >>> type(["list", "could", "include", "different", "data type", 5, 3.2]) - - >>> type(("you could not change elements", "in a tuple")) - - >>> type({"a": 1, "b":20}) - - >>> type(True) - - >>> - - Try to play with the Python IDE to see different data types by yourself. - - In this task, you are asked to use these datatype. - - Args: - input_list: a list with several items - target_index: target index for the input_list. You need to get the - list element with this index (i.e, 'input_list[target_index]') - input_dictionary: a dictionary with several key-value pairs. - target_key: target key for the input_dictionary You need to get the - value with this key (i.e., input_dictionary[target_key]) - - Returns: - input_list_length_and_sentence: a tuple that contains two elements. - The first one is an integer that indicates the length of input_list - The second one is a string that contains the combination of - input_list[target_index] and input_dictionary[target_key] - - Examples: - Inputs: - input_list = [1, 3, 5, 7, 9] - target_index = 0 - input_dictionary = {"1": "8", "f": "abc", "s": 5.5, "5.5" 900} - target_key = "5.5" - - Returns: - input_list_length_and_sentence = (5, "1900") - - Hints: - * Try to use print() to print out the inputs. - * Use len() to get the length of the list. - * Different data types could not be added. Use str() to convert data - to string. - * Run `python src/autograder.py -task 2 -student_id ` - to see if you pass this task. - * The correct output would be (5, '1 taiwan') - ''' - # TODO: change length and sentence to fit the requirement - length = len(input_list) - sentence = str(input_list[target_index])+input_dictionary[target_key] - # End of TODO - input_list_length_and_sentence = (length, sentence) - print(input_list_length_and_sentence) - return input_list_length_and_sentence - - -def task_3( - number: int = 1314151677777 -) -> list: - ''' - Task 3: Conditions - - Args: - number: a integer input - - Returns: - prime_factors_below_10: a list of the number's largest factors - below 10 - if the number is negative, return [-1] - if the number is zero, return [0] - - Hints: - * Use % to get the remainder - * Using a loop (introduced in the next task) will make some - conditions simpler - ''' - prime_factors_below_10 = [] - # TODO: fill in the conditions - if number < 0: - prime_factors_below_10 = [-1] - # elif stands for "else if" in Python. - elif number == 100: - prime_factors_below_10 = [0] - else: - if number % 2 == 0: - prime_factors_below_10.append(2) - if number % 3 == 0: - prime_factors_below_10.append(3) - if number % 5 == 0: - prime_factors_below_10.append(5) - if number % 7 == 0: - prime_factors_below_10.append(7) - # End of TODO - print(prime_factors_below_10) - return prime_factors_below_10 - - -def task_4( - numbers: list = [2, 4, 5, 6, 9] -) -> list: - ''' - Task 4: For and While Loop - - Args: - numbers: a list of integers - - Returns: - list_of_stars: a list of stars (*) - For each number n in the list, you need to - append n lines of stars to the list, where - the first line has one star, the last line - has n stars. - - Examples: - input: - [1, 3, 5] - output: - ['*', - '*', - '**', - '***', - '*', - '**', - '***', - '****', - '*****'] - - Hints: - * You could create a string with repetitive substring by * - ''' - list_of_stars = [] - # In Python, the for loop could iterate through a list directly - for number in numbers: - # TODO: change stars to correct length - for i in range(1, number+1): - stars = "*"*i - list_of_stars.append(stars) - # End of TODO - - # This could be done by the while loop - list_of_stars_while = [] - i = 0 - while i < len(numbers): - # TODO: change stars to correct length - j = 1 - while j <= numbers[i]: - stars = "*"*j - j += 1 # This line is equivalant to j = j + 1 - list_of_stars_while.append(stars) - i += 1 - # End of TODO - - print("=====> Output list_of_stars") - for stars in list_of_stars: - print(stars) - print("=====> Output list_of_stars_while") - for stars in list_of_stars_while: - print(stars) - - for ans1, ans2 in zip(list_of_stars, list_of_stars_while): - assert ans1 == ans2 - return list_of_stars - - -def task_5( - input_filename: str = 'task_5_input.txt', - output_filename: str = 'task_5_output.txt' -) -> str: - ''' - Task 5: I/O with files - - Args: - input_filename: input filename - output_filename: output filename - - Returns: - lines: content in the output file without commas - - Hints: - * Use .split(something) to split a string into several substring - * Use fout.write(something) to write text into the output file - - ''' - input_filename = os.path.join(TEST_DATA_DIR, input_filename) - output_filename = os.path.join(TEST_DATA_DIR, output_filename) - # Remove previous output file - if os.path.exists(output_filename): - os.remove(output_filename) - - with open(input_filename, 'r') as fin, open(output_filename, 'w') as fout: - lines = fin.readlines() - print(f"=======> Input file content:") - for line in lines: - line = line.split(',') - print(''.join(line)) - fout.write(''.join(line)) - # TODO: read the content of the input file, where words are separate by - # commas. Please remove the commas and write words to the output file - pass - # End of TODO - - with open(output_filename, 'r') as fin: - lines = fin.readlines() - print(f"=======> Output file content:") - print(lines) - return "".join(lines) - - -def task_6( - matrix: list = [[-0.5, 1], [1, 0.5], [-1, 0.5], [-1, -0.5]], - vector: list = [1, 0.5] -) -> list: - ''' - Task 6: Functions - - Args: - matrix: a list of v1 - vector: v2 - - Returns: - cos_sims: a list of cosine similarity between v1s and v2 - - Hints: - * A good function name should be self-explained - * A good function should be less than 30 lines - * A good function should include comments to explain how to use it - * Cosine similarity of the vector itself will be 0.9999999 instead of 1 - ''' - # You could define function B in function A, but function B could only - # be used in the scope of function A - def dot_product(v1, v2): - assert len(v1) == len(v2) - return sum(a*b for a, b in zip(v1, v2)) - - def norm(vector): - # Note that this function would have some minor error due to the - # approximation of square root - return dot_product(vector, vector) ** 0.5 - - def get_cosine_simialrity(v1, v2): - ''' - Calculate the cosine similarity = v1 * v2 / ( |v1| * |v2|) - ''' - # TODO: use the above functions to calculate cosine similarity of - # the two vectors v1 and v2 - cos_sim = (dot_product(v1, v2)) / (norm(v1) * norm(v2)) - # End of TODO - - return cos_sim - - cos_sims = [] - for v1 in matrix: - cos_sim = get_cosine_simialrity(v1, vector) - print(f"Cosine similarity between {v1} and {vector}: {cos_sim}") - cos_sims.append(cos_sim) - return cos_sims - - -class Student(): - def __init__(self, student_id, time): - self.student_id = student_id - self.time = time - self.words_to_say = "initial value" - - def set_words_to_say(self, words_to_say): - self.words_to_say = words_to_say - - def hello(self): - return ( - f"Hello, {self.student_id}! Time is {self.time}. " - f"I want to say {self.words_to_say}" - ) - - -def task_7( - student_id: str = 'test_id', - time: str = '2018_11_24_0000' -) -> Student: - ''' - Task 7: Class - - Args: - student_id: someone's student ID - time: a certain time - - Returns: - student: an Student object - - Hints: - * Use Student(parameters1, parameters2 ...) to create an object - and assign it to a variable - * Use . to call object function - ''' - # TODO: create a student object with different words to say - student = Student(student_id, time) - student.set_words_to_say("asadsa") - # End of TODO - - print(student.hello()) - return student - - -def task_8( - img_url: str = 'https://i.imgur.com/B75zq0x.jpg' -) -> object: - ''' - Task 8: Module - - Args: - img_url: address of an image - - Returns: - result_img: an PIL Image - - Hints: - * Make sure you have installed the PIL package - * Take a look at utils.py first - * You could easily find answers with Google - ''' - from PIL import Image, ImageFont, ImageDraw - from urllib import request - result_img = Image.open(request.urlopen(img_url)) - draw = ImageDraw.Draw(result_img) - font = ImageFont.load_default().font - draw.text((0, 0), "B06902079", (0, 0, 0), font=font) - # TODO: download the image from img_url with the request module - # and add your student ID on it with draw_name() in the utils module - # under src/. - - # You are allowed to change the img_url to your own image URL. - - # Display the image: - # result_img.show() - # Note: please comment this line when hand in. - - # If you are running on a server, use - # result.save('test.jpg') - # and copy the file to local or use Jupyter Notebook to render. - - # End of TODO - - return result_img diff --git a/src/students/b06902101.py b/src/students/b06902101.py deleted file mode 100644 index c905c02a..00000000 --- a/src/students/b06902101.py +++ /dev/null @@ -1,368 +0,0 @@ -''' -This is the sample code from the homework. You shold NOT modify this file. -Instead, please copy this file to src/students/.py and -edit it there. -''' -import os - -# Define global variables with upper case -SRC_PATH = os.path.dirname(os.path.dirname(os.path.realpath(__file__))) -TEST_DATA_DIR = os.path.join(SRC_PATH, 'test_data') - - -def task_1(dummy=None): - ''' - Task 1: Basic Syntax and Flake8 Checker - Python uses indentations to separate blocks instead of backets. - Unlike most programming language (like C++), indentations in Python - are required. - See https://www.python-course.eu/python3_blocks.php for some examples. - Flake8 (http://flake8.pycqa.org/en/latest/) could help you check these - syntax error. It also regular your coding style. For example, using - two whitespaces as indentation is allowed in Python. However, Flake8 - will tell you it is an error "E111: indentation is not a multiple of four". - This is because when many people work on the same project, it would be - confusing if people are using different identation style. - Following the coding style in Flake8 is strongly suggested. - ''' - # Hint: - # Run `python autograder.py -task 1 -student_id ` - # under src/ to see if you pass this task. - # The correct output would be "Hello world" without any - # error. Note that passing this task does NOT mean you pass the - # Flake8 chcker. Please check your style with - # `flake8 src/student/.py` - - # TODO: fix the syntax error for the following code - if True: - sentence = "Hello world" - print(sentence) - # End of TODO (do not change the code below) - return True - - -def task_2( - input_list: list = [1, 4, 53, 27, 9], - target_index: int = 0, - input_dictionary: dict = {"a": " taiwan", "b": 20, "c": "CSIE"}, - target_key: str = "a" -) -> tuple: - ''' - Task 2: Data Types - Python has many data types, including Boolean, String, Integer, Float, - List, Dictionary, etc. - You could use the function type() to see the data type: - >>> type(5) - - >>> type("hi") - - >>> type(9.2) - - >>> type(["list", "could", "include", "different", "data type", 5, 3.2]) - - >>> type(("you could not change elements", "in a tuple")) - - >>> type({"a": 1, "b":20}) - - >>> type(True) - - >>> - Try to play with the Python IDE to see different data types by yourself. - In this task, you are asked to use these datatype. - Args: - input_list: a list with several items - target_index: target index for the input_list. You need to get the - list element with this index (i.e, 'input_list[target_index]') - input_dictionary: a dictionary with several key-value pairs. - target_key: target key for the input_dictionary You need to get the - value with this key (i.e., input_dictionary[target_key]) - Returns: - input_list_length_and_sentence: a tuple that contains two elements. - The first one is an integer that indicates the length of input_list - The second one is a string that contains the combination of - input_list[target_index] and input_dictionary[target_key] - Examples: - Inputs: - input_list = [1, 3, 5, 7, 9] - target_index = 0 - input_dictionary = {"1": "8", "f": "abc", "s": 5.5, "5.5" 900} - target_key = "5.5" - Returns: - input_list_length_and_sentence = (5, "1900") - Hints: - * Try to use print() to print out the inputs. - * Use len() to get the length of the list. - * Different data types could not be added. Use str() to convert data - to string. - * Run `python src/autograder.py -task 2 -student_id ` - to see if you pass this task. - * The correct output would be (4, '1 taiwan') - ''' - # TODO: change length and sentence to fit the requirement - length = len(input_list) - sentence = str(input_list[target_index]) - sentence = sentence + str(input_dictionary[target_key]) - - # End of TODO - input_list_length_and_sentence = (length, sentence) - print(input_list_length_and_sentence) - return input_list_length_and_sentence - - -def task_3( - number: int = 1314151677777 -) -> list: - ''' - Task 3: Conditions - Args: - number: a integer input - Returns: - prime_factors_below_10: a list of the number's largest factors - below 10 - if the number is negative, return [-1] - if the number is zero, return [0] - Hints: - * Use % to get the remainder - * Using a loop (introduced in the next task) will make some - conditions simpler - ''' - prime_factors_below_10 = [] - # TODO: fill in the conditions - if number < 0: - prime_factors_below_10 = [-1] - # elif stands for "else if" in Python. - elif number == 0: - prime_factors_below_10 = [0] - else: - if number % 2 == 0: - prime_factors_below_10.append(2) - if number % 3 == 0: - prime_factors_below_10.append(3) - if number % 5 == 0: - prime_factors_below_10.append(5) - if number % 7 == 0: - prime_factors_below_10.append(7) - # End of TODO - print(prime_factors_below_10) - return prime_factors_below_10 - - -def task_4( - numbers: list = [2, 4, 5, 6, 9] -) -> list: - ''' - Task 4: For and While Loop - Args: - numbers: a list of integers - Returns: - list_of_stars: a list of stars (*) - For each number n in the list, you need to - append n lines of stars to the list, where - the first line has one star, the last line - has n stars. - Examples: - input: - [1, 3, 5] - output: - ['*', - '*', - '**', - '***', - '*', - '**', - '***', - '****', - '*****'] - Hints: - * You could create a string with repetitive substring by * - ''' - list_of_stars = [] - # In Python, the for loop could iterate through a list directly - for number in numbers: - # TODO: change stars to correct length - for i in range(1, number + 1): - stars = "*" * i - list_of_stars.append(stars) - # End of TODO - - # This could be done by the while loop - list_of_stars_while = [] - i = 0 - while i < len(numbers): - # TODO: change stars to correct length - j = 1 - while j <= numbers[i]: - stars = "*" * j - j += 1 # This line is equivalant to j = j + 1 - list_of_stars_while.append(stars) - i += 1 - # End of TODO - - print("=====> Output list_of_stars") - for stars in list_of_stars: - print(stars) - print("=====> Output list_of_stars_while") - for stars in list_of_stars_while: - print(stars) - - for ans1, ans2 in zip(list_of_stars, list_of_stars_while): - assert ans1 == ans2 - return list_of_stars - - -def task_5( - input_filename: str = 'task_5_input.txt', - output_filename: str = 'task_5_output.txt' -) -> str: - ''' - Task 5: I/O with files - Args: - input_filename: input filename - output_filename: output filename - Returns: - lines: content in the output file without commas - Hints: - * Use .split(something) to split a string into several substring - * Use fout.write(something) to write text into the output file - ''' - input_filename = os.path.join(TEST_DATA_DIR, input_filename) - output_filename = os.path.join(TEST_DATA_DIR, output_filename) - # Remove previous output file - if os.path.exists(output_filename): - os.remove(output_filename) - - with open(input_filename, 'r') as fin, open(output_filename, 'w') as fout: - lines = fin.readlines() - print(f"=======> Input file content:") - for line in lines: - print(f"{line}") - # TODO: read the content of the input file, where words are separate by - # commas. Please remove the commas and write words to the output file - pass - # End of TODO - - for i in range(0, len(lines)): - lines[i] = lines[i].split(',') - lines[i] = ''.join(lines[i]) - - with open(output_filename, 'r') as fin: - print(f"=======> Output file content:") - print(lines) - return "".join(lines) - - -def task_6( - matrix: list = [[-0.5, 1], [1, 0.5], [-1, 0.5], [-1, -0.5]], - vector: list = [1, 0.5] -) -> list: - ''' - Task 6: Functions - Args: - matrix: a list of v1 - vector: v2 - Returns: - cos_sims: a list of cosine similarity between v1s and v2 - Hints: - * A good function name should be self-explained - * A good function should be less than 30 lines - * A good function should include comments to explain how to use it - * Cosine similarity of the vector itself will be 0.9999999 instead of 1 - ''' - # You could define function B in function A, but function B could only - # be used in the scope of function A - def dot_product(v1, v2): - assert len(v1) == len(v2) - return sum(a * b for a, b in zip(v1, v2)) - - def norm(vector): - # Note that this function would have some minor error due to the - # approximation of square root - return dot_product(vector, vector) ** 0.5 - - def get_cosine_similarity(v1, v2): - ''' - Calculate the cosine similarity = (v1 * v2) / (|v1| * |v2|) - ''' - # TODO: use the above functions to calculate cosine similarity of - # the two vectors v1 and v2 - cos_sim = dot_product(v1, v2) / (norm(v1) * norm(v2)) - # End of TODO - - return cos_sim - - cos_sims = [] - for v1 in matrix: - cos_sim = get_cosine_similarity(v1, vector) - print(f"Cosine similarity between {v1} and {vector}: {cos_sim}") - cos_sims.append(cos_sim) - return cos_sims - - -class Student(): - def __init__(self, student_id, time): - self.student_id = student_id - self.time = time - self.words_to_say = "initial value" - - def set_words_to_say(self, words_to_say): - self.words_to_say = words_to_say - - def hello(self): - return ( - f"Hello, {self.student_id}! Time is {self.time}. " - f"I want to say {self.words_to_say}" - ) - - -def task_7( - student_id: str = 'test_id', - time: str = '2018_11_24_0000' -) -> Student: - ''' - Task 7: Class - Args: - student_id: someone's student ID - time: a certain time - Returns: - student: an Student object - Hints: - * Use Student(parameters1, parameters2 ...) to create an object - and assign it to a variable - * Use . to call object function - ''' - # TODO: create a student object with different words to say - student = Student(student_id, time) - # End of TODO - student.set_words_to_say("yo") - - print(student.hello()) - return student - -def task_8( - img_url: str = 'https://i.imgur.com/B75zq0x.jpg' -) -> object: - ''' - Task 8: Module - Args: - img_url: address of an image - Returns: - result_img: an PIL Image - Hints: - * Make sure you have installed the PIL package - * Take a look at utils.py first - * You could easily find answers with Google - ''' - - #font = ImageFont.truetype("arial.ttf", 15) - - result_img = None - from urllib import request - from utils import draw_text - from PIL import Image - import io - - image = request.urlopen(img_url).read() - result_img = Image.open(io.BytesIO(image)) - result_img = draw_text(result_img, 'B06902101') - - return result_img \ No newline at end of file diff --git a/src/students/b07902079.py b/src/students/b07902079.py deleted file mode 100644 index d6d37164..00000000 --- a/src/students/b07902079.py +++ /dev/null @@ -1,415 +0,0 @@ -''' -This is the sample code from the homework. You shold NOT modify this file. -Instead, please copy this file to src/students/.py and -edit it there. -''' -import os - -# Define global variables with upper case -SRC_PATH = os.path.dirname(os.path.dirname(os.path.realpath(__file__))) -TEST_DATA_DIR = os.path.join(SRC_PATH, 'test_data') - - -def task_1(dummy=None): - ''' - Task 1: Basic Syntax and Flake8 Checker - - Python uses indentations to separate blocks instead of backets. - Unlike most programming language (like C++), indentations in Python - are required. - - See https://www.python-course.eu/python3_blocks.php for some examples. - - Flake8 (http://flake8.pycqa.org/en/latest/) could help you check these - syntax error. It also regular your coding style. For example, using - two whitespaces as indentation is allowed in Python. However, Flake8 - will tell you it is an error "E111: indentation is not a multiple of four". - This is because when many people work on the same project, it would be - confusing if people are using different identation style. - - Following the coding style in Flake8 is strongly suggested. - - ''' - # Hint: - # Run `python autograder.py -task 1 -student_id ` - # under src/ to see if you pass this task. - # The correct output would be "Hello world" without any - # error. Note that passing this task does NOT mean you pass the - # Flake8 chcker. Please check your style with - # `flake8 src/student/.py` - - # TODO: fix the syntax error for the following code - if True: - sentence = "Hello world" - print(sentence) - - # End of TODO (do not change the code below) - return True - - -def task_2( - input_list: list = [1, 4, 53, 27, 9], - target_index: int = 0, - input_dictionary: dict = {"a": " taiwan", "b": 20, "c": "CSIE"}, - target_key: str = "a" -) -> tuple: - ''' - Task 2: Data Types - - Python has many data types, including Boolean, String, Integer, Float, - List, Dictionary, etc. - - You could use the function type() to see the data type: - - >>> type(5) - - >>> type("hi") - - >>> type(9.2) - - >>> type(["list", "could", "include", "different", "data type", 5, 3.2]) - - >>> type(("you could not change elements", "in a tuple")) - - >>> type({"a": 1, "b":20}) - - >>> type(True) - - >>> - - Try to play with the Python IDE to see different data types by yourself. - - In this task, you are asked to use these datatype. - - Args: - input_list: a list with several items - target_index: target index for the input_list. You need to get the - list element with this index (i.e, 'input_list[target_index]') - input_dictionary: a dictionary with several key-value pairs. - target_key: target key for the input_dictionary You need to get the - value with this key (i.e., input_dictionary[target_key]) - - Returns: - input_list_length_and_sentence: a tuple that contains two elements. - The first one is an integer that indicates the length of input_list - The second one is a string that contains the combination of - input_list[target_index] and input_dictionary[target_key] - - Examples: - Inputs: - input_list = [1, 3, 5, 7, 9] - target_index = 0 - input_dictionary = {"1": "8", "f": "abc", "s": 5.5, "5.5" 900} - target_key = "5.5" - - Returns: - input_list_length_and_sentence = (5, "1900") - - Hints: - * Try to use print() to print out the inputs. - * Use len() to get the length of the list. - * Different data types could not be added. Use str() to convert data - to string. - * Run `python src/autograder.py -task 2 -student_id ` - to see if you pass this task. - * The correct output would be (4, '1 taiwan') - ''' - # TODO: change length and sentence to fit the requirement - length = len(input_list) - sentence = input_dictionary[target_key] - # End of TODO - input_list_length_and_sentence = (length, sentence) - print(input_list_length_and_sentence) - return input_list_length_and_sentence - - -def task_3( - number: int = 1314151677777 -) -> list: - ''' - Task 3: Conditions - - Args: - number: a integer input - - Returns: - prime_factors_below_10: a list of the number's largest factors - below 10 - if the number is negative, return [-1] - if the number is zero, return [0] - - Hints: - * Use % to get the remainder - * Using a loop (introduced in the next task) will make some - conditions simpler - ''' - prime_factors_below_10 = [] - # TODO: fill in the conditions - if number < 0: - prime_factors_below_10 = [-1] - # elif stands for "else if" in Python. - elif number == 0: - prime_factors_below_10 = [0] - else: - if number % 2 == 0: - prime_factors_below_10.append(2) - if number % 3 == 0: - prime_factors_below_10.append(3) - if number % 5 == 0: - prime_factors_below_10.append(5) - if number % 7 == 0: - prime_factors_below_10.append(7) - # End of TODO - print(prime_factors_below_10) - return prime_factors_below_10 - - -def task_4( - numbers: list = [2, 4, 5, 6, 9] -) -> list: - ''' - Task 4: For and While Loop - - Args: - numbers: a list of integers - - Returns: - list_of_stars: a list of stars (*) - For each number n in the list, you need to - append n lines of stars to the list, where - the first line has one star, the last line - has n stars. - - Examples: - input: - [1, 3, 5] - output: - ['*', - '*', - '**', - '***', - '*', - '**', - '***', - '****', - '*****'] - - Hints: - * You could create a string with repetitive substring by * - ''' - list_of_stars = [] - # In Python, the for loop could iterate through a list directly - for number in numbers: - # TODO: change stars to correct length - for i in range(1, number + 1): - stars = "*" - list_of_stars.append(stars * i) - # End of TODO - - # This could be done by the while loop - list_of_stars_while = [] - i = 0 - while i < len(numbers): - # TODO: change stars to correct length - j = 1 - while j <= numbers[i]: - stars = "*" - j += 1 # This line is equivalant to j = j + 1 - list_of_stars_while.append(stars * (j - 1)) - i += 1 - # End of TODO - - print("=====> Output list_of_stars") - for stars in list_of_stars: - print(stars) - print("=====> Output list_of_stars_while") - for stars in list_of_stars_while: - print(stars) - - for ans1, ans2 in zip(list_of_stars, list_of_stars_while): - assert ans1 == ans2 - return list_of_stars - - -def task_5( - input_filename: str = 'task_5_input.txt', - output_filename: str = 'task_5_output.txt' -) -> str: - ''' - Task 5: I/O with files - - Args: - input_filename: input filename - output_filename: output filename - - Returns: - lines: content in the output file without commas - - Hints: - * Use .split(something) to split a string into several substring - * Use fout.write(something) to write text into the output file - - ''' - input_filename = os.path.join(TEST_DATA_DIR, input_filename) - output_filename = os.path.join(TEST_DATA_DIR, output_filename) - # Remove previous output file - if os.path.exists(output_filename): - os.remove(output_filename) - - with open(input_filename, 'r') as fin, open(output_filename, 'w') as fout: - lines = fin.readlines() - print(f"=======> Input file content:") - for line in lines: - print(f"{line}") - # TODO: read the content of the input file, where words are separate by - # commas. Please remove the commas and write words to the output file - for word in line.split(','): - fout.write(word) - pass - # End of TODO - - with open(output_filename, 'r') as fin: - lines = fin.readlines() - print(f"=======> Output file content:") - print(lines) - return "".join(lines) - - -def task_6( - matrix: list = [[-0.5, 1], [1, 0.5], [-1, 0.5], [-1, -0.5]], - vector: list = [1, 0.5] -) -> list: - ''' - Task 6: Functions - - Args: - matrix: a list of v1 - vector: v2 - - Returns: - cos_sims: a list of cosine similarity between v1s and v2 - - Hints: - * A good function name should be self-explained - * A good function should be less than 30 lines - * A good function should include comments to explain how to use it - * Cosine similarity of the vector itself will be 0.9999999 instead of 1 - ''' - # You could define function B in function A, but function B could only - # be used in the scope of function A - def dot_product(v1, v2): - assert len(v1) == len(v2) - return sum(a*b for a, b in zip(v1, v2)) - - def norm(vector): - # Note that this function would have some minor error due to the - # approximation of square root - return dot_product(vector, vector) ** 0.5 - - def get_cosine_simialrity(v1, v2): - ''' - Calculate the cosine similarity = v1 * v2 / (|v1| * |v2|) - ''' - # TODO: use the above functions to calculate cosine similarity of - # the two vectors v1 and v2 - cos_sim = dot_product(v1, v2) / (norm(v1) * norm(v2)) - # End of TODO - - return cos_sim - - cos_sims = [] - for v1 in matrix: - cos_sim = get_cosine_simialrity(v1, vector) - print(f"Cosine similarity between {v1} and {vector}: {cos_sim}") - cos_sims.append(cos_sim) - return cos_sims - - -class Student(): - def __init__(self, student_id, time): - self.student_id = student_id - self.time = time - self.words_to_say = "initial value" - - def set_words_to_say(self, words_to_say): - self.words_to_say = words_to_say - - def hello(self): - return ( - f"Hello, {self.student_id}! Time is {self.time}. " - f"I want to say {self.words_to_say}" - ) - - -def task_7( - student_id: str = 'test_id', - time: str = '2018_11_24_0000' -) -> Student: - ''' - Task 7: Class - - Args: - student_id: someone's student ID - time: a certain time - - Returns: - student: an Student object - - Hints: - * Use Student(parameters1, parameters2 ...) to create an object - and assign it to a variable - * Use . to call object function - ''' - # TODO: create a student object with different words to say - student = Student(student_id, time) - student.set_words_to_say('Python is so hard') - # End of TODO - - print(student.hello()) - return student - - -def task_8( - img_url: str = 'https://i.imgur.com/B75zq0x.jpg' -) -> object: - ''' - Task 8: Module - - Args: - img_url: address of an image - - Returns: - result_img: an PIL Image - - Hints: - * Make sure you have installed the PIL package - * Take a look at utils.py first - * You could easily find answers with Google - ''' - from urllib import request - result_img = None - - # TODO: download the image from img_url with the request module - # and add your student ID on it with draw_text() in the utils module - # under src/. - - # You are allowed to change the img_url to your own image URL. - - from PIL import Image - import utils - import io - r = request.urlopen(img_url) - result_img = Image.open(io.BytesIO(r.read())) - result_img = utils.draw_text(result_img, 'b07902079') - - # Display the image: - # result_img.show() - # Note: please comment this line when hand in. - - # If you are running on a server, use - # result.save('test.jpg') - # and copy the file to local or use Jupyter Notebook to render. - - # End of TODO - - return result_img diff --git a/src/students/b07902091.py b/src/students/b07902091.py deleted file mode 100644 index 3dd1b37e..00000000 --- a/src/students/b07902091.py +++ /dev/null @@ -1,417 +0,0 @@ -''' -This is the sample code from the homework. You shold NOT modify this file. -Instead, please copy this file to src/students/.py and -edit it there. -''' -import os - -# Define global variables with upper case -SRC_PATH = os.path.dirname(os.path.dirname(os.path.realpath(__file__))) -TEST_DATA_DIR = os.path.join(SRC_PATH, 'test_data') - - -def task_1(dummy=None): - ''' - Task 1: Basic Syntax and Flake8 Checker - - Python uses indentations to separate blocks instead of backets. - Unlike most programming language (like C++), indentations in Python - are required. - - See https://www.python-course.eu/python3_blocks.php for some examples. - - Flake8 (http://flake8.pycqa.org/en/latest/) could help you check these - syntax error. It also regular your coding style. For example, using - two whitespaces as indentation is allowed in Python. However, Flake8 - will tell you it is an error "E111: indentation is not a multiple of four". - This is because when many people work on the same project, it would be - confusing if people are using different identation style. - - Following the coding style in Flake8 is strongly suggested. - - ''' - # Hint: - # Run `python src/autograder.py -task 1 -student ` - # to see if you pass this task. - # The correct output would be "Hello world" without any - # error. Note that passing this task does NOT mean you pass the - # Flake8 chcker. Please check your style with - # `flake8 src/student/.py` - - # TODO: fix the syntax error for the following code - if True: - sentence = "Hello world" - print(sentence) - - # End of TODO (do not change the code below) - return True - - -def task_2( - input_list: list = [1, 4, 53, 27, 9], - target_index: int = 0, - input_dictionary: dict = {"a": " taiwan", "b": 20, "c": "CSIE"}, - target_key: str = "a" -) -> tuple: - ''' - Task 2: Data Types - - Python has many data types, including Boolean, String, Integer, Float, - List, Dictionary, etc. - - You could use the function type() to see the data type: - - >>> type(5) - - >>> type("hi") - - >>> type(9.2) - - >>> type(["list", "could", "include", "different", "data type", 5, 3.2]) - - >>> type(("you could not change elements", "in a tuple")) - - >>> type({"a": 1, "b":20}) - - >>> type(True) - - >>> - - Try to play with the Python IDE to see different data types by yourself. - - In this task, you are asked to use these datatype. - - Args: - input_list: a list with several items - target_index: target index for the input_list. You need to get the - list element with this index (i.e, 'input_list[target_index]') - input_dictionary: a dictionary with several key-value pairs. - target_key: target key for the input_dictionary You need to get the - value with this key (i.e., input_dictionary[target_key]) - - Returns: - input_list_length_and_sentence: a tuple that contains two elements. - The first one is an integer that indicates the length of input_list - The second one is a string that contains the combination of - input_list[target_index] and input_dictionary[target_key] - - Examples: - Inputs: - input_list = [1, 3, 5, 7, 9] - target_index = 0 - input_dictionary = {"1": "8", "f": "abc", "s": 5.5, "5.5" 900} - target_key = "5.5" - - Returns: - input_list_length_and_sentence = (5, "1900") - - Hints: - * Try to use print() to print out the inputs. - * Use len() to get the length of the list. - * Different data types could not be added. Use str() to convert data - to string. - * Run `python src/autograder.py -task 2 -student_id ` - to see if you pass this task. - * The correct output would be (5, '1 taiwan') - ''' - # TODO: change length and sentence to fit the requirement - length = len(input_list) - sentence = str(input_list[target_index])+input_dictionary[target_key] - # End of TODO - input_list_length_and_sentence = (length, sentence) - print(input_list_length_and_sentence) - return input_list_length_and_sentence - - -def task_3( - number: int = 1314151677777 -) -> list: - ''' - Task 3: Conditions - - Args: - number: a integer input - - Returns: - prime_factors_below_10: a list of the number's largest factors - below 10 - if the number is negative, return [-1] - if the number is zero, return [0] - - Hints: - * Use % to get the remainder - * Using a loop (introduced in the next task) will make some - conditions simpler - ''' - prime_factors_below_10 = [] - # TODO: fill in the conditions - if number < 0: - prime_factors_below_10 = [-1] - # elif stands for "else if" in Python. - elif number == 0: - prime_factors_below_10 = [0] - else: - if number % 2 == 0: - prime_factors_below_10.append(2) - if number % 3 == 0: - prime_factors_below_10.append(3) - if number % 5 == 0: - prime_factors_below_10.append(5) - if number % 7 == 0: - prime_factors_below_10.append(7) - # End of TODO - print(prime_factors_below_10) - return prime_factors_below_10 - - -def task_4( - numbers: list = [2, 4, 5, 6, 9] -) -> list: - ''' - Task 4: For and While Loop - - Args: - numbers: a list of integers - - Returns: - list_of_stars: a list of stars (*) - For each number n in the list, you need to - append n lines of stars to the list, where - the first line has one star, the last line - has n stars. - - Examples: - input: - [1, 3, 5] - output: - ['*', - '*', - '**', - '***', - '*', - '**', - '***', - '****', - '*****'] - - Hints: - * You could create a string with repetitive substring by * - ''' - list_of_stars = [] - # In Python, the for loop could iterate through a list directly - for number in numbers: - # TODO: change stars to correct length - for i in range(1, number+1): - stars = "*"*i - list_of_stars.append(stars) - # End of TODO - - # This could be done by the while loop - list_of_stars_while = [] - i = 0 - while i < len(numbers): - # TODO: change stars to correct length - j = 1 - while j <= numbers[i]: - stars = "*"*j - j += 1 # This line is equivalant to j = j + 1 - list_of_stars_while.append(stars) - i += 1 - # End of TODO - - print("=====> Output list_of_stars") - for stars in list_of_stars: - print(stars) - print("=====> Output list_of_stars_while") - for stars in list_of_stars_while: - print(stars) - - for ans1, ans2 in zip(list_of_stars, list_of_stars_while): - assert ans1 == ans2 - return list_of_stars - - -def task_5( - input_filename: str = 'task_5_input.txt', - output_filename: str = 'task_5_output.txt' -) -> str: - ''' - Task 5: I/O with files - - Args: - input_filename: input filename - output_filename: output filename - - Returns: - lines: content in the output file without commas - - Hints: - * Use .split(something) to split a string into several substring - * Use fout.write(something) to write text into the output file - - ''' - input_filename = os.path.join(TEST_DATA_DIR, input_filename) - output_filename = os.path.join(TEST_DATA_DIR, output_filename) - # Remove previous output file - if os.path.exists(output_filename): - os.remove(output_filename) - - with open(input_filename, 'r') as fin, open(output_filename, 'w') as fout: - lines = fin.readlines() - print(f"=======> Input file content:") - for line in lines: - print(f"{line}") - # TODO: read the content of the input file, where words are separateet by - # commas. Please remove the commas and write words to the output file - # source for split and join in python :https://www.hackerrank.com/challenges/python-string-split-and-join/problem - list = line.split(",") - output = "".join(list) - fout.write(output) - pass - # End of TODO - - with open(output_filename, 'r') as fin: - lines = fin.readlines() - print(f"=======> Output file content:") - print(lines) - return "".join(lines) - - -def task_6( - matrix: list = [[-0.5, 1], [1, 0.5], [-1, 0.5], [-1, -0.5]], - vector: list = [1, 0.5] -) -> list: - ''' - Task 6: Functions - - Args: - matrix: a list of v1 - vector: v2 - - Returns: - cos_sims: a list of cosine similarity between v1s and v2 - - Hints: - * A good function name should be self-explained - * A good function should be less than 30 lines - * A good function should include comments to explain how to use it - * Cosine similarity of the vector itself will be 0.9999999 instead of 1 - ''' - # You could define function B in function A, but function B could only - # be used in the scope of function A - def dot_product(v1, v2): - assert len(v1) == len(v2) - return sum(a*b for a, b in zip(v1, v2)) - - def norm(vector): - # Note that this function would have some minor error due to the - # approximation of square root - return dot_product(vector, vector) ** 0.5 - - def get_cosine_simialrity(v1, v2): - ''' - Calculate the cosine similarity = v1 * v2 / (|v1| * |v2|) - ''' - # TODO: use the above functions to calculate cosine similarity of - # the two vectors v1 and v2 - cos_sim = dot_product(v1, v2)/(norm(v1)*norm(v2)) - # End of TODO - - return cos_sim - - cos_sims = [] - for v1 in matrix: - cos_sim = get_cosine_simialrity(v1, vector) - print(f"Cosine similarity between {v1} and {vector}: {cos_sim}") - cos_sims.append(cos_sim) - return cos_sims - - -class Student(): - def __init__(self, student_id, time): - self.student_id = student_id - self.time = time - self.words_to_say = "initial value" - - def set_words_to_say(self, words_to_say): - self.words_to_say = words_to_say - - def hello(self): - return ( - f"Hello, {self.student_id}! Time is {self.time}. " - f"I want to say {self.words_to_say}" - ) - - -def task_7( - student_id: str = 'test_id', - time: str = '2018_11_24_0000' -) -> Student: - ''' - Task 7: Class - - Args: - student_id: someone's student ID - time: a certain time - - Returns: - student: an Student object - - Hints: - * Use Student(parameters1, parameters2 ...) to create an object - and assign it to a variable - * Use . to call object function - ''' - # TODO: create a student object with different words to say - student = Student(student_id, time) - student.set_words_to_say('Hello World') - # End of TODO - - print(student.hello()) - return student - - -def task_8( - img_url: str = 'https://i.imgur.com/B75zq0x.jpg' -) -> object: - ''' - Task 8: Module - - Args: - img_url: address of an image - - Returns: - result_img: an PIL Image - - Hints: - * Make sure you have installed the PIL package - * Take a look at utils.py first - * You could easily find answers with Google - ''' - from urllib import request - result_img = None - - # TODO: download the image from img_url with the request module - # and add your student ID on it with draw_name() in the utils module - # under src/. - - # You are allowed to change the img_url to your own image URL. - # source from internet,161 : https://stackoverflow.com/questions/7391945/how-do-i-read-image-data-from-a-url-in-python - # source for utils.draw_text : https://stackoverflow.com/questions/34052647/add-text-on-image-using-pil-error-message - import utils - import io - from PIL import Image - with request.urlopen(img_url) as respone: - r = respone.read() - result_img = Image.open(io.BytesIO(r)) - result_img = utils.draw_text(result_img, 'b07902091') - # Display the image: - # result_img.show() - # Note: please comment this line when hand in. - # If you are running on a server, use - # result.save('test.jpg') - # and copy the file to local or use Jupyter Notebook to render. - - # End of TODO - - return result_img diff --git a/src/students/r06522601.py b/src/students/r06522601.py deleted file mode 100644 index ace19025..00000000 --- a/src/students/r06522601.py +++ /dev/null @@ -1,411 +0,0 @@ -''' -This is the sample code from the homework. You shold NOT modify this file. -Instead, please copy this file to src/students/.py and -edit it there. -''' -import os - -# Define global variables with upper case -SRC_PATH = os.path.dirname(os.path.dirname(os.path.realpath(__file__))) -TEST_DATA_DIR = os.path.join(SRC_PATH, 'test_data') - - -def task_1(dummy=None): - ''' - Task 1: Basic Syntax and Flake8 Checker - - Python uses indentations to separate blocks instead of backets. - Unlike most programming language (like C++), indentations in Python - are required. - - See https://www.python-course.eu/python3_blocks.php for some examples. - - Flake8 (http://flake8.pycqa.org/en/latest/) could help you check these - syntax error. It also regular your coding style. For example, using - two whitespaces as indentation is allowed in Python. However, Flake8 - will tell you it is an error "E111: indentation is not a multiple of four". - This is because when many people work on the same project, it would be - confusing if people are using different identation style. - - Following the coding style in Flake8 is strongly suggested. - - ''' - # Hint: - # Run `python autograder.py -task 1 -student_id ` - # under src/ to see if you pass this task. - # The correct output would be "Hello world" without any - # error. Note that passing this task does NOT mean you pass the - # Flake8 chcker. Please check your style with - # `flake8 src/student/.py` - - # TODO: fix the syntax error for the following code - if True: - sentence = "Hello world" - print(sentence) - - # End of TODO (do not change the code below) - return True - - -def task_2( - input_list: list = [1, 4, 53, 27, 9], - target_index: int = 0, - input_dictionary: dict = {"a": " taiwan", "b": 20, "c": "CSIE"}, - target_key: str = "a" -) -> tuple: - ''' - Task 2: Data Types - - Python has many data types, including Boolean, String, Integer, Float, - List, Dictionary, etc. - - You could use the function type() to see the data type: - - >>> type(5) - - >>> type("hi") - - >>> type(9.2) - - >>> type(["list", "could", "include", "different", "data type", 5, 3.2]) - - >>> type(("you could not change elements", "in a tuple")) - - >>> type({"a": 1, "b":20}) - - >>> type(True) - - >>> - - Try to play with the Python IDE to see different data types by yourself. - - In this task, you are asked to use these datatype. - - Args: - input_list: a list with several items - target_index: target index for the input_list. You need to get the - list element with this index (i.e, 'input_list[target_index]') - input_dictionary: a dictionary with several key-value pairs. - target_key: target key for the input_dictionary You need to get the - value with this key (i.e., input_dictionary[target_key]) - - Returns: - input_list_length_and_sentence: a tuple that contains two elements. - The first one is an integer that indicates the length of input_list - The second one is a string that contains the combination of - input_list[target_index] and input_dictionary[target_key] - - Examples: - Inputs: - input_list = [1, 3, 5, 7, 9] - target_index = 0 - input_dictionary = {"1": "8", "f": "abc", "s": 5.5, "5.5" 900} - target_key = "5.5" - - Returns: - input_list_length_and_sentence = (5, "1900") - - Hints: - * Try to use print() to print out the inputs. - * Use len() to get the length of the list. - * Different data types could not be added. Use str() to convert data - to string. - * Run `python src/autograder.py -task 2 -student_id ` - to see if you pass this task. - * The correct output would be (4, '1 taiwan') - ''' - # TODO: change length and sentence to fit the requirement - length = len(input_list) - sentence = str(input_list[target_index]) + input_dictionary[target_key] - # End of TODO - input_list_length_and_sentence = (length, sentence) - print(input_list_length_and_sentence) - return input_list_length_and_sentence - - -def task_3( - number: int = 1314151677777 -) -> list: - ''' - Task 3: Conditions - - Args: - number: a integer input - - Returns: - prime_factors_below_10: a list of the number's largest factors - below 10 - if the number is negative, return [-1] - if the number is zero, return [0] - - Hints: - * Use % to get the remainder - * Using a loop (introduced in the next task) will make some - conditions simpler - ''' - prime_factors_below_10 = [] - # TODO: fill in the conditions - if number < 0: - prime_factors_below_10 = [-1] - # elif stands for "else if" in Python. - elif number == 0: - prime_factors_below_10 = [0] - else: - if number % 2 == 0: - prime_factors_below_10.append(2) - if number % 3 == 0: - prime_factors_below_10.append(3) - if number % 5 == 0: - prime_factors_below_10.append(5) - if number % 7 == 0: - prime_factors_below_10.append(7) - # End of TODO - print(prime_factors_below_10) - return prime_factors_below_10 - - -def task_4( - numbers: list = [2, 4, 5, 6, 9] -) -> list: - ''' - Task 4: For and While Loop - - Args: - numbers: a list of integers - - Returns: - list_of_stars: a list of stars (*) - For each number n in the list, you need to - append n lines of stars to the list, where - the first line has one star, the last line - has n stars. - - Examples: - input: - [1, 3, 5] - output: - ['*', - '*', - '**', - '***', - '*', - '**', - '***', - '****', - '*****'] - - Hints: - * You could create a string with repetitive substring by * - ''' - list_of_stars = [] - # In Python, the for loop could iterate through a list directly - for number in numbers: - # TODO: change stars to correct length - for i in range(1, number+1): - stars = "*" * i - list_of_stars.append(stars) - # End of TODO - - # This could be done by the while loop - list_of_stars_while = [] - i = 0 - while i < len(numbers): - # TODO: change stars to correct length - j = 1 - while j <= numbers[i]: - stars = "*" * j - j += 1 # This line is equivalant to j = j + 1 - list_of_stars_while.append(stars) - i += 1 - # End of TODO - - print("=====> Output list_of_stars") - for stars in list_of_stars: - print(stars) - print("=====> Output list_of_stars_while") - for stars in list_of_stars_while: - print(stars) - - for ans1, ans2 in zip(list_of_stars, list_of_stars_while): - assert ans1 == ans2 - return list_of_stars - - -def task_5( - input_filename: str = 'task_5_input.txt', - output_filename: str = 'task_5_output.txt' -) -> str: - ''' - Task 5: I/O with files - - Args: - input_filename: input filename - output_filename: output filename - - Returns: - lines: content in the output file without commas - - Hints: - * Use .split(something) to split a string into several substring - * Use fout.write(something) to write text into the output file - - ''' - input_filename = os.path.join(TEST_DATA_DIR, input_filename) - output_filename = os.path.join(TEST_DATA_DIR, output_filename) - # Remove previous output file - if os.path.exists(output_filename): - os.remove(output_filename) - - with open(input_filename, 'r') as fin, open(output_filename, 'w') as fout: - lines = fin.readlines() - print(f"=======> Input file content:") - for line in lines: - print(f"{line}") - # TODO: read the content of the input file, where words are separate by - # commas. Please remove the commas and write words to the output file - for line in line: - x = line.replace(',', '') - fout.write(x) - # End of TODO - - with open(output_filename, 'r') as fin: - lines = fin.readlines() - print(f"=======> Output file content:") - print(lines) - return "".join(lines) - - -def task_6( - matrix: list = [[-0.5, 1], [1, 0.5], [-1, 0.5], [-1, -0.5]], - vector: list = [1, 0.5] -) -> list: - ''' - Task 6: Functions - - Args: - matrix: a list of v1 - vector: v2 - - Returns: - cos_sims: a list of cosine similarity between v1s and v2 - - Hints: - * A good function name should be self-explained - * A good function should be less than 30 lines - * A good function should include comments to explain how to use it - * Cosine similarity of the vector itself will be 0.9999999 instead of 1 - ''' - # You could define function B in function A, but function B could only - # be used in the scope of function A - def dot_product(v1, v2): - assert len(v1) == len(v2) - return sum(a*b for a, b in zip(v1, v2)) - - def norm(vector): - # Note that this function would have some minor error due to the - # approximation of square root - return dot_product(vector, vector) ** 0.5 - - def get_cosine_simialrity(v1, v2): - ''' - Calculate the cosine similarity = v1 * v2 / (|v1| * |v2|) - ''' - # TODO: use the above functions to calculate cosine similarity of - # the two vectors v1 and v2 - cos_sim = dot_product(v1, v2) / (norm(v1) * norm(v2)) - # End of TODO - - return cos_sim - - cos_sims = [] - for v1 in matrix: - cos_sim = get_cosine_simialrity(v1, vector) - print(f"Cosine similarity between {v1} and {vector}: {cos_sim}") - cos_sims.append(cos_sim) - return cos_sims - - -class Student(): - def __init__(self, student_id, time): - self.student_id = student_id - self.time = time - self.words_to_say = "HEY!" - - def set_words_to_say(self, words_to_say): - self.words_to_say = words_to_say - - def hello(self): - return ( - f"Hello, {self.student_id}! Time is {self.time}. " - f"I want to say {self.words_to_say}" - ) - - -def task_7( - student_id: str = 'test_id', - time: str = '2018_11_24_0000' -) -> Student: - ''' - Task 7: Class - - Args: - student_id: someone's student ID - time: a certain time - - Returns: - student: an Student object - - Hints: - * Use Student(parameters1, parameters2 ...) to create an object - and assign it to a variable - * Use . to call object function - ''' - # TODO: create a student object with different words to say - student = Student(student_id, time) - # End of TODO - - print(student.hello()) - return student - - -def task_8( - img_url: str = 'https://i.imgur.com/B75zq0x.jpg' -) -> object: - ''' - Task 8: Module - - Args: - img_url: address of an image - - Returns: - result_img: an PIL Image - - Hints: - * Make sure you have installed the PIL package - * Take a look at utils.py first - * You could easily find answers with Google - ''' - from urllib import request - result_img = None - - # TODO: download the image from img_url with the request module - # and add your student ID on it with draw_text() in the utils module - # under src/. - - # You are allowed to change the img_url to your own image URL. - - # Display the image: - # result_img.show() - # Note: please comment this line when hand in. - - # If you are running on a server, use - # result.save('test.jpg') - # and copy the file to local or use Jupyter Notebook to render. - reslt_img = request.urlopen(img_url) - from .. import ultis - draw_text(result_img, 'r06522601', (0.0), (255,255,255)) - #result_img.show() - result_img.save('test.jpg') - # End of TODO - - return result_img