Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
8e5143d
Completed lab-00
fuetser Sep 19, 2022
a94006e
Completed lab1
fuetser Oct 2, 2022
eb69249
Completed lab-01
fuetser Oct 2, 2022
2a5dc50
Completed lab-01
fuetser Oct 2, 2022
3c2ac2e
Completed lab-01
fuetser Oct 2, 2022
b768a80
Completed lab-01
fuetser Oct 2, 2022
a4dc7ad
Completed lab-01
fuetser Oct 5, 2022
ccc77a8
Completed lab-01
fuetser Oct 5, 2022
6314f66
Done
fuetser Oct 12, 2022
e1ed86d
Done
fuetser Oct 12, 2022
4e33aeb
Done
fuetser Oct 12, 2022
8874973
Done
fuetser Oct 12, 2022
a846b3c
Done
fuetser Oct 12, 2022
a115a39
Done
fuetser Oct 12, 2022
5e1a584
Done
fuetser Oct 29, 2022
f9437ef
Done
fuetser Oct 29, 2022
2557fd6
Done
fuetser Oct 29, 2022
bd57e7f
Done
fuetser Oct 29, 2022
3c2329a
Done
fuetser Nov 1, 2022
f6d92ae
Done
fuetser Nov 1, 2022
ea3a136
Done
fuetser Nov 1, 2022
1b9c77f
Done!
fuetser Nov 1, 2022
a288218
Temp commit
fuetser Nov 10, 2022
3f1fab4
done
fuetser Nov 18, 2022
b87d190
Update cs102.yml
fuetser Nov 18, 2022
4d0567f
Formated imports
fuetser Nov 18, 2022
b2a5a8d
Changed python version
fuetser Nov 18, 2022
b214e4b
Merge branch 'homework04' of https://github.com/fuetser/fuetser into …
fuetser Nov 18, 2022
adcca95
Done
fuetser Nov 18, 2022
0ea139e
Update cs102.yml
fuetser Nov 20, 2022
07602ac
Done:
fuetser Nov 20, 2022
13491ae
Merge branch 'homework03' of https://github.com/fuetser/fuetser into …
fuetser Nov 20, 2022
5eca32b
Done
fuetser Nov 25, 2022
d2aa9ed
Fixed imports
fuetser Nov 25, 2022
a6974cc
done
fuetser Nov 25, 2022
5a34e01
done
fuetser Nov 25, 2022
1d5935d
Added missing library
fuetser Nov 25, 2022
e1a8b2b
done
fuetser Nov 25, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/cs102.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Set up Python 3.8.6
- name: Set up Python 3.10.7
uses: actions/setup-python@v2
with:
python-version: '3.8.6'
python-version: '3.10.7'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,6 @@ coverage.xml
# Mypy cache
.mypy_cache/

venv
.pyvcs
pyvcs.*
2 changes: 1 addition & 1 deletion homework00/hello.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
def get_greeting(name: str) -> str:
pass
return f"Hello, {name}!"


if __name__ == "__main__":
Expand Down
46 changes: 14 additions & 32 deletions homework01/caesar.py
Original file line number Diff line number Diff line change
@@ -1,46 +1,28 @@
import typing as tp
from string import ascii_lowercase as lower
from string import ascii_uppercase as upper


def encrypt_caesar(plaintext: str, shift: int = 3) -> str:
"""
Encrypts plaintext using a Caesar cipher.

>>> encrypt_caesar("PYTHON")
'SBWKRQ'
>>> encrypt_caesar("python")
'sbwkrq'
>>> encrypt_caesar("Python3.6")
'Sbwkrq3.6'
>>> encrypt_caesar("")
''
"""
ciphertext = ""
# PUT YOUR CODE HERE
for char in plaintext:
if char.isalpha():
if char.islower():
char = lower[(lower.index(char) + shift) % 26]
else:
char = upper[(upper.index(char) + shift) % 26]
ciphertext += char
return ciphertext


def decrypt_caesar(ciphertext: str, shift: int = 3) -> str:
"""
Decrypts a ciphertext using a Caesar cipher.

>>> decrypt_caesar("SBWKRQ")
'PYTHON'
>>> decrypt_caesar("sbwkrq")
'python'
>>> decrypt_caesar("Sbwkrq3.6")
'Python3.6'
>>> decrypt_caesar("")
''
"""
plaintext = ""
# PUT YOUR CODE HERE
return plaintext
return encrypt_caesar(ciphertext, -shift)


def caesar_breaker_brute_force(ciphertext: str, dictionary: tp.Set[str]) -> int:
"""
Brute force breaking a Caesar cipher.
"""
best_shift = 0
# PUT YOUR CODE HERE
for shift in range(26):
decrypted_msg = decrypt_caesar(ciphertext, shift)
if decrypted_msg in dictionary:
best_shift = shift
return best_shift
55 changes: 22 additions & 33 deletions homework01/rsa.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,43 +3,32 @@


def is_prime(n: int) -> bool:
"""
Tests to see if a number is prime.

>>> is_prime(2)
True
>>> is_prime(11)
True
>>> is_prime(8)
False
"""
# PUT YOUR CODE HERE
pass
for div in range(2, int(n**0.5) + 1):
if n % div == 0:
return False
return n != 1


def gcd(a: int, b: int) -> int:
"""
Euclid's algorithm for determining the greatest common divisor.
if b == 0:
return a
return gcd(b, a % b)

>>> gcd(12, 15)
3
>>> gcd(3, 7)
1
"""
# PUT YOUR CODE HERE
pass

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

def multiplicative_inverse(e: int, phi: int) -> int:
"""
Euclid's extended algorithm for finding the multiplicative
inverse of two numbers.

>>> multiplicative_inverse(7, 40)
23
"""
# PUT YOUR CODE HERE
pass
def multiplicative_inverse(a: int, n: int) -> int:
d, x, y = gcdex(a, n)
if d == 1:
return (x % n + n) % n
else:
return 0


def generate_keypair(p: int, q: int) -> tp.Tuple[tp.Tuple[int, int], tp.Tuple[int, int]]:
Expand All @@ -49,10 +38,10 @@ def generate_keypair(p: int, q: int) -> tp.Tuple[tp.Tuple[int, int], tp.Tuple[in
raise ValueError("p and q cannot be equal")

# n = pq
# PUT YOUR CODE HERE
n = p * q

# phi = (p-1)(q-1)
# PUT YOUR CODE HERE
phi = (p - 1) * (q - 1)

# Choose an integer e such that e and phi(n) are coprime
e = random.randrange(1, phi)
Expand Down Expand Up @@ -85,7 +74,7 @@ def decrypt(pk: tp.Tuple[int, int], ciphertext: tp.List[int]) -> str:
# Unpack the key into its components
key, n = pk
# Generate the plaintext based on the ciphertext and key using a^b mod m
plain = [chr((char ** key) % n) for char in ciphertext]
plain = [chr((char**key) % n) for char in ciphertext]
# Return the array of bytes as a string
return "".join(plain)

Expand Down
40 changes: 14 additions & 26 deletions homework01/vigenere.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,18 @@
def encrypt_vigenere(plaintext: str, keyword: str) -> str:
"""
Encrypts plaintext using a Vigenere cipher.
from caesar import decrypt_caesar, encrypt_caesar

>>> encrypt_vigenere("PYTHON", "A")
'PYTHON'
>>> encrypt_vigenere("python", "a")
'python'
>>> encrypt_vigenere("ATTACKATDAWN", "LEMON")
'LXFOPVEFRNHR'
"""
ciphertext = ""
# PUT YOUR CODE HERE
return ciphertext

def encrypt_vigenere(plaintext: str, keyword: str, encode: bool = True) -> str:
ciphertext = []
for index, char in enumerate(plaintext.lower()):
shift = ord(keyword[index % len(keyword)].lower()) - 97
if encode:
ciphertext.append(encrypt_caesar(char, shift))
else:
ciphertext.append(decrypt_caesar(char, shift))
if plaintext[index].isupper():
ciphertext[-1] = ciphertext[-1].upper()
return "".join(ciphertext)

def decrypt_vigenere(ciphertext: str, keyword: str) -> str:
"""
Decrypts a ciphertext using a Vigenere cipher.

>>> decrypt_vigenere("PYTHON", "A")
'PYTHON'
>>> decrypt_vigenere("python", "a")
'python'
>>> decrypt_vigenere("LXFOPVEFRNHR", "LEMON")
'ATTACKATDAWN'
"""
plaintext = ""
# PUT YOUR CODE HERE
return plaintext
def decrypt_vigenere(ciphertext: str, keyword: str) -> str:
return encrypt_vigenere(ciphertext, keyword, encode=False)
Loading