Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

group_3_ai #20

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions data_generator.py
Original file line number Diff line number Diff line change
@@ -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))
33 changes: 33 additions & 0 deletions exceptions.py
Original file line number Diff line number Diff line change
@@ -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)

15 changes: 15 additions & 0 deletions list_comp1.py
Original file line number Diff line number Diff line change
@@ -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)
60 changes: 60 additions & 0 deletions range_dict.py
Original file line number Diff line number Diff line change
@@ -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()