Python Coding Challenge (Level Intermediate)

Created: Mon 03 Apr 2023 Updated: 8 months 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:
LINUX post image
Install LAMP on Ubuntu or CentOS with firewall configuration

This tutorial will be focused on installing the LAMP stack (Linux Apache MySQL PHP) …

LINUX post image
Django Checklist for Deploying Application in Production

While experience in the development and production environment of django application is almost similar, …

LINUX post image
Sync Cloud Storage Drives using Rclone

Rclone is an Open-source, multi-threaded, command line tool. It makes data synchronization much more …

CLOUD post image
Setup AWS cross-account RDS MySQL master and slave servers

This article is about AWS RDS master and slave replication. I'll explain how to …

SECURITY post image
Large Data Encryption & Decryption using Cryptography

In the past few years, keeping your data safe and secure is challenging than …

Sign up or Login to post comment.

Sign up Login

Comments (0)