From 9b97c545cda02c0197cc76bf9870014a2578c75f Mon Sep 17 00:00:00 2001 From: meoulove Date: Mon, 25 Jan 2021 22:55:46 +0800 Subject: [PATCH] group_3_ai --- data_generator.py | 52 ++++++++++++++++++++++++++++++++++++++++ exceptions.py | 33 ++++++++++++++++++++++++++ list_comp1.py | 15 ++++++++++++ range_dict.py | 60 +++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 160 insertions(+) create mode 100644 data_generator.py create mode 100644 exceptions.py create mode 100644 list_comp1.py create mode 100644 range_dict.py diff --git a/data_generator.py b/data_generator.py new file mode 100644 index 0000000..dbc47f2 --- /dev/null +++ b/data_generator.py @@ -0,0 +1,52 @@ +""" +create a class that given a list of text files will at each iteration return +the first line of the text only if the file is not suppose to be ignored (see example files below) + +example: + +lets assume we have the following files and that the first line of each file is the name of the file + +files = ["file1.txt", "file2.txt", "ingore_this_file1.txt", "file3.txt", "ingore_this_file1.txt", "file4.txt"] + + +following code should run: + +>> my_generator = DataGenerator(files) +>> for data in my_generator: +.. print(data) +.. if "3" in data: +.. break +>>"file1.txt" +>>"file2.txt" +>>"file3.txt" +>> next(my_generator) +>>"file4.txt" + +""" + +class DataGenerator(): + def __init__(self,files): + self.data = files + self.current_num = 0 + + def __iter__(self): + return self + + def __next__(self): + for item in self.data: + if 'ingore' in item: + self.data.remove(item) + if self.current_num < len(self.data): + ret = self.data[self.current_num] + self.current_num += 1 + return ret + +files = ["file1.txt", "file2.txt", "ingore_this_file1.txt", "file3.txt", "ingore_this_file1.txt", "file4.txt"] + +my_generator = DataGenerator(files) +for data in my_generator: + print(data) + if "3" in data: + break +print('================') +print(next(my_generator)) diff --git a/exceptions.py b/exceptions.py new file mode 100644 index 0000000..0e3fe6d --- /dev/null +++ b/exceptions.py @@ -0,0 +1,33 @@ + + +""" +fill in the fucntions so that the catch the appropriate errors and return "caught". + +example: +>> test_list = [] +>> catch_all_exceptions(test_list[10]) +>> "caught" + +""" + +def catch_all_exceptions(object): + catch_index_error(object) + catch_value_error(object) + +def catch_index_error(object): + try: + print(object[10]) + except IndexError: + print('caught') + +def catch_value_error(object): + try: + object = str(object) + print(int(object)) + except ValueError: + print('caught') + + +test_list = [] +catch_all_exceptions(test_list) + diff --git a/list_comp1.py b/list_comp1.py new file mode 100644 index 0000000..6a735ea --- /dev/null +++ b/list_comp1.py @@ -0,0 +1,15 @@ +def list_comp1(input_data): + """ + make this into a list comprehension + + example: + >> list_comp1([1,0,90,40]) + >> [0,0,1,1] + """ + + transformed_data = [] + transformed_data = [0 if i < 10 else 1 for i in [i for i in input_data if i >= 0]] + return transformed_data + +transformed_data = list_comp1([1,0,90,40]) +print(transformed_data) \ No newline at end of file diff --git a/range_dict.py b/range_dict.py new file mode 100644 index 0000000..363bef9 --- /dev/null +++ b/range_dict.py @@ -0,0 +1,60 @@ + +""" + +implemet a child-class of dict called RangeDict that takes pairs of ints; (int, int) which +signifies a range. eg. (1,10), (30,34). +Given an int will return the value of the range the int is inside + +so if out dict contain the keys (1,10), (30,34), and we run: + +>> my_range_dict[7] +>> (1,10) + +furthermore; + +when you try to add a range that overlaps with an existing range, for example +trying to add (5,11) when we have (1,10), overwrite the overlapping span. + +>> my_range_dict +>> {(1,10):1, (30,34): 2} +>> my_range_dict[(5,11)] = 3 +>> my_range_dict +>> {(30,34): 2, (5,11):3} + +""" + +class RangeDict(): + ranges = [(1,10),(30,34)] + keys = [1, 2] + dict = {} + for i in range(len(ranges)): + dict[ranges[i]] = keys[i] + +class My_range_dict(RangeDict): + + def __getitem__(self, key): + self.key = key + for eachRange in self.ranges: + if self.key in range(eachRange[0],eachRange[1]+1): + print(eachRange) + + def __setitem__(self, key, value): + new_range = set(range(key[0],key[1]+1)) + for each_range in self.ranges: + old_range = set(range(each_range[0],each_range[1]+1)) + if new_range.intersection(old_range): + del self.dict[each_range] + self.dict[key] = value + return self.dict + else: + self.dict[key] = value + return self.dict + + def __call__(self): + print(self.dict) + +my_range_dict = My_range_dict() +my_range_dict() +my_range_dict[7] +my_range_dict[(20,24)] = 3 +my_range_dict()