summary refs log tree commit diff stats
path: root/python/code/2
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/2
parenta70e0a59817ce06a3dd23b3750ae16ee6660deaf (diff)
downloadzadania-7451edff7692e86c5238ff7bc6659825e242a84e.tar.gz
Quick backup, need to be rewritten
Diffstat (limited to 'python/code/2')
-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
7 files changed, 127 insertions, 0 deletions
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)