0% found this document useful (0 votes)
24 views14 pages

Week-4

The document outlines programming exercises for a Python lab, focusing on functions that handle lists, strings, and dictionaries. Key tasks include checking if a list is sorted, identifying duplicates, removing duplicates, and manipulating string cases. Additionally, it requires writing a recursive function to generate binary strings of a specified length.

Uploaded by

leucinexyz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views14 pages

Week-4

The document outlines programming exercises for a Python lab, focusing on functions that handle lists, strings, and dictionaries. Key tasks include checking if a list is sorted, identifying duplicates, removing duplicates, and manipulating string cases. Additionally, it requires writing a recursive function to generate binary strings of a specified length.

Uploaded by

leucinexyz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Python

Programming Lab
Week-4

3 May 2025 Week-4 1


Week-4
1. Write a function called is_sorted that takes a list as a parameter and
returns True if the list is sorted in ascending order and False
otherwise.
2. Write a function called has_duplicates that takes a list and returns
True if there is any element that appears more than once. It should
not modify the original list.
3. i).Write a function called remove_duplicates that takes a list and
returns a new list with only the unique elements from the original.
Hint: they don’t have to be in the same order.
ii).The wordlist I provided, words.txt, doesn’t contain single letter
words. So you might want to add “I”, “a”, and the empty string.
iii).Write a python code to read dictionary values from the user.
Construct a function to invert its content. i.e., keys should be values
and values should be keys.

3 May 2025 Week-4 2


Week-4
3. i) Add a comma between the characters. If the given word is 'Apple', it
should become 'A,p,p,l,e'
ii) Remove the given word in all the places in a string?
iii) Write a function that takes a sentence as an input parameter and
replaces the first letter of every word with the corresponding upper
case letter and the rest of the letters in the word by corresponding
letters in lower case without using a built-in function?
4. Writes a recursive function that generates all binary strings of n-bit
length

3 May 2025 Week-4 3


Week-4: 1)Write a function called is_sorted that takes a list
as a parameter and returns True if the list is sorted in
ascending order and False otherwise.

3 May 2025 Week-4 4


2) Write a function called has_duplicates that takes a list and returns
True if there is any element that appears more than once. It should not
modify the original list.

3 May 2025 Week-4 5


2) Write a function called has_duplicates that takes a list and returns
True if there is any element that appears more than once. It should not
modify the original list.

3 May 2025 Week-4 6


i). Write a function called remove_duplicates that takes a list and
returns a new list with only the unique elements from the original. Hint:
they don’t have to be in the same order.

3 May 2025 Week-4 7


ii). The wordlist I provided, words.txt, doesn’t contain single
letter words. So you might want to add “I”, “a”, and the
empty string.

3 May 2025 Week-4 8


iii). Write a python code to read dictionary values from the user. Construct a
function to invert its content. i.e., keys should be values and values should be
keys.

3 May 2025 Week-4 9


Output:

3 May 2025 Week-4 10


i) Add a comma between the characters. If the
given word is 'Apple', it should become 'A,p,p,l,e'

3 May 2025 Week-4 11


ii) Remove the given word in all the places in
a string?

3 May 2025 Week-4 12


iii) Write a function that takes a sentence as an input parameter and replaces
the first letter of every word with the corresponding upper case letter and the
rest of the letters in the word by corresponding letters in lower case without
using a built-in function?

3 May 2025 Week-4 13


4) Writes a recursive function that generates all
binary strings of n-bit length

3 May 2025 Week-4 14

You might also like