Просмотр исходного кода

Merge branch 'master' of http://37.252.12.59/Home/codewars

Dmitry Telenkov 1 год назад
Родитель
Сommit
9ac9149af9
1 измененных файлов с 247 добавлено и 0 удалено
  1. 247 0
      courses/python_for_begginers/exam1.py

+ 247 - 0
courses/python_for_begginers/exam1.py

@@ -0,0 +1,247 @@
+import random
+
+def game_guesse():
+    x = random.randint(1, 101)
+    print(x)
+    print("Добро пожаловать в числовую угадайку")
+    number = int(input())
+    trys = 1
+    while number:
+        if is_valid(number) == False:
+            print('А может быть все-таки введем целое число от 1 до 100?')
+            number = int(input())
+            trys += 1
+            continue
+        if number > x:
+            print('Ваше число больше загаданного, попробуйте еще разок')
+        elif number < x:
+            print('Ваше число меньше загаданного, попробуйте еще разок')
+        else:
+            print('Вы угадали, поздравляем!')
+            print(f'Число попыток: {trys}')
+            print('Спасибо, что играли в числовую угадайку. Еще увидимся...')
+            break
+        number = int(input())
+        trys += 1
+
+def is_valid(num):
+    return 1 <= num <= 100
+
+# game_guesse()
+
+# Шифр Цезаря. Очень плохая реализация.
+def caesar_cipher_1(data: str, rot: int):
+    RU_LEN = 32
+    chipher_date = ''
+    a = ord('а')
+    A = ord('А')
+    low_case_ch = [chr(x) for x in range(a, a + RU_LEN)]
+    low_case = [x for x in range(a, a + RU_LEN)]
+
+    up_case_ch = [chr(x) for x in range(A, A + RU_LEN)]
+    up_case = [x for x in range(A, A + RU_LEN)]
+
+    print(''.join(low_case_ch))
+    # print(up_case, len(up_case))
+
+    if rot > 31:
+        rot = rot%31
+
+    for i in data:
+        code = ord(i)
+        if code in low_case:
+            if rot > (low_case[-1] - code):
+                chipher_date += chr(low_case[0] + rot - (low_case[-1] - code) - 1)
+            else:
+                chipher_date += chr(code + rot)
+        elif code in up_case:
+            if rot > (up_case[-1] - code):
+                chipher_date += chr(up_case[0] + rot - (up_case[-1] - code) - 1)
+            else:
+                chipher_date += chr(code + rot)
+            
+        else:
+            chipher_date += i
+
+    return chipher_date
+
+
+# Шифр Цезаря. Нормальная реализация.
+def caesar_cipher_2(data: str, rot: int, lang='ru'):
+
+    RU_LEN = 31
+    EN_LEN = 25
+
+    chipher_date = ''
+    d = [chr(x) for x in range(ord('а'), ord('я') + 1)]
+    ru_low_case = ''.join(d) * 2
+    d = [chr(x) for x in range(ord('А'), ord('Я') + 1)]
+    ru_up_case = ''.join(d) * 2
+
+    d = [chr(x) for x in range(ord('a'), ord('z') + 1)]
+    en_low_case = ''.join(d) * 2
+    d = [chr(x) for x in range(ord('A'), ord('Z') + 1)]
+    en_up_case = ''.join(d) * 2
+
+    if lang == 'ru':
+        if rot > RU_LEN:
+            rot = rot%RU_LEN
+        for i in data:
+            if i in ru_low_case:
+                chipher_date += ru_low_case[ru_low_case.index(i) + rot]
+            elif i in ru_up_case:
+                chipher_date += ru_up_case[ru_up_case.index(i) + rot]
+            else:
+                chipher_date += i
+    elif lang == 'en':
+        if rot > EN_LEN:
+            rot = rot%EN_LEN
+        for i in data:
+            if i in en_low_case:
+                chipher_date += en_low_case[en_low_case.index(i) + rot]
+            elif i in en_up_case:
+                chipher_date += en_up_case[en_up_case.index(i) + rot]
+            else:
+                chipher_date += i
+    else:
+        return '-1'
+    
+    return chipher_date
+
+
+# Шифр Цезаря. Нормальная реализация.
+def caesar_recipher_2(data: str, rot: int, lang='ru'):
+
+    RU_LEN = 31
+    EN_LEN = 25
+
+    chipher_date = ''
+    d = [chr(x) for x in range(ord('а'), ord('я') + 1)]
+    ru_low_case = ''.join(d) * 2
+    d = [chr(x) for x in range(ord('А'), ord('Я') + 1)]
+    ru_up_case = ''.join(d) * 2
+
+    d = [chr(x) for x in range(ord('a'), ord('z') + 1)]
+    en_low_case = ''.join(d) * 2
+    d = [chr(x) for x in range(ord('A'), ord('Z') + 1)]
+    en_up_case = ''.join(d) * 2
+
+    if lang == 'ru':
+        if rot > RU_LEN:
+            rot = rot%RU_LEN
+        for i in data:
+            if i in ru_low_case:
+                chipher_date += ru_low_case[ru_low_case.index(i) - rot]
+            elif i in ru_up_case:
+                chipher_date += ru_up_case[ru_up_case.index(i) - rot]
+            else:
+                chipher_date += i
+    elif lang == 'en':
+        if rot > EN_LEN:
+            rot = rot%EN_LEN
+        for i in data:
+            if i in en_low_case:
+                chipher_date += en_low_case[en_low_case.index(i) - rot]
+            elif i in en_up_case:
+                chipher_date += en_up_case[en_up_case.index(i) - rot]
+            else:
+                chipher_date += i
+    else:
+        return '-1'
+    
+    return chipher_date
+
+# print(caesar_cipher_2("Блажен, кто верует, тепло ему на свете!", 10, 'ru'))
+# print(caesar_recipher_2("Лхкрпч, фьш мпъэпь, ьпщхш пцэ чк ымпьп!", 10, 'ru'))
+# print(caesar_cipher_2("To be, or not to be, that is the question!", 17, 'en'))
+
+
+def length(data: str):
+    ret = 0
+    for i in data:
+        if i.isalpha():
+            ret += 1
+    return len([x for x in data if x.isalpha()])
+    
+
+def caesar_cipher_test(data: str):
+    l = data.split()
+    ret = ''
+    for sim in l:
+        rot = length(sim)
+        for i in sim:
+            ret += caesar_cipher_2(i, rot, 'en')
+        ret += ' '
+    return ret
+
+# print(caesar_cipher_test("my name is Python!"))
+# print(caesar_cipher_test('Day, mice. "Year" is a mistake!'))
+
+def test(num):
+    bin_num = bin(num)  # 0b1111111
+    oct_num = oct(num)  # 0o177
+    hex_num = hex(num)  # 0x7f
+
+    print(bin_num[2:])  # 1111111
+    print(oct_num[2:])  # 177
+    print(hex_num[2:])  # 7f
+
+# test(124)
+
+def my_range_gen(*args):
+    if len(args) == 1:
+        cnt = 0
+        while cnt != args[0]:
+            yield cnt
+            cnt += 1
+    elif len(args) == 2:
+        cnt = args[0]
+        while cnt < args[1]:
+            yield cnt
+            cnt += 1
+    elif len(args) == 3:
+        if args[2] > 0:
+            cnt = args[0]
+            while cnt < args[1]:
+                yield cnt
+                cnt += args[2]
+        elif args[2] == 0:
+            return
+        else:
+            cnt = args[0]
+            while cnt > args[1]:
+                yield cnt
+                cnt += args[2]
+
+
+for i in my_range_gen(8):
+    print(i)
+
+# for i in my_range_gen(8, 5, -1):
+#     print(i)
+
+# for i in my_range_gen(10, 30, 3):
+#     print(i)
+
+# for i in my_range_gen(30, 1, 0):
+#     print(i)
+
+# for i in my_range_gen(30, 1, -5):
+#     print(i)
+
+# for i in my_range_gen(5):
+#     print(i)
+
+
+# s = my_range_gen(5)
+# print(next(s))
+# print(next(s))
+# print(next(s))
+# print(next(s))
+# print(next(s))
+# print(next(s))
+        
+# def f(*args, **kwargs):
+# 	print(args, kwargs)
+
+# f(5, 4, 5, 6, 1, a = 1, b = 5, c = 6, name = 123)