-
Notifications
You must be signed in to change notification settings - Fork 0
/
Ex20_ElementSearch.py
44 lines (39 loc) · 1.08 KB
/
Ex20_ElementSearch.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# Check out my Youtube channel:
# https://www.youtube.com/channel/UCRYxPJle46XMws4ewJE-ShQ
def element_search(li, num):
li = list(set(li))
print(li)
center = 1
while center != 0:
center = len(li) // 2
print(f"center index is: {center}")
if li[center] == num:
print(f'{num} is in the list')
break
elif li[center] > num:
li = li[:center]
print(f'cut list is: {li}')
elif li[center] < num:
li = li[center + 1:]
print(f'cut list is: {li}')
else:
print("the number isn't in the list")
# there are lot's of methods for receiving a list
# I've just told one of them, try others by yourself
inp_list = input('Enter a list: ').split(' ')
managed_list = []
for x in inp_list:
if x.isdigit():
managed_list.append(int(x))
del inp_list
element_search(managed_list, int(input('enter a number: ')))
"""
another method:
inp_list = []
try:
while True:
inp_list.append(int(input(f'the {len(inp_list)} of your list: ')))
except:
pass
print(inp_list)
"""