Skip to content

Commit 9e64a3b

Browse files
committed
all strings programs
1 parent 11c3c3c commit 9e64a3b

7 files changed

+249
-0
lines changed

Diff for: Milestone 2/strings/CheckPalindrome.py

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Given a string, determine if it is a palindrome, considering only alphanumeric characters.
2+
# Palindrome
3+
# A palindrome is a word, number, phrase, or other sequences of characters which read the same backwards and forwards.
4+
5+
# Sample Input 1 :
6+
# abcdcba
7+
# Sample Output 1 :
8+
# true
9+
10+
s = input()
11+
r = s[::-1]
12+
13+
if s == r :
14+
print('true')
15+
else:
16+
print('false')
17+
18+
# CN Code
19+
from sys import stdin
20+
21+
def ispalindrome(string):
22+
left = 0
23+
right = len(string) - 1
24+
25+
while left < right:
26+
if string[left] != string[right]:
27+
return False
28+
left += 1
29+
right -= 1
30+
return True
31+
32+
#main
33+
string = stdin.readline().strip()
34+
ans = ispalindrome(string)
35+
if ans:
36+
print("true")
37+
else:
38+
print("false")

Diff for: Milestone 2/strings/CheckPermutation.py

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# For a given two strings, 'str1' and 'str2', check whether they are a permutation of each other or not.
2+
# Permutations of each other
3+
# Two strings are said to be a permutation of each other when either of the string's characters can be rearranged so that it becomes identical to the other one.
4+
5+
# Example:
6+
# str1= "sinrtg"
7+
# str2 = "string"
8+
9+
# The character of the first string(str1) can be rearranged to form str2 and hence we can say that the given strings are a permutation of each other.
10+
11+
# Sample Input 1:
12+
# abcde
13+
# baedc
14+
# Sample Output 1:
15+
# true
16+
17+
18+
from sys import stdin
19+
20+
21+
def isPermutation(string1, string2) :
22+
if sorted(string1) == sorted(string2):
23+
return True
24+
else:
25+
return False
26+
27+
28+
29+
#main
30+
string1 = stdin.readline().strip();
31+
string2 = stdin.readline().strip();
32+
33+
ans = isPermutation(string1, string2)
34+
35+
if ans :
36+
print('true')
37+
else :
38+
print('false')
39+

Diff for: Milestone 2/strings/CompresstheString.py

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Write a program to do basic string compression. For a character which is consecutively repeated more than once, replace consecutive duplicate occurrences with the count of repetitions.
2+
# Example:
3+
# If a string has 'x' repeated 5 times, replace this "xxxxx" with "x5".
4+
5+
# The string is compressed only when the repeated character count is more than 1.
6+
# Note:
7+
# Consecutive count of every character in the input string is less than or equal to 9. You are not required to print anything. It has already been taken care of. Just implement the given function and return the compressed string.
8+
9+
# Sample Input 1:
10+
# aaabbccdsa
11+
# Sample Output 1:
12+
# a3b2c2dsa
13+
14+
from sys import stdin,setrecursionlimit
15+
setrecursionlimit(10**7)
16+
17+
18+
def getCompressedString(input) :
19+
# Write your code here.
20+
a = input
21+
i = 0
22+
x = ''
23+
while(i < len(a)):
24+
j = i+1
25+
c = 1
26+
while j < len(a) and (a[i] == a[j]):
27+
j += 1
28+
c += 1
29+
if c == 1:
30+
x += a[i]
31+
else:
32+
x += a[i]+str(c)
33+
i = j
34+
return x
35+
36+
37+
38+
# Main.
39+
string = stdin.readline().strip();
40+
ans = getCompressedString(string)
41+
print(ans)

Diff for: Milestone 2/strings/HighestOccuringCharacter.py

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# For a given a string(str), find and return the highest occurring character.
2+
# Example:
3+
# Input String: "abcdeapapqarr"
4+
# Expected Output: 'a'
5+
# Since 'a' has appeared four times in the string which happens to be the highest frequency character, the answer would be 'a'.
6+
# If there are two characters in the input string with the same frequency, return the character which comes first.
7+
# Consider:
8+
# Assume all the characters in the given string to be in lowercase always.
9+
10+
# Sample Input 1:
11+
# abdefgbabfba
12+
# Sample Output 1:
13+
# b
14+
15+
16+
from sys import stdin
17+
18+
19+
def highestOccuringChar(string) :
20+
ASCII_SIZE = 256
21+
ctr = [0] * ASCII_SIZE
22+
max = -1
23+
ch = ''
24+
for i in string:
25+
ctr[ord(i)] += 1
26+
27+
for i in string:
28+
if max < ctr[ord(i)]:
29+
max = ctr[ord(i)]
30+
ch = i
31+
return ch
32+
33+
34+
35+
#main
36+
string = stdin.readline().strip();
37+
ans = highestOccuringChar(string)
38+
39+
print(ans)

Diff for: Milestone 2/strings/RemoveConsecutiveDuplicates.py

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# For a given string(str), remove all the consecutive duplicate characters.
2+
# Example:
3+
# Input String: "aaaa"
4+
# Expected Output: "a"
5+
6+
# Input String: "aabbbcc"
7+
# Expected Output: "abc"
8+
9+
# Note:
10+
# You are not required to print anything. It has already been taken care of.
11+
12+
13+
from sys import stdin
14+
15+
def removeConsecutiveDuplicates(string) :
16+
s=''
17+
i=0
18+
while i<len(string):
19+
x = string[i]
20+
j=i+1
21+
while j<len(string) and string[j] == x:
22+
j=j+1
23+
s += string[i]
24+
i = j
25+
return s
26+
27+
28+
#main
29+
string = stdin.readline().strip()
30+
31+
ans = removeConsecutiveDuplicates(string)
32+
33+
print(ans)

Diff for: Milestone 2/strings/Removecharacter.py

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# For a given a string(str) and a character X, write a function to remove all the occurrences of X from the given string.
2+
# The input string will remain unchanged if the given character(X) doesn't exist in the input string.
3+
4+
# Note:
5+
# You are not required to print anything explicitly. It has already been taken care of.
6+
7+
# Sample Input 1:
8+
# aabccbaa
9+
# a
10+
# Sample Output 1:
11+
# bccb
12+
13+
14+
from sys import stdin
15+
16+
17+
def removeAllOccurrencesOfChar(string, ch) :
18+
# Your code goes here
19+
s = string
20+
x=""
21+
for i in s:
22+
if i != ch:
23+
x=x+i
24+
return x
25+
26+
27+
28+
#main
29+
string = stdin.readline().strip()
30+
ch = stdin.readline().strip()[0]
31+
32+
ans = removeAllOccurrencesOfChar(string, ch)
33+
34+
print(ans)

Diff for: Milestone 2/strings/ReverseEachWord.py

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Aadil has been provided with a sentence in the form of a string as a function parameter. The task is to implement a function so as to print the sentence such that each word in the sentence is reversed. A word is a combination of characters without any spaces.
2+
# Example:
3+
# Input Sentence: "Hello, I am Aadil!"
4+
# The expected output will print, ",olleH I ma !lidaA".
5+
6+
7+
from sys import stdin
8+
9+
10+
def reverseEachWord(string) :
11+
s = string
12+
new = s.split()
13+
ns = ''
14+
for i in range(len(new)):
15+
sad = new[i][::-1]
16+
ns = ns+sad+' '
17+
return ns
18+
19+
20+
#main
21+
string = stdin.readline().strip()
22+
23+
ans = reverseEachWord(string)
24+
25+
print(ans)

0 commit comments

Comments
 (0)