summary refs log tree commit diff stats
path: root/python/code/3
diff options
context:
space:
mode:
authorSudipto Mallick <>2024-01-02 03:38:10 +0000
committerSudipto Mallick <>2024-01-02 03:38:10 +0000
commit7451edff7692e86c5238ff7bc6659825e242a84e (patch)
tree830fe25caf403869fe77df83bc44822ea6f5f11d /python/code/3
parenta70e0a59817ce06a3dd23b3750ae16ee6660deaf (diff)
downloadzadania-7451edff7692e86c5238ff7bc6659825e242a84e.tar.gz
Quick backup, need to be rewritten
Diffstat (limited to 'python/code/3')
-rw-r--r--python/code/3/3.py11
-rw-r--r--python/code/3/4.py25
-rw-r--r--python/code/3/5.py5
-rw-r--r--python/code/3/6.py27
-rw-r--r--python/code/3/7.py9
5 files changed, 77 insertions, 0 deletions
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)