Python

Practice Assignments

  1. a) Print Mathematical tables from 3 to 20.
    3 * 1 = 3
    3 * 2 = 6

    3 * 10 = 30

    4 * 1 = 4
    4 * 2 = 8

    20 * 1 = 20
    20 * 2 = 40

    b) Take Input Parameters Start=3 and End=20 from User through Keyboard. Print the same Mathematical Tables. If user gives the two inputs as 4 and 25, then the tables should print from 4 to 25.

    c) Take Input Parameters from a file called in.txt having 5 and 30 in first and second line. Your program should read the first two lines of this file and print Mathematical Tables from 5 to 30.

    d) Take Input Parameters 6,12 from a file called in.txt written on the same line separated by a comma. The output should get written to a file called out.txt. To go to next line, f1.write("/n")

    e) Take Input Parameters 7 and 10 from console. Output to 4 different files 7.txt, 8.txt, 9.txt, 10.txt

    f) Take Input Parameters 8 and 9 from console. Output in a separate directory called TABLES and with 8_yyyymmdd_hhmmss.txt (eg 8_20210828_110503.txt). Import datetime as dt, today=dt.datetime.now()


  2. a) School Clock is showing 9 am. Compute the angle between the Hour hand and the Minute hand. It should show Time: Angle in Degrees from 9:00 am to 8:55 pm with a spacing of 5 minutes. First compute angle traversed by hour hand in one hour. Use this value to compute angle traversed in 5 minutes.
    9:05 -
    9:10 -
    9:15 -
    9:20 -
    9:25 -
    9:30 -
    9:35 -
    9:40 -
    9:45 -
    9:50 -
    9:55 -

    10:00 - 60.00
    10:05 -

    b) Use angle%360 to get modulo of 360 and avoid negative values. Initially let the clock show 24-hour time format eg. 13:00 hrs, 14:00 hrs etc. Later change to 12-hour format with AM, PM.

  3. a) Write a program to read each line of file inConvert.txt and write the equivalent reverse conversion into outConvert.txt
    Example 1: If input is
    1 metre = 100 com, then output should be

    1 metre = 100 cm
    1 cm = 0.01 metre

    Example 2: If input is
    8 kilometres = 5 miles, then output should be

    1 kilometer = (5/8) miles.
    1 mile = (8/5) kilometres

    b) Take units and value from input console. Print output in above format. Eg. USD 2 should give INR 150


  4. a) Andaman Prisoner Problem - There are 100 prison cells in a row. All cells are locked. Jailer is given permission by the Prime Minister of India to release any number of prisoners.
    In Round 1, Jailer opens all the doors.
    In Round 2, he closes every alternate door (2, 4, 6...).
    In Round 3, every third door (3, 6, 9,....) if Door is Open,, he closes it. If Door is Closed, he opens it.
    In Round 4, every fourth door (4, 8, 12..), if Door is open,, he closes it. If Door is Closed, he opens it.
    He does this for 100 Rounds. At the end, who are the lucky prisoners ?
    b) Prepare 2 letters. Letter1 to PM giving list of lucky prisoners and release date as today. Letter2 to Jailer giving list of unlucky prisoners who will be released after 4 weeks.


  5. a) Gold Medal Problem. There are 26 students in a class who have scored the following marks in 10th Std as per attached file Marks.txt (Name, Gender, RollNo....).Write a Program to read the contents of the file. Please print the Gold Medalist (overall top scorer) and individual Topper in each subject along with the respective marks.

    Amar,M,E80BD46CS0001,English:74,Maths:90,Physics:86,Chemistry:78,Biology:60,PASS
    Babu,M,E80BD46CS0002,English:76,Maths:91,Physics:87,Chemistry:70,Biology:70,PASS
    Charles,M,E80BD46CS0003,English:78,Maths:92,Physics:88,Chemistry:73,Biology:80,PASS
    David,M,E80BD46CS0004,English:80,Maths:93,Physics:89,Chemistry:76,Biology:90,PASS
    Ekalavya,M,E80BD46CS0005,English:82,Maths:94,Physics:90,Chemistry:79,Biology:100,PASS
    Fabin,M,E80BD46CS0006,English:84,Maths:95,Physics:91,Chemistry:82,Biology:90,PASS
    Govind,M,E80BD46CS0007,English:48,Maths:96,Physics:92,Chemistry:85,Biology:80,PASS
    Harnish,M,E80BD46CS0008,English:56,Maths:97,Physics:93,Chemistry:88,Biology:70,PASS
    Irene,F,E80BD46CS0009,English:64,Maths:98,Physics:95,Chemistry:91,Biology:60,PASS
    James,M,E80BD46CS0010,English:72,Maths:99,Physics:96,Chemistry:92,Biology:70,PASS
    Kamaraj,M,E80BD46CS0011,English:80,Maths:100,Physics:97,Chemistry:93,Biology:80,PASS
    Latha,F,E80BD46CS00012,English:88,Maths:99,Physics:98,Chemistry:94,Biology:90,PASS
    Manish,M,E80BD46CS0013,English:70,Maths:98,Physics:99,Chemistry:49,Biology:100,PASS
    Nagesh,M,E80BD46CS0014,English:76,Maths:97,Physics:87,Chemistry:59,Biology:90,PASS
    Omar,M,E80BD46CS0015,English:82,Maths:96,Physics:89,Chemistry:69,Biology:80,PASS
    Padma,F,E80BD46CS0016,English:88,Maths:95,Physics:91,Chemistry:79,Biology:70,PASS
    Queenie,F,E80BD46CS0017,English:60,Maths:94,Physics:93,Chemistry:89,Biology:75,PASS
    Roopa,F,E80BD46CS0018,English:68,Maths:93,Physics:95,Chemistry:79,Biology:80,PASS
    Sundar,M,E80BD46CS0019,English:77,Maths:92,Physics:97,Chemistry:80,Biology:85,PASS
    Tara,F,E80BD46CS0020,English:79,Maths:93,Physics:99,Chemistry:81,Biology:90,PASS
    Ullas,M,E80BD46CS0021,English:75,Maths:94,Physics:87,Chemistry:82,Biology:95,PASS
    Vasu,M,E80BD46CS0022,English:85,Maths:95,Physics:89,Chemistry:83,Biology:90,PASS
    Wendy,F,E80BD46CS0023,English:65,Maths:96,Physics:91,Chemistry:84,Biology:85,PASS
    Xero,M,E80BD46CS0024,English:25,Maths:98,Physics:93,Chemistry:85,Biology:80,PASS
    Yasmin,F,E80BD46CS0025,English:75,Maths:100,Physics:95,Chemistry:86,Biology:85,PASS
    Zafar,M,E80BD46CS0026,English:75,Maths:98,Physics:87,Chemistry:79,Biology:89,PASS


  6. a) One of your friends has been tested COVID-19 positive from location (28.5383° N, 81.3792° W). There are only 4 COVID-19 testing centres in the country as per the list below
    New York : 40.7128° N, 74.0060° W
    Chicago: 41.8781° N, 87.6298° W
    Denver: 39.7392° N, 104.9903° W
    Los Angeles: 34.0522° N, 118.2437° W

    Please determine the nearest location to where the patient has to be sent for Corona virus investigation.
    b) If each latitude is around 60 miles, if each longitude is around 54 miles, what is the distance that the patient has to travel.


  7. a) List top 10 countries with highest Corona deaths. The required statistics may be taken from WHO or MyGov or Download hereSample data below
    2020-10-15,AF,Afghanistan,EMRO,32,40112,0,1485
    2020-10-16,AF,Afghanistan,EMRO,47,40159,4,1489
    2020-10-17,AF,Afghanistan,EMRO,68,40227,3,1492


  8. Find list of difficult words to learn for TOEFL exam from the passage below. These difficult words are enclosed between 2 asterix characters
    Another day, and more cases reported from across the country. The number of COVID-19 cases tested positive in India is 156 and there are 139 active cases. According to WHO, as of March 17, there were 184,976 confirmed COVID-19 cases and 7,529 deaths in 159 countries.Chennai reported its second positive case today, West Bengal reported its first case yesterday, and the government of Goa made a *faux pas*.The State's health minister said a Norwegian national had tested positive but shortly afterwards retracted his remark.


  9. Remove special characters from a string.
    old = "How are you ? Oh my God, you are bleeding! Let us go to the doctor quickly."
    new = How are you Oh my God you are bleeding Let us go to the doctor quickly

  10. a) Create a unique set of words arranged in alphabetical order. This list of words should be stored in a file called sorted_passage1.txt

    Adapted from "The Colors of Animals" by Sir John Lubbock in A Book of Natural History (1902, ed. David Starr Jordan)

    The color of animals is by no means a matter of chance; it depends on many considerations, but in the majority of cases tends to protect the animal from danger by rendering it less conspicuous. Perhaps it may be said that if coloring is mainly protective, there ought to be but few brightly colored animals. There are, however, not a few cases in which vivid colors are themselves protective. The kingfisher itself, though so brightly colored, is by no means easy to see. The blue harmonizes with the water, and the bird as it darts along the stream looks almost like a flash of sunlight.

    Desert animals are generally the color of the desert. Thus, for instance, the lion, the antelope, and the wild donkey are all sand-colored. “Indeed,” says Canon Tristram, “in the desert, where neither trees, brushwood, nor even undulation of the surface afford the slightest protection to its foes, a modification of color assimilated to that of the surrounding country is absolutely necessary. Hence, without exception, the upper plumage of every bird, and also the fur of all the smaller mammals and the skin of all the snakes and lizards, is of one uniform sand color.”

    The next point is the color of the mature caterpillars, some of which are brown. This probably makes the caterpillar even more conspicuous among the green leaves than would otherwise be the case. Let us see, then, whether the habits of the insect will throw any light upon the riddle. What would you do if you were a big caterpillar? Why, like most other defenseless creatures, you would feed by night, and lie concealed by day. So do these caterpillars. When the morning light comes, they creep down the stem of the food plant, and lie concealed among the thick herbage and dry sticks and leaves, near the ground, and it is obvious that under such circumstances the brown color really becomes a protection. It might indeed be argued that the caterpillars, having become brown, concealed themselves on the ground, and that we were reversing the state of things. But this is not so, because, while we may say as a general rule that large caterpillars feed by night and lie concealed by day, it is by no means always the case that they are brown; some of them still retaining the green color. We may then conclude that the habit of concealing themselves by day came first, and that the brown color is a later adaptation.

    The example of the mature caterpillar in the third paragraph is primarily intended to demonstrate

    b) Now, create this as a function and apply this to all the files in a given directory.
    If there are 10 files called
    a. txt
    b. txt

    then, we should have
    sorted_a.txt
    sorted_b.txt
    sorted_c.txt

    c) Merge all these files into one single sorted set of words which have unique words. This may be called sorted_20200326.txt

    ie. sorted_yyyymmdd.txt

  11. MailMerge for Insurance Payment Reminder
    We have a template of a message to be sent. Input file - template.txt - containing below template
    Dear $1salutation $2name ,You are kindly requested to make a payment of $3amount by $4date

    Input file - list1.txt - containing list of people who have to pay payment

    M, James, 1000, 01-Apr-2020
    F, Swati, 2000, 02-Apr-2020
    M, Khan, 3000, 03-Apr-2020
    F, Asha, 4000, 04-Apr-2020

    Output file - message.txt - containing a personalized message for each person
    This output file should have all merged content after taking inputs from both the files

    Dear Mr. James, please pay a premium of Rs 1000 by 01-Apr-2020
    Dear Ms. Swati, please pay a premium of Rs 2000 by 02-Apr-2020
    Dear Mr. Khan, please pay a premium of Rs 3000 by 03-Apr-2020
    Dear Ms. Asha, please pay a premium of Rs 4000 by 03-Apr-2020

  12. a) Find Resolution of an Image

    with open(filename,'rb') as img:
    img_file.seek(163)
    a = img_file.read(2)
    height = (a[0] << 8) + a[1]
    a = img_file.read(2)
    width = (a[0] << 8) + a[1]


  13. a) Find Hash of a File In any website, when we register, we are asked to set a Password. The service provider converts this Password into a HASH value and stores it in database. Next time, when you input your password, whatever you have input is converted into a HASH value. Both HASH values are compared. If they match, you are allowed entry. Else, you are blocked. Please generate the HASH of a given string or a file


  14.             
                a.  Create a list months=[Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec]
                b.  Create a list days=[31,28,31,30,31,30,31,31,30,31,30,31] 
                
                c.  Output to be as below
                     Jan 1 
                     Jan 2 
                     Jan 3 
                     
                d.  Output to be as below
                     Wed, Jan 1, 2025
                     Thu, Jan 2, 2025
                     Fri, Jan 3, 2025
                     
                e.  If your country has holiday on Sundays, mention it 
                     Wed, Jan 1, 2025
                     Thu, Jan 2, 2025
                     Fri, Jan 3, 2025
                     Sat, Jan 4, 2025
                     Sun, Jan 5, 2025 - Holiday
                     
                f.  Read file holidays.txt and mark the holidays against the applicable days
                     Wed, Jan 1, 2025
                     Thu, Jan 2, 2025
                     Fri, Jan 3, 2025
                     Sat, Jan 4, 2025
                     Sun, Jan 5, 2025 - Weekly Holiday
                     Sat, Jan 6, 2025
                     Sat, Jan 7, 2025
                     Sat, Jan 8, 2025
                     Sat, Jan 9, 2025
                     Sat, Jan 10, 2025
                     Sat, Jan 11, 2025 
                     Sat, Jan 12, 2025 - Weekly Holiday
                     Sat, Jan 13, 2025
                     Sat, Jan 14, 2025 - Makara Sankranthi