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

Nalanda Python Assignment Day-1 Ans

The document outlines a series of programming tasks, primarily focused on string manipulation, list operations, and basic algorithm implementations in Python. Tasks include validating Indian pin codes, checking membership in lists, finding overlaps between lists, and creating functions for palindromes and character frequency. Additional exercises involve generating song lyrics, finding digital roots, and correcting string formatting.

Uploaded by

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

Nalanda Python Assignment Day-1 Ans

The document outlines a series of programming tasks, primarily focused on string manipulation, list operations, and basic algorithm implementations in Python. Tasks include validating Indian pin codes, checking membership in lists, finding overlaps between lists, and creating functions for palindromes and character frequency. Additional exercises involve generating song lyrics, finding digital roots, and correcting string formatting.

Uploaded by

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

1) Write a function (def is_valid_pin(pin_code)) which should return whether a string represents a valid

Indian pin code. Consider a valid pin code is any string consisting of exactly 6 digits.

2) Write a function is_member() that takes a value (i.e. a number, string, etc) x and a list of values a, and
returns True if x is a member of a, False otherwise. (Note that this is exactly what the in operator does,
but for the sake of the exercise you should pretend Python did not have this operator.)

3) Define a function overlapping() that takes two lists and returns True if they have at least one member in
common, False otherwise. You may use your is_member() function, or the in operator, but for the sake
of the exercise, you should (also) write it using two nested for-loops.
4) Write a program that maps a list of words into a list of integers representing the lengths of the
corresponding words.

5) Write a function find_longest_word() that takes a list of words and returns the length of the longest one.
6) Write a version of a palindrome recognizer that also accepts phrase palindromes such as "Go hang a
salami I'm a lasagna hog.", "Was it a rat I saw?", "Step on no pets", "Sit on a potato pan, Otis", "Lisa
Bonet ate no basil", "Satan, oscillate my metallic sonatas", "Rise to vote sir", or the exclamation
"Dammit, I'm mad!". Note that punctuation, capitalization, and spacing are usually ignored.

7) Write a function char_freq() that takes a string and builds a frequency listing of the characters contained
in it. Represent the frequency listing as a Python dictionary. Try it with something like
char_freq("abbabcbdbabdbdbabababcbcbab").
8) Write a python program that prints the frequency of all the unique words. You can have a list of words
or you can take data from the user.

9) Write a python function to test, if a given number (passed as argument) is a perfect number
(https://en.wikipedia.org/wiki/Perfect_number)
10) Define a function reverse() that computes the reversal of a string. For example, reverse("I am testing")
should return the string "gnitset ma I".

11) Write a python function to find the digital root of a number.


(To find digital root, you need to add the digits of a number till you get a single digit number. That is say
you have 56789. In the first iteration you add 5, 6, 7, 8 and 9. The result is 35. In the second iteration
you add 3 and 5. You get 8. So 8 is the answer.)

12) Define a function is_palindrome() that recognizes palindromes (i.e. words that look the same written
backwards). For example, is_palindrome("radar") should return True.

13) "99 Bottles of Beer" is a traditional song in the United States and Canada. It is popular to sing on long
trips, as it has a very repetitive format which is easy to memorize, and can take a long time to sing. The
song's simple lyrics are as follows:

99 bottles of beer on the wall, 99 bottles of beer.


Take one down, pass it around, 98 bottles of beer on the wall.
The same verse is repeated, each time with one fewer bottle. The song is completed when the singer
or singers reach zero.

Your task here is write a Python program capable of generating all the verses of the song.

14) Write a function which accepts a string and returns a list of all possible permutations of the given string
in python.

15) Define a simple "spelling correction" function correct() that takes a string and sees to it that 1) two or
more occurrences of the space character is compressed into one, and 2) inserts an extra space after a
period if the period is directly followed by a letter. E.g. correct("This is very funny and cool.Indeed!")
should return "This is very funny and cool. Indeed!".

You might also like