Python Projects for Beginners a ten-Week Bootcamp Approach to Python Programming



Download 2,61 Mb.
bet147/200
Sana20.06.2022
Hajmi2,61 Mb.
#681748
1   ...   143   144   145   146   147   148   149   150   ...   200
Bog'liq
Python Projects for Beginners A Ten Week Bootcamp Approach to Python

Step 5: Check if Value Is Less


This step is the exact same as step 4 but with the opposite condition. If the value at the middle index is less than the number we’re looking for, we want to remove the left half:

19| aList = aList[ : mid ] ◽◽◽
21| # step 5: check if value is less, if so, cut off left half of list using slicing
21| elif aList[mid] < num:
22| aList = aList[ mid + 1 : ]
23| print(aList) # remove after working properly
25| print( sorted(nums) ) # for debugging purposes ◽◽◽

Go ahead and run the cell. On line 22 we perform the opposite slice from step 4. This time we declare “mid + 1” because we don’t want to include the middle index, as it’s already been checked. The logic has now been implemented for our Binary Search. All that’s left is to set up a loop to repeat steps 2 through 5 and return False if we don’t find what we’re looking for.

Step 6: Set Up a Loop to Repeat Steps


We’ll need to loop until the argument is found, or until the list is empty. This sounds like a great case for a while loop. After creating the while statement, we need to make sure we execute the code for steps 2 through 5 within the loop:

8| aList.sort( ) ◽◽◽
10| # step 6: setup a loop to repeat steps 2 through 6 until list is empty 11| while aList:
12| mid = len(aList) // 2 14| if aList[mid] == num:
15| return True 16| elif aList[mid] > num:
17| aList = aList[ : mid ] 18| elif aList[mid] < num:
19| aList = aList[ mid + 1 : ]
21| print(aList) # remove after working properly
21| print( sorted(nums) ) # for debugging purposes ◽◽◽

Go ahead and run the cell. Our Binary Search is now performing all the necessary steps to either return True when the argument is found or create an empty list, in which case the loop will end. Remember that our preceding while statement is the same as
while len(aList) > 0:”. All that’s left is to return False if the loop ends, as that means that the list does not contain our number.

Download 2,61 Mb.

Do'stlaringiz bilan baham:
1   ...   143   144   145   146   147   148   149   150   ...   200




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish