Python Programming

Priya Rammohan
3 min readDec 30, 2020

Me : what is de-dup ?

Nila : list contain duplicates. set(any-list) - removes the duplicates from a list.

Me : what are the different data structures available in python ?

Nila: list ->[], tuples ->( ), dictionary->{} -> set . some are mutable(lists) and some aren’t. all the above mentioned are separated by a comma to make the sequence more readable.type() command is useful in checking the datatypes of variables. Consider casting to an another datatype , to the one you are very much familiar with.As days goes by, picking the right datatype, do I use while, for,recursion comes natural to you in making the right decision. Example of casting: list(generator), set(), int() etc.

Me : How do you know which one to use ?

Nila : Let me make a bold statement, you can almost always solve your problem with list comprehension.(aka map, reduce, filter)

Me : Let’s talk about while, for & recursion.

Nila : when I want to chase my own tail its called recursion. the combination of for, return and list comprehension is a beauty to behold. while has 3 parts left , middle , right. some times the right part is a constant like 4 million. The left part, is the piece of cloth between you and me , just so we can spin in circles. once you are done spinning and out of breath, you exit out of the loop. some where in the middle you scale the left part with addition or shrink it with division.some times you perform crazy things like, (while r * r ≤ n ) and start an another while within the outer while, when you need to perform factorization.

Me : what are the factors of a number say 18.

Nila : its [2,3,3] .

Me : There are two while loops. The outer loop checks, if n > 1. Step into the inner while loop, with the condition, is n % factor == 0 , then divide n = n / factor. append the factor in a list or yield the factor.if n % factor != 0 then increment the factor by 1.

Nila : That’ sweet. Let’s talk about reversing a digit.

Me : If n is the number to be reversed into r . use a while, to get each individual bits, starting from the right most digit. The right most digit can be obtained using a mod 10 and push the right most digit to the left, using r * 10 and sum them up for each iteration, until n > 0 and reduce n with n //=10 should do it. n == isReverse(n) then its a palindrome. [Rewind : I solved this on a white board once with counting the length of the string, is the length odd or even and looping forward and backward. Can’t remember, if I found a solution. it would have been simpler with a zoom , a problem statement, raise a PR and ask some one to review the code, answer any questions on the PR and have a working code in your repo, at the end of the exercise. I love GIT]

Nila : The largest palindrome made from the product of 3 digit numbers . Possibly this can be solved with list comprehension.

from itertools import combinations

list = [x for x in range(100,999)]

pairs = combinations(list,2)

palindrome = [ a[0] * a[1] for a in pairs if isPalindrome(a[0] * a[1]) == True]

print(max(palindromes))

--

--