nighthawkcoders / teacher

Jupyter Notebooks and Posts dedicated to learning Python
MIT License
0 stars 63 forks source link

P1 Procedures | Compci Blogs #44

Open utterances-bot opened 10 months ago

utterances-bot commented 10 months ago

P1 Procedures | Compci Blogs

Lesson for developing procedures in Python

https://nighthawkcoders.github.io/teacher/2023/10/22/P1_Procedures_IPYNB2.html

shuban-789 commented 10 months ago

import random import math

def is_prime(n, k=5): if n <= 1: return False if n <= 3: return True

def miller_rabin(n, d):
    a = random.randint(2, n - 2)
    x = pow(a, d, n)
    if x == 1 or x == n - 1:
        return True
    for _ in range(r - 1):
        x = pow(x, 2, n)
        if x == n - 1:
            return True
    return False

r, d = 0, n - 1
while d % 2 == 0:
    r += 1
    d //= 2

for _ in range(k):
    if not miller_rabin(n, d):
        return False
return True

def generate_prime(bits): while True: p = random.getrandbits(bits) if p % 2 == 0: p += 1 if is_prime(p): return p

def mod_inverse(a, m): g, x, y = extended_gcd(a, m) if g != 1: raise ValueError("DNE: Modular inverse does not exist") return x % m

def extended_gcd(a, b): if a == 0: return (b, 0, 1) else: g, x, y = extended_gcd(b % a, a) return (g, y - (b // a) * x, x)

def rsa_keygen(bits=2048): p = generate_prime(bits) q = generate_prime(bits) n = p q phi = (p - 1) (q - 1) e = 65537 d = mod_inverse(e, phi) return (n, e), (n, d)

def rsa_encrypt(public_key, plaintext): n, e = public_key cipher = pow(plaintext, e, n) return cipher

def rsa_decrypt(private_key, ciphertext): n, d = private_key plaintext = pow(ciphertext, d, n) return plaintext

if name == "main": public_key, private_key = rsa_keygen(bits=2048) plaintext = input("Enter plaintext: ") plaintext = int.from_bytes(plaintext.encode(), byteorder='big')

ciphertext = rsa_encrypt(public_key, plaintext)
decrypted = rsa_decrypt(private_key, ciphertext)

decrypted_text = decrypted.to_bytes((decrypted.bit_length() + 7) // 8, byteorder='big').decode()

print(f"Public Key (n, e): "+str(public_key))
print(f"Private Key (n, d): "+str(private_key))
print(f"Encrypted: "+str(ciphertext))
print(f"Decrypted: "+str(decrypted_text))
shuban-789 commented 10 months ago

Please ignore the code above.

Return e to the power of parameter

def procedural_abstraction(x): e = 2.718 return e**x

Return sum of two numbers num1 and num2

def sum(num1, num2): return num1 + num2

Upon running the program, print e^2 and sum of 5 and 7

if name == "main": print(procedural_abstraction(2)) print(sum(5, 7))

alaraipek commented 10 months ago

https://alaraipek.github.io/student//2023/10/24/Developing-Procedures_IPYNB_2_.html

nitinsandiego commented 10 months ago

https://nitinsandiego.github.io/student//2023/10/24/3.12-And-3.13_IPYNB_2_.html

ShakeSphereStuff commented 10 months ago

First: def exponenchal(exponent, simTime): count = "" for x in range(0, simTime + 1): count += f"Term {x}; {exponent x}:\n\t" for y in range(1, (exponent x) + 1): count += str(y%10) if y%10 == 0: count += f" {y}\'s " count += "\n" return count nums = exponenchal(2, 14) print(nums)

Second:

array1 = [0, 2, 4, 6, 8] array2 = [1, 3, 5, 7, 9]

def summingMachine(num1, num2): if(isinstance(num1, list) == False and isinstance(num2, list) == False): return sum([num1, num2]) elif(isinstance(num1, list) == True and isinstance(num2, list) == True): return sum(num1) + sum(num2) else: return "Error" print(summingMachine(array1, array2)) print(summingMachine(5, 8))

AidanLau10 commented 10 months ago

https://aidanlau10.github.io/student//2023/10/24/procedure_IPYNB_2_.html

monke7769 commented 10 months ago

https://monke7769.github.io/copy1/312313

AustinZhang1 commented 10 months ago

Function that finds the volume of a rectangular prism with the length, width, and height

def procedural_abstraction(length, width, height): volume = length width height return volume print(procedural_abstraction(5,2,3))

Function to sum two numbers

def summing_machine(first_number, second_number): sum = first_number + second_number return sum print(summing_machine(7, 5))

Flying-Book commented 10 months ago
def procedural_abstraction(num):
    print(num*2)
procedural_abstraction(5) 

def summing_machine(num1, num2):
    print(num1+num2)
summing_machine(5, 6)
AnvayYadav commented 10 months ago

def procedural_abstraction(parameter):

Perform a specific task

result = parameter * 2
return result

Function to calculate the sum of two numbers

def summing_machine(first_number, second_number):

Perform the specific task of adding two numbers

sum_result = first_number + second_number
return sum_result

Example usage of procedural_abstraction function

input_value = 5 abstraction_result = procedural_abstraction(input_value) print("Result of procedural_abstraction:", abstraction_result)

Example usage of summing_machine function

num1 = 7 num2 = 5 sum_result = summing_machine(num1, num2) print(f"The sum of {num1} and {num2} is:", sum_result)

ChrisP121 commented 10 months ago

1. def calculate_rectangle_area(length, width): area = length * width return area

length = 6 width = 4 area_of_rectangle = calculate_rectangle_area(length, width) print("The area of the rectangle is:", area_of_rectangle) 2. def summing_machine(first_number, second_number): sum_result = first_number + second_number return sum_result

result = summing_machine(7, 5) print("The sum of 7 and 5 is:", result)

AkshayNagesh commented 10 months ago

Demonstrating procedural abstraction

def procedural_abstraction(task): if task == "greet": return "Hello, world!" elif task == "goodbye": return "Goodbye, world!" else: return "Unknown task"

Function to add two numbers

def summing_machine(first_number, second_number): return first_number + second_number

Calculate and print the sum of 7 and 5

result = summing_machine(7, 5) print(result)

SriS126 commented 10 months ago

https://sris126.github.io/student//2023/10/26/ProceduresHW_IPYNB_2_.html

SrinivasNampalli commented 10 months ago

def procedural_abstraction(number1, number2): result = number1 * number2 return result

x = 5 y = 7 product = procedural_abstraction(x, y) print("The product of {} and {} is: {}".format(x, y, product))

2

def summing_machine(first_number, second_number): result = first_number + second_number return result

sum_result = summing_machine(7, 5)

print("The sum of 7 and 5 is:", sum_result)

Nathaniel633 commented 10 months ago

https://nathaniel633.github.io/student//2023/10/26/Procedures_IPYNB_2_.html

devaSas1 commented 10 months ago

homework

def summing_machine(first_number, second_number):

total = first_number + second_number

return total

result = summing_machine(7, 5)

print(f"The sum of 7 and 5 is: {result}")

This fills the requirement for both homework hacks at the same time

spooketti commented 10 months ago

Problem 1

# demonstrating procedural abstraction to find the sum of (3 * 5)+(6*2)
def multip(a,b):
    return a*b

def solveEq(a,b,c,d):
    print(f"({a}*{b})+({c}*{d})={multip(a,b)+multip(c,d)}")

Problem 2

def summing_machine(a,b):
    return a+b
print(f"the sum of 5+7={summing_machine(5,7)}")

Problem 2 Java implementation

class Main {
  public static int summing(int a,int b)
  {
    return a+b;
  }
  public static void main(String[] args) {
    System.out.println("the sum of 5+7 is " + summing(5,7));
  }
}
RayyanDarugar commented 10 months ago

https://rayyandarugar.github.io/student//2023/10/25/ProceduresHW_IPYNB_2_.html

tarunja1ks commented 10 months ago

https://tarunja1ks.github.io/student//2023/10/29/Procedureshw_IPYNB_2_.html

tarasehdave commented 10 months ago

https://tarasehdave.github.io//c4.1/2023/10/26/ProceduresTT_IPYNB_2_.html

DavidL0914 commented 10 months ago

HW1: def procedural_abstraction(a, b): result = a + b return result

Example usage of procedural_abstraction

x = 7 y = 5 result = procedural_abstraction(x, y) print(f"The sum of {x} and {y} is {result}")

HW2: def summing_machine(first_number, second_number): sum_result = first_number + second_number return sum_result

Calculate the sum of 7 and 5 using the summing_machine function

num1 = 7 num2 = 5 total = summing_machine(num1, num2) print(f"The sum of {num1} and {num2} is {total}")

peytonl11098 commented 10 months ago

1 Procedural Abstraction

def procedural_abstraction(number1, number2): result = number1 * number2 return result

x = 5 y = 7 product = procedural_abstraction(x, y) print("The product of {} and {} is: {}".format(x, y, product))

2 Summing Machine

def summing_machine(first_number, second_number): result = first_number + second_number return result

sum_result = summing_machine(5, 7)

print("The sum of 5 and 7 is:", sum_result)

aidenk1 commented 10 months ago
  1. def procedural_abstraction(input_value): result = input_value * 3 # Multiply the input by 3 return result input_number = 7 abstraction_result = procedural_abstraction(input_number) print(f"Result of procedural abstraction for {input_number}: {abstraction_result}")
  2. def summing_machine(first_number, second_number): total = first_number + second_number return total result = summing_machine(7, 5) print("The sum of 7 and 5 is:", result)
Djxnxnx commented 10 months ago

https://djxnxnx.github.io/student//5.a/c4.1/2023/10/30/procedureshomework_IPYNB_2_.html

sharonkodali commented 10 months ago

https://sharonkodali.github.io/sharonk//2023/11/01/procedures_IPYNB_2_.html

alishahussain commented 10 months ago

https://alishahussain.github.io/student2//2023/10/24/procedural_abstraction_IPYNB_2_.html

Tanuj253 commented 10 months ago

https://tanuj253.github.io/student//5.a/c4.1/2023/10/27/procedures_IPYNB_2_.html

TejM123 commented 10 months ago

sorry i forgot to submit:

https://tejm123.github.io/student2//5.a/c4.1/2023/10/30/proceduresHW_IPYNB_2_.html

parkib commented 10 months ago

Q1: def procedural_abstraction(value): return value ** 2

result = procedural_abstraction(4) print("Result:", result)

Q2: def summing_machine(first_number, second_number): return first_number + second_number

result = summing_machine(7, 5) print("Sum:", result)

SOoctosnake commented 10 months ago

https://sooctosnake.github.io/student_october//2023/10/24/procedures.html

vidhaganji commented 10 months ago

1)

def procedural_abstraction(number):

Calculate the square of the input number

result = number ** 2
return result

Call the procedural_abstraction function with a parameter

input_number = 5 square_result = procedural_abstraction(input_number)

Print the result

print(f"The square of {input_number} is {square_result}")

2)

def summing_machine(first_number, second_number):

Calculate the sum of the two input numbers

result = first_number + second_number
return result

Call the summing_machine function with parameters 7 and 5

result_sum = summing_machine(7, 5)

Print the result

print(f"The sum of 7 and 5 is {result_sum}")

vibha-yganji commented 10 months ago

1)

def procedural_abstraction(number):

Calculate the square of the input number

result = number ** 2
return result

Call the procedural_abstraction function with a parameter

input_number = 5 square_result = procedural_abstraction(input_number)

Print the result

print(f"The square of {input_number} is {square_result}")

2)

def summing_machine(first_number, second_number):

Calculate the sum of the two input numbers

result = first_number + second_number
return result

Call the summing_machine function with parameters 7 and 5

result_sum = summing_machine(7, 5)

Print the result

print(f"The sum of 7 and 5 is {result_sum}")