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...

  • using lubuntu ce the following activities on your Linux virtual machine. Then complete estions. Write a script file which when executed will perform the following: First display today's date in d...

    using lubuntu ce the following activities on your Linux virtual machine. Then complete estions. Write a script file which when executed will perform the following: First display today's date in dd.mm.YYYY format (e.g. 20.04.2019). Ask user to enter a name to create a directory. Create a directory by the given directory name above. . . Ask user to enter a name to create a file. Copy the file /etc/passwd to the given filename above inside the newly created directory Copy...

  • 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>...

  • Please write a BASH Script in LINUX that... 1. is actually executable 2. has a comment...

    Please write a BASH Script in LINUX that... 1. is actually executable 2. has a comment to tell us what you did, why and how. 3. allows a user to enter their name and a number between 1 than 100 (this must be prompted so the user knows what to do) 4. creates a random number between 1 and 100 for you to guess. The command to create a random number is shown below. (if you find a better one...use...

  • Part 1: Using Idle Write a Python Script that Does the Following 1. At the top...

    Part 1: Using Idle Write a Python Script that Does the Following 1. At the top of your program, import the math library as in from math import * to make the functions in the math module available. Create a variable and assign into it a constant positive integer number of your choice. The number should be at most 10. 1 Suppose we call this variable x for this writeup document Try something like x = 4 2. Create another...

  • Question III This question carries 20% of the marks for this assignment. Given the following mix...

    Question III This question carries 20% of the marks for this assignment. Given the following mix of tasks, task lengths and arrival times, compute the completion [5 marks and response time time from the arrival to the finish time) (5 marks for each task, along with the average response time for the FIFO. RR and SJF algorithms. Assume a time slice of 10 milliseconds and that all times are in milliseconds. You are kindly asked to provide the Gantt Chart...

  • You need not run Python programs on a computer in solving the following problems. Place your...

    You need not run Python programs on a computer in solving the following problems. Place your answers into separate "text" files using the names indicated on each problem. Please create your text files using the same text editor that you use for your .py files. Answer submitted in another file format such as .doc, .pages, .rtf, or.pdf will lose least one point per problem! [1] 3 points Use file math.txt What is the precise output from the following code? bar...

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
ADVERTISEMENT