A reason why I love Pythonic way - Series (Part1)
Problem Statement - 1
Lets say we have to validate whether a string has at least one Alphanumeric or Alphabetical or Digit or Lower Case or Upper Case character in it.
Pythonic Solution
Like many other programming language , python has also string methods such as isalnum, isalpha. isdigit, islower, isupper. So to solve the problem with these methods , we need to check each characters of the string against each method. That needs many lines of codes. However, using getattr, any method & list comprehension , we can solve the problem in just three lines of code:
s = input()
my_methods = ['isalnum', 'isalpha', 'isdigit', 'islower', 'isupper']
for i in my_methods:
print(any([getattr(char, i)() for char in s]))
Is not that beautiful code ? do let me know in comments if you find it hard to understand.
Another pythonic approach is to solve above problem could be by using for-else statements instead of any something like this:
s = input()
my_methods = ['isalnum', 'isalpha', 'isdigit', 'islower', 'isupper']
for i in my_methods:
for char in s:
obj = getattr(char, i)
if obj():
print(obj())
break
else:
print(False)
Problem Statement - 2
Given the a List of Integers and value of 'k', find the most common 'k' values in the list. Lets consider the following as example:
input_arr = [1, 3, 4, 5, 6, 7, 8, 4, 4, 8, 10, 20, 17, 15, 1, 3, 5]
k = 5
# Solution - 1 : Using Naive Approach
def k_most_frequent(k, input_arr):
""" Given an Array find the Most K-frequent Numbers"""
results = []
mapdict = {}
my_bucket = [0] * len(input_arr)
for i in input_arr:
if i in mapdict:
mapdict[i] += 1
else:
mapdict[i] = 1
for key in mapdict:
value = mapdict[key]
if my_bucket[value] == 0:
my_bucket[value] = [key]
else:
my_bucket[value].append(key)
i = len(my_bucket) - 1
while i >= 0:
if my_bucket[i] != 0:
if len(my_bucket[i]) == k:
results.extend(my_bucket[i])
break
elif len(my_bucket[i]) < k:
results.extend(my_bucket[i])
k -= len(my_bucket[i])
i -= 1
else:
results.extend(my_bucket[i][0:k])
break
else:
i -= 1
return results
print(k_most_frequent(k, input_arr))
# Solution - 2 : Using Python Sort by Values on Dictionary Approach
from collections import defaultdict
my_dict=defaultdict(int)
for num in input_arr:
my_dict[num] += 1
my_dict = dict(sorted(my_dict.items(),reverse=True, key= lambda x: x[1]))
print(list(my_dict.keys())[:k] )
# Solution - 3 : Using Python Counter module from collection
from collections import Counter
Counter = Counter(input_arr)
print(list(dict(Counter.most_common(k)).keys()))
So this is the most pythonic solution and only two lines of code solve the problem stated.
Is not that beautiful code ? do let me know in comments if you find it hard to understand.
Happy Python-ing ✌
Comments