0% found this document useful (0 votes)
93 views7 pages

Write Python Script For Following: Practical:Set - 3

The document provides code snippets for 8 Python programming problems: 1) Extracting the middle three characters of a string 2) Inserting a string in the middle of another string 3) Combining the first, middle, and last characters of two strings 4) Counting substring occurrences regardless of case 5) Creating a list by picking odd and even index elements from two lists 6) Counting the frequency of elements in a list using a dictionary 7) Pairing elements from two lists of the same length into a set 8) Removing elements from a list if they are not values in a given dictionary For each problem, the code, sample inputs/outputs, execution time and memory usage are

Uploaded by

nency ahir
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
93 views7 pages

Write Python Script For Following: Practical:Set - 3

The document provides code snippets for 8 Python programming problems: 1) Extracting the middle three characters of a string 2) Inserting a string in the middle of another string 3) Combining the first, middle, and last characters of two strings 4) Counting substring occurrences regardless of case 5) Creating a list by picking odd and even index elements from two lists 6) Counting the frequency of elements in a list using a dictionary 7) Pairing elements from two lists of the same length into a set 8) Removing elements from a list if they are not values in a given dictionary For each problem, the code, sample inputs/outputs, execution time and memory usage are

Uploaded by

nency ahir
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

PDS_3 190170107147

Practical :Set -3

AIM: Write python script for following

1. Given a string of odd length greater 7, return a string made of the middle three
chars of a given String .

Program:

def getMiddleThreeChars(sampleStr):
middleIndex = int(len(sampleStr) /2)
print("Original String is", sampleStr)
middleThree = sampleStr[middleIndex-1:middleIndex+2]
print("Middle three chars are", middleThree)

getMiddleThreeChars("nencyahir")
getMiddleThreeChars("ahirnencyy")

Output:
Output
Original String is nencyahir

Middle three chars are cya

Original String is ahirnencyy

Middle three chars are nen

Executed in: 0.028 sec(s)

Memory: 4372 kilobyte(s)

2. Given 2 strings, s1 and s2, create a new string by appending s2 in the middle of s1

1
PDS_3 190170107147

def appendMiddle(s1, s2):


middleIndex = int(len(s1) /2)
print("Original Strings are", s1, s2)
middleThree = s1[:middleIndex:]+ s2 +s1[middleIndex:]
print("After appending new string in middle", middleThree)

appendMiddle("ahir", "nency")

Output
Original Strings are ahir nency

After appending new string in middle ahnencyir

Executed in: 0.028 sec(s)

Memory: 4384 kilobyte(s

3. Given 2 strings, s1, and s2 return a new string made of the first, middle and last
char each input string

Program:
def mix_string(s1, s2):
first_char = s1[:1] + s2[:1]
middle_char = s1[int(len(s1) / 2):int(len(s1) / 2) + 1] + s2[int(len(s2)
/ 2):int(len(s2) / 2) + 1]
last_char = s1[len(s1) - 1] + s2[len(s2) - 1]
res = first_char + middle_char + last_char
print("Mix String is ", res)

s1 = "vaghamshi"
s2 = "nency"
mix_string(s1, s2)

2
PDS_3 190170107147

Output:
Output
Mix String is vnaniy

Executed in: 0.028 sec(s)

Memory: 4360 kilobyte(s)

4. Find all occurrences of “USA” in given string ignoring the case

Program:
inputString = "Welcome to USA. usa awesome, isn't it?"
substring = "USA"
tempString = inputString.lower()
count = tempString.count(substring.lower())
print("The USA count is:", count)

Output:
Output
The USA count is: 2

Executed in: 0.029 sec(s)

Memory: 4288 kilobyte(s)

5. Given a two list. Create a third list by picking an odd-index element from the
first list and even index elements from second.

Program:
listOne = [7, 6, 9, 56, 15, 18, 21]
listTwo = [6, 8, 12, 16, 60, 24, 28]
listThree = list()
3
PDS_3 190170107147

oddElements = listOne[1::2]
print("Element at odd-index positions from list one")
print(oddElements)

EvenElement = listTwo[0::2]
print("Element at even-index positions from list two")
print(EvenElement)

print("Printing Final third list")


listThree.extend(oddElements)
listThree.extend(EvenElement)
print(listThree)

Output:

Output
Element at odd-index positions from list one

[6, 56, 18]

Element at even-index positions from list two

[6, 12, 60, 28]

Printing Final third list

[6, 56, 18, 6, 12, 60, 28]

Executed in: 0.036 sec(s)

Memory: 5020 kilobyte(s)

6. Given a list iterate it and count the occurrence of each element and create a
dictionary to show the count of each element

Program:
4
PDS_3 190170107147

sampleList = [11, 45, 8, 11, 23, 45, 23, 45, 89]


print("Original list ", sampleList)

countDict = dict()
for item in sampleList:
if(item in countDict):
countDict[item] += 1
else:
countDict[item] = 1

print("Printing count of each item ",countDict)

Output
Original list [11, 45, 8, 11, 23, 45, 23, 45, 89]

Printing count of each item {11: 2, 45: 3, 8: 1, 23: 2, 89: 1}

Executed in: 0.033 sec(s)

Memory: 4352 kilobyte(s)

7. Given a two list of equal size create a set such that it shows the element from
both lists in the pair

Program:
firstList = [2, 3, 4, 5, 6, 7, 8]
print("First List ", firstList)

secondList = [4, 9, 16, 25, 36, 49, 64]


print("Second List ", secondList)

result = zip(firstList, secondList)


resultSet = set(result)
print(resultSet)

5
PDS_3 190170107147

Output:
Output
First List [2, 3, 4, 5, 6, 7, 8]

Second List [4, 9, 16, 25, 36, 49, 64]

{(6, 36), (8, 64), (4, 16), (5, 25), (3, 9), (7, 49), (2, 4)}

Executed in: 0.041 sec(s)

Memory: 4280 kilobyte(s)

8. Iterate a given list and Check if a given element already exists in a dictionary as
a key’s value if not delete it from the list

Program:

rollNumber = [37, 64, 69, 37, 76, 83, 95, 97]


sampleDict ={'nency':37, 'keyur':69, 'charmy':76, 'abhi’:97}

print("List -", rollNumber)


print("Dictionary - ", sampleDict)

rollNumber[:] = [item for item in rollNumber if item in sampleDict.values()]


print("after removing unwanted elemnts from list ", rollNumber)

Output:
Output
List - [37, 64, 69, 37, 76, 83, 95, 97]

Dictionary - {'nency': 37, 'keyur': 69, 'charmy': 76, 'abhi': 97}

after removing unwanted elemnts from list [37, 69, 37, 76, 97]

6
PDS_3 190170107147

Executed in: 0.031 sec(s)

Memory: 4300 kilobyte(s)

You might also like