-
Notifications
You must be signed in to change notification settings - Fork 0
030_lesson #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
030_lesson #3
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| # 1. В диапазоне натуральных чисел от 2 до 99 определить, сколько из них кратны любому из чисел в диапазоне от 2 до 9. | ||
|
|
||
| a = [0] * 8 | ||
| for i in range(2, 100): | ||
| for j in range(2, 10): | ||
| if i % j == 0: | ||
| a[j - 2] += 1 | ||
| i = 0 | ||
| while i < len(a): | ||
| print(i + 2, ' - ', a[i]) | ||
| i += 1 | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| # 2. Во втором массиве сохранить индексы четных элементов первого массива. Например, если дан массив со значениями 8, 3, 15, 6, 4, 2, то | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Так же отлично |
||
| # во второй массив надо заполнить значениями 0, 3, 4, 5 (индексация начинается с нуля), т.к. именно в этих позициях первого массива | ||
| # стоят четные числа. | ||
|
|
||
| from random import random | ||
|
|
||
| N = 6 | ||
| arr = [0] * N | ||
| even = [] | ||
| for i in range(N): | ||
| arr[i] = int(random() * 10) + 10 | ||
| if arr[i] % 2 == 0: | ||
| even.append(i) | ||
| print(arr) | ||
| print(f'Indices of even elements of the array N: {even}') | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| # 3. В массиве случайных целых чисел поменять местами минимальный и максимальный элементы. | ||
|
|
||
| from random import random | ||
|
|
||
| N = 10 | ||
| arr = [0] * N | ||
| for i in range(N): | ||
| arr[i] = int(random() * 100) | ||
| print(arr[i], end=' ') | ||
| print() | ||
|
|
||
| mn = min(arr) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Как не просил обойтись без |
||
| mx = max(arr) | ||
| imn = arr.index(mn) | ||
| imx = arr.index(mx) | ||
| print(f'arr[{imn + 1}]={mn} arr[{imx + 1}]={mx}') | ||
| arr[imn], arr[imx] = arr[imx], arr[imn] | ||
|
|
||
| for i in range(N): | ||
| print(arr[i], end=' ') | ||
| print() | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| # 4. Определить, какое число в массиве встречается чаще всего. | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Отлично. Я бы даже сказал, что это классика |
||
|
|
||
| from random import random | ||
|
|
||
| N = 9 | ||
| arr = [0] * N | ||
| for i in range(N): | ||
| arr[i] = int(random() * 10) | ||
| print(arr) | ||
|
|
||
| num = arr[0] | ||
| max_frq = 1 | ||
| for i in range(N - 1): | ||
| frq = 1 | ||
| for k in range(i + 1, N): | ||
| if arr[i] == arr[k]: | ||
| frq += 1 | ||
| if frq > max_frq: | ||
| max_frq = frq | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Тут в блок-схеме потеряли стрелочку |
||
| num = arr[i] | ||
|
|
||
| if max_frq > 1: | ||
| print(f'{max_frq} times the number {num} is repeated') | ||
| else: | ||
| print('All items are unique') | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| # 5. В массиве найти максимальный отрицательный элемент. Вывести на экран его значение и позицию в массиве. | ||
|
|
||
| from random import random | ||
|
|
||
| N = 15 | ||
| arr = [] | ||
| for i in range(N): | ||
| arr.append(int(random() * 100) - 90) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. А почему такие большие отступы внезапно? |
||
| print(arr) | ||
|
|
||
| i = 0 | ||
| index = -1 | ||
| while i < N: | ||
| if arr[i] < 0 and index == -1: | ||
| index = i | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Так же нет стрелочек в блок-схеме. Впрочем, на оценку это не повлияет ))) |
||
| elif arr[i] < 0 and arr[i] > arr[index]: | ||
| index = i | ||
| i += 1 | ||
|
|
||
| print(f'The index of element in array is {index}\nThe element number is {arr[index]}') | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| # 6. В одномерном массиве найти сумму элементов, находящихся между минимальным и максимальным элементами. | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Отлично |
||
| # Сами минимальный и максимальный элементы в сумму не включать. | ||
|
|
||
| from random import random | ||
|
|
||
| N = 10 | ||
| a = [0] * N | ||
| for i in range(N): | ||
| a[i] = int(random() * 50) | ||
| print(f'{a[i]:2d}', end=' ') | ||
| print() | ||
|
|
||
| min_id = 0 | ||
| max_id = 0 | ||
| for i in range(1, N): | ||
| if a[i] < a[min_id]: | ||
| min_id = i | ||
| elif a[i] > a[max_id]: | ||
| max_id = i | ||
| print(f'Min array element is: {a[min_id]}\nMax array element is: {a[max_id]}') | ||
|
|
||
| if min_id > max_id: | ||
| min_id, max_id = max_id, min_id | ||
|
|
||
| summa = 0 | ||
| for i in range(min_id + 1, max_id): | ||
| summa += a[i] | ||
| print(f'Sum of elements between max and min element is: {summa}') | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| # 7. В одномерном массиве целых чисел определить два наименьших элемента. | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Классика. Отлично |
||
| # Они могут быть как равны между собой (оба являться минимальными), так и различаться. | ||
|
|
||
| from random import random | ||
|
|
||
| N = 10 | ||
| a = [] | ||
| for i in range(N): | ||
| a.append(int(random() * 100)) | ||
| print(f'{a[i]:3d}', end='') | ||
| print() | ||
|
|
||
| if a[0] > a[1]: | ||
| min1 = 0 | ||
| min2 = 1 | ||
| else: | ||
| min1 = 1 | ||
| min2 = 0 | ||
|
|
||
| for i in range(2, N): | ||
| if a[i] < a[min1]: | ||
| b = min1 | ||
| min1 = i | ||
| if a[b] < a[min2]: | ||
| min2 = b | ||
| elif a[i] < a[min2]: | ||
| min2 = i | ||
|
|
||
| print(f'#{min1:3d}:{a[min1]:3d}') | ||
| print(f'#{min2:3d}:{a[min2]:3d}') | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| # 8. Матрица 5x4 заполняется вводом с клавиатуры, кроме последних элементов строк. | ||
| # Программа должна вычислять сумму введенных элементов каждой строки и записывать | ||
| # ее в ее последнюю ячейку. В конце следует вывести полученную матрицу. | ||
|
|
||
| M = 5 | ||
| N = 4 | ||
| a = [] | ||
| for i in range(N): | ||
| b = [] | ||
| s = 0 | ||
| print(f'{i} string:') | ||
| for j in range(M - 1): | ||
| n = int(input()) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Молчаливый |
||
| s += n | ||
| b.append(n) | ||
| b.append(s) | ||
| a.append(b) | ||
|
|
||
| for i in a: | ||
| print(i) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| # 9. Найти максимальный элемент среди минимальных элементов столбцов матрицы. | ||
|
|
||
| from random import random | ||
|
|
||
| M = 10 | ||
| N = 5 | ||
| a = [] | ||
| for i in range(N): | ||
| b = [] | ||
| for j in range(M): | ||
| n = int(random() * 200) | ||
| b.append(n) | ||
| print(f'{n:4d}', end='') | ||
| a.append(b) | ||
| print() | ||
|
|
||
| mx = -1 | ||
| for j in range(M): | ||
| mn = 200 | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Если в 11 строке изменить диапазон (например дописать в конце |
||
| for i in range(N): | ||
| if a[i][j] < mn: | ||
| mn = a[i][j] | ||
| if mn > mx: | ||
| mx = mn | ||
| print(f'Maximum of the minimum: {mx}') | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Отлично. И блок-схема тоже