-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday1-method.txt
36 lines (32 loc) · 1.04 KB
/
day1-method.txt
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
#coding focus DSA
BINARY SEARCH
QUESTION 1: Alice has some cards with numbers written on them. She arranges the cards in decreasing order,
given number by turning over as few cards as possible. Write a function to help Bob locate the card
solution by me:
cards_str = input()
cards = list(map(int, cards_str.split()))
query = int(input())
def locate_cards(cards, query):
mid = int(len(cards)/2)
a = 0
while a == 0:
if cards[mid] == query:
print("located on:", mid+1, "position")
a = 1
elif cards[mid] > query:
mid += 1
while a == 0:
if cards[mid] == query:
print("located on", mid+1, "position")
a = 1
else:
break
elif cards[mid] < query:
mid -= 1
while a == 0:
if cards[mid] == query:
print("located on", mid+1, "position")
a = 1
else:
break
locate_cards(cards, query)