- Notifications
You must be signed in to change notification settings - Fork 46
Feature/project matrix#123
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
base:master
Are you sure you want to change the base?
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
c6a3e94f9ba13251191a6cf307b5320281bb88fcb3a63a026628d3a242f36e0499d82490a54a255dfa7bbc2dbbb8e3624f6c1580aed2961592899f18b89b74a025a81eb89c11bb8241eFile filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,145 @@ | ||
| import sys | ||
| FILE_NAME = 1 | ||
| ID = 0 | ||
| PASSWORD = 1 | ||
| BALANCE = 2 | ||
| def update_file(file_name, dictionary): | ||
| PASSWORD = 0 | ||
| BALANCE = 1 | ||
| with open(file_name, 'w') as atm_file: | ||
| for key in dictionary: | ||
| my_list = dictionary[key] | ||
| atm_file.write('{} {} {}\n'.format(key, my_list[PASSWORD], | ||
| my_list[BALANCE])) | ||
| def change_password(dictionary, consumer_id): | ||
| PASSWORD = 0 | ||
| new_pass = input('Please enter a new password: ') | ||
| my_list = dictionary[consumer_id] | ||
| my_list[PASSWORD] = new_pass | ||
| dictionary[consumer_id] = my_list # update the dictionary | ||
| return dictionary | ||
| def cash_deposit(dictionary, consumer_id): | ||
| BALANCE = 1 | ||
| cash = input('Please enter the amount of money you wish to deposit: ') | ||
| cash = int(cash) | ||
| if cash < 0: | ||
| print("Can't do this, please enter a positive number! ") | ||
| my_list = dictionary[consumer_id] | ||
| temp_list = my_list # create temporary list | ||
| temp_list[BALANCE] += cash # add the cash amount | ||
| my_list = temp_list | ||
| dictionary[consumer_id] = my_list # update the dictionary | ||
| return dictionary | ||
| def cash_withdrawal(dictionary, consumer_id): | ||
| BALANCE = 1 | ||
| cash = input('Please enter the amount of money you wish to withdraw: ') | ||
| cash = int(cash) | ||
| if cash < 0: | ||
| print("Can't do this, please enter a positive number! ") | ||
| my_list = dictionary[consumer_id] | ||
| temp_list = my_list # create temporary list | ||
| temp_list[BALANCE] -= cash # sub the cash amount | ||
| my_list = temp_list | ||
| dictionary[consumer_id] = my_list # update the dictionary | ||
| return dictionary | ||
| def check_the_balance(dictionary, consumer_id): | ||
| BALANCE = 1 | ||
| my_list = dictionary[consumer_id] | ||
| return my_list[BALANCE] | ||
| def verify_password(dictionary, consumer_id): | ||
| PASSWORD = 0 | ||
| times = 3 # the user have 3 tries to enter his password correctly | ||
| while True: | ||
| password = input('Please enter your ATM password: ') | ||
| my_list = dictionary[consumer_id] | ||
| if my_list[PASSWORD] == password: | ||
| print('OK, we proceed ') | ||
| return True | ||
| else: | ||
| print('Wrong password, you have {} more tries' | ||
| .format(times - 1)) | ||
| times -= 1 | ||
| if times == 0: | ||
| return False | ||
| def who_are_you(dictionary): | ||
| while True: | ||
| consumer_id = input('Please enter you ID: ') | ||
| if (consumer_id in dictionary) or (consumer_id == '-1'): | ||
| return consumer_id | ||
| else: | ||
| print('Sorry, you are not in the system, please try again! ') | ||
| def read_file(file_name, dictionary): | ||
| try: | ||
| with open(file_name, 'r') as atm_file: | ||
| for line in atm_file: | ||
| my_list = line.split() | ||
| dictionary[my_list[ID]] = \ | ||
| [my_list[PASSWORD], int(my_list[BALANCE])] | ||
| # dictionary = {ID: [password, balance]} | ||
| return dictionary | ||
| except Exception as error: | ||
| print(error) | ||
| def main(): | ||
| file_name = sys.argv[FILE_NAME] | ||
| dictionary = {} | ||
| dictionary = read_file(file_name, dictionary) | ||
| while True: | ||
| consumer_id = who_are_you(dictionary) | ||
| if consumer_id == '-1': | ||
| print('Turning off the ATM...') | ||
| break | ||
| if verify_password(dictionary, consumer_id) is True: | ||
| while True: | ||
| action = input('Press:\n1 -- see balance\n' + | ||
| '2 -- cash withdraw\n' + | ||
| '3-- cash deposit\n' + | ||
| '4 -- change password\n' + | ||
| 'STOP -- if you are done\n') | ||
| if action == '1': | ||
| balance = check_the_balance(dictionary, consumer_id) | ||
| print('Your balance is: {}'.format(balance)) | ||
| elif action == '2': | ||
| dictionary = cash_withdrawal(dictionary, consumer_id) | ||
| update_file(file_name, dictionary) | ||
| elif action == '3': | ||
| dictionary = cash_deposit(dictionary, consumer_id) | ||
| update_file(file_name, dictionary) | ||
| elif action == '4': | ||
| dictionary = change_password(dictionary, consumer_id) | ||
| update_file(file_name, dictionary) | ||
| elif action == 'STOP': | ||
| print('Thank you, have a nice day!') | ||
| break | ||
| else: | ||
| print('Wrong action, try again!') | ||
| else: # if the password was wrong for 3 times | ||
| print('You failed 3 times, go home pretender!') | ||
| if __name__ == '__main__': | ||
| main() |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,157 @@ | ||
| class Matrix: | ||
| def __init__(self, tuple_matrix): | ||
| """ | ||
| Please insert a matrix as tuple of tuples | ||
| """ | ||
| # ========== tuple to list =============== | ||
| my_list = list(tuple_matrix) | ||
| for index in range(len(tuple_matrix)): | ||
| my_list[index] = list(tuple_matrix[index]) | ||
| # ======================================== | ||
| self.my_list = my_list | ||
| self.my_tuple = tuple_matrix | ||
| self.counter = 0 | ||
| def __str__(self): | ||
| return str(self.my_tuple) | ||
| def __repr__(self): | ||
| return 'Matrix({})'.format(self.my_tuple) | ||
| def __next__(self): | ||
| dim = len(self.my_tuple) | ||
| if self.counter < dim: | ||
| res = self.my_tuple[self.counter] | ||
| self.counter += 1 | ||
| return res | ||
| self.counter = 0 | ||
| raise StopIteration() | ||
| def __iter__(self): | ||
| return self | ||
| def __mul__(self, other): | ||
| new_list = [] | ||
| dim = len(self.my_list) | ||
| for i in range(dim): | ||
| new_list.append([0] * dim) | ||
| if issubclass(type(other), Matrix): # mul by a matrix | ||
| for i in range(dim): # i - num of column | ||
| new_sub_list = [] | ||
| for j in range(dim): # j - num of row | ||
| sub_list_1 = self.my_list[i] | ||
| sub_list_2 = [element[j] for element in other.my_list] | ||
| res = sum([sub_list_1[k] * sub_list_2[k] | ||
| for k in range(dim)]) | ||
| new_sub_list.append(res) | ||
| new_list[i] = [element for element in new_sub_list] | ||
| elif issubclass(type(other), int): # mul by a number | ||
| for i in range(dim): | ||
| sub_list1 = self.my_list[i] | ||
| new_sub_list = [(element * other) for element in sub_list1] | ||
| new_list[i] = new_sub_list | ||
| # ========== list to tuple =============== | ||
| for index in range(dim): | ||
| new_list[index] = (*new_list[index],) | ||
| list2tuple = (*new_list,) | ||
| return Matrix(list2tuple) | ||
| __rmul__ = __mul__ | ||
| def __sub__(self, other): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the sub and the add function look quite similar, try to unify them | ||
| new_list = [] | ||
| dim = len(self.my_list) | ||
| for i in range(dim): | ||
| new_list.append([0] * dim) | ||
| for i in range(dim): | ||
| sub_list_1 = self.my_list[i] | ||
| sub_list_2 = other.my_list[i] | ||
| new_sub_list = [sub_list_1[index] - sub_list_2[index] | ||
| for index in range(dim)] | ||
| new_list[i] = new_sub_list | ||
| # ========== list to tuple =============== | ||
| for index in range(dim): | ||
| new_list[index] = (*new_list[index],) | ||
| list2tuple = (*new_list,) | ||
| return Matrix(list2tuple) | ||
| def __add__(self, other): | ||
| new_list = [] | ||
| dim = len(self.my_list) | ||
| for i in range(dim): | ||
| new_list.append([0] * dim) | ||
| for i in range(dim): | ||
| sub_list_1 = self.my_list[i] | ||
| sub_list_2 = other.my_list[i] | ||
| new_sub_list = [sub_list_1[index] + sub_list_2[index] | ||
| for index in range(dim)] | ||
| new_list[i] = new_sub_list | ||
| # ========== list to tuple =============== | ||
| for index in range(dim): | ||
| new_list[index] = (*new_list[index],) | ||
| list2tuple = (*new_list,) | ||
| return Matrix(list2tuple) | ||
| def __truediv__(self, other): | ||
| new_list = [] | ||
| dim = len(self.my_list) | ||
| for i in range(dim): | ||
| new_list.append([0] * dim) | ||
| for i in range(dim): | ||
| sub_list_1 = self.my_list[i] | ||
| new_sub_list = [sub_list_1[index] / other | ||
| for index in range(dim)] | ||
| new_list[i] = new_sub_list | ||
| # ========== list to tuple =============== | ||
| for index in range(dim): | ||
| new_list[index] = (*new_list[index],) | ||
| list2tuple = (*new_list,) | ||
| return Matrix(list2tuple) | ||
| def __eq__(self, other): | ||
| return self.my_list == other.my_list | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if the other object isn't matrix? and don't have my_list? | ||
| def __hash__(self): | ||
| return hash(self.my_tuple) | ||
| def get_tuple(self): | ||
| return self.my_tuple | ||
| @classmethod | ||
| def unity(cls, dim): | ||
| unity_matrix = [] | ||
| if dim == 1: | ||
| unity_matrix = (1,) | ||
| return 'Matrix({})'.format(unity_matrix) | ||
| sub_list = [0] * dim | ||
| for i in range(dim): | ||
| unity_matrix.append([0] * dim) | ||
| for i in range(dim): | ||
| for j in range(dim): | ||
| if j == i: | ||
| sub_list[j] = 1 | ||
| else: | ||
| sub_list[j] = 0 | ||
| unity_matrix[i] = tuple([element for element in sub_list]) | ||
| unity_matrix = tuple(unity_matrix) | ||
| return Matrix(unity_matrix) | ||
| @classmethod | ||
| def ones(cls, dim): | ||
| ones_matrix = [] | ||
| if dim == 1: | ||
| ones_matrix = (1,) | ||
| return 'Matrix({})'.format(ones_matrix) | ||
| sub_list = [0] * dim | ||
| for i in range(dim): | ||
| ones_matrix.append([0] * dim) | ||
| for i in range(dim): | ||
| for j in range(dim): | ||
| sub_list[j] = 1 | ||
| ones_matrix[i] = tuple([element for element in sub_list]) | ||
| ones_matrix = tuple(ones_matrix) | ||
| return Matrix(ones_matrix) | ||
| tuples = property(get_tuple) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| from Matrix import Matrix | ||
| def main(): | ||
| a = Matrix(((1, 2, 3), (4, 5, 6), (7, 8, 9))) | ||
| b = Matrix(((6, 1, 5), (3, 1, 4), (-7, 4, 1))) | ||
| c = Matrix(((1, 0, 0), (0, 1, 0), (0, 0, 1))) | ||
| # print(type(a)) | ||
| # print(repr(a != b)) | ||
| # print(repr(a.tuples)) | ||
| # print(repr(Matrix.ones(5))) | ||
| # print(b) | ||
| print(a * b) | ||
| # print(a.my_list) | ||
| # print(a.tuples) | ||
| # print(a + b) | ||
| # print(a * b) | ||
| # print(Matrix.unity(3).my_tuple) | ||
| print(repr(c - Matrix.ones(3))) | ||
| # print(c == (Matrix.unity(3))) | ||
| for line in a: | ||
| print(line) | ||
| dictionary = {Matrix(((1, 1), (2, 2))): 2, Matrix(((1, 1), (2, 3))): 3} | ||
| print(dictionary) | ||
| if __name__ == '__main__': | ||
| main() |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| import sys | ||
| FILE_NAME = 1 | ||
| FILE_NAME_NEW = "D:/Python Training/Pr1/stam_new.txt" | ||
| def write_to_file(file_name, FILE_NAME_NEW, first_name, last_name): | ||
| """ Read from file stam.txt line by line | ||
| and write to file stam_new.txt with the first and last name | ||
| """ | ||
| try: # if stam.txt exist | ||
| with open(file_name, 'r') as orig_data: | ||
| for line in orig_data: | ||
| # print(line.strip()) | ||
| line = line.strip() | ||
| with open(FILE_NAME_NEW, 'a') as new_file: | ||
| new_file.write(first_name + ' ' + line.replace("\n", " ") | ||
| + ' ' + last_name + '\n') | ||
| except Exception as err: # if stam.txt doesn't exist | ||
| print(err) # print the error | ||
| def receive_first_name(): | ||
| return input('Please enter your first name: ') | ||
| def receive_last_name(): | ||
| return input('Please enter your last name: ') | ||
| def main(): | ||
| file_name = sys.argv[FILE_NAME] | ||
| first_name = receive_first_name() | ||
| last_name = receive_last_name() | ||
| write_to_file(file_name, FILE_NAME_NEW, first_name, last_name) | ||
| if __name__ == "__main__": | ||
| main() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you can skip this and just send
(*new_list,)to the matrix creation