Python is one of the most powerfull programming language. Python is used in web-development, machine learning, data science, data scraping and the list is endless. This article is about Intermediate-level Python Challenges and Solutions. If you know basics of Python or you have working knowledge of Python, then this article is for you. If you are a beginner in Python, then you should probably checkout this article: Python Challenges for beginner
Question: 1
Write a Python function that takes a string as input and returns a dictionary that contains the count of each character in the string without any punctuation and space.
from collections import Counter
import string
usr_input = input("Enter any string: ")
def str_to_dict():
punc_remove = usr_input.translate(str.maketrans('', '', string.punctuation))
punc_remove = punc_remove.replace(' ', '')
char_list = []
for i in punc_remove:
char_list.append(i)
count = Counter(char_list)
return dict(count)
print(str_to_dict())
# output
Enter any string: Hello World!
{'H': 1, 'e': 1, 'l': 3, 'o': 2, 'W': 1, 'r': 1, 'd': 1}
Question: 2
Write a Python function called palindrome that takes in a string as an argument and returns True if the string is a palindrome (reads the same backwards as forwards), and False otherwise. The function should ignore case and non-alphabetic characters.
import string
def palindrome(para):
punc_remove = para.translate(str.maketrans('', '', string.punctuation)).lower().replace(' ', '')
rev = punc_remove[::-1]
if punc_remove == rev:
return True
else:
return False
print(palindrome("eye:eye@eye"))
print(palindrome("cat"))
# output
True
False
Question: 3
Write a Python function called frequent that takes in a string as an argument and returns a dictionary of the most frequently occurring characters in the string, along with their frequencies. It should ignore case sensitivity and non-alphabetic characters.
from collections import Counter
def frequent(para):
count = Counter()
for i in para:
if i.isalpha():
lower_case = i.lower()
count[lower_case] += 1
max_value = max(count.values())
max_char = {
key: value for key, value in count.items() if value == max_value
}
return f"Frequent character and it's occurrence is: {max_char}"
# output Frequent character and it's occurrence is: {'l': 3}
These are some of the Intermediate level Challenges in Python to understand the concept of Functions and modules. More questions will be added soon.




