summary refs log tree commit diff stats
path: root/python/code
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
parenta70e0a59817ce06a3dd23b3750ae16ee6660deaf (diff)
downloadzadania-7451edff7692e86c5238ff7bc6659825e242a84e.tar.gz
Quick backup, need to be rewritten
Diffstat (limited to 'python/code')
-rw-r--r--python/code/1/1.txt17
-rw-r--r--python/code/1/2.txt52
-rw-r--r--python/code/1/3.txt17
-rw-r--r--python/code/1/4.py8
-rw-r--r--python/code/1/6.py4
-rw-r--r--python/code/1/7.py5
-rw-r--r--python/code/2/1.py27
-rw-r--r--python/code/2/2.py12
-rw-r--r--python/code/2/3.py6
-rw-r--r--python/code/2/4.py27
-rw-r--r--python/code/2/5.py35
-rw-r--r--python/code/2/6.py11
-rw-r--r--python/code/2/7.py9
-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
18 files changed, 307 insertions, 0 deletions
diff --git a/python/code/1/1.txt b/python/code/1/1.txt
new file mode 100644
index 0000000..827a09a
--- /dev/null
+++ b/python/code/1/1.txt
@@ -0,0 +1,17 @@
+>>> 2 + 3
+5
+>>> 1 + 2 * 3 - 4
+3
+>>> 12 * 5 / 3 * 2
+40.0
+>>> 5 / 8
+0.625
+>>> 23 // 7
+3
+>>> 17 % 5 - 1
+1
+>>> 3 * (4 + 5) + 5
+32
+>>> (2 + (7 + 2) / 3 - 4) / (-2 / 7 + (19 - 3 * 4) / 8)
+1.696969696969697
+
diff --git a/python/code/1/2.txt b/python/code/1/2.txt
new file mode 100644
index 0000000..08c95b5
--- /dev/null
+++ b/python/code/1/2.txt
@@ -0,0 +1,52 @@
+>>> import math
+>>> math.cos(math.pi / 3)
+0.5000000000000001
+>>> math.sin(math.pi / 6)
+0.49999999999999994
+>>> math.tan(math.pi / 4)
+0.9999999999999999
+>>> math.pow(1.123, 0.123)
+1.0143707323622344
+>>> math.exp(379)
+3.959210944514706e+164
+>>> math.log(10, 2)
+3.3219280948873626
+>>> math.hypot(3, 4)
+5.0
+>>> math.hypot(3, 4, 5)
+7.0710678118654755
+>>> math.degrees(math.pi / 4)
+45.0
+>>> math.radians(90) / math.pi
+0.5
+>>> [*(print(i, math.sqrt(i)) for i in range(1, 10))]
+1 1.0
+2 1.4142135623730951
+3 1.7320508075688772
+4 2.0
+5 2.23606797749979
+6 2.449489742783178
+7 2.6457513110645907
+8 2.8284271247461903
+9 3.0
+[None, None, None, None, None, None, None, None, None]>>> math.modf(12.5)
+(0.5, 12.0)
+>>> math.gamma(6)
+120.0
+>>> [math.floor(x) for x in [12.3, -12.3]]
+[12, -13]
+>>> [math.ceil(x) for x in [12.3, -12.3]]
+[13, -12]
+>>> [math.trunc(x) for x in [12.3, -12.3]]
+[12, -12]
+>>> math.cbrt(8), math.cbrt(10)
+(2.0, 2.154434690031884)
+>>> math.dist((1, 1), (2, 3))
+2.23606797749979
+>>> math.dist((1, 1), (5, 4))
+5.0
+>>> math.isqrt(23)
+4
+>>> math.exp2(10)
+1024.0
+
diff --git a/python/code/1/3.txt b/python/code/1/3.txt
new file mode 100644
index 0000000..90ebdbe
--- /dev/null
+++ b/python/code/1/3.txt
@@ -0,0 +1,17 @@
+>>> a, b = 3 + 2j, 4 - 3j
+>>> a + b, a - b, a * b, a / b
+((7-1j), (-1+5j), (18-1j), (0.24+0.68j))
+>>> -a, -b
+((-3-2j), (-4+3j))
+>>> (a + b) / (a - b)
+(-0.4615384615384615-1.3076923076923077j)
+>>> abs(a), abs(b)
+(3.605551275463989, 5.0)
+>>> a.imag, a.real
+(2.0, 3.0)
+>>> a.conjugate(), b.conjugate()
+((3-2j), (4+3j))
+>>> from cmath import phase
+>>> phase(a), phase(b)
+(0.5880026035475675, -0.6435011087932844)
+
diff --git a/python/code/1/4.py b/python/code/1/4.py
new file mode 100644
index 0000000..7579373
--- /dev/null
+++ b/python/code/1/4.py
@@ -0,0 +1,8 @@
+from math import gcd
+a = int(input('Enter first number: '))
+b = int(input('Enter second number: '))
+print('Sum:', a + b)
+print('Product:', a * b)
+print('Difference:', a - b)
+print('GCD:', gcd(a, b))
+
diff --git a/python/code/1/6.py b/python/code/1/6.py
new file mode 100644
index 0000000..e3a3cd0
--- /dev/null
+++ b/python/code/1/6.py
@@ -0,0 +1,4 @@
+a = float(input('Enter first number: '))
+b = float(input('Enter second number: '))
+print('Concatenation of integer parts:', ''.join(str(int(i)) for i in (a, b)))
+print('Sum rounded to 2 decimal places:', round(a + b, 2))
diff --git a/python/code/1/7.py b/python/code/1/7.py
new file mode 100644
index 0000000..37ecfa3
--- /dev/null
+++ b/python/code/1/7.py
@@ -0,0 +1,5 @@
+a = int(input('Enter first number: '))
+b = int(input('Enter second number: '))
+q, r = a // b, a % b
+q1d, r1d = q % 10, r % 10
+print(f'The one’s place digits of quotient and remainder are {"" if q1d == r1d else "not"} equal.')
diff --git a/python/code/2/1.py b/python/code/2/1.py
new file mode 100644
index 0000000..7ccd78f
--- /dev/null
+++ b/python/code/2/1.py
@@ -0,0 +1,27 @@
+def gaussian_sum_even(numbers):
+    assert len(numbers) % 2 == 0
+    start_index, end_index = 0, len(numbers) - 1
+    sum = 0
+    while start_index < end_index:
+        print(numbers[start_index], '+', numbers[end_index])
+        sum += numbers[start_index] + numbers[end_index]
+        start_index += 1
+        end_index -= 1
+    print('Result:', sum)
+
+gaussian_sum_even(list(range(1, 7)))
+
+def gaussian_sum(numbers):
+    start_index, end_index = 0, len(numbers) - 1
+    sum = 0
+    while start_index < end_index:
+        print(numbers[start_index], '+', numbers[end_index])
+        sum += numbers[start_index] + numbers[end_index]
+        start_index += 1
+        end_index -= 1
+    middle_value = numbers[len(numbers) // 2]
+    print(middle_value)
+    sum += middle_value
+    print('Result:', sum)
+
+gaussian_sum(list(range(1, 10)))
diff --git a/python/code/2/2.py b/python/code/2/2.py
new file mode 100644
index 0000000..bb3e9d2
--- /dev/null
+++ b/python/code/2/2.py
@@ -0,0 +1,12 @@
+def is_prime(n):
+    if n < 2: return False
+    for i in range(2, n):
+        if n % i == 0: return False
+    return True
+
+def filter_prime(numbers):
+    return [number for number in numbers if is_prime(number)]
+
+numbers = [int(n) for n in input('Enter a list of numbers: ').split()]
+primes = [number for number in numbers if is_prime(number)]
+print('List of primes:', primes)
diff --git a/python/code/2/3.py b/python/code/2/3.py
new file mode 100644
index 0000000..2a8dd10
--- /dev/null
+++ b/python/code/2/3.py
@@ -0,0 +1,6 @@
+items = [(0, 'd'), (1, 'a'), (2, 'c'), (3, 'b')]
+
+print(max(items, key=lambda it: it[0]))
+print(min(items, key=lambda it: it[1]))
+items.sort(key=lambda x: ord(x[1]) - x[0])
+print(items)
diff --git a/python/code/2/4.py b/python/code/2/4.py
new file mode 100644
index 0000000..21b1bf4
--- /dev/null
+++ b/python/code/2/4.py
@@ -0,0 +1,27 @@
+from random import randrange
+
+def game():
+    secret = randrange(1, 11)
+    for i in range(3):
+        guess = int(input('Enter a guess: '))
+        if guess != secret:
+            print('Wrong guess!', end=' ')
+        else:
+            print(f'Correct guess in {i + 1} {"try" if i == 0 else "tries"}!')
+            return
+        if i != 2:
+            print('Try again.')
+        else:
+            print('Game over!')
+            print(f'Secret value was {secret}')
+
+while True:
+    print('Welcome to number guessing game')
+    print('Try to guess the secret random number between 1 to 10 correctly in 3 tries')
+    game()
+    choice = input('Do you want to try playing again (y/n, default n): ')
+    if choice == '' or choice == 'n':
+        print('Thanks for playing. Bye!')
+        break
+    else:
+        print('Enjoy your next play\n')
diff --git a/python/code/2/5.py b/python/code/2/5.py
new file mode 100644
index 0000000..ba78eec
--- /dev/null
+++ b/python/code/2/5.py
@@ -0,0 +1,35 @@
+import math, sys
+start_day = int(input('Enter the starting day (1 for Sunday, 2 for Monday, ..., 7 for Saturday): '))
+if not (1 <= start_day <= 7):
+    print('Invalid day, must be between 1 and 7, inclusive')
+    sys.exit(1)
+days_in_month = int(input('Enter the number of days in the month: '))
+print('Calender for this month:')
+print(' SUN MON TUE WED THU FRI SAT')
+if start_day == 2:
+    print(' ' * 4, end='')
+else:
+    print(' ' * (4 * (start_day - 1)), end='')
+
+day = 1
+if start_day != 1:
+    for i in range(7 - start_day + 1):
+        print(f' {day:3}', end='')
+        day += 1
+    print()
+    weeks = math.ceil((days_in_month - (7 - start_day + 1)) / 7)
+else:
+    weeks = math.ceil(days_in_month / 7)
+
+class StopIt(Exception): pass
+
+try:
+    for w in range(weeks):
+        for i in range(7):
+            if day > days_in_month:
+                raise StopIt()
+            print(f' {day:3}', end='')
+            day += 1
+        print()
+except StopIt: pass
+print()
diff --git a/python/code/2/6.py b/python/code/2/6.py
new file mode 100644
index 0000000..8013ae4
--- /dev/null
+++ b/python/code/2/6.py
@@ -0,0 +1,11 @@
+line = input('Enter a string: ')
+uppercase_count, lowercase_count, digit_count = 0, 0, 0
+for c in line:
+    if c.isupper(): uppercase_count += 1
+    elif c.islower(): lowercase_count += 1
+    elif c.isdigit(): digit_count += 1
+
+print('Number of')
+print('  Uppercase characters:', uppercase_count)
+print('  Lowercase characters:', lowercase_count)
+print('  Digits:', digit_count)
diff --git a/python/code/2/7.py b/python/code/2/7.py
new file mode 100644
index 0000000..3212851
--- /dev/null
+++ b/python/code/2/7.py
@@ -0,0 +1,9 @@
+size = int(input('Enter pattern size: '))
+if size < 0:
+    print('Invalid size')
+else:
+    print('*' * size)
+    for i in range(size - 2):
+        print('*' + ' ' * (size - 2) + '*')
+    if size > 1:
+        print('*' * size)
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)