Question

Answer the following questions using linux 1) Write a Python script so that a file whoComesForDinner.txt...

Answer the following questions using linux

1) Write a Python script so that a file whoComesForDinner.txt formatted like this:     

Tuesday we have Joe McHungry and Paul McHungry

Wednesday we have Cindy Johnston and Paul Paulsen Thursday we have Amin Mirzaei and Atefeh Mirzaei gets turned into that:

The McHungry on Tuesday

Cindy and Paul on Wednesday

The Mirzaei on Thursday

2)Write a script that takes as argument a port number. It returns the next port that is not assigned

to anything. To know if a port is assigned, the file /etc/services contains a list of assigned ports, one per line. Here is an example of 3 lines in that file:       

ftp           21/udp http         80/tcp

http-alt 8080/tcp

Here are two examples of how the script should answer:

./nextFreePort.sh 80

82

./nextFreePort.sh 10024

10024

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Part 1:

import os
import sys

if not os.path.isfile("whoComesForDinner.txt"):
    print "\nFile not found."
    sys.exit(1)

inputfile = open("whoComesForDinner.txt", "r")
for line in inputfile.readlines(): #Reading line by line
    line = line.rstrip('\n') #Removing new lines.
    words = line.split(' ') #Splitting into words.
    if words[4].lower() == words[7].lower(): #If surnames are same.
        sentence = "The " + words[7] + " on " + words[0]
    else:
        sentence = words[3] + " and " + words[6] + " on " + words[0]
    print "\n", sentence
inputfile.close()

Output:

sh-4.3$ python main.py The McHungry on Tuesdav Cindy and Paul on Wednesday The Mirzaei on Thursdav

Part 2:

import os

import sys

if not os.path.isfile("/etc/services"):
    print "\nFile services not found."
    sys.exit(1)

ports = [] #To store busy ports
port = int(raw_input("\nEnter port no.: "))
inputfile = open("/etc/services", "r")
for line in inputfile.readlines():
    line = line.rstrip('\n')
    words = line.split(' ')
    for word in words:
        if word.split('/')[0].isdigit(): #if word contains port number.
            ports.append(int(word.split('/')[0])) #insert port no. into ports.
ports.sort() #Sorting ports
for i in range(port, (ports[len(ports) - 1] + 1)): #From port to all other large nos.
    if i not in ports: #if port is not busy.
        print "Next available port: ", i
        break
inputfile.close()


Output:

Enter port no.: 22 Next available port: 26 sh-4.3

Add a comment
Know the answer?
Add Answer to:
Answer the following questions using linux 1) Write a Python script so that a file whoComesForDinner.txt...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 166 Chapter 8: TCP/IP Applications Getting Down to Business The way network communication all those ls...

    166 Chapter 8: TCP/IP Applications Getting Down to Business The way network communication all those ls and Os) goes in and out of a machine physically is through the NIC (network interface card). The way network communication goes in and out of a machine logically though, is through a program or service. A service is a program that runs in the background, independent of a logon, that provides functionalities to a system. Windows client machines, for instance, have a Workstation...

  • USING Unix/Linux shell !!!: 1) Write a script (project2.sh) that will take information from a file,...

    USING Unix/Linux shell !!!: 1) Write a script (project2.sh) that will take information from a file, and print (user’s choice of printers) identical form letters to each recipient. Either E197, E-199, E-194 printers or to project2.output. Prompt the user for their choice. 2) The script and output file are due: per Moodle timeline. Please upload your fully functioning script (project2.sh) your data file (project2.input) and your output file (project2.output) into Moodle. 3) Requirements of the script. a. You can use...

  • computer networks help please !!! could someone help with the following tasks! ANY HELP WILL BE...

    computer networks help please !!! could someone help with the following tasks! ANY HELP WILL BE IMMENSELY APPRECIATED! THANKS. Task 1 In the following scenario, we would like to find out more information about a host that is on out network. Given an IP address we would like to search for additional information, we can start with obtaining the IP address off the default route. When running on a Linux VM, this like most likely to be the internal IP...

  • Python Coding The script, `eparser.py`, should: 1. Open the file specified as the first argument to...

    Python Coding The script, `eparser.py`, should: 1. Open the file specified as the first argument to the script (see below) 2. Read the file one line at a time (i.e., for line in file--each line will be a separate email address) 3. Print (to the screen) a line that "interprets" the email address as described below 4. When finished, display the total number of email addresses and a count unique domain names Each email address will either be in the...

  • Write a C++ Program. You have a following class as a header file (dayType.h) and main()....

    Write a C++ Program. You have a following class as a header file (dayType.h) and main(). #ifndef H_dayType #define H_dayType #include <string> using namespace std; class dayType { public:     static string weekDays[7];     void print() const;     string nextDay() const;     string prevDay() const;     void addDay(int nDays);     void setDay(string d);     string getDay() const;     dayType();     dayType(string d); private:     string weekDay; }; #endif /* // Name: Your Name // ID: Your ID */ #include <iostream>...

  • Read the attached article and answer the following questions. 1. What is the role of potassium...

    Read the attached article and answer the following questions. 1. What is the role of potassium in blood pressure? 2. Looking back at last weeks Super Tracker report - do you meet the 4700mg daily target for potassium? 3. Do you meet the 400mg daily target for magnesium? 4. Research shows that hypertension and type 2 diabetes can be prevented by dietary choices. List 3 ways that employers, government, doctors, etc. (you can use another group ) can either motivate...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT