From 75439cc7bde59ea4febd4fdbf7b98cab8d754b4b Mon Sep 17 00:00:00 2001 From: Abdulrhman Altabali <70818818+altbalybalrhmn@users.noreply.github.com> Date: Tue, 31 Jan 2023 22:30:03 +0100 Subject: [PATCH] odev odev --- odev_1.py | 26 ++++++++++++++++++ odev_2.py | 9 +++++++ odev_3.py | 15 +++++++++++ odev_4.py | 0 operations.py | 75 +++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 125 insertions(+) create mode 100644 odev_1.py create mode 100644 odev_2.py create mode 100644 odev_3.py create mode 100644 odev_4.py create mode 100644 operations.py diff --git a/odev_1.py b/odev_1.py new file mode 100644 index 0000000..e645a71 --- /dev/null +++ b/odev_1.py @@ -0,0 +1,26 @@ + +from operations import factorial + +def pascal_triangle(n): + # Create a list to hold the rows of the triangle + triangle = [] + + # Loop through each row + for i in range(n): + # Create a list to hold the values in the row + row = [] + + # Loop through each position in the row + for j in range(i + 1): + # Calculate the value using the binomial coefficient formula + value = int(factorial(i) / (factorial(j) *factorial(i - j))) + + # Append the value to the row + row.append(value) + + # Append the row to the triangle + triangle.append(row) + + return triangle + +pascal_triangle(10) \ No newline at end of file diff --git a/odev_2.py b/odev_2.py new file mode 100644 index 0000000..4dfd9a1 --- /dev/null +++ b/odev_2.py @@ -0,0 +1,9 @@ +def hyphen_to_sorted(): + items=[] + items=[n for n in input('enter an string : ').split('-')] + items.sort() + print('-'.join(items)) + + +hyphen_to_sorted() + diff --git a/odev_3.py b/odev_3.py new file mode 100644 index 0000000..cf043ff --- /dev/null +++ b/odev_3.py @@ -0,0 +1,15 @@ +def perfect_number(n): + + + sum1 = 0 + for i in range(1, n): + if(n % i == 0): + sum1 = sum1 + i + if (sum1 == n): + print("The number is a Perfect number!") + else: + print("The number is not a Perfect number!") + +n = int(input("Enter any number: ")) + +perfect_number(n) \ No newline at end of file diff --git a/odev_4.py b/odev_4.py new file mode 100644 index 0000000..e69de29 diff --git a/operations.py b/operations.py new file mode 100644 index 0000000..37e23a8 --- /dev/null +++ b/operations.py @@ -0,0 +1,75 @@ +def add(a, b): + """This program adds two + numbers and return the result""" + + result = a + b + return result + +def subtract(a, b): + """This program adds two + numbers and return the result""" + + result = a - b + return result + +def multiply(a, b): + """This program adds two + numbers and return the result""" + + result = a * b + return result + +def divide(a, b): + """This program adds two + + numbers and return the result""" + + result = a / b + return result + +def power(a, b): + """This program adds two + numbers and return the result""" + result = a ** b + return result + +def modulo(a, b): + """This program adds two + numbers and return the result""" + + + result = a % b + return result + +def sqroot(a): + + result = a ** 0.5 + return result + + result = a ** b + return result + +#recurrsion +def factorial(n): + if n<=1: #stop machanizim + return 1 + else: + return n*factorial(n-1) #factorial +#factorical (4) +#4*factorical(3) +#4*3*factorial(2) +#4*3*2*factorial(1) +#4*3*2*1 + + +def fib(n): + if n<=2: #stop machanisim + return 1 + else : + return fib(n-1)+fib(n-2) #fiboncai function + +fib(10) + +#it is also possible to define a function + +PI = 3.1415926535897932 \ No newline at end of file