0% found this document useful (0 votes)
6 views

Write_Functions[1]

The document outlines a practice assignment consisting of 21 programming tasks that involve writing functions in Python. These tasks cover various topics such as arithmetic operations, list manipulations, string processing, and character frequency analysis. Each task includes specific requirements and examples to guide the implementation of the functions.

Uploaded by

karungs2007
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Write_Functions[1]

The document outlines a practice assignment consisting of 21 programming tasks that involve writing functions in Python. These tasks cover various topics such as arithmetic operations, list manipulations, string processing, and character frequency analysis. Each task includes specific requirements and examples to guide the implementation of the functions.

Uploaded by

karungs2007
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Practice Assignment - Functions

1. Write a function sumEven(n1,n2), where n1 and n2 are positive integers such that n1<n2. The
function should return the sum of all the even integers between n1 and n2. For example, if n1=5 and
n2=10, the function should return 14 (6+8).
2. Write a function proDigits(n), where n is a positive integer. The function should return the product
of the digits of n. For example, if n=3214, then the function should return 24 (3x2x1x4).
3. Write a function countOddDigits(n), where n is a positive integer. The function should count and
display the number of odd digits in the number n. For example, if n=3214, then the function should
display "Number of odd digits = 2".
4. Write a function which accepts two lists, say A and B, as parameters. The function should delete all
elements from list A which are present in list B too. For example,
If A=[1,2,2,3,4,5,6,5,9,7,8,9] and B=[2,4,15,9,4,5,10,3], then after execution of
the function, the list A should be [1, 6, 7, 8]
5. Write a function which accepts a list, say A, as a parameter. The function should then exchange the values
of first half side elements with the second half side elements of the list.
For example,
if A=[2,4,1,6,7,9,23,10], then after the function execution, the list A should be
[7,9,23,10,2,4,1,6]
if A=[2,4,1,6,7,9,23], then after the function execution, the list A should be
[7,9,23,6,2,4,1]
6. Write a function REVERSE(N), where N is a list. The function should reverse the contents of the list
without slicing the list and without using any second list.
Example: If the list initially contains 2, 15, 3, 14, 7, 9, 19, 6, 1, 10

then after reversal the list should contain 10, 1, 6, 19, 9, 7, 14, 3, 15, 2

7. Write a function Alternate(nums), where nums is a list. The function should rearrange the list
nums in such a way that the values of each pair of consecutive elements are interchanged. For example:
If the list initially contains 2, 5, 9, 14, 17, 8
Then after rearrangement the list should contain 5, 2, 14, 9, 8, 17

If the list initially contains 2, 5, 9, 14, 17, 8,10


Then after rearrangement the list should contain 5, 2, 14, 9, 8, 17,10

8. Write a function combine(A,B,C), where A and B are equi-sized lists of numbers, and C is an empty
list. The function should combine the elements of A and B into C such that C[i]=2*A[i]+3*B[i],
where i is a valid index.
9. Write a function in calculate(A), where A is a list of integers. The function should divide all the
elements of the list by 5 which are divisible by 5, and multiply the other list elements by 2.
10. Write a function merge(A,B,C) where A and B are equi-sized lists and C is an empty list. The function
should merge the elements of A and B into C such that all even positions of C are occupied by the elements
of A and all odd positions of C are occupied by the elements of B. For example:
If A=['a','b','c','d'] and B=[10,2,5,6], then C should be
['a',10,'b',2,'c',5,'d',6]
11. Write a function in replace(A), where A is a list of integers. The function should replace
each element of the list having even value with its half and each element having odd value with its double.
For example, if A=[3, 4, 5, 16, 9] then after execution of the function, the list A should be [
6, 2, 10, 8, 18]
12. Write a function shortName(ft,md,last), where ft,md, and last are strings respectively
representing the first name, middle name, and the last name of a person. The function should return the
short name of the person. For example, if ft='Chander', md='Mohan', and
last='Subramaniam', then the function should return 'C. M. Subramaniam'.
13. Write a function that accepts a string as a parameter and checks whether it is a palindrome or not. The
checking should not be case sensitive. The function should display 'Palindrome' or 'Not
Palindrome' as per the case.
14. Write a function to palindrome(n), where n is a positive integer. The function should input n strings
from the user and return the total number of palindrome strings entered by the user.
15. Write a function to input a string from the user and display the number of digits, number of uppercase
alphabets, number of lowercase alphabets, and number of whitespace characters appearing in the string.
(space, '\t', and '\n' are whitespace characters.)
16. Write a function to input a string and replace each occurrence of multiple consecutive spaces, if any, with
a single space in the string. Then return the resultant string. For example, if the user enters '1 2 3
4 5 6666 7 8', then the function should return '1 2 3 4 5 6666 7 8'.
17. Write a function to input a string and count the number of words not starting with an uppercase vowel in
the string. Two consecutive words may be separated by one or more white spaces.
18. Write a function characters(S), where S is a string. The function should return a list of distinct
characters appearing in the string S. For example, if S='Apple in Pineapple', then the function
should return ['A', 'p', 'l', 'e', ' ', 'i', 'n', 'P', 'a']
19. Write a function frequency(S), where S is a string. The function should display the frequency of
each alphabet appearing in the string. (Alphabet counting should not be case-sensitive) For example, if
S= 'Shine in 2025', then the output should be:
S - 1
H - 1
I - 2
N - 2
E - 1
Hint: Use a dictionary to form the frequency table.
20. Write a function sortWords(S), where S is a string. The function should display all the words of the
string in alphabetical order. For example, if S='NCERT CBSE NIOS KV', then the output
should be: ['CBSE', 'KV', 'NCERT', 'NIOS']
21. Write a function vowelStar(S,ch), where S and ch are strings. The function should replace each
lower case vowel of the string S with the string ch, and return the resultant string. For example, if
S='Enormous Influence' and ch='i*', then the function should return the string
'En@$rm@$@$s Infl@$@$nc@$'.

You might also like