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: 1 year, 2 months ago

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) …

WINDOWS post image
Reset windows 10 password using bootable usb drive

Windows 10 by Microsoft is the most used operating system nowadays. Despite being heavily …

LINUX post image
Install Latest Version of PHP on CentOS 8 and 7


CentOS is great. I have to admit that all those SELinux enforcement and other …

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 …

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 …

Sign up or Login to post comment.

Comments (0)