From 7451edff7692e86c5238ff7bc6659825e242a84e Mon Sep 17 00:00:00 2001 From: Sudipto Mallick <> Date: Tue, 2 Jan 2024 03:38:10 +0000 Subject: Quick backup, need to be rewritten --- python/code/3/3.py | 11 +++++++++++ python/code/3/4.py | 25 +++++++++++++++++++++++++ python/code/3/5.py | 5 +++++ python/code/3/6.py | 27 +++++++++++++++++++++++++++ python/code/3/7.py | 9 +++++++++ 5 files changed, 77 insertions(+) create mode 100644 python/code/3/3.py create mode 100644 python/code/3/4.py create mode 100644 python/code/3/5.py create mode 100644 python/code/3/6.py create mode 100644 python/code/3/7.py (limited to 'python/code/3') diff --git a/python/code/3/3.py b/python/code/3/3.py new file mode 100644 index 0000000..7f9a98f --- /dev/null +++ b/python/code/3/3.py @@ -0,0 +1,11 @@ +full_name = input("Enter your full name: ") +space_index = full_name.rfind(' ') + +if space_index == -1: + print(f"Your name in reverse: {full_name}") +else: + first_name = full_name[:full_name.index(' ')] + last_name = full_name[space_index + 1:] + print(f"Your name in reverse: {last_name}, {first_name}") + + diff --git a/python/code/3/4.py b/python/code/3/4.py new file mode 100644 index 0000000..3c0a566 --- /dev/null +++ b/python/code/3/4.py @@ -0,0 +1,25 @@ +def our_str_count(s, sub, start=None, end=None): + ss = s[slice(start, end)] + if len(sub) == 0: return len(ss) + 1 + elif len(ss) < len(sub): return 0 + i, l, ls = 0, len(ss), len(sub) + count = 0 + while i < l: + if ss[i:i+ls] == sub: + count += 1 + i += ls + else: + i += 1 + return count + +s = input('Enter a string: ') +sub = input('Enter substring to count: ') +idcs = input('Enter starting and ending indices (default: whole string): ') +if len(idcs) == 0: + idcs = (None, None) +else: + idcs = (int(i) for i in idcs.split()) +start, end = idcs + +print('Count:', our_str_count(s, sub, start, end)) + diff --git a/python/code/3/5.py b/python/code/3/5.py new file mode 100644 index 0000000..7db13a0 --- /dev/null +++ b/python/code/3/5.py @@ -0,0 +1,5 @@ +text = input('Enter a line of text: ') +vowels = set('aeiouAEIOU') +ctext = ''.join(c for c in text if c not in vowels) +print('Given text with all vowels removed:', ctext) + diff --git a/python/code/3/6.py b/python/code/3/6.py new file mode 100644 index 0000000..f9b4b99 --- /dev/null +++ b/python/code/3/6.py @@ -0,0 +1,27 @@ +import sys +message = input('Enter message: ') +key = None +try: + key = int(input('Enter key (1-25, -1 - -25): ')) +except ValueError: + print('Invalid key, must be an integer') + sys.exit(1) + +if key < 0: key += 26 +if not (1 <= key <= 25): + print('Invalid key, must be an integer with absolute value between 1 and 25, inclusive') + sys.exit(2) + +marr = bytearray(message, 'ascii') +for i in range(len(marr)): + c = marr[i] + if 64 < c < 64 + 27: + c = ((c - 65) + key) % 26 + 65 + elif 96 < c < 96 + 27: + c = ((c - 97) + key) % 26 + 97 + marr[i] = c + +print('Ceesar cipher with k =', key, 'applied to the given message:') +print(str(marr, encoding='ascii')) + + diff --git a/python/code/3/7.py b/python/code/3/7.py new file mode 100644 index 0000000..7a17f08 --- /dev/null +++ b/python/code/3/7.py @@ -0,0 +1,9 @@ +import sys +address = input('Enter an E-mail address: ') +if '@' not in address: + print(address, 'is not a valid E-mail address') + sys.exit(1) +at_position = address.rindex('@') +username, domain = address[:at_position], address[at_position+1:] +print('Username:', username) +print('Domain:', domain) -- cgit 1.4.1-2-gfad0