Python Coding Challenge (Level Intermediate)

article-featured-image

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.

Python Intermediate level challenges
protocolten-admin

Author: Harpreet Singh

Created: Mon 03 Apr 2023

Updated: 10 months, 3 weeks ago

Suggested Posts:
WINDOWS post image
Create environment variables and paths in windows

Windows environment variables and paths are essential components of the operating system, acting as pointers …

PROGRAMMING post image
Python web crawler to download images from web page

In this article, I'll be explaining the working of a Python web crawler whose …

LINUX post image
Setup Network boot for installing OS using PXE server

Whenever you are installing a new operating system in the machine, you insert your …

SCRIPTS post image
Python script convert S3 bucket images to a PDF file

AWS S3 is an object-level storage service that can be utilized in various ways, …

WINDOWS post image
Run any program as service in windows

Running a program as a service in Windows can be incredibly useful, allowing you to …

Sign up or Login to post comment.

Comments (0)