AssignmentQuestions(sampleQuestionswithAnswers)_7ab1ae7fda537733eb109f9fc955886a
AssignmentQuestions(sampleQuestionswithAnswers)_7ab1ae7fda537733eb109f9fc955886a
def perfect_square(number):
if number < 0:
return -1
sqrt = int(math.sqrt(number))
if sqrt * sqrt == number:
return number
else:
return -1
def sort_words(input_str):
# Split the input string into a list of words
words = input_str.split(', ')
# Example usage:
input_sequence = input("Enter a comma-separated sequence of words: ")
sort_words(input_sequence)
2. Construct a program that accepts a comma-separated sequence of words as input and
prints the words in a comma-separated sequence after sorting them in reverse
alphabetical order.
Input: without, hello, bag, world
Output: world, without, hello, bag
3. Construct a program that accepts a comma-separated sequence of words as input and
prints the words in a comma-separated sequence after sorting them based on their
length (shortest to longest).
Input: without, hello, bag, world
Output: bag, hello, world, without
4. Construct a program that accepts a comma-separated sequence of words as input and
prints each word along with its frequency of occurrence in the input sequence.
Input: hello, world, hello, python, world
Output:
hello: 2
world: 2
python: 1
5. Construct a program that accepts a comma-separated sequence of words as input and
prints the unique words in a comma-separated sequence.
Input: hello, world, hello, python, world
Output: hello, world, python
6. Construct a program that accepts a comma-separated sequence of words as input and
concatenates them into a single string without spaces between them.
Input: without, hello, bag, world
Output: withouthellobagworld
7. Construct a program that accepts a comma-separated sequence of words as input and
counts and displays the number of uppercase and lowercase letters in the entire
sequence.
Input: Hello, WoRlD, Python
Output:
Uppercase letters: 5
Lowercase letters: 10
8. Construct a program that accepts a comma-separated sequence of words as input and
checks and prints if each word is a palindrome or not.
Input: radar, level, hello, world
Output:
radar: True
level: True
hello: False
world: False
9. Construct a program that accepts two comma-separated sequences of words as input
and checks if they are anagrams of each other.(anagram: a word, phrase, or name
formed by rearranging the letters of another, such as spar, formed from rasp.)
Input 1: listen, silent
Input 2: hello, world
Output:
listen and silent are anagrams.
hello and world are not anagrams.
# Call the function with user input and print the result
result = removenth(input_string, input_index)
print("Resulting string:", result)
1. Construct a function retsmaller(1) that returns smallest list from a nested list. If two
lists have same length then return the first list that is encountered.
For example:
retsmaller([ [-2, -1, 0, 0.12, 1, 2], [3, 4, 5], [6, 7, 8, 9, 10], [11, 12, 13, 14, 15]])
returns [3,4,5]
retsmaller([ [-2, -1, 0, 0.12, 1, 2], ['a', 'b', 'c', 'd', 3, 4, 5], [6, 7, 8, 9, 10], [11, 12, 13,
14, 15]] )
returns [6, 7, 8, 9, 10]
def ret_smaller(nested_list):
# Initialize the smallest list with the first list in the nested list
smallest_list = nested_list[0]
return smallest_list
# Call the function with user input and print the result
result = ret_smaller(nested_list)
print("The smallest list is:", result)
2. Construct a function ret_larger(nested_list) that returns the largest list from a nested
list. If two lists have the same length, then return the first list that is encountered.
ret_larger([ [-2, -1, 0, 0.12, 1, 2], [3, 4, 5], [6, 7, 8, 9, 10], [11, 12, 13, 14, 15]])
returns [11, 12, 13, 14, 15]
ret_larger([ [-2, -1, 0, 0.12, 1, 2], ['a', 'b', 'c', 'd', 3, 4, 5], [6, 7, 8, 9, 10], [11, 12, 13, 14,
15]] )
returns ['a', 'b', 'c', 'd', 3, 4, 5]
3. Construct a function ret_max_sum(nested_list) that returns the list with the maximum
sum of elements from a nested list. If two lists have the same sum, then return the
first list that is encountered.
ret_max_sum([[1, 2, 3], [4, 5], [1, 1, 1, 1]])
returns [4, 5]
4. Construct a function ret_min_sum(nested_list) that returns the list with the minimum
sum of elements from a nested list. If two lists have the same sum, then return the
first list that is encountered.
ret_min_sum([[1, 2, 3], [4, 5], [1, 1, 1, 1]])
returns [1, 1, 1, 1]
6. Construct a function ret_max_avg(nested_list) that returns the list with the highest
average value from a nested list. If two lists have the same average, then return the
first list that is encountered.
ret_max_avg([[1, 2, 3], [4, 5], [10, 20]])
returns [10, 20]
7. Construct a function ret_min_avg(nested_list) that returns the list with the lowest
average value from a nested list. If two lists have the same average, then return the
first list that is encountered.
ret_min_avg([[1, 2, 3], [0, 0, 0], [1, 1, 1]])
returns [0, 0, 0]
9. Construct a function ret_min_median(nested_list) that returns the list with the lowest
median value from a nested list. If two lists have the same median, then return the
first list that is encountered.
ret_min_median([[1, 2, 3], [0, 0, 0], [1, 1, 1]])
returns [0, 0, 0]
def check_password_validity(passwords):
valid_passwords = []
return valid_passwords
2. A website requires the users to input their email addresses to register. Construct
a program to check the validity of email inputs by users. Following are the criteria
for checking the email:
i. It should contain '@'.
ii. It should contain at least one '.' after the '@'.
iii. The '@' should not be at the beginning or end of the email.
iv. The '.' should not be at the beginning or end of the email and should be
after '@'.
v. The email should not contain spaces.
Your program should accept a sequence of comma-separated emails and check
them according to the above criteria. Emails that match the criteria are to be
printed, each separated by a comma.
5. A website requires users to input their credit card number for making transactions.
Construct a program to check the validity of credit card numbers input by users.
The following are the criteria for checking the credit card number:
i. Must be exactly 16 digits long.
ii. Must contain only numbers.
iii. The first digit must be a 4, 5, or 6.
Your program should accept a sequence of comma-separated credit card numbers
and will check them according to the above criteria. Credit card numbers that
match the criteria are to be printed, each separated by a comma.
6. A website requires users to input their birth date for registration. Construct a
program to check the validity of birth dates input by users. The following are the
criteria for checking the date:
i. Must be in the format DD/MM/YYYY.
ii. Day must be between 01 and 31.
iii. Month must be between 01 and 12.
iv. Year must be a four-digit number.
Your program should accept a sequence of comma-separated dates and will check
them according to the above criteria. Dates that match the criteria are to be
printed, each separated by a comma.
1. Illustrate different list slicing constructs for the following operations on the
following list:
L=[1,2,3,4,5,6,7,8,9]
i. Return a list of numbers starting from the last to second item of the list.
ii. Return a list that starts from 3rd item to second last item.
iii. Return a list that has only even position elements of list L to List M.
iv. Return a list that starts from the middle of the list L.
v. Return a list that reverses all the elements starting from element at index
0 to middle index only and return the entire list.
Divide each element of the list by 2 and replace it with the remainder.
i. Return a list of numbers starting from the last to second item of the list:
L=[1,2,3,4,5,6,7,8,9]
result = L[-1:1:-1]
print(result) # Output: [9, 8, 7, 6, 5, 4, 3]
ii. Return a list that starts from 3rd item to second last item:
L=[1,2,3,4,5,6,7,8,9]
result = L[2:-1]
print(result) # Output: [3, 4, 5, 6, 7, 8]
iii. Return a list that has only even position elements of list L to List M:
L=[1,2,3,4,5,6,7,8,9]
M = L[1::2]
print(M) # Output: [2, 4, 6, 8]
iv. Return a list that starts from the middle of the list L:
L=[1,2,3,4,5,6,7,8,9]
middle_index = len(L) // 2
result = L[middle_index:]
print(result) # Output: [5, 6, 7, 8, 9]
v. Return a list that reverses all the elements starting from element at index 0
to middle index only and return the entire list:
L=[1,2,3,4,5,6,7,8,9]
middle_index = len(L) // 2
result = L[:middle_index][::-1] + L[middle_index:]
print(result) # Output: [4, 3, 2, 1, 5, 6, 7, 8, 9]
Divide each element of the list by 2 and replace it with the remainder:
L=[1,2,3,4,5,6,7,8,9]
result = [x % 2 for x in L]
print(result) # Output: [1, 0, 1, 0, 1, 0, 1, 0, 1]