0% found this document useful (0 votes)
8 views3 pages

Assignment 5

The document contains several Python programs demonstrating basic programming concepts such as nested lists, string manipulation, substring counting, string validation, and string mutation. Each section includes a brief description of the program's functionality along with the corresponding code. The programs are designed to be run in a main block and take user input for execution.

Uploaded by

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

Assignment 5

The document contains several Python programs demonstrating basic programming concepts such as nested lists, string manipulation, substring counting, string validation, and string mutation. Each section includes a brief description of the program's functionality along with the corresponding code. The programs are designed to be run in a main block and take user input for execution.

Uploaded by

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

Assignment-5

1. Nested Lists

Program:
if __name__ == '__main__':

students = []
for _ in range(int(input())):
name = input()
score = float(input())
students.append([name, score])
unique_grades = sorted(set(score for name, score in stud
ents))
second_lowest_grade = unique_grades[1]
second_lowest_students = sorted(name for name, score in
students if score == second_lowest_grade)
for name in second_lowest_students:
print(name)

2. String split and join

Program:

def split_and_join(line):

parts = line.split(" ")


result = "-".join(parts)
return result

if __name__ == '__main__':
line = input()
result = split_and_join(line)
print(result)

3. Find a string
Program:

def count_substring(string, sub_string):

count = 0
sub_length = len(sub_string)
for i in range(len(string) - sub_length + 1):
if string[i:i + sub_length] == sub_string:
count += 1
return count

if __name__ == '__main__':
string = input().strip()
sub_string = input().strip()

count = count_substring(string, sub_string)


print(count)

String Validators

Program:

s = input()

print(any([i.isalnum() for i in s]))


print(any([i.isalpha() for i in s]))
print(any([i.isdigit() for i in s]))
print(any([i.islower() for i in s]))
print(any([i.isupper() for i in s]))

4. Mutation

Program:

def mutate_string(string, position, character):

modified_string = string[:position] + character +


string[position+1:]
return modified_string

if __name__ == '__main__':
s = input()
i, c = input().split()
s_new = mutate_string(s, int(i), c)
print(s_new)

You might also like