Python Coding Challenge (Level Intermediate)

Created: Mon 03 Apr 2023 Updated: 9 months, 1 week ago

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
Server Administrator

Suggested Posts:
PROGRAMMING post image
Python Tool to Download YouTube Video in 4K HD

This article is about my YouTube Downloader tool which is written in Python. It …

LINUX post image
Install Wordpress on LAMP Stack (Ubuntu)

According to a survey done by W3Techs, Wordpress is used by 41% of all …

LINUX post image
Configure Django with Apache, MySQL and WSGi on Ubuntu

In this article, I'll be demonstrating how we can deploy Django website on an …

CLOUD post image
Create IAM user policy for single S3 bucket access

Are you looking to grant specific access to an AWS S3 bucket for an IAM …

LINUX post image
Configure Firewalld in Linux

Firewalld is a powerful dynamic firewall management tool whose sole purpose is to manage firewall …

Sign up or Login to post comment.

Sign up Login

Comments (0)