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


Step 3: Check the Value at the Middle Index



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

Step 3: Check the Value at the Middle Index


Now that we have the middle index, we want to see if the value at that given index is the number that we’re looking for. If it is, then we want to return True:

11| mid = len(aList) // 2 # two slashes ... ◽◽◽
13| # step 3: check the value at middle index, if it is equal to num return True
14| if aList[mid] == num:
15| return True
17| print( sorted(nums) ) # for debugging purposes ◽◽◽

Go ahead and run the cell. You’ll get an output of either True or None, depending on the list that was randomly generated for you. If the number 3 appears at index 5, then your output will be True as our condition on line 14 is True and will run the return statement.

Step 4: Check if Value Is Greater


If the number that we’re looking for isn’t at the middle index, then we need to figure out which half of the list to remove. Let’s first check if the value at the middle index is greater than the number we’re searching for. If it is, we need cut off the right half of the list:

15| return True ◽◽◽
17| # step 4: check if value is greater, if so, cut off right half of list using slicing
18| elif aList[mid] > num:
19| aList = aList[ : mid ]
21| print(aList) # remove after working properly
23| print( sorted(nums) ) # for debugging purposes ◽◽◽

Go ahead and run the cell. On line 18 we check to see if the value at the middle index of the list is greater than the argument that we passed in during the function call. Line 19 is where the magic of Binary Search occurs though. Using slicing, we’re able to re-declare the value of aList to the beginning half of the list.
Note Remember that slicing allows you to input the start, stop, and step. If you don’t input a number like earlier, it implies that you are using default values.
default values are start = 0, stop = len(list), and step = 1.
We imply that we want to keep the all items from index zero up to the middle index.
Remove line 21 after you’re done, as it will simply output the result of our new aList.

Download 2,61 Mb.

Do'stlaringiz bilan baham:
1   ...   142   143   144   145   146   147   148   149   ...   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