From e0b23bb0d02e0f4369947b2b2df86f3b0aa30db6 Mon Sep 17 00:00:00 2001 From: KalenikAI Date: Fri, 18 Dec 2020 14:39:04 +0300 Subject: [PATCH] Lesson_1 --- task1.py | 11 +++++++++++ task2.py | 13 +++++++++++++ task3.py | 15 ++++++++++++++ task4.py | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ task5.py | 36 ++++++++++++++++++++++++++++++++++ task6.py | 17 ++++++++++++++++ task7.py | 19 ++++++++++++++++++ task8.py | 7 +++++++ task9.py | 16 +++++++++++++++ 9 files changed, 193 insertions(+) create mode 100644 task1.py create mode 100644 task2.py create mode 100644 task3.py create mode 100644 task4.py create mode 100644 task5.py create mode 100644 task6.py create mode 100644 task7.py create mode 100644 task8.py create mode 100644 task9.py diff --git a/task1.py b/task1.py new file mode 100644 index 00000000..327d19e4 --- /dev/null +++ b/task1.py @@ -0,0 +1,11 @@ +# Найти сумму и произведение цифр трехзначного числа, которое вводит пользователь. + +inp_str = input('Please provide 3 digit integer value : ').lstrip().rstrip() +if len(inp_str) != 3: + print (f'It is not 3 digit integer value') +else: + d1 = int(inp_str[0]) + d2 = int(inp_str[1]) + d3 = int(inp_str[2]) + print(f'Summa is : {d1 + d2 + d3}') + print(f'Product is : {d1 * d2 * d3}') diff --git a/task2.py b/task2.py new file mode 100644 index 00000000..888472e0 --- /dev/null +++ b/task2.py @@ -0,0 +1,13 @@ +# Выполнить логические побитовые операции "И", "ИЛИ" и др. над числами 5 и 6. +# Выполнить над числом 5 побитовый сдвиг вправо и влево на два знака. + +a = 5 # '101' +b = 6 # '110' + +print (f'Binary AND is {a & b}') # '100' +print (f'Binary OR is {a | b}') # '111' +print (f'Binary XOR is {a ^ b}') # '011' + +print (f'Binary shift 2 bits right is {a >> 2}') # '1' +print (f'Binary shift 2 bits left is {a << 2}') # '10100' + diff --git a/task3.py b/task3.py new file mode 100644 index 00000000..ee4d651d --- /dev/null +++ b/task3.py @@ -0,0 +1,15 @@ +# По введенным пользователем координатам двух точек вывести уравнение прямой вида +# y = kx + b, проходящей через эти точки. + +print('Plese provide coordinates for a(x1;y1):') +x1 = float(input('\tx1 = ')) +y1 = float(input('\ty1 = ')) + +print('Plese provide coordinates for b(x2;y2):') +x2 = float(input('\tx2 = ')) +y2 = float(input('\ty2 = ')) + +k = (y1 - y2) / (x1 - x2) +b = y2 - k*x2 +print(f'Formula of the line through points: a({x1};{y1}) and b({x2};{y2})') +print(' y = %.2f*x + %.2f' % (k, b)) \ No newline at end of file diff --git a/task4.py b/task4.py new file mode 100644 index 00000000..dea44c1a --- /dev/null +++ b/task4.py @@ -0,0 +1,59 @@ +# Написать программу, которая генерирует в указанных пользователем границах +# -случайное целое число, +# -случайное вещественное число, +# -случайный символ. +# Для каждого из трех случаев пользователь задает свои границы диапазона. +# Например, если надо получить случайный символ от 'a' до 'f', то вводятся эти символы. +# Программа должна вывести на экран любой символ алфавита от 'a' до 'f' включительно. + +# случайное целое число +from random import randint +inp_str = input('Please provide 2 integer values separated by space: ').lstrip().rstrip() +val1, val2 = inp_str.split(' ') +val1 = int(val1) +val2 = int(val2) +if val1 > val2: + val1, val2 = val2, val1 +print(f'Random integer is: {randint(val1, val2)}') + +# случайное вещественное число +from random import uniform +inp_str = input('Please provide 2 decimal values separated by space: ').lstrip().rstrip() +val1, val2 = inp_str.split(' ') +val1 = float(val1) +val2 = float(val2) +if val1 > val2: + val1, val2 = val2, val1 +print(f'Random integer is: {uniform(val1, val2)}') + +from random import randint +# случайный символ вариант 1 +inp_str = input('Please provide 2 lowercase English alphabet letters separated by space: ').lstrip().rstrip() +val1, val2 = inp_str.split(' ') +code1 = ord(val1) +code2 = ord(val2) +if code1 > code2: + code1, code2 = code2, code1 +if code1 >122 or code1 < 97: + print (f'Error. {val1} is not lowercase English alphabet letters') +elif code2 >122 or code2 < 97: + print (f'Error. {val2} is not lowercase English alphabet letters') +else: + print(f'Random symbol is: {chr(randint(code1, code2))}') + +from random import randint +# случайный символ вариант 2 +inp_str = input('Please provide 2 lowercase English alphabet letters separated by space: ').lstrip().rstrip() +val1, val2 = inp_str.split(' ') +ascii_lowercase = 'abcdefghijklmnopqrstuvwxyz' +indx1 = ascii_lowercase.find(val1) +indx2 = ascii_lowercase.find(val2) +if indx1 > indx2: + indx1, indx2 = indx2, indx1 +if indx1 == -1: + print (f'Error. {val1} is not lowercase English alphabet letters') +elif indx2 == -1: + print (f'Error. {val2} is not lowercase English alphabet letters') +else: + print(f'Random symbol is: {ascii_lowercase[randint(indx1, indx2)]}') + diff --git a/task5.py b/task5.py new file mode 100644 index 00000000..47b07722 --- /dev/null +++ b/task5.py @@ -0,0 +1,36 @@ +# Пользователь вводит две буквы. +# Определить, на каких местах алфавита они стоят, и сколько между ними находится букв. +# Вариант 1 +inp_str = input('Please provide 2 lowercase English alphabet letters separated by space: ').lstrip().rstrip() +val1, val2 = inp_str.split(' ') +ascii_lowercase = 'abcdefghijklmnopqrstuvwxyz' +indx1 = ascii_lowercase.find(val1) +indx2 = ascii_lowercase.find(val2) +if indx1 > indx2: + indx1, indx2 = indx2, indx1 +if indx1 == -1: + print (f'Error. {val1} is not lowercase English alphabet letters') +elif indx2 == -1: + print (f'Error. {val2} is not lowercase English alphabet letters') +else: + print(f'Symbol [ {val1} ] is on pos {indx1 + 1}') + print(f'Symbol [ {val2} ]is on pos {indx2 + 1}') + print(f'Distance between symbol {val1} and symbol {val2} is {indx2 - indx1}') + +# Вариант 2 +mincode = 97 +maxcode = 122 +inp_str = input('Please provide 2 lowercase English alphabet letters separated by space: ').lstrip().rstrip() +val1, val2 = inp_str.split(' ') +code1 = ord(val1) +code2 = ord(val2) +if code1 > code2: + code1, code2 = code2, code1 +if code1 < mincode or code1 > maxcode: + print (f'Error. {val1} is not lowercase English alphabet letters') +elif code2 < mincode or code2 > maxcode: + print (f'Error. {val2} is not lowercase English alphabet letters') +else: + print(f'Symbol [ {val1} ] is on pos {code1 - mincode +1}') + print(f'Symbol [ {val2} ]is on pos {code2 - mincode +1}') + print(f'Distance between symbol {val1} and symbol {val2} is {code2 - code1}') \ No newline at end of file diff --git a/task6.py b/task6.py new file mode 100644 index 00000000..43576520 --- /dev/null +++ b/task6.py @@ -0,0 +1,17 @@ +# Пользователь вводит номер буквы в алфавите. Определить, какая это буква. +# способ 1 +number = int(input('Please provide lowercase English alphabet letter number : ').lstrip().rstrip()) +ascii_lowercase = 'abcdefghijklmnopqrstuvwxyz' +if number > 26 or number <= 0: + print(f'Provided number is not valid, please retype: ') +else: + print(f'Your letter is {ascii_lowercase[number - 1]}') + + +# способ 2 +number = int(input('Please provide lowercase English alphabet letter number : ').lstrip().rstrip()) +mincode = 97 +if number > 26 or number <= 0: + print(f'Provided number is not valid, please retype: ') +else: + print(f'Your letter is {chr(mincode + number - 1)}') diff --git a/task7.py b/task7.py new file mode 100644 index 00000000..f1fb309d --- /dev/null +++ b/task7.py @@ -0,0 +1,19 @@ +# По длинам трех отрезков, введенных пользователем, определить возможность существования треугольника, +# составленного из этих отрезков. Если такой треугольник существует, то определить, +# является ли он разносторонним, равнобедренным или равносторонним. + +print('Plese provide lengths sides of triangle:') +l1 = float(input('\tl1 = ')) +l2 = float(input('\tl2 = ')) +l3 = float(input('\tl3 = ')) + +if l1 < l2 + l3 and l2 < l1 + l3 and l3 < l1 + l2 : + print (f'It is possible to create triangle from provided sides') + if l1 != l2 and l2 != l3 and l3 != l1 : + print(f'Here is an scalene triangle') + elif l1 == l2 and l2 == l3 and l3 == l1 : + print(f'Here is an equilateral triangle') + elif l1 == l2 or l1 == l3 or l2 == l3 : + print(f'Here is an isosceles triangle') +else: + print (f'It is impossible to create triangle from provided sides') diff --git a/task8.py b/task8.py new file mode 100644 index 00000000..11a6befe --- /dev/null +++ b/task8.py @@ -0,0 +1,7 @@ +# Определить, является ли год, который ввел пользователем, високосным или не високосным. + +year = int(input('Please provide Year number as 4 digits value: ').rstrip().lstrip()) +if year % 4 == 0 and year % 100 != 0 or year % 400 == 0: + print(f'{year} is leap year') +else: + print(f'{year} is not leap year') diff --git a/task9.py b/task9.py new file mode 100644 index 00000000..2424278a --- /dev/null +++ b/task9.py @@ -0,0 +1,16 @@ +# Вводятся три разных числа. +# Найти, какое из них является средним (больше одного, но меньше другого). + +print('Plese provide 3 different numbers:') +n1 = float(input('\tn1 = ')) +n2 = float(input('\tn2 = ')) +n3 = float(input('\tn3 = ')) + +if (n1 > n2 and n1 < n3) or (n1 < n2 and n1 > n3): + print(f'{n1} is between {n2} and {n3}') +elif (n2 > n3 and n2 < n1) or (n2 < n3 and n2 > n1): + print(f'{n2} is between {n1} and {n3}') +elif (n3 > n1 and n3 < n2) or (n3 < n1 and n3 > n2): + print(f'{n3} is between {n1} and {n2}') +else: + print(f'All 3 numbers must be different')