From ca5a4e33a75156f5ce45b885563e6a3ab6734dfb Mon Sep 17 00:00:00 2001 From: Saurabh Malik Date: Mon, 19 Apr 2021 09:21:21 -0700 Subject: [PATCH 1/7] update quiz --- Quizzes/quiz01.md | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/Quizzes/quiz01.md b/Quizzes/quiz01.md index 382bccc..96b0e3f 100644 --- a/Quizzes/quiz01.md +++ b/Quizzes/quiz01.md @@ -2,12 +2,12 @@ **In Python 3, the maximum value for an integer is 263 - 1:** -1. [ ] False +1. [ X ] False 1. [ ] True **How would you express the hexadecimal value a5 as a base-16 integer constant in Python?** -1. [ ] 0xa5 +1. [X ] 0xa5 1. [ ] a5x0 1. [ ] 00xa5 1. [ ] 0xaa @@ -15,10 +15,10 @@ **Which of the following are valid ways to specify the string literal blue'car in Python:** -1. [ ] """blue'car""" +1. [X ] """blue'car""" 1. [ ] 'blue''car' -1. [ ] 'blue\'car' -1. [ ] "blue'car" +1. [ X] 'blue\'car' +1. [ X] "blue'car" 1. [ ] 'blue'car' **Which of the following is the correct output?** @@ -30,12 +30,12 @@ print(r'blue\\car\nwindow') 1. [ ] blue\car\nwindow 1. [ ] blue\\carwindow 1. [ ] blue\car window -1. [ ] blue\\car\nwindow +1. [X ] blue\\car\nwindow **Which of the following is not a Python built-in function:** 1. [ ] round() 1. [ ] repr() -1. [ ] diff() +1. [X ] diff() 1. [ ] map() 1. [ ] isinstance() From 5d71ea3b36c2a459bca7390f353838684a0e166e Mon Sep 17 00:00:00 2001 From: Saurabh Malik Date: Mon, 19 Apr 2021 09:34:46 -0700 Subject: [PATCH 2/7] simple calc --- my_labs_solution/simple_calculator.py | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100755 my_labs_solution/simple_calculator.py diff --git a/my_labs_solution/simple_calculator.py b/my_labs_solution/simple_calculator.py new file mode 100755 index 0000000..8d5ba03 --- /dev/null +++ b/my_labs_solution/simple_calculator.py @@ -0,0 +1,10 @@ +#importing math lib to get the value of pi +import math + +#get the radius from the user, convert to float as radius can be anything +radius = float(input("Please Provide the Radius of the Circle: ")) + +#calculate the circumference with 2*pi*r +Circumference = 2 * math.pi * radius + +print(Circumference) \ No newline at end of file From 926bf41a797c945faee6aaf644b1bdd2c9d7f093 Mon Sep 17 00:00:00 2001 From: Saurabh Malik Date: Mon, 19 Apr 2021 12:14:05 -0700 Subject: [PATCH 3/7] lab3 solution calc --- my_labs_solution/lab3_sol.py | 70 ++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100755 my_labs_solution/lab3_sol.py diff --git a/my_labs_solution/lab3_sol.py b/my_labs_solution/lab3_sol.py new file mode 100755 index 0000000..b7086bc --- /dev/null +++ b/my_labs_solution/lab3_sol.py @@ -0,0 +1,70 @@ + +def add(fnum, snum): + try: + result = float(fnum) + float(snum) + except Exception as e: + result = 'N/A' + print(e) + print('something went wrong') + finally: + print(f'Result:{result}') + +def sub(fnum, snum): + try: + result = float(fnum) - float(snum) + except Exception as e: + result = 'N/A' + print(e) + print('something went wrong') + finally: + print(f'Result:{result}') + +def mul(fnum, snum): + try: + result = float(fnum) * float(snum) + except Exception as e: + result = 'N/A' + print(e) + print('something went wrong') + finally: + print(f'Result:{result}') + +def div(fnum, snum): + try: + result = float(fnum)/float(snum) + except ZeroDivisionError as e: + result = 'N/A' + print(e) + print('snum cannot be zero for operation "/"') + except Exception as ex: + result = 'N/A' + print(ex) + print('something went wrong') + finally: + print(f'Result:{result}') + + +def calculate(operation, fnum, snum): + if(operation in ('+', '-', '*', '/')): + if(operation == '+'): + add(fnum, snum) + elif (operation == '-'): + sub(fnum, snum) + elif (operation == '*'): + mul(fnum, snum) + else: + div(fnum, snum) + else: + print(f'Operation not supported, {operation}\nOperations supported are : +, -, *, /') + +def main(): + #get the operation + operation = input("Please enter Operation: ") + #get the input params + fnum = input("Please enter First Num: ") + snum = input("Please enter the Second Num: ") + #calculate + calculate(operation, fnum, snum) + +if __name__ == "__main__": + main() \ No newline at end of file From fd1d233afa404c47e437577e544ae59582cf7e98 Mon Sep 17 00:00:00 2001 From: Saurabh Malik Date: Mon, 19 Apr 2021 12:42:00 -0700 Subject: [PATCH 4/7] quiz2 updates --- Quizzes/quiz02.md | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/Quizzes/quiz02.md b/Quizzes/quiz02.md index 3ae7b5e..f5138a9 100644 --- a/Quizzes/quiz02.md +++ b/Quizzes/quiz02.md @@ -9,7 +9,7 @@ print(sum(xs)) ``` 1. [ ] It's impossible to know. -1. [ ] 13 +1. [ X ] 13 1. [ ] Something else. **In the Python statement x = a + 5 - b:** @@ -17,7 +17,7 @@ print(sum(xs)) - a and b are ________ - a + 5 - b is ________ -1. [ ] operands, an expression +1. [ X ] operands, an expression 1. [ ] terms, a group 1. [ ] operands, an equation 1. [ ] operators, a statement @@ -25,12 +25,12 @@ print(sum(xs)) **What is the value of the expression 100 / 25? 1. [ ] 4 -1. [ ] 4.0 +1. [ X ] 4.0 **You should use the == operator to determine whether objects of type float are equal.** 1. [ ] True -1. [ ] False +1. [ X ] False ```python 1.1 + 2.2 == 3.3 @@ -44,7 +44,7 @@ y = (x < 100.0) and isinstance(x, float) ``` 1. [ ] None -1. [ ] True +1. [ X ] True 1. [ ] Correct 1. [ ] 1 1. [ ] 0 @@ -52,7 +52,7 @@ y = (x < 100.0) and isinstance(x, float) **Which of the following are truthy:** -1. [ ] 0.000001 +1. [X ] 0.000001 1. [ ] 0 1. [ ] [] 1. [ ] True @@ -67,7 +67,7 @@ b = 200 ``` 1. [ ] 0 -1. [ ] 200 +1. [ X ] 200 1. [ ] 100 1. [ ] False 1. [ ] True @@ -80,11 +80,11 @@ from math import sqrt x > 0 and sqrt(x) ``` 1. [ ] Yes -1. [ ] No +1. [ X ] No **Which of the following operators has the lowest precedence?** -1. [ ] and +1. [ X ] and 1. [ ] not 1. [ ] % 1. [ ] + @@ -92,7 +92,7 @@ x > 0 and sqrt(x) **What is the value of the expression 1 + 2 ** 3 * 4?** -1. [ ] 33 +1. [ X ] 33 1. [ ] 36 1. [ ] 108 1. [ ] 4097 @@ -106,13 +106,13 @@ x *= 5 x = x * 5 ``` -1. [ ] True +1. [ X ] True ** considering that both statements are not used in the same program one after the other else the first x returns 500, the second one returns 2500. The statements are equivalent one or the other can be used 1. [ ] False **The Python Enhancement Proposal (PEP) that enumerates stylistic guidelines for Python code is:** 1. [ ] PEP 8000 -1. [ ] PEP 8 +1. [ X ] PEP 8 1. [ ] PEP 257 1. [ ] PEP 20 @@ -126,16 +126,16 @@ x, y = 1, 2 print(x, y, z) ``` 1. [ ] True -1. [ ] False +1. [ X ] False **Variables must be declared before they are assigned a value.** 1. [ ] True -1. [ ] False +1. [ X ] False **Variables may be assigned a value of one type, and after be assigned a value of a different type.** -1. [ ] True +1. [ X ] True 1. [ ] False **How many objects and how many references are created by this program?** @@ -146,11 +146,11 @@ y = x 1. [ ] One object, one reference 1. [ ] Two objects, one reference 1. [ ] Two objects, two references -1. [ ] One object, two references +1. [ X ] One object, two references **PEP8 recommend which of the following styles for multi-word variable names:** 1. [ ] customerFirstName (Camel Case) 1. [ ] CustomerFirstName (Pascal Case) -1. [ ] customer_first_name (Snake Case) +1. [ X ] customer_first_name (Snake Case) 1. [ ] customer-first-name (Kebab Case) \ No newline at end of file From b054684b632583981344d0dabbdaa455791cf02d Mon Sep 17 00:00:00 2001 From: Saurabh Malik Date: Mon, 19 Apr 2021 12:46:04 -0700 Subject: [PATCH 5/7] quiz updates --- Quizzes/quiz02.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Quizzes/quiz02.md b/Quizzes/quiz02.md index f5138a9..8fcbfbe 100644 --- a/Quizzes/quiz02.md +++ b/Quizzes/quiz02.md @@ -145,8 +145,8 @@ y = x 1. [ ] One object, one reference 1. [ ] Two objects, one reference -1. [ ] Two objects, two references -1. [ X ] One object, two references +1. [ X ] Two objects, two references +1. [ ] One object, two references **PEP8 recommend which of the following styles for multi-word variable names:** From 146451964f8fdb0684a4731bdf1ad6e311d4d847 Mon Sep 17 00:00:00 2001 From: Saurabh Malik Date: Mon, 19 Apr 2021 12:53:47 -0700 Subject: [PATCH 6/7] lab3 comment resolve --- my_labs_solution/lab3_sol.py | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/my_labs_solution/lab3_sol.py b/my_labs_solution/lab3_sol.py index b7086bc..dac6e98 100755 --- a/my_labs_solution/lab3_sol.py +++ b/my_labs_solution/lab3_sol.py @@ -7,7 +7,7 @@ def add(fnum, snum): print(e) print('something went wrong') finally: - print(f'Result:{result}') + return result def sub(fnum, snum): try: @@ -17,7 +17,7 @@ def sub(fnum, snum): print(e) print('something went wrong') finally: - print(f'Result:{result}') + return result def mul(fnum, snum): try: @@ -27,7 +27,7 @@ def mul(fnum, snum): print(e) print('something went wrong') finally: - print(f'Result:{result}') + return result def div(fnum, snum): try: @@ -41,19 +41,19 @@ def div(fnum, snum): print(ex) print('something went wrong') finally: - print(f'Result:{result}') + return result def calculate(operation, fnum, snum): if(operation in ('+', '-', '*', '/')): if(operation == '+'): - add(fnum, snum) + return add(fnum, snum) elif (operation == '-'): - sub(fnum, snum) + return sub(fnum, snum) elif (operation == '*'): - mul(fnum, snum) + return mul(fnum, snum) else: - div(fnum, snum) + return div(fnum, snum) else: print(f'Operation not supported, {operation}\nOperations supported are : +, -, *, /') @@ -64,7 +64,8 @@ def main(): fnum = input("Please enter First Num: ") snum = input("Please enter the Second Num: ") #calculate - calculate(operation, fnum, snum) + result = calculate(operation, fnum, snum) + print(f'Result: {result}') if __name__ == "__main__": main() \ No newline at end of file From 86c42af7d7bccee70bb728129f9935f7b7dc5a6f Mon Sep 17 00:00:00 2001 From: Saurabh Malik Date: Tue, 20 Apr 2021 09:29:10 -0700 Subject: [PATCH 7/7] updates lab4_cal.py --- my_labs_solution/lab_4_calc.py | 142 +++++++++++++++++++++++++++++++++ 1 file changed, 142 insertions(+) create mode 100755 my_labs_solution/lab_4_calc.py diff --git a/my_labs_solution/lab_4_calc.py b/my_labs_solution/lab_4_calc.py new file mode 100755 index 0000000..713f859 --- /dev/null +++ b/my_labs_solution/lab_4_calc.py @@ -0,0 +1,142 @@ + + +def add(fnum, snum): + return fnum + snum + +def sub(fnum, snum): + return fnum - snum + +def mul(fnum, snum): + return fnum * snum + +def div(fnum, snum): + try: + result = fnum/snum + except ZeroDivisionError as e: + result = 'N/A' + print(e) + print('snum cannot be zero for operation "/"') + finally: + return result + + +def arithm_calc(operation, fnum, snum): + if(operation in ('+', '-', '*', '/')): + if(operation == '+'): + return add(fnum, snum) + elif (operation == '-'): + return sub(fnum, snum) + elif (operation == '*'): + return mul(fnum, snum) + else: + return div(fnum, snum) + else: + print(f'Operation not supported, {operation}\nOperations supported are : +, -, *, /') + +def list_attr_calc(list_1, list_2): + result = {} + list_1.sort() + list_2.sort() + result['list_1'] = {'min_value' : list_1[0], 'max_value' : list_1[-1], 'len' : len(list_1)} + result['list_2'] = {'min_value' : list_2[0], 'max_value' : list_2[-1], 'len' : len(list_2)} + return result + +def list_sort_calc(list_1, list_2): + result = [] + result.extend(list_1) + result.extend(list_2) + result.sort() + return result + +def set_calc(operation, list_1, list_2): + if operation in ('Union', 'Difference', 'Intersect'): + result = set() + s1 = {*list_1} + s2 = {*list_2} + if(operation == 'Union'): + result = s1.union(s2) + elif(operation == 'Difference'): + result = s1.difference(s2) + else: + result = s1.intersection(s2) + return result + else: + print(f'Operation not supported, {operation}\nOperation supported are : [Union, Difference, Intersect]') + +def check_value(num): + try: + val = float(num) + except Exception as e: + print(e) + print("num is not numeric") + finally: + return val + +def check_list(l): + for i in range(len(l)): + l[i] = check_value(l[i]) + return l + +def get_inputs_for_arithm(): + operation = input("Please enter Operation: ") + #get the input params + fnum = input("Please enter First Num: ") + fnum = check_value(fnum) + snum = input("Please enter the Second Num: ") + snum = check_value(snum) + + input_dict = {} + input_dict['operation'] = operation + input_dict['fnum'] = fnum + input_dict['snum'] = snum + return input_dict + +def get_inputs_for_list(): + st_list_1 = input("Please enter List1 in following format: 1,2,3,4,5: ") + list_1 = st_list_1.split(',') + list_1 = check_list(list_1) + st_list_2 = input("Please enter List2 in following format: 1,2,3,4,5: ") + list_2 = st_list_2.split(',') + list_2 = check_list(list_2) + input_dict = {} + input_dict['list1'] = list_1 + input_dict['list2'] = list_2 + return input_dict + +def get_inputs_for_set(): + operation = input("Please enter operation performed on set -> [Union, Difference, Intersect]: ") + st_list_1 = input("Please enter List1 in following format: 1,2,3,4,5: ") + list_1 = st_list_1.split(',') + list_1 = check_list(list_1) + st_list_2 = input("Please enter List2 in following format: 1,2,3,4,5: ") + list_2 = st_list_2.split(',') + list_2 = check_list(list_2) + input_dict = {} + input_dict['operation'] = operation + input_dict['list1'] = list_1 + input_dict['list2'] = list_2 + return input_dict + + +def main(): + #get the operation + mode = input("Please enter one of the following Modes[SET, ARITHM, LISTATTR, LISTSORT]: ") + if(mode == 'SET'): + input_dict = get_inputs_for_set() + result = set_calc(input_dict['operation'], input_dict['list1'], input_dict['list2']) + elif(mode == 'ARITHM'): + input_dict = get_inputs_for_arithm() + result = arithm_calc(input_dict['operation'], input_dict['fnum'], input_dict['snum']) + elif(mode == 'LISTATTR'): + input_dict = get_inputs_for_list() + result = list_attr_calc(input_dict['list1'], input_dict['list2']) + elif(mode == 'LISTSORT'): + input_dict = get_inputs_for_list() + result = list_sort_calc(input_dict['list1'], input_dict['list2']) + else: + print(f'Mode not supported, {mode}\nModes supported are : [SET, ARITHM, LISTATTR, LISTSORT]') + + print(f'Result: {result}') + +if __name__ == "__main__": + main() \ No newline at end of file