Assignment
Assignment #: 6
Topic : String
Level 1: Easy
[Link] OPERATION
Given a string and reverse the using Negative slicing.
Input format
The first line of each test case consists of string with Unicode character.
Output format
Print the reversed string.
Constraints
Using Negative slicing operation.
Sample test Cases:
Test Cases Test Case 1: Test Case 2: Test Case 3: Test Case 4:
Input “Mycomputer” “Student” “playing” “videogame”
Output “retupmocyM” “tnedutS” “gniyalp” “emagoediv”
[Link] BUILT IN METHODS
Given a string to replace each special symbol with # in the following string
Input format
The input string with special symbol.
Output format
Print replaced string
Constraints
Input should have special symbol.
Sample test Cases
Test Cases Test Case 1: Test Case 2: Test Case 3: Test Case 4:
Input str1 = '/*Jon is str1 = str1 = str1 =
@developer & ui!w@ghy^ui' ‘phy%chy$bio@’ 'g@d&he$tea*%!'
musician!!'
Output ##Jon is #developer
ui#w#ghy#ui'
# phy#chy#bio# g#d#he#tea###
musician##
[Link] ALPHANUMERIC STRING
Rohan have a string with sequence of words Can you help him to find the alphanumeric word of given
string?
Input format
The string with sequence of words or sentences
Output format
Print the alphanumeric string.
Constraints
String contain alphanumeric character.
Sample test Cases
Test Cases Test Case 1: Test Case 2: Test Case 3: Test Case 4:
Input "Emma25 is “Hi21 this is “raja1 and his2 “krishnan88 and
Data scientist50 good35” daughter” play6 game”
and AI Expert"
Output Emma25 Hi21 raja1 krishnan88
scientist50 good35 his2 play6
4. Anagram
Given two strings a and b consisting of lowercase characters. The task is to check whether two given
strings are an anagram of each other or not. An anagram of a string is another string that contains the
same characters, only the order of characters can be different. For example, act and tac are an anagram
of each other.
Sample test Cases
Test Cases Test Case1: Test Case2:
Input Str1 = listen Str1 = hello
Str2 = silent Str2 = bello
Output YES NO
5. Replace All Digits with Characters
You are given a 0-indexed string s that has lowercase English letters in its even indices and digits in its
odd [Link] is a function shift(c, x), where c is a character and x is a digit, that returns the xth
character after c.
For example, shift('a', 5) = 'f' and shift('x', 0) = 'x'.
For every odd index i, you want to replace the digit s[i] with shift(s[i-1], s[i]).
Return s after replacing all digits. It is guaranteed that shift(s[i-1], s[i]) will never exceed 'z'.
Constraints:
1 <= [Link] <= 100
s consists only of lowercase English letters and digits.
shift(s[i-1], s[i]) <= 'z' for all odd indices i.
Sample test Cases
Test Cases Test Case1: Test Case2:
Input s = "a1c1e1 s = "a1b2c3d4e"
Output "abcdef" "abbdcfdhe”
Level 2: Medium
[Link] STRING
Shalini is given a string and help her to Count and print out the number of lower case, upper case, and
non-letters.
Input format
Getting an input string with different format
Output format
You need to print number of lower case, upper case, and non-letters.
Constraints
Contains combination of words
Sample test Cases
Test Test Case 1: Test Case 2: Test Case 3: Test Case 4:
Cases
Input abcXYZ784*&ˆdef cirCULarCom@#s%^ REmoTE@#con&* lapTOP12(&^)
Output Lower case letters 6 Lower case letters 8 Lower case letters 5 Lower case letters :3
Upper case letters 3 Upper case letters 4 Upper case letters 4 Upper case letters:3
Non - letters: 6 Non - letters: 4 Non - letters: 4 Non - letters: 6
2. MIXED STRING
Rohan has two strings, s1 and s2. And to create a new string s3 made of the first char of s1, then the
last char of s2, Next, the second char of s1 and second last char of s2, and so on. Any leftover chars go
at the end of the result. Can you help him to complete the task?
Input format
The first string str1 is declared in program
The second string str2 is declared in program
Output format
Print the third string str3
Constraints
Input should be in string format
Sample test Cases
Test Cases Test Case 1: Test Case 2: Test Case 3: Test Case 4:
Input s1 = "Abc" s1 =” Def” s1 =” Klo” s1 = ”Mno”
s2 = "Xyz" s2 =” Hji” s2 =” Pyt” s2 =”Kgf”
Output AzbycX DiejfH KtlyoP MFngoK
3. ESCAPE SEQUENCING IN PYTHON
Rohan printing Strings with single and double quotes in it causes SyntaxError because String already
contains Single and Double Quotes and hence cannot be printed with the use of either of these. Hence,
can you help Rohan to print such a String either Triple Quotes are used or Escape sequences are used
to print such Strings?
Input format
The first line of Escape sequences starts with a backslash and can be interpreted differently.
The input string with single quotes is used to represent a string
Then all the single quotes present in the string must be escaped.
Output format
You need to print single quoted, double quoted and backslash string
Constraints
The single quotes present in the string must be escaped and same is done for Double Quotes.
Sample test Cases
Test Test Case 1: Test Case 2: Test Case 3: Test Case 4:
Cases
Input String1 = ''' I'm a String1 = ''' String1 = ''' It’s a String1 = = ''' I'm a
"Geek" ''' Escaped “Easy” ''' “Good “''' "Concept" '''
Output Initial String with Initial String with Initial String Initial String with
use of Triple Quotes: use of Triple Quotes: with use of Triple use of Triple Quotes:
I'm a "Geek" Escaped “Easy” "Quotes: I'm a "Concept"
it’s a “Good “
Escaping Single Quote: Escaping Single Quote: Escaping Single
I'm a "Geek" Escaped “Easy” Escaping Single Quote:
Quote: I'm a "Concept"
Escaping Double Escaping Double It’s a “Good “
Quotes: Quotes: Escaping Double
I'm a "Geek" Escaped “Easy” Escaping Double Quotes:
Quotes: I'm a "Concept"
Escaping Backslashes: Escaping Backslashes: It’s a “Good “
C:\Python\Geeks\ C:\Python\Easy\ Escaping Backslashes:
Escaping Backslashes:
C:\Python\Concept\
C:\Python\Good\
Tab: Tab: Tab:
Hi Geeks Hi Easy Tab: Hi Concept
Hi Good
New Line: New Line: New Line:
Python Python New Line: Python
Geeks Easy Python Concept
Good
Note:
To ignore the escape sequences in a String, r or R is used, this implies that the string is a raw string
and escape sequences inside it are to be ignored.
4. FIND SMALLEST WORD IN SRING
Rohan is given a sequence of words. Can you help him to find the smallest word in that sequence?
Input format
The input test case consists multiple words with different length
Output format
Print the smallest word.
Constraints
Using len() methods
Sample test Cases
Test Cases Test Case 1: Test Case 2: Test Case 3: Test Case 4:
Input Enter any String: Enter any String: Enter any String: Enter any String:
Python is simple Developer are You needs more Self-discipline
technically strong Training Plays Best roles
Output Smallest word: Smallest word: Smallest word: Smallest word:
is are more Best
[Link] WITH BUILT IN FUNCTION
Create a new string made of the first, middle, and last characters of each input string
Input format
Get the first character from both strings, concatenate them, and store them in variable x
Get the middle character from both strings, concatenate them, and store them in variable y
Output format
Print the result after join x, y, and z
Constraints
Using concatenate operation
Sample test Cases
Test Cases Test Case 1: Test Case 2: Test Case 3: Test Case 4:
Input s1 = "America" s1 = “Korea" s1 = "America" s1 = "Canadaa"
s2 = "Japan" s2 = "Japan" s2 = "China" s2 = "Sudan"
Output AJrpan KJrpan ACriaa CSadan
Level 3: Hard
1. Remove All Occurrences of a Substring
Given two strings s and part, perform the following operation on s until all occurrences of the substring
part are removed:
● Find the leftmost occurrence of the substring part and remove it from s.
● Return s after removing all occurrences of part.
● A substring is a contiguous sequence of characters in a string.
Example 1:
Input: s = "daabcbaabcbc", part = "abc"
Output: "dab"
Explanation: The following operations are done:
- s = "daabcbaabcbc", remove "abc" starting at index 2, so s = "dabaabcbc".
- s = "dabaabcbc", remove "abc" starting at index 4, so s = "dababc".
- s = "dababc", remove "abc" starting at index 3, so s = "dab".
Now s has no occurrences of "abc".
Example 2:
Input: s= "axxxxyyyyb", part = "xy"
Output: "ab"
Explanation: The following operations are done:
- s = "axxxxyyyyb", remove "xy" starting at index 4 so s = "axxxyyyb".
- s = "axxxyyyb", remove "xy" starting at index 3 so s = "axxyyb".
- s = "axxyyb", remove "xy" starting at index 2 so s = "axyb".
- s = "axyb", remove "xy" starting at index 1 so s = "ab".
Now s has no occurrences of "xy".
Constraints:
● 1 <= [Link] <= 1000
● 1 <= [Link] <= 1000
● s and part consists of lowercase English letters.
2. Minimum Number of Frogs Croaking
You are given the string croakOfFrogs, which represents a combination of the string "croak" from
different frogs, that is, multiple frogs can croak at the same time, so multiple "croak" are [Link]
the minimum number of different frogs to finish all the croaks in the given string.A valid "croak" means
a frog is printing five letters 'c', 'r', 'o', 'a', and 'k' sequentially. The frogs have to print all five letters to
finish a croak. If the given string is not a combination of a valid "croak" Print “Not Valid”.
Example 1:
Input: croakOfFrogs = "croakcroak"
Output: 1
Explanation: One frog yelling "croak" twice.
Example 2:
Input: croakOfFrogs = "crcoakroak"
Output: 2
Explanation: The minimum number of frogs is two.
The first frog could yell "crcoakroak".
The second frog could yell later "crcoakroak".
Constraints:
● 1 <= [Link] <= 105
● croakOfFrogs is either 'c', 'r', 'o', 'a', or 'k'.
3. Minimum Characters required to make a String Palindrome
Given a string A. The only operation allowed is to insert characters at the beginning of the [Link]
how many minimum characters are needed to be inserted to make the string a palindrome string.
Example Input
Input 1:
A = "ABC"
Input 2:
A = "AACECAAAA"
Example Output
Output 1:
Output 2:
Example Explanation
Explanation 1:
Insert 'B' at beginning, string becomes: "BABC".
Insert 'C' at beginning, string becomes: "CBABC".
Explanation 2:
Insert 'A' at beginning, string becomes: "AAACECAAAA".
Insert 'A' at beginning, string becomes: "AAAACECAAAA".
4. Check if frequencies can be equal
Given a string s which contains only lower alphabetic characters, check if it is possible to
remove at most one character from this string in such a way that frequency of each distinct
character becomes same in the string. Print the respective prompt.
Note: The driver code print 1 if the value returned is true, otherwise 0.
Example 1:
Input:
s = "xyyz"
Output:
Explanation:
Removing one 'y' will make frequency of each character to be 1.
Example 2:
Input:
s = "xxxxyyzz"
Output:
Explanation:
Frequency can not be made same by removing at most one character.