0% found this document useful (0 votes)
32 views2 pages

1

The document contains a Python function that takes a string, two special characters, a list, and a substring as parameters. It performs several string processing tasks, including removing special characters from the string, reversing part of the string, joining characters with a special character, checking if strings from the list are in the string, splitting the string into a list of words, and finding the last index of a substring.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views2 pages

1

The document contains a Python function that takes a string, two special characters, a list, and a substring as parameters. It performs several string processing tasks, including removing special characters from the string, reversing part of the string, joining characters with a special character, checking if strings from the list are in the string, splitting the string into a list of words, and finding the last index of a substring.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

# !

/bin/python3

# Basic libraries to import


import math
import os
import random
import re
import sys

# Complete the 'strmethod' function below.


#
# The function accepts following parameters:
# 1. STRING para
# 2. STRING spch1
# 3. STRING spch2
# 4. LIST li1
# 5. STRING strf
#

def stringmethod(para, special1, special2, list1, strfind):


# Write your code here
""" Remove all the special characters from **para** specified in special1 and
save them in the variable word1"""
#creating the variable to store the data while looping on the different special
characters
word1 = para
for word in special1:
word1 = word1.replace(word, '')

""" Get the first 70 characters from word1, reverse the strings, save it in
variable rword2, and print it"""
#creating the variable to store the data first and then loop through in reverse
rword2 = word1[:70] # saving only the first 70 characters
rword2 = rword2[::-1] # reversing the characters
print(rword2)

""" Remove all the wide spaces from rword2, join the characters using the
special character specified in special2, and print the joint string"""
rword2_no_spaces = rword2.replace(' ', '') # replacing the spaces first with
nothing
joint_string = special2.join(rword2_no_spaces)
print(joint_string)

""" If every string from list1 is present in para, then format the **print**
statement as follows: 'Every string in {list1} were present', else:
'Every string in {list1} were not present'"""
# Method 1 using the flag method
all_strings_present = True
for strings in list1:
if strings not in para:
all_strings_present = False # when we find the first element that's not
present in para, we can stop looking, otherwise we continue
break
# Now we check if the flag remains true or not
if all_strings_present == True:
print(f'Every string in {list1} were present')
else:
print(f'Every string in {list1} were not present')
"""# Method 2 checking the if all the words were present in para or not
# We define a counter to check at the end if it's equal to the len of the list
counter = 0
# We begin the loop to check every string inside list1
for strings in list1:
if (strings in para):
counter += 1 # the string was inside para
else:
counter += 0 # the string was not inside para

# We verify if the condition is met


if counter == len(list1):
print(f'Every string in {list1} were present')
else:
print(f'Every string in {list1} were not present')
"""

""" Split every word from word1 and print the first 20 strings as a list"""
# we first split the words from word1 into a list to separate everything
list_of_words = word1.split() # create the list with all the words separated
first_20_words = list_of_words[:20] # only the first 20
print(first_20_words)

""" Calculate the less frequently used words whose count < 13. Print the last
20 less frequent words as a list. Note, count the words in the order they appear"""
# We create a list to store the less frequent words
less_frequent_words = [] # empty list to add to
for strings in list_of_words:
if word1.count(strings) < 13:
if strings not in less_frequent_words:
less_frequent_words.append(strings)
# Now we select only the last 20 less frequent words from the list
print(less_frequent_words[-20:]) # 20th word is the -19th in the index, hence
the -20 to select it

""" Print the last index in word1, where the substring **strfind** is found"""
# We can use rfind or rindex. The difference being that using rfind and not
finding the sub-string returns a -1, while rindex() returns a ValueError exception
print(word1.rfind(strfind))

if __name__ == '__main__':
para = input()

spch1 = input()

spch2 = input()

qw1_count = int(input().strip())

qw1 = []

for _ in range(qw1_count):
qw1_item = input()
qw1.append(qw1_item)

strf = input()

stringmethod(para, spch1, spch2, qw1, strf)

You might also like