Assignments 3 String List Dictionary Functions Recursion Python Programming Bcc402
Assignments 3 String List Dictionary Functions Recursion Python Programming Bcc402
[Assignment Set – 1]
1. Write a program that accepts a string from user. Your program should count and display
number of vowels in that string. Solution
).
* The number of digits in the string
* The number of whitespace characters in the string Solution
02
3. Write a Python program that accepts a string from user. Your program should create and
C4
display a new string where the first and last characters have been exchanged.
For example if the user enters the string 'HELLO' then new string would be 'OELLH' Solution
C
(B
4. Write a Python program that accepts a string from user. Your program should create a new
string in reverse of first string and display it.
g_
For example if the user enters the string 'EXAM' then new string would be 'MAXE' Solution
in
5. Write a Python program that accepts a string from user. Your program should create a new
m
string by shifting one position to left.
m
For example if the user enters the string 'examination 2021' then new string would be
'xamination 2021e' Solution
ra
6. Write a program that asks the user to input his name and print its initials. Assuming that the
og
user always types first name, middle name and last name and does not include any unnecessary
spaces.
Pr
For example, if the user enters Ajay Kumar Garg the program should display A. K. G.
on
7. A palindrome is a string that reads the same backward as forward. For example, the words
th
dad, madam and radar are all palindromes. Write a programs that determines whether the string
Py
is a palindrome.
).
02
C C4
(B
g_
in
m
m
ra
og
Pr
on
th
Py
Lists
[Assignment Set – 1]
1. Write a program that accepts a list from user and print the alternate element of list. Solution
2. Write a program that accepts a list from user. Your program should reverse the content of list
and display it. Do not use reverse() method. Solution
).
3. Find and display the largest number of a list without using built-in function max(). Your
02
program should ask the user to input values in list from keyboard. Solution
C4
4. Write a program that rotates the element of a list so that the element at the first index moves
to the second index, the element in the second index moves to the third index, etc., and the
element in the last index moves to the first index. Solution
C
(B
5. Write a program that input a string and ask user to delete a given word from a string. Solution
6. Write a program that reads a string from the user containing a date in the form mm/dd/yyyy.
g_
It should print the date in the form March 12, 2021. Solution
in
7. Write a program with a function that accepts a string from keyboard and create a new string
after converting character of each word capitalized. For instance, if the sentence is "stop and
m
smell the roses." the output should be "Stop And Smell The Roses"
m
8. Find the sum of each row of matrix of size m x n. For example for the following matrix output
ra
Sum of row 1 = 32
th
Sum of row 2 = 31
Py
Sum of row 3 = 63
Solution
2. Write a program to print all elements in a list those have only single occurrence.
Example: if contents of list is [7, 5, 5, 1, 6, 7, 8, 7, 6].
Your output should be:
).
18
Solution
02
3. Write a program to enter names of employees and their salaries as input and store them in a
C4
dictionary. Solution
4. Write a program to read 6 numbers and create a dictionary having keys EVEN and ODD.
C
Dictionary's value should be stored in list. Your dictionary should be like:
(B
{'EVEN':[8,10,64], 'ODD':[1,5,9]}
Solution
g_
5. Write a program that reads string from user. Your program should create a dictionary having
in
key as word length and value is count of words of that length. For example, if user enters 'A fat
cat is on the mat'.
m
m
A 1
og
fat 3
cat 3
Pr
is 2
on
on 2
th
the 3
Py
mat 3
The content of dictionary should be {1:1, 3:4, 2:2}
Solution
6. Write a program to input roll numbers and their names of students of your class and store
them in the dictionary as the key-value pair. Perform the following operations on the dictionary:
a) Display the Roll numbers and name for all students.
b) Add a new key-value pair in this dictionary and display the modified dictionary
c) Delete a particular student's record from the dictionary
d) Modify the name of an existing students.
Solution
Function
[Assignment Set – 1]
1. Write a function find_max that accepts three numbers as arguments and returns the largest
number among three. Write another function main, in main() function accept three numbers
from user and call find_max. Solution
2. Write a function, is_vowel that returns the value true if a given character is a vowel, and
otherwise returns false. Write another function main, in main() function accept a string from
).
user and count number of vowels in that string. Solution
02
3. Write a function named is_prime, which takes an integer as an argument and returns true if
the argument is a prime number, or false otherwise. Also, write the main function that displays
C4
prime numbers between 1 to 500. Solution
4. Write a function in python to find the sum of the cube of elements in a list. The list is received
C
as an argument to the function, in turn, the function must return the sum. Write the main
(B
function which invokes the above function. Solution
g_
5. Write the definition of a function zero_ending(scores) to add all those values in the list of
scores, which are ending with zero and display the sum.
in
For example: If the scores contain [200, 456, 300, 100, 234, 678] The sum should be displayed
m
as 600. Solution
m
6. Write a definition of a method count_now(places) to find and display those place names, in
which there are more than 5 characters.
ra
For example :
og
7. Write a method in python to display the elements of list thrice if it is a number and display the
element terminated with ‘#’ if it is not a number.
Py
9. Write a function half_and_half that takes in a list and change the list such that the elements of
the second half are now in the first half.
For example, if the size of list is even and content of list is as follows :
my_liist = [10,20,30,40,50,60]
The output should be
).
[40,50,60,10,20,30]
02
if the size of list is odd and content of list is as follows :
my_liist = [10,20,30,40,50,60,70]
C4
The output should be
[50,60,70,40,10,20,30] Solution
C
10. Write a function that accepts a dictionary as an argument. If the dictionary contains
(B
duplicate values, it should return an empty dictionary. Otherwise, it should return a new
dictionary where the values become the keys and the keys become the values.
g_
For example, if the dictionary contains the following key-value pairs:
{'a': 10, 'b': 20, 'c': 20}
in
the function should return an empty dictionary {} because there are duplicate values.
m
On the other hand, if the dictionary contains the following key-value pairs:
m
original dictionary become the keys, and the keys from the original dictionary become the
og
values. Solution
Pr
on
th
Py
Recursion
[Assignment Set – 1]
1. Write a recursive function that accepts an integer argument and returns the factorial.
Solution
2. Write a recursive function that accepts two numbers as its argument and returns its
power. Solution
).
3. Write a recursive function that accepts a number as its argument and returns the sum
02
of digits. Solution
C4
4. Write a program that reads two integers from keyboard and calculate the greatest
common divisor (gcd) using recursive function. Solution
C
5. Write a recursive function that accepts an integer argument in n. This function returns
(B
the nth Fibonacci number. Call the function to print fibonacci sequences. Solution
g_
6. Write a recursive function that accepts a decimal integer and display its binary
equivalent. Solution
in
7. Write a recursive function that calculate sum of first n natural numbers. Solution
m
element X to be searched from the List Arr having R elements, where L represents lower
ra
1. Multiples of 3 or 5
Pr
If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and
9. The sum of these multiples is 23.
on
Each new term in the Fibonacci sequence is generated by adding the previous two
terms. By starting with 1 and 2, the first 10 terms will be:
By considering the terms in the Fibonacci sequence whose values do not exceed four
million, find the sum of the even-valued terms.
A palindromic number reads the same both ways. The largest palindrome made from
the product of two 2-digit numbers is 9009 = 91 × 99.
Find the largest palindrome made from the product of two 3-digit numbers.
5. Smallest multiple
).
02
2520 is the smallest number that can be divided by each of the numbers from 1 to 10
without any remainder.
C4
What is the smallest positive number that is evenly divisible by all of the numbers from
1 to 20?
C
(B
6. Sum square difference
g_
The sum of the squares of the first ten natural numbers is,
12+22+...+102=385
in
m
The square of the sum of the first ten natural numbers is,
m
(1+2+...+10)2=552=3025
ra
Hence the difference between the sum of the squares of the first ten natural numbers
og
Find the difference between the sum of the squares of the first one hundred natural
numbers and the square of the sum.
on
7. 10001st prime
th
By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime
Py
is 13.
A Pythagorean triplet is a set of three natural numbers, a < b < c, for which,
a2 + b2 = c2
9. Summation of primes
).
02
215 = 32768 and the sum of its digits is 3 + 2 + 7 + 6 + 8 = 26.
C4
What is the sum of the digits of the number 21000
C
(B
g_
in
m
m
ra
og
Pr
on
th
Py