Heyy, how are your guys doing?? Welcome to another lesson from NCP. Today’s lesson will be an introduction to algorithms. Are you ready for that interview or test that you have been waiting for so long? Are you sure?
Algorithms is a series of steps that can be followed to solve a problem. The problem could be searching a list, printing the lyrics to a song or just basically anything. Let us take a look at some of the most famous algorithms commonly asked in many interviews and tests.
FizzBuzz:
The first on the list is FizzBuzz which is a popular interview question. The FIzzBuzz challenge requires you to write a program to print the numbers from 1-100 but print “Fizz” for the multiples of 3 and “Buzz” for the multiples of 5 and “FizzBuzz” for the multiples of 3 and 5. To solve this problem you can use the mudulo (%) operator to check if the number is a multiple of another number (Operators are discussed in this lesson). Basically the modulo operator performs a division between the two numbers and returns the remainder of the division. To check if a number is a multiple of 5, you can use the following code:
x = 25
print(x%5)
If the program prints ‘0’ then the value of ‘x’ is a multiple of ‘5’ otherwise it is not a multiple of ‘5’.
To solve FizzBuzz you merely have to iterate through all the numbers from 1-100 and check if each number is a multiple of ‘3’, ‘5’, or both ‘3’ and ‘5’. I want you to solve FizzBuzz using the knowledge you have gained so far. If you come across any issues you can contact me personally with my contact info given on the ‘Contact Us’ page and at the end of this article. But I know you guys can do it. If you have been reading all the lessons on NCP then this should be a piece of cake for you ;).
This is how your first ten numbers should look like:
SEARCH ALGORITHMS:
A search algorithm finds information in a data structure like a list.
A sequential search is a simple search algorithm that checks each item in a data structure to see if the item matches what it is looking for. For example, when you look for a card in a deck, you perform a sequential search. You go through each card in the deck and if the card is not the one you are looking for you move on to the next card. When you finally come to the card you are looking for you stop. If you make it through the entire deck without finding the card, you also stop because you know the card isn’t there. Here is an example of a sequential search algorithm that looks for the number ‘n’ in a list.
def ss(number_list, n):
found = False
for i in number_list:
if i == n:
found = True
break
return found
The variable ‘found’ keeps track of whether or not your algorithm has found ‘n’ in the list. You loop through each number in the list and check if it is the number you are looking for. If it is, you set ‘found’ to ‘True’ and your algorithm stops, otherwise your algorithm continues until it either finds ‘n’ or reaches the end of the list and returns ‘False’. To test your algorithm, create a list of numbers and look for a random number in the list.
PALINDROME:
A palindrome is a number spelled the same way forwards and backwards. For example, the word ‘level’ is a palindrome. You can check if a word is a palindrome by reversing all the letters in the word and testing if the reversed word and the original word are the same, if they are, the word is a palindrome.
Here is an example of an algorithm that checks if a word is a palindrome:
def is_palindrome(word):
word = word.lower()
return word[::-1] == word
First, you make sure the word is lowercase because Python treats capitalized and lowercase letters differently and for this algorithm, they are the same. ‘word[::-1]’ reverses the original word by returning a slice of the entire word in reverse.
Finally, you compare the two words (the reversed and the original word) and return True if they are same and return False if they are not. Test your algorithm with different words and see if they are palindrome or not.
ANAGRAM:
An anagram is a word created by rearranging the letters of another word. For example, the word ‘iceman’ is an anagram of ‘cinema’.
You can determine if two words are anagrams by sorting the letters in each word alphabetically and testing if they are the same. Here is the algorithm to check if two words are anagrams:
def is_anagram(w1, w2):
w1 = w1.lower()
w2 = w2.lower()
return sorted(w1) == sorted(w2)
Python’s built-in ‘sorted()’ method takes a string and returns a new string in alphabetical order. Since, Python takes care of the hard work for you, all you have to do is test if the two sorted strings are the same.
REPEATING CHARACTERS:
Another popular interview question is to print how many times a character appears in the string. Here is the algorithm to solve this problem:
def count_characters(string):
count_dict = {}
for c in string:
if c in count_dict:
count_dict[c] += 1
else:
count_dict[c] = 1
print(count_dict)
First we create a dictionary that is going to store the characters in the string as ‘keys’ and the number of times it has appeared in the string as ‘values’. (I have created an article for dictionaries in Python you can check it out here)
Next, we iterate through each character in the string and if the character is already present in ‘count_dict’, we will increment its value by 1 and if it is not present then we add the character to the dictionary and assign the value 1 to it.
Here is how the algorithm looks like in action:
CONGRATULATIONS !!! You now know some of the famous algorithms asked in interviews and tests, you are now one step closer to nailing that test that has been bothering you for so long ;).
Let me know, if you loved it, hated it, want to kill me or any other sort of feedback in the comments section below. Also, if you have any queries regarding the topics taught in this lesson or previous lessons, you can always find me in the comments section or in the telegram channel or on my pinterest profile where you can personally talk to me and ask me any question about anything we have learnt so far.
So, this was an introduction to algorithms and we also discussed about some of the famous algorithms. Stay tuned for another article next week, same time. DON’T MISS IT !! And I'll see you next week. Goodbye and Good Luck :)
I hope this article answered all of your questions and even helped you in becoming a better programmer. IF IT DID, leave a like AND FOLLOW THIS BLOG TO BECOME A PROFESSIONAL PYTHON PROGRAMMER FROM A TOTAL BEGINNER. IF IT DIDN'T, feel free to ask any further queries in the comment section below.
If you are a beginner, intermediate, advanced or just someone interested in programming, feel free to join our telegram channel and be among people like you:
https://t.me/joinchat/AAAAAFVduJs3bF00nwKGUA
And do you know the best part? Joining it is FREE !!!
So go ahead click on the link and I will see you there.
You can also contact me through my email: code2learnofficial@gmail.com
or
HOPE YOU HAVE AN AWESOME DAY AHEAD !!!










0 Comments
Welcome to the comments section, this is where you can contact me personally for any doubts or feedback