1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Jlenok [28]
2 years ago
10

# q7 - create function readFileFirstLast() to meet the conditions below

Computers and Technology
1 answer:
MAVERICK [17]2 years ago
3 0

Answer:

See explaination

Explanation:

def readFileFirstLast(filename):

# doc string

''' Function accept the filename and opens the fle

and reads all lines and strips new line character and

stores first and last in a string and return that string'''

#eception handle if file not found

try:

#opening the file

f = open(filename)

#reading the first line and striping the ne line

string = f.readline().strip()

#iterating until last line

for line in f:

pass

#concate the last line after strip the new line character to the string

string = string + " " + line.strip()

#return the string

return string

except:

#if file not found

return "File not found"

#taking the file name from user

filename = input("Enter a file name: ")

#printing the doc string in function

print("\ndoc_sting: \n"+ readFileFirstLast.__doc__+"\n")

#printing the returned string by calling the readFileFirstLast()

print("output string :")

print(readFileFirstLast(filename))

You might be interested in
Write a program that reads numbers from a file (or allow user to input data) and creates an ordered binary tree. The program sho
IgorLugansk [536]

Answer:

See explaination

Explanation:

include<bits/stdc++.h>

using namespace std;

typedef struct Node

{

int data;

struct Node *left,*right;

}Node;

bool search(Node *root,int data)

{

if(root==NULL)

return false;

if(root->data==data)

return true;

queue<Node*> q;

q.push(root);

while(!q.empty())

{

Node *temp=q.front();

q.pop();

if(temp->data==data)

return true;

if(temp->left)

q.push(temp->left);

if(temp->right)

q.push(temp->right);

}

return false;

}

Node *insert(Node *root,int data)

{

if(root==NULL)

{

Node *temp=new Node();

temp->data=data;

temp->left=NULL;

temp->right=NULL;

return temp;

}

if(data < root->data)

root->left=insert(root->left,data);

if(data>root->data)

root->right=insert(root->right,data);

return root;

}

Node *get_smallest_element_right_subtree(Node *root)

{

while(root && root->left!=NULL)

root=root->left;

return root;

}

Node *delete_node(Node *root,int data)

{

if(root==NULL)

return root;

if(data < root->data)

root->left=delete_node(root->left,data);

else if(data > root->data)

root->right=delete_node(root->right,data);

else

{

if(root->left==NULL) //If right only presents means - delete the curr node and return right node

{

Node *temp=root->right;

free(root);

return temp;

}

else if(root->right==NULL) //If left only presents means - delete the curr node and return let node

{

Node *temp=root->left;

free(root);

return temp;

}

else

{

Node *temp=get_smallest_element_right_subtree(root->right);

root->data=temp->data;

root->right=delete_node(root->right,temp->data);

}

return root;

}

}

void inorder(Node *root)

{

if(root!=NULL)

{

inorder(root->left);

cout<<root->data<<" ";

inorder(root->right);

}

}

void postorder(Node *root)

{

if(root!=NULL)

{

inorder(root->left);

inorder(root->right);

cout<<root->data<<" ";

}

}

void preorder(Node *root)

{

if(root!=NULL)

{

cout<<root->data<<" ";

inorder(root->left);

inorder(root->right);

}

}

int main()

{

fstream f;

string filename;

cout<<"\n\n1 - Input through File ";

cout<<"\n\n2 - Input through your Hand";

int h;

cout<<"\n\n\nEnter Your Choice : ";

cin>>h;

Node *root=NULL; // Tree Declaration

if(h==1)

{

cout<<"\n\nEnter the Input File Name : ";

cin>>filename;

f.open(filename.c_str());

if(!f)

cout<<"\n\nError in Opening a file !";

else

{

cout<<"\n\nFile is Being Read ........";

string num;

int value;

int node=0;

while(f>> num)

{

value=stoi(num);

root=insert(root,value);

node++;

}

cout<<"\n\nTree has been successfully created with : "<<node<<" Nodes"<<endl;

}

}

if(h==2)

{

int y;

cout<<"\n\nEnter the Total No of Input :";

cin>>y;

int i=1,g;

while(i!=y+1)

{

cout<<"\n\nEnter Input "<<i<<" : ";

cin>>g;

root=insert(root,g);

i++;

}

cout<<"\n\nTree has been successfully created with : "<<y<<" Nodes"<<endl;

}

if(h>=3)

{

cout<<"\n\nInvalid Choice !!! ";

return 0;

}

int n=0;

while(n!=6)

{

cout<<"\n\n\n1 - Insert Element";

cout<<"\n\n2 - Remove Element";

cout<<"\n\n3 - Inorder (LNR) Display ";

cout<<"\n\n4 - Pre (NLR) Order Display";

cout<<"\n\n5 - Post (LRN) Order Display";

cout<<"\n\n6 - Quit";

cout<<"\n\nEnter Your Choice : ";

cin>>n;

switch(n)

{

case 1:

{

int k;

cout<<"\n\nEnter Element to insert : ";cin>>k;

root=insert(root,k);

cout<<"\n\nElement Sucessfully Inserted !!!!!";

break;

}

case 2:

{

int k;

cout<<"\n\nEnter Element to Remove : ";

cin>>k;

if(search(root,k))

{

root=delete_node(root,k);

cout<<"\n\nValue Successfully Deleted !!!";

}

else

cout<<"\n\n!!!!!!!!!!!!!!!!!!!! No Such Element !!!!!!!!!!!!!!!!!!!!!!";

break;

}

case 3:

{

cout<<"\n\nThe Elements (LNR) are : ";

inorder(root);

break;

}

case 4:

{

cout<<"\n\nThe Elements (NLR) are : ";

preorder(root);

break;

}

case 5:

{

cout<<"\n\nThe Elements (LRN) are : ";

postorder(root);

break;

}

case 6:

{

break;

}

}

}

cout<<"\n\nBye!!!! See You !!!"<<endl;

7 0
3 years ago
Create and test a user-defined function named ufnFullName that combines the values of the two parameters named FirstName and Las
8090 [49]

Answer:

   public static String ufnFullName(String Fname, String Lname){

       String FullName = Fname+" "+Lname;

       return  FullName;

   }

Explanation:

Using Java programming language, the function is created to accept two String parameters (Fname and Lname)

These are concatenated into a new String FullName and returned by the function

See below a complete code that prompts users to enter value for first and last names then calls this method to display the full name

<em>import java.util.Scanner;</em>

<em>public class FullName {</em>

<em>    public static void main(String[] args) {</em>

<em>        System.out.println("Enter First Name");</em>

<em>        Scanner in = new Scanner(System.in);</em>

<em>        String Fname = in.next();</em>

<em>        System.out.println("Enter Last Name");</em>

<em>        String Lname = in.next();</em>

<em />

<em>        System.out.println("Your Full Name is "+ufnFullName(Fname,Lname));</em>

<em>    }</em>

<em>    public static String ufnFullName(String Fname, String Lname){</em>

<em>        String FullName = Fname+" "+Lname;</em>

<em>        return  FullName;</em>

<em>    }</em>

<em>}</em>

8 0
2 years ago
Employees don't have to comply with PPE requirements if they feel the PPE isn't necessary for the particular task they performed
ANTONII [103]
You know what...ITS IS VERY VERY FREAKIN FALSE!!
7 0
2 years ago
Read 2 more answers
What is included on the HP PC Hardware Diagnostics 3-in-1 USB Key that allows you to recover systems that cannot boot?
algol13

Answer:

HP PC Hardware Diagnostics UEFI

Explanation:

The 3-in-1 HP PC Hardware Diagnostics UEFI USB Key helps the client distinguish potential software, hardware, and Operating System issues. Both older and newer HP Desktop and Laptop PCs come with this tool installed. By default, it operates outside the OS to detach equipment issues from other system issues. In the event you do not have it pre-installed, you can get it from the official HP site.

3 0
3 years ago
How do i build a supercomputer.?
stira [4]
You will need one head node, at least a dozen identical compute nodes, an Ethernet switch, a power distribution unit, and a rack. Determine the electrical demand, cooling and space required. Also decide on what IP address you want for your private networks, what to name the nodes, what software packages you want installed, and what technology you want to provide the parallel computing capabilities
3 0
3 years ago
Other questions:
  • Semiconductor memory is used mainly for primary storage even with its high cost. In another hand, the magnetic tape is the cheap
    6·1 answer
  • How does consumption of alcohol affect your driving skills? Name three ways that alcohol can affect your driving.
    15·2 answers
  • How many packets does your computer send/receive in a single mouse click when you visit a website?
    6·1 answer
  • Working together, printer A and printer B would finish the task in 24 minutes. Printer A alone would finish the task in 60 minut
    8·1 answer
  • Limitations of the information systems used by tesco​
    7·1 answer
  • Your organization is planning to deploy wireless access points across their campus network, and you have been tasked with securi
    5·1 answer
  • Write the definition of a class WeatherForecast that provides the following behavior (methods): A method called set_skies that h
    13·1 answer
  • If Anyone can help me out that'll be great
    11·1 answer
  • What power points feature will you use to apply motion effects to different objects of a slide
    5·1 answer
  • How can I master networking my home/business computer(s) - Tv's - iot devices and make the whole system as secure as possible?
    12·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!