1.) Given A List, Write A Python Program To Swap First and Last Element of The List
1.) Given A List, Write A Python Program To Swap First and Last Element of The List
1.) Given A List, Write A Python Program To Swap First and Last Element of The List
) Given a list, write a Python program to swap first and last element of
the list.
Input : [23,35,19,6,24]
Output : [24,35,19,6,23]
Input : [1,2,3]
Output : [3,2,1]
2.) You are given a square matrix A with dimensions N X N. Your task is to find the
determinant. Note: Round the answer to 2 places after the decimal.
Input Format:
Sample Input
2
1.1 1.1
1.1 1.1
Sample output
0.0
3.) Write the program, takes a string and checks whether a string is a palindrome or
not using recursion.
Program Explanation
1. User must enter a string.
2. The string is passed as an argument to a recursive function.
3. In the function, if the length of the string is less than 1, True is returned.
4. If the last letter is equal to the first letter, the function is called recursively with
the argument as the sliced list with the first character and last character removed,
else return False.
5. The if statement is used to check if the returned value is True or False and the
final result is printed.
4.) Write the program, takes two strings and displays which letters are in the two
strings but not in both.
Program Explanation
1. User must enter two input strings and store it in separate variables.
2. Both of the strings are converted into sets and the letters which are present in the
two strings but not in both are found using the ‘^’ operator.
3. These letters are stored in a list.
4. A for loop is used to print the letters of the list.
Program Explanation
1. The user is asked to enter a string.
2. The function print_permutations_lexicographic_order is called on the string.
3. The function then prints all permutations of the string in order.
6.) Write the program, takes a list and finds the second largest number in the list
using bubble sort.
Program Explanation
1. User must enter the number of elements for the list and store it in a variable.
2. User must then enter the elements of the list one by one using a for loop and
store it in a list.
3. Then bubble sort algorithm may be used to sort the elements of the list.
4. The second last element of the sorted list is then printed which is basically the
second largest element in the list.
7.) Fibonacci numbers are defined by the sequence f(0) = 0, f(1) = 1 and f(n) = f(n – 1)
+ f(n – 2) for n >= 2. The program prompts the user to enter n and it prints the nth
Fibonacci number.
Program Explanation
1. The user is prompted to enter n.
2. fibonacci is called to compute the nth Fibonacci number.
3. The result is then displayed.
8.) The program takes a list and i as input and prints the ith smallest element in the
list.
Program Explanation
1. The user is prompted to enter a list of numbers.
2. The user is then asked to enter i.
3. The ith smallest element is found by calling select.
4. The result is displayed.
9.) Example to check whether an integer is a prime number or not using for loop
and if...else statement. If the number is not prime, it's explained in output why it is
not a prime number.
10.) Write the program, takes a string and counts the number of lowercase letters
and uppercase letters in the string.
Program Explanation
1. User must enter a string and store it in a variable.
2. Both the count variables are initialized to zero.
3. The for loop is used to traverse through the characters in the string.
4. The first count variable is incremented each time a lowercase character is
encountered and the second count variable is incremented each time a uppercase
character is encountered.
5. The total count of lowercase characters and uppercase characters in the string
are printed.
11.) write the program, takes a string and determines how many times a given
letter occurs in a string recursively.
Program Explanation
1. User must enter a string and a character and store it in separate variables.
2. The string and the character is passed as arguments to the recursive function.
3. The base condition defined is that the string isn’t empty.
4. If the first character of the string is equal to the character taken from the user, the
count is incremented.
5. The string is progressed by passing it recursively back to the function.
6. The number of times the letter is encountered in the string is printed.
12.) write the program, takes in two strings and display the larger string without
using built-in function.
Program Explanation
1. User must enter two strings and store it in separate variables.
2. The count variables are initialized to zero.
3. The for loop is used to traverse through the characters in the strings.
4. The count variables are incremented each time a character is encountered.
6. The count variables are then compared and the larger string is printed.
13.) Write a Python program to create a file where all letters of English
alphabet are listed by specified number of letters on each line.