-
Notifications
You must be signed in to change notification settings - Fork 0
Lesson3 #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: lesson2
Are you sure you want to change the base?
Lesson3 #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,15 @@ | ||
| low = int(0) | ||
| high = int(101) | ||
| tries = 10 | ||
| while tries > 0: | ||
| mytry = low + (high - low) // 2 | ||
| print(f'Мое число {mytry}') | ||
| answer = input('Введите знак >, < или =: ') | ||
| if answer == '=': | ||
| print('Ура!') | ||
| exit(0) | ||
| if answer == '>': | ||
| high = mytry | ||
| if answer == '<': | ||
| low = mytry | ||
| tries = tries - 1 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| ans2 = ans3 = ans4 = ans5 = ans6 = ans7 = ans8 = ans9 =0 | ||
|
|
||
| SIZE = 100 | ||
| for i in range(2, SIZE): | ||
| if i % 2 == 0: | ||
| ans2 += 1 | ||
| if i % 3 == 0: | ||
| ans3 += 1 | ||
| if i % 4 == 0: | ||
| ans4 += 1 | ||
| if i % 5 == 0: | ||
| ans5 += 1 | ||
| if i % 6 == 0: | ||
| ans6 += 1 | ||
| if i % 7 == 0: | ||
| ans7 += 1 | ||
| if i % 8 == 0: | ||
| ans8 += 1 | ||
| if i % 9 == 0: | ||
| ans9 += 1 | ||
|
|
||
| print (ans2, ans3, ans4, ans5, ans6, ans7, ans8, ans9) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| 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 | ||
| array = [] | ||
| for _ in range(SIZE): | ||
| array.append(random.randint(0, SIZE * SIZE)) | ||
|
|
||
| arrayb = [] | ||
| for i in range(SIZE): | ||
| if array[i] % 2 == 0: | ||
| arrayb.append(i) | ||
|
|
||
| print(array) | ||
| print(arrayb) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| 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 | ||
| array = [] | ||
| for _ in range(SIZE): | ||
| array.append(random.randint(0, SIZE * SIZE)) | ||
|
|
||
| print(array) | ||
|
|
||
| maxpos = minpos = 0 | ||
| for i in range(SIZE): | ||
| if array[i] > array[maxpos]: | ||
| maxpos = i | ||
| if array[i] < array[minpos]: | ||
| minpos = i | ||
|
|
||
| array[maxpos], array[minpos] = array[minpos], array[maxpos] | ||
|
|
||
| print(array) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| import random | ||
|
|
||
| # Определяем параметры генерируемого массива SIZE - число элементов, RANGEMAX - максимальное значение каждого элемента | ||
| SIZE = 10 | ||
| RANGEMAX = 10 | ||
|
|
||
| #Определяем массив и заполняем случайными числами | ||
| array = [] | ||
| for _ in range(SIZE): | ||
| array.append(random.randint(0, RANGEMAX)) | ||
|
|
||
| # Определяем массив счетчиков | ||
| countarray = [0] * (RANGEMAX + 1) | ||
|
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. Представил задачу, где в массиве 10 чисел в диапазоне от нуля до миллиарда. |
||
|
|
||
| # Считаем число вхождений каждого элемента исходного массива | ||
| for i in range(SIZE): | ||
| num = array[i] | ||
| countarray[num] +=1 | ||
|
|
||
| # Ищем максимальное значение в массиве счетчиков | ||
| maxpos = 0 | ||
| for i in range(RANGEMAX): | ||
| if countarray[i] > maxpos: | ||
| maxpos = i | ||
|
|
||
| # Выводим реузльтаты | ||
| print(array) | ||
| print(maxpos) | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| import random | ||
|
|
||
| # Определяем параметры генерируемого массива SIZE - число элементов, RANGEMAX - максимальное значение каждого элемента RANGEMIN - минимальное значение каждого элемента | ||
| SIZE = 10 | ||
| RANGEMIN = -10 | ||
| RANGEMAX = 10 | ||
|
|
||
| #Определяем массив и заполняем случайными числами | ||
| array = [] | ||
| for _ in range(SIZE): | ||
| array.append(random.randint(RANGEMIN, RANGEMAX)) | ||
|
|
||
| #Выводим исходный массив | ||
| print(array) | ||
| minmax = RANGEMIN | ||
|
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. Алгоритм работает, когда вы точно знаете диапазон значений массива |
||
| minpos = 0 | ||
|
|
||
| #Находим позицию максимального орицательного числа | ||
| for i in range(SIZE): | ||
| if array[i] < 0: | ||
| if array[i] > minmax: | ||
| minpos = i | ||
| minmax = array[i] | ||
|
|
||
| #Выводим максимальное отрицательное число и его позицию (с корректировкой) | ||
| print(array[minpos], minpos + 1) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| 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 | ||
| array = [] | ||
| for _ in range(SIZE): | ||
| array.append(random.randint(0, SIZE * SIZE)) | ||
|
|
||
| print(array) | ||
|
|
||
| maxpos = minpos = 0 | ||
| for i in range(SIZE): | ||
| if array[i] > array[maxpos]: | ||
| maxpos = i | ||
| if array[i] < array[minpos]: | ||
| minpos = i | ||
|
|
||
| #Если позиция максимального выше позиции минимального - поменять их местами | ||
| if maxpos < minpos: | ||
| maxpos, minpos = minpos, maxpos | ||
|
|
||
| summ = 0 | ||
|
|
||
| for i in range(minpos + 1, maxpos): | ||
| summ += array[i] | ||
|
|
||
| print(summ) | ||
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.
Отлично, но не оптимально по объёму написания кода