Created
October 5, 2022 15:30
-
-
Save bannovGB/8ac21996ee3591e72ab672911b486626 to your computer and use it in GitHub Desktop.
ДЗ3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'''Задайте список из нескольких чисел. Напишите программу, которая найдёт сумму элементов списка, | |
стоящих на нечётной позиции. | |
Пример: [2, 3, 5, 9, 3] -> на нечётных позициях элементы 3 и 9, ответ: 12''' | |
n = int(input('Размер массива: ')) | |
lst = [] | |
import random | |
for i in range(n): | |
lst.append(random.randint(1, 10)) | |
print(lst) | |
sum = 0 | |
for i in range(len(lst)): | |
if i % 2 == 1: | |
print(lst[i], end = "+" ) | |
sum += lst[i] | |
print('=', sum) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'''Напишите программу, которая найдёт произведение пар чисел списка. | |
Парой считаем первый и последний элемент, второй и предпоследний и т.д. | |
Пример: | |
- [2, 3, 4, 5, 6] => [12, 15, 16]; | |
- [2, 3, 5, 6] => [12, 15] ''' | |
n = int(input('Размер массива: ')) | |
lst = [] | |
import random | |
for i in range(n): | |
lst.append(random.randint(1, 10)) | |
print(lst) | |
for i in range(len(lst)): | |
if i >= len(lst)/2: | |
break | |
print(lst[i] * lst[-1-i], end=' ') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'''Задайте список из вещественных чисел. | |
Напишите программу, которая найдёт разницу между максимальным и минимальным значением дробной части элементов. | |
Пример: [1.1, 1.2, 3.1, 5, 10.01] => 0.19 ''' | |
n = int(input('Размер массива: ')) | |
lst = [] | |
import random | |
for i in range(n): | |
lst.append(round(random.uniform(1.1, 9.9), 2)) | |
print(lst) | |
new_lst = [] | |
# new_lst = [round(i%1, 2) for i in lst if i%1 != 0] | |
# print(new_lst) | |
for i in range(n): | |
new_lst.append(round(lst[i]%1, 2)) | |
print(new_lst) | |
lst_max = new_lst[0] | |
lst_min = new_lst[0] | |
for i in range(n): | |
if new_lst[i] > lst_max: lst_max = new_lst[i] | |
if new_lst[i] < lst_min: lst_min = new_lst[i] | |
print(f'{lst_max} - {lst_min} = {lst_max- lst_min}') | |
# print(f"Разницу между максимальным и минимальным значением дробной части элементов: {max(new_list) - min(new_list)}") # выводим разницу между максимальным и минимальным значением |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'''Напишите программу, которая будет преобразовывать десятичное число в двоичное. | |
Пример: | |
- 45 -> 101101 | |
- 3 -> 11 | |
- 2 -> 10 ''' | |
a = int(input('Введите число: ')) | |
b = '' # остатки от деления на 2 | |
while a>0: | |
b = str(a%2)+b | |
a = a//2 | |
print(b) | |
# Второй вариант | |
# a = int(input('Введите число: ')) | |
# print(bin(a)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'''Задайте число. Составьте список чисел Фибоначчи, в том числе для отрицательных индексов. | |
Пример: | |
- для k = 8 список будет выглядеть так: [-21 ,13, -8, 5, −3, 2, −1, 1, 0, 1, 1, 2, 3, 5, 8, 13, 21] ''' | |
n = int(input('Введите число: ')) | |
fibo_nums = [] | |
a, b = 1, 1 | |
for i in range(n+1-1): | |
fibo_nums.append(a) | |
a, b = b, a + b | |
a, b = 0, 1 | |
for i in range (n+1): | |
fibo_nums.insert(0, a) | |
a, b = b, a - b | |
print(fibo_nums) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment