Showing posts with label Python Practice Problems. Show all posts
Showing posts with label Python Practice Problems. Show all posts

Monday, January 27, 2020

Python Practice problem 4: Check for palindrome

Palindromes are the words that do not get changed when you put it in reverse order. So, "Mom", "Madam" are examples of the palindrome. This is a very simple problem where we use reversing a list using negative list slicing.


Thursday, January 23, 2020

Python Practice problem 3: Determining the overlaps of two lists.

Problem:

This is the practice problem 5 from https://www.practicepython.org/exercise/2014/03/05/05-list-overlap.html. The problem is -



1. We need to make two lists, the items of the lists might be unique or have common items within them.


2. We need to write a program that returns a list containing only the elements that are common between the lists while not considering the duplicates.


For example, we are given two lists- list_A and list_B as the following


list_A=[1,3,5,6,7,8,9,10,13,15,20]


list_B=[2,4,6,8,9,10,12,14]


Our program should be able to find duplicates.


Solving Part:

The problem is pretty straight forward, but our objective is to explore different ways and learn python through problem-solving.

While solving problems, one problem I often fetch is indexing error which is caused by putting variable() instead of variable[]. The problem arises because I came from a Matlab background where the function calling and the variable indexing uses the same "()" which is confusing actually.


Anyway, let's go to coding. The first quick draft is shown at the following which is not taking care of duplicate values. The first thing comes to mind is the use of Brute Force. which is -


1. Take any of the two lists and iterate through it

2. While iterating through the items, check for the existence in the second list using "in" keyword.

The code is shown below-




You can obviously see the problem here. Look at the program output. [6, 8, 9, 10, 10]. We have duplicates in the result. It is expected. The last piece of code I  want to add to this program is, well I know it's not the best way to do this, is using a set operation on the generated list to remove the duplicate items i.e., to keep the unique items only in the list.


So the final version is here.


Learnings:


Few things are important to note from here


"in" operator:

The "in" operator in python is for checking membership of value against a sequence. For example,
If we declare a list and then if we want to check for the existence of an item in that list we can use this keyword "in". I am writing these expressions for clarification

There are two operators which are called membership operators. 

a. in operator
b. not in operator
About 'not in' operator, It basically does the opposite of  "in" operator. i.e., the expression containing it evaluates to true if it does not find the variable in the sequence.

We need to learn about Identity operators too. There are two identity operators like the membership operator.


1. 'is' operator: It checks if the object on the left side of  'is' is pointing to the same object on the right side. Personally, at first, I was confused about this operator.  But, when I think of the variables as a just human-readable version of memory tags,  it becomes a little bit clear. The following example and the output is a good way to understand the behavior of the 'is' operator. Here we took three variables p,q, and r. Then we initialized p and q with the same content. In the third line of code, we make p and r equal. As expected, although the content of the p and q are the same, they are pointing to the different locations in the memory. The code snippets will clarify the rest-



2.' is not' operator: just the opposite of the 'is' operator. 






Monday, January 20, 2020

Python Practice Problem 2: Print the Fibonacci series up to a input point

Let us go to practice problem 14 ( https://www.practicepython.org/exercise/2014/04/30/13-fibonacci.html) and solve the problem of the Fibonacci series. If you want more information on this special type of numbers you can google the term or you can go to https://en.wikipedia.org/wiki/Fibonacci_number, they have some practical examples also.
I am putting my solution here .

Tuesday, January 14, 2020

Python Practice Problem 1: Finding the List of divisors of an integer number

Well, let us do exercise number 4, which is finding the divisors of an integer number. There are many ways to find a solution. Let's outline the objective and possible solution.

Objective:
1. User will input an integer
2. Find and Print the divisors
3. Also, Count the number of divisors

My First solution is given below.

This code can be modified a little bit to include the numbers in the list instead of printing the numbers. Now the modified code appends the divisors to a list. The purpose of this code is to understand the behavior of the empty list and learn how to append the code. The following code is the modified version of the previous one.
If we want to use list comprehension we can be more compact. But, personally, I am not a big fan of list comprehension. My personal opinion against is, sometimes it gets more complex to decode a list comprehension code which goes against the simplicity of Python. However, the following code is the list comprehension version. Again, from an efficiency standpoint, if we look carefully, the code is wasting half of its iterations by finding nothing. Let me explain with an example. Say, we are finding the divisors of 24. The divisors of 24 are [1, 2, 3, 4, 6, 8, 12, 24]. The way this divisor finder program is now working is checking with every number starting from 1, incrementing at one step, checking again and running the same process up to all the way to the input integer.

We already know that the number itself (24 in this case) and 1 is always will be in its divisor list.  And there is no possibility of getting any divisor when the program starts to check beyond half of the number (i.e., after 12 there will be no divisors). So, we can make use of this point by checking only up to half of the numbers. The code is below- Although I did not do any quantitative comparison of efficiency, it should be twice faster than the previous one. Again, other optimizations can be done (e.g., for even inputs the odd numbers cant be a divisor, some error handling for floating-point inputs) but they are not the purpose of this topic.

Pyhton exercises from Michele Pratusevich's website

While looking for python problems (because I do believe the best way to learn is by solving problems) to nurture my python skill I found this website https://www.practicepython.org by Michele Pratusevich. At first look, I liked the website and also sad by the fact that it is not being updated anymore. I will try to practice some problems from there. Will post those problems and my solution here.