Grokking Algorithms



Download 6,4 Mb.
Pdf ko'rish
bet74/120
Sana21.12.2022
Hajmi6,4 Mb.
#893167
1   ...   70   71   72   73   74   75   76   77   ...   120
Bog'liq
Grokking Algorithms An Illustrated Guide for Programmers and Other

Calculating the answer
Now you need to calculate what stations you’ll use. Take a look 
at the image at right, and see if you can predict what stations you 
should use.
here can be more than one correct solution. You need to go 
through every station and pick the one that covers the most 
uncovered states. I’ll call this 
best_station
:
best_station = None
states_covered = set()
for
station, states_for_station in stations.items():
states_covered
is a set of all the states this station covers that 
haven’t been covered yet. he 
for
loop allows you to loop over 
every station to see which one is the best station. Let’s look at the body 
of the 
for
loop:
covered = states_needed & states_for_station
if
len(covered) > len(states_covered):
best_station = station
states_covered = covered
here’s a funny-looking line here:
covered = states_needed & states_for_station
What’s going on?
Sets
Suppose you have a set of fruits.
You also have a set of vegetables.
When you have two sets, you can do some fun things with them.
New syntax! This is
called a set intersection.


150
Chapter 8
 
 
I
 
 
Greedy algorithms
Here are some things you can do with sets.
• A set union means “combine both sets.”
• A set intersection means “
ind the items that show up in both sets”
(in this case, just the tomato).
• A set diference means “subtract the items in one set from the items
in the other set.”
For example:
>>> fruits = set([“avocado”, “tomato”, “banana”])
>>> vegetables = set([“beets”, “carrots”, “tomato”])
>>> fruits | vegetables 

Download 6,4 Mb.

Do'stlaringiz bilan baham:
1   ...   70   71   72   73   74   75   76   77   ...   120




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