/Python-Questions

Various collection of questions from different topics in PYTHON

Primary LanguagePython

Python-Questions

Various collection of questions from different topics in PYTHON

Assignment 1
Q1) Python Program to find GCD of Two Numbers
Q2) Python program to check if the number provided by the user is an Armstrong number or not
Q3) SUM OF SERIES OF N/N+1
Q4) CREATE A LIST OF AUTO PARTS ALONG WITH NAME AND PRICE


Assignment 2
1. Write a Python program to get a string made of the first 2 and the last 2 chars from a given a string. If the string length is less than 2, return instead of the empty string.
Sample String : 'a1resource'
Expected Result : 'a1ce'
Sample String : 'a1'
Expected Result : 'a1a1'
Sample String : ' a'
Expected Result : Empty String
2. Write a Python program to get a string from a given string where all occurrences of its first char have been changed to '$', except the first char itself.
Sample String : 'restart'
Expected Result : 'resta$t'
3. Write a Python program to find the first appearance of the substring 'not' and 'poor' from a given string, if 'not' follows the 'poor', replace the whole 'not'...'poor' substring with 'good'. Return the resulting string.
Sample String : 'The lyrics is not that poor!' 'The lyrics is poor!'
Expected Result : 'The lyrics is good!' 'The lyrics is poor!'
4. Write a Python function to insert a string in the middle of a string.
Sample function and result :
insert_sting_middle('[[]]', 'Python') -> [[Python]]
insert_sting_middle('{{}}', 'PHP') -> {{PHP}}
5. Write a Python program to sort a string lexicographically.
6. Write a Python program to count repeated characters in a string.
Sample string: 'thequickbrownfoxjumpsoverthelazydog'
Expected output :
o 4
e 3
u 2
h 2
r 2
t 2
7. Write a Python function that prints out the first ‘n’ rows of Pascal's triangle. ‘n’ is user input.
8. Write a Python function that accepts a hyphen-separated sequence of words as input and prints the words in a hyphen-separated sequence after sorting them alphabetically.
Sample Items : green-red-yellow-black-white
Expected Result : black-green-red-white-yellow
9. Write a Python function to merge two sorted arrays.
Sample Items :
m=[1, 4, 8]
n=[2, 3, 7]
Expected Output: mn=[1, 2, 3, 4, 7, 8]
10. Write a Python function that accepts a string and calculate the number of upper case letters and lower case letters.
Sample String : 'The quick Brow Fox'
Expected Output :
No. of Upper case characters : 3
No. of Lower case Characters : 12
11. Write a Python program which accepts a sequence of comma separated 4 digit binary numbers as its input and print the numbers that are divisible by 5 in a comma separated sequence.
Sample Data : 0100,0011,1010,1001,1100,1001
Expected Output : 1010
12. Write a Python program to insert a new item before the second element in an existing array.
13. Write a Python program to print alphabet pattern 'R'.
Expected Output:
****
* *
* *
****
**
* *
* *
14. Write a Python program to get the number of occurrences of a specified element in an array.
15. Write a Python program to remove a specified item using the index from an array.
16. Write a Python program to convert an array to an ordinary list with the same items.
17. Write a Python program to remove duplicates from a list of lists.
Sample list : [[10, 20], [40], [30, 56, 25], [10, 20], [33], [40]]
New List : [[10, 20], [30, 56, 25], [33], [40]]
18. Write a Python program to find the list in a list of lists whose sum of elements is the highest.
Sample lists: [1,2,3], [4,5,6], [10,11,12], [7,8,9]
Expected Output: [10, 11, 12]
19. Write a Python program to insert a given string at the beginning of all items in a list.
Sample list : [1,2,3,4], string : emp
Expected output : ['emp1', 'emp2', 'emp3', 'emp4']
20. Write a Python program to compute the similarity between two lists.
Sample data: ["red", "orange", "green", "blue", "white"], ["black", "yellow", "green", "blue"]
Expected Output:
Color1-Color2: ['white', 'orange', 'red']
Color2-Color1: ['black', 'yellow']
21. Write a Python program to split a list every Nth element.
Sample list: ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n']
Expected Output: [['a', 'd', 'g', 'j', 'm'], ['b', 'e', 'h', 'k', 'n'], ['c', 'f', 'i', 'l']]
22. Write a Python program to convert a list of multiple integers into a single integer.
Sample list: [11, 33, 50]
Expected Output: 113350
23. Write a Python program to iterate over two lists simultaneously.
24. Write a Python program to create a list by concatenating a given list which range goes from 1 to n.
Sample list : ['p', 'q']
n =5
Sample Output : ['p1', 'q1', 'p2', 'q2', 'p3', 'q3', 'p4', 'q4', 'p5', 'q5']


Assignment 3
1. Write a Python program to match key values in two dictionaries.
Sample dictionary: {'key1': 1, 'key2': 3, 'key3': 2}, {'key1': 1, 'key2': 2}
Expected output: key1: 1 is present in both x and y.
2. Write a Python program to create a dictionary from two lists without losing duplicate values.
Sample lists: ['Class-V', 'Class-VI', 'Class-VII', 'Class-VIII'], [1, 2, 2, 3]
Expected Output: defaultdict(<class 'set'>, {'Class-VII': {2}, 'Class-VI': {2}, 'Class-VIII': {3}, 'Class-V': {1}})
3. Write a Python program to replace dictionary values with their sum.
Example: Input: {'id' : 1, 'subject' : 'math', 'V' : 70, 'VI': 82}, {'id' : 2, 'subject' : 'math', 'V' : 73, 'VI' : 74}, {'id' : 3, 'subject' : 'math', 'V' : 75, 'VI' : 86}
Output: [{'subject': 'math', 'id': 1, 'V+VI': 76.0}, {'subject': 'math', 'id': 2, 'V+VI': 73.5} {'subject': 'math', 'id': 3, 'V+VI': 80.5}]
4. Write a Python program to sort a tuple by its float element.
Sample data: [('item1', '12.20'), ('item2', '15.10'), ('item3', '24.5')]
Expected Output: [('item3', '24.5'), ('item2', '15.10'), ('item1', '12.20')]
5. Write a Python program to remove an empty tuple(s) from a list of tuples.
Sample data: [(), (), ('',), ('a', 'b'), ('a', 'b', 'c'), ('d')]
Expected output: [('',), ('a', 'b'), ('a', 'b', 'c'), 'd']
6. Write a Python program to convert a list of tuples into a dictionary.
Example: Input: ((2, "w"),(3, "r"))
Output: {'w': 2, 'r': 3}
7. Write a Python program to count the elements in a list until an element is a tuple.
Example: Input: [10,20,30,(10,20),40]
Output: 3
8. Write a Python program to find maximum and the minimum value in a set.
Example: Input: ([5, 10, 3, 15, 2, 20])
Output: Maximum value: 20, Minimum value: 2
9. Write a Python program to create set difference, union, and intersection of sets.
Example: Input: set(["green", "blue"]), set(["blue", "yellow"])
Output: Difference: {'blue'}, {'green'}, Union: {'yellow', 'green', 'blue'} Intersection: {'blue'}
10. Write a Python program to make a chain of function decorators (bold, italic, underline etc.).
Example: Input: hello world
Output: hello world
12. Write a Python program of recursion list sum.
Test Data: [1, 2, [3,4], [5,6]]
Expected Result: 21
13. Write a Python program for binary search.
Example: Enter the sorted list of numbers: 3 5 10 12 15 20
The number to search for: 12
12 was found at index 3.
14. Write a Python program to sort a list of elements using the bubble sort algorithm.
Example: Sample Data: [14, 46, 43, 27, 57, 41, 45, 21, 70]
Expected Result: [14, 21, 27, 41, 43, 45, 46, 57, 70]
15. Write a Python program to sort a list of elements using the selection sort algorithm.
Example: Sample Data: [14, 46, 43, 27, 57, 41, 45, 21, 70]
Expected Result: [14, 21, 27, 41, 43, 45, 46, 57, 70]
16. Write a Python program to sort a list of elements using the merge sort algorithm.
Example: Split Sample Data: [14, 46, 43, 27, 57, 41, 45, 21, 70]
Merge and Sort(Expected Result): [14, 21, 27, 41, 43, 45, 46, 57, 70]
17. Write a Python program using functions that asks the user for a long string containing multiple words. Print back to the user the same string, except with the words in backwards order.
For example, say I type the string: My name is Michele;
Then I would see the string: Michele is name My; shown back to me.
18. Define a function reverse() that computes the reversal of a string.
For example, reverse(“I am testing”) should return the string ”gnitset ma I”.
19. Write a Python program to find the available built-in modules.
Example: math, random, uuid, sys, syslog etc.
20. 12Write a Python program to get the size of an object in bytes by using module “sys”.
Example: Memory size of 'one' = 52 bytes
Memory size of 'four' = 53 bytes
Memory size of 'three' = 54 bytes
21. Using the module random and time in python generate a random date between given start and end dates.
Example: Printing random date between 1/1/2016 and 3/23/2018
Random Date = 02/25/2016
22. Generate three random password string of length 10 with special characters, letters, and digits by using python modules (random and string).
Example: First Random String: yrjmcyi^VS
Second Random String: |}Hd]!^>~l
Third Random String: 3^a93@x=|Z
23. Write a python code using module “uuid” to generate universally unique secure randon string id of length 8.
Example: random string using a UUID module is: 9C8E13FF
random string using a UUID module is: 9cb3561d
24. Write a python code using module “random” to generate a 100 Lottery tickets and pick two lucky tickets from it as a winner.
Note: You must adhere to the following conditions:
1. Lottery number must be 10 digits long.
2. All 100 ticket number must be unique.
Example: Creating 100 random lottery tickets
Lucky 2 lottery tickets are [7184805696, 7380986204]