[926, 510, 13, 963, 441, 444, 755, 934, 31, 548, 506, 161, 339, 452, 310, 725, 230, 75, 702, 205, 525, 625, 165, 993, 147, 35, 305, 694, 3, 560, 357, 757, 312, 358, 107, 697, 727, 147, 695, 957, 423, 564, 793, 272, 235, 115, 590, 43, 611, 149, 590, 154, 296, 537, 163, 818, 439, 130, 377, 20, 352, 666, 290, 786, 562, 296, 44, 275, 62, 396, 384, 719, 197, 227, 52, 397, 333, 926, 427, 332, 212, 563, 613, 530, 719, 696, 591, 86, 735, 79, 275, 508, 958, 947, 108, 957, 529, 798, 515, 661, 381, 865, 337, 969, 457, 508, 233, 475, 966, 844, 165, 273, 734, 657, 941, 949, 239, 179, 168, 700, 402, 989, 227, 309, 570, 709, 679, 538, 71, 5, 957, 429, 641, 389, 408, 547, 590, 54, 519, 773, 299, 428, 543, 803, 855, 359, 517, 957, 435, 628, 366, 775, 281, 473, 398, 592, 913, 839, 101, 756, 405, 46, 364, 641, 25, 550, 931, 873, 318, 563, 838, 81, 818, 656, 937, 161, 746, 368, 57, 351, 617, 273, 167, 886, 655, 367, 671, 816, 744, 740, 394, 296, 285, 45, 372, 525, 953, 378, 374, 43, 390, 965, 54, 310, 655, 601, 258, 884, 422, 756, 204, 846, 422, 920, 191, 897, 609, 285, 390, 335, 819, 208, 537, 737, 564, 814, 262, 907, 201, 807, 103, 913, 874, 177, 762, 329, 886, 838, 990, 897, 348, 89, 802, 28, 918, 427, 299, 824, 393, 9] надо найти место в списке где стоит 374, без index в питоне. Срочно!!!!
Ответы
Бинарный поиск нам в помощь, ввести можно любое число (если нужно замени строку со вводом на присвоение)
A = [926, 510, 13, 963, 441, 444, 755, 934, 31, 548, 506, 161, 339, 452, 310, 725, 230, 75, 702, 205, 525, 625, 165, 993, 147, 35, 305, 694, 3, 560, 357, 757, 312, 358, 107, 697, 727, 147, 695, 957, 423, 564, 793, 272, 235, 115, 590, 43, 611, 149, 590, 154, 296, 537, 163, 818, 439, 130, 377, 20, 352, 666, 290, 786, 562, 296, 44, 275, 62, 396, 384, 719, 197, 227, 52, 397, 333, 926, 427, 332, 212, 563, 613, 530, 719, 696, 591, 86, 735, 79, 275, 508, 958, 947, 108, 957, 529, 798, 515, 661, 381, 865, 337, 969, 457, 508, 233, 475, 966, 844, 165, 273, 734, 657, 941, 949, 239, 179, 168, 700, 402, 989, 227, 309, 570, 709, 679, 538, 71, 5, 957, 429, 641, 389, 408, 547, 590, 54, 519, 773, 299, 428, 543, 803, 855, 359, 517, 957, 435, 628, 366, 775, 281, 473, 398, 592, 913, 839, 101, 756, 405, 46, 364, 641, 25, 550, 931, 873, 318, 563, 838, 81, 818, 656, 937, 161, 746, 368, 57, 351, 617, 273, 167, 886, 655, 367, 671, 816, 744, 740, 394, 296, 285, 45, 372, 525, 953, 378, 374, 43, 390, 965, 54, 310, 655, 601, 258, 884, 422, 756, 204, 846, 422, 920, 191, 897, 609, 285, 390, 335, 819, 208, 537, 737, 564, 814, 262, 907, 201, 807, 103, 913, 874, 177, 762, 329, 886, 838, 990, 897, 348, 89, 802, 28, 918, 427, 299, 824, 393, 9]
first = 0
A = sorted(A)
num = int(input("Введите число, которое нужно найти: "))
mid = len(A) // 2
last = len(A) - 1
while A[mid] != num and first <= last:
if num > A[mid]:
first = mid + 1
else:
last = mid - 1
mid = (first + last) // 2
if first > last:
print("Увы, но такого числа нет :( ")
else:
print("Индекс нужного числа(последнее вхождение):", mid)