The Self-Taught Computer Scientist


factorial: The product of all positive integers less than or equal to a number. Challenge 1


Download 1.48 Mb.
Pdf ko'rish
bet32/147
Sana17.06.2023
Hajmi1.48 Mb.
#1540634
1   ...   28   29   30   31   32   33   34   35   ...   147
Bog'liq
books-library.net-11301817Az7X6

factorial: The product of all positive integers less than or equal to a number.
Challenge
1. 
Print the numbers from 1 to 10 recursively.



3
Search Algorithms
An algorithm must be seen to be believed.
Donald Knuth
As a professional programmer, you will spend a lot of time working with data. If you become a 
web or app developer, you will display data to users when they visit your website or app. Often, you 
have to manipulate the data before you can show it to them. If you become a data scientist, you will 
spend even more time working with data. Maybe Netflix will hire you to use its data to improve its 
algorithm for recommending movies. Or Instagram will employ you to analyze its data to help keep 
users on its platform longer.
One of the most fundamental things a programmer working with data needs to know is how to 
search it. Computer scientists search data by writing a search algorithm: an algorithm that looks for 
data in a data set. A data set is a collection of data. Two common examples of search algorithms are 
linear and binary searches. As a professional programmer, you most likely will not spend much time 
implementing search algorithms because programming languages like Python have them built-in. 
However, learning how to code a few search algorithms will make you a better programmer because it 
will help you better understand fundamental computer science concepts such as linear and logarithmic 
order of magnitudes. It is also crucial for you to understand these algorithms to know which of Python’s 
built- in search algorithms to use and how they will perform on different data sets.
In this chapter, you will learn how to search a list for a number using two different algorithms: a 
linear search and a binary search. Then, after you code each search algorithm yourself, I will show 
you how to perform the same search using Python’s built- in tools.
Linear Search
In a linear search, you iterate through every element in a data set and compare it to the target number. 
If your comparison finds a match, the number is in the list. If your algorithm ends without finding a 
match, the number is not in the list.



Download 1.48 Mb.

Do'stlaringiz bilan baham:
1   ...   28   29   30   31   32   33   34   35   ...   147




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