Showing posts with label string. Show all posts
Showing posts with label string. Show all posts

PYTHON ASSIGNMENT 2 - STRING


Read a string

     Check for empty string

     Display string length

     Display reverse of a string

     Character at a given position

     Index of a substring in a string

     Copy first n characters

     Copy last n character

     Copy n characters from mth position

     Convert all characters to uppercase

     Convert all characters to lower case

     Replace a character

     Delete a character

     Count number of vowels

     Split a string based on the spaces / delimiter



Write a program to read and join a list of strings

Write a program to check if a given string is a palindrome or not.

Write a program to count and display the number of capital letters in a given string.

Write a program to count the number of vowels in a string.

Write a program to display the number of times each vowel occurs in a string. (ignore case)

Write a program to remove all punctuations and special characters (~!@#$%^&*()_{}[]\:”;’<>?,./) in a given string.

Write a program to read a string and count and display the number of upper, lower, digit and special characters in a given string.

Write a Python program to accept a string and display the resultant string in a reverse order. The resultant string should contain all characters at the even position of accepted string ignoring blank spaces.
     Example:
          Given string: An apple a day keeps the doctor away
          Required output: ywrtoetpeydepaA

Given a string containing both upper and lower case letters, write a python program to count the number of repeated characters and display the maximum count of a character along with the character
     Sample input: ABaBCbGc
     Output:
          2A
          3B
          2C
          1G
          B is repeated 3 times

Consider 2 strings string1 and string2 and display the merged_string as output. The merged_string should be capital letters from both the strings in the order they appear.
     Sample Input:
     String1: I Like Java
     String2: Mary Likes Python
     merged_string: ILJMLP




Histogram - String - Word frequency

# Histogram - String - Word Frequency

import operator

string1 = "An algorithm is a step-by-step procedure to solve a given problem. It is a well-defined computational procedure that takes some values as input, manipulates them using a set of instructions and produces some values as output and terminates in a finite amount of time. An algorithm, when formally written in a programming language is called a program, or code. Derivation of an algorithm that solves the problem and conversion of the algorithm into code, together,  is known as Algorithmic Problem Solving."
string1 = string1.lower()

string1 = string1.replace(".", "")
string1 = string1.replace(",", "")
string1 = string1.replace("-", " ")

List1 = string1.split(" ")

dict1 = {}
for ele in List1:
    if ele in dict1:
        dict1[ele] += 1
    else:
        dict1[ele] = 1
print("Histogram")
print("Length of dictionary = ", len(dict1))
print(dict1)

List2 = sorted(dict1.items(), key=operator.itemgetter(1), reverse=True)

dict2 = dict(List2[:3])
print("Most frequently used 3 words:")
print(dict2)

Histogram - String - Character frequency

# Histogram - Character frequency

import operator

string1 = "An algorithm is a step-by-step procedure to solve a given problem. It is a well-defined computational procedure that takes some values as input, manipulates them using a set of instructions and produces some values as output and terminates in a finite amount of time. An algorithm, when formally written in a programming language is called a program, or code. Derivation of an algorithm that solves the problem and conversion of the algorithm into code, together,  is known as Algorithmic Problem Solving."
string1 = string1.lower()

dict1 = {}
for idx in range(97, 123, 1):
    count = string1.count(chr(idx))
    if(count != 0):
        dict1[chr(idx)] = count

print("Histogram")
print(dict1)


List1 = sorted(dict1.items(), key=operator.itemgetter(1), reverse=True)
dict2 = dict(List1[:5])
print("Most frequent 5 characters")
print(dict2)