-
Notifications
You must be signed in to change notification settings - Fork 0
hw3 #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?
hw3 #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,18 @@ | ||
| # 1. В диапазоне натуральных чисел от 2 до 99 определить, сколько из них кратны любому из чисел в диапазоне от 2 до 9. | ||
|
|
||
| RANGE_START = 2 | ||
| RANGE_STOP = 99 | ||
|
|
||
| nums_data = dict() | ||
| for i in range(2, 10): | ||
| nums_data[i] = 0 | ||
|
|
||
| # print(nums_data) | ||
|
|
||
| for num in range(RANGE_START, RANGE_STOP + 1): | ||
| for i in nums_data.keys(): | ||
| if num % i == 0: | ||
| nums_data[i] += 1 | ||
|
|
||
| for i in nums_data.keys(): | ||
| print(f' {i} - кратно {nums_data[i]} чисел') | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| # Во втором массиве сохранить индексы четных элементов первого массива. Например, если дан массив со значениями 8, 3, 15, 6, 4, 2, то во второй массив надо заполнить значениями 1, 4, 5, 6 (или 0, 3, 4, 5 - если индексация начинается с нуля), т.к. именно в этих позициях первого массива стоят четные числа. | ||
|
|
||
| array_1=[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. Отлично |
||
| array_2=[] | ||
|
|
||
| for i, item in enumerate(array_1): | ||
| if item % 2 == 0: | ||
| array_2.append(i) | ||
|
|
||
| print(array_1) | ||
| print(array_2) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| # 3. В массиве случайных целых чисел поменять местами минимальный и максимальный элементы. | ||
|
|
||
| import random | ||
|
|
||
| SIZE = 10 | ||
| MIN_ITEM = 0 | ||
| MAX_ITEM = 100 | ||
|
|
||
| array = [random.randint(MIN_ITEM, MAX_ITEM) for _ in range(SIZE)] | ||
|
|
||
| print(array, end="\n\n") | ||
|
|
||
| max = dict(index=0, value=array[0]) | ||
|
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. Похоже я вас перехвалил в прошлый раз ))) |
||
| min = dict(index=0, value=array[0]) | ||
|
|
||
| for i, item in enumerate(array): | ||
|
|
||
| if max["value"] <= item: | ||
| max["value"] = item | ||
| max["index"] = i | ||
|
|
||
| if min["value"] >= item: | ||
| min["value"] = item | ||
| min["index"] = i | ||
|
|
||
| array[max["index"]] = min["value"] | ||
| array[min["index"]] = max["value"] | ||
|
|
||
| print(f'Максимальный элемент {max["value"]} с индексом {max["index"]}') | ||
| print(f'Минимальный элемент {min["value"]} с индексом {min["index"]}', end="\n\n") | ||
|
|
||
| print(array) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| # 4. Определить, какое число в массиве встречается чаще всего. | ||
|
|
||
| import random | ||
|
|
||
| SIZE = 20 | ||
| MIN_ITEM = 0 | ||
| MAX_ITEM = 100 | ||
|
|
||
| array = [random.randint(MIN_ITEM, MAX_ITEM) for _ in range(SIZE)] | ||
|
|
||
| print(array, end="\n\n") | ||
|
|
||
| nums = dict() | ||
|
|
||
| for item in array: | ||
| if item in nums.keys(): | ||
| nums[item] += 1 | ||
| else: | ||
| nums[item] = 1 | ||
|
|
||
| max = dict(num=0, count=0) | ||
|
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. Отлично... Наверное... Могло бы быть... ))) |
||
|
|
||
| for num in nums.keys(): | ||
| if(nums[num]>=max["count"]): | ||
| max["count"]=nums[num] | ||
| max["num"] = num | ||
|
|
||
| print(f'Число {max["num"]} встречается чаще всего - {max["count"]} раз') | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| # 5. В массиве найти максимальный отрицательный элемент. Вывести на экран его значение и позицию в массиве. | ||
|
|
||
| import random | ||
|
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. Отличный алгоритм |
||
|
|
||
| SIZE = 10 | ||
| MIN_ITEM = -100 | ||
| MAX_ITEM = 100 | ||
|
|
||
| array = [random.randint(MIN_ITEM, MAX_ITEM) for _ in range(SIZE)] | ||
|
|
||
| print(array, end="\n\n") | ||
|
|
||
| max = dict(index=None, value=None) | ||
|
|
||
| for i, item in enumerate(array): | ||
| if item < 0: | ||
| if max["value"] is None or max["value"] < item: | ||
| max["value"] = item | ||
| max["index"] = i | ||
|
|
||
| if max["value"] is None: | ||
| print(f'В массиве нет отрицательных чисел') | ||
| else: | ||
| print(f'Максимальное отрицательное число {max["value"]} с индексом {max["index"]}') | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| # 7. В одномерном массиве целых чисел определить два наименьших элемента. Они могут быть как равны между собой (оба являться минимальными), так и различаться. | ||
|
|
||
| import random | ||
|
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. Отличное решение |
||
|
|
||
| SIZE = 20 | ||
| MIN_ITEM = 0 | ||
| MAX_ITEM = 100 | ||
|
|
||
| array = [random.randint(MIN_ITEM, MAX_ITEM) for _ in range(SIZE)] | ||
|
|
||
| print(array, end="\n\n") | ||
|
|
||
| min1 = dict(index=0, value=array[0]) | ||
| min2 = dict(index=1, value=array[1]) | ||
|
|
||
| if array[0] < array[1]: | ||
| min1["value"] = array[1] | ||
| min1["index"] = 1 | ||
| min2["value"] = array[0] | ||
| min2["index"] = 0 | ||
|
|
||
| for i, item in enumerate(array): | ||
| if i > 1 and item <= min1["value"]: | ||
| if item <= min2["value"]: | ||
| min1["value"] = min2["value"] | ||
| min1["index"] = min2["index"] | ||
|
|
||
| min2["value"] = item | ||
| min2["index"] = i | ||
| else: | ||
| min1["value"] = item | ||
| min1["index"] = i | ||
|
|
||
| print(f'Минимальное число {min2["value"]} с индексом {min2["index"]}') | ||
| print(f'Минимальное число {min1["value"]} с индексом {min1["index"]}') | ||
|
|
||
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.
Отлично.
Для краткости можно удалить
.keys(). Без него всё равно будет проверка по ключам.