From e549f5cb02b865601aa398161d164a821de0340e Mon Sep 17 00:00:00 2001 From: hfarouk99 Date: Thu, 7 Jul 2022 21:51:57 +0200 Subject: [PATCH] lab functions hassan --- main.py | 312 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 312 insertions(+) create mode 100644 main.py diff --git a/main.py b/main.py new file mode 100644 index 0000000..446fb72 --- /dev/null +++ b/main.py @@ -0,0 +1,312 @@ +#!/usr/bin/env python +# coding: utf-8 + +# # Functions + +# On this lab we will put to practice some of the concepts we have learned on this past few days. +# +# `NOTE: On this lab you should try to write all the functions yourself using only the most basic of python syntax and without functions such as len, count, sum, max, min, in, etc. Give it a try. 🧑ðŸŧ‍ðŸ’ŧðŸ‘ĐðŸŧ‍ðŸ’ŧ` +# +# The cell after each exercise contains a few tests to check if your function works as expected. + +# In[2]: + + +from testing import * +import unittest + + +# ## 1. Write a function that returns the greater of two numbers + +# In[21]: + + +def greater(a,b): + if a > b: + return (a) + else: + return (b) +greater (5,3) + + +# In[22]: + + +# This will test your function +test_greater(greater) + + +# ## 2. Now write a function that returns the largest element on a list + +# In[153]: + + +def greatest(arr): + x = 0 + for i in arr: + if i > x: + x=i + return x + + +# In[154]: + + +# This will test your function +test_greatest(greatest) + + +# ## 3. Write a function that sums all the elements on a list + +# In[160]: + + +list_1 = [] +def sum_all(arr): + x=0 + for i in arr: + x+=i + return x + + +# In[161]: + + +# This will test your function +test_sum(sum_all) + + +# ## 4. Write another function that multiplies all the elements on a list + +# In[178]: + + +def mult_all(arr): + x=1 + for i in arr: + x*=i + return x + + +# In[179]: + + +# This will test your function +test_mult(mult_all) + + +# ## 5. Now combine those two ideas and write a function that receives a list and either "+" or "*" and outputs acordingly + +# In[218]: + + +def oper_all(arr, oper): + if oper=='*': + c=1 + for i in arr: + c*=i + return c + if oper=="+": + x=0 + for i in arr: + x+=i + return x + + +# In[219]: + + +# This will test your function +test_operations(oper_all) + + +# ## 6. Write a function that returns the factorial of a number. + +# In[220]: + + +def factorial(n): + arr=[] + i=1 + s=1 + while i= 8 and upper_letter and lower_letter and special_boolean and numbers_boolean: + return True + else: + return False + + +# In[217]: + + +# This will test your function +test_pass(check_pass) + + +# In[ ]: + + + +