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



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

Challenge Question Solution
In the following, you can find the solution to the challenge question this week:

1| # ask user for input, return whether it is prime or not 3| def isPrime(num):
4| for i in range( 2, int(num**0.5) + 1 ):
5| if num % i == 0:
6| return False 7| else:
8| return True
10| n = int( input("Type a number: ") ) 12| if isPrime(n):
13| print("That is a prime number.") 14| else:
15| print("That is not a prime number")

The most important part of this program is on line 4. Although you may have gotten it correct, we wanted to create this program so that it was efficient. The statement on line 4 could have also looked like the following:
>>> for i in range(2, num):
The problem with this line, however, is that it’s not efficient. When you are trying to calculate whether a number is prime or not, the square root of the number is as high as you need to go. If a number isn’t divisible between two and the square root of itself, then it means it’s a prime number. If we didn’t take the square root of the number passed in to calculate prime, then we would’ve had to loop all the way to the prime number itself. Let’s take the number 97, for instance, which is a prime number. Using the second for loop statement, we would’ve looped for a total of 96 iterations. With the statement written in the code block, however, we would only loop for a total of nine iterations. As the number you’re passing in gets larger, so too does the iteration count. Therefore, it’s always important to keep efficiency in mind when programming.
Weekly Challenges
To test out your skills, try these challenges:

  1. Recursive Binary Search: Turn the Binary Search algorithm that we created together into a recursive function. Rather than using a while loop, it should call itself in order to cut the list down and eventually return True or False.


  2. Download 2,61 Mb.

    Do'stlaringiz bilan baham:
1   ...   145   146   147   148   149   150   151   152   ...   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