Python 3: Part B needed
Write a function "closest_pair" to determine the closest pair in Theta(N^2) time, return that pair as a tuple. The closest pair is defined as the pair of numbers whose difference is the minimum of all pairs in the list. For example:
Given the list [ 3,6,4,10,23] the closest pair would be (3,4) since the difference is just 1.
Given the list [10, 45, 55, 42, 20], the closest pair would be (42,45) since the different is just 3
Given the list [4,10,6,7,10,5], the closest pair would be (10,10) since the distance is zero.
B) Write a function "fast_closest_pair" to determine the closest pair (same as in part A), except this time you should do it in better than Theta (N^2).
In: Computer Science
The purpose of this homework is to test your knowledge of File IO and Collections and the language would be java.
Suppose that we have a file Employee.csv which contains employee information of a certain company.
The first line of the file contains the following header:
employeeId,firstName,lastName,dob,gender,position
After the header line, each employee information is written on a separate line which consists of 6 comma-separated fields, for example:1234567, John, Smith,20/12/1989, M, Software Engineer
Question 1. Write code to read the file and return a list of employee objects.
Question 2. Given a list of employee objects, write code to sort the list by first name and print the sorted list on the console.
In: Computer Science
/**
* Loads a list of items from the given file. Each line is in the
format
* <item type>~<item data>. The createFromString()
methods in the item
* classes will be used to parse the item data.
* @param filename the filename
* @return the list of items
* @throws IOException if the file can't be opened
*/
public static List<Item> loadItems(String filename) throws
IOException {
List<Item> items = new ArrayList<>();
Scanner inPut = new Scanner(new FileReader(filename));
while(inPut.hasNextLine()) {
String line =
inPut.nextLine();
String [] tokens
= line.split(":");
String type =
tokens[0];
String data =
tokens[1];
items.add(type,data);
}
return items;
}
}
can you please check this line " items.add(type,data);" . I am having problem with it
In: Computer Science
How can I do this in python ???
In order to create the Exam Result objects correctly, one must find
the student object and the subject object that the exam result
should refer to. The distributed code contains O (n) methods that
perform sequential searches to find them in the lists. Create new
implementations of oving7. find _student and oving7.find_ topic
that is more effective.
oving7. find _student:
def find _student (student no, student list):
for student in student list:
if
student.get_studentnumber () == studentnr:
return
student
return None
oving7.find_ topic:
def find _ topic (topic code, topic list):
for subject in subject list:
if topic.get_
topic code () == topic code:
return
topic
return None
In: Computer Science
In the Python:
Note 1: You may not use these python built-in functions:
sorted(), min(), max(), sum(), pow(), zip(), map(), append(), count() and counter().
(7 points) unique.py: A number in a list is unique if it appears only once. Given a list of
random numbers, print the unique numbers and their count. Print the duplicate numbers and
their count.
Sample runs:
Enter the size of the list : 7
[8, 2, 6, 5, 2, 4, 5]
There are 3 unique numbers: 8 6 4
There are 2 duplicate numbers: 2 5
Sample run:
Enter the size of the list : 7
[2, 2, 4, 3, 3, 3, 4]
There are 0 unique numbers:
There are 3 duplicate numbers: 2 3 4
In: Computer Science
Python Programming
1. Write a function name split_name which takes one parameter called name. If the name parameter value is a name in the form LastName, FirstName(e.g., ”Grounds, Nic”) then the function should return a list of two elements where FirstName is the first element and LastName is the second element. If the name parameter value is a name in the form FirstName LastName(e.g., ”Nic Grounds”) then the function should return a list of two elements where FirstName is the first element and LastName is the second element.
2. Repeatedly prompt the user for a name (which the user may give in Last-name, FirstName form or FirstName LastName form). If the user’s response is an empty string (i.e., they simply press Return) cease looping. If the user’s response is not the empty string, call the split_name function you wrote and append the returned value to a list (initially empty).
3. Using the sorted function (which can take a single parameter, a list) and returns a copy of the list in sorted order) sort the list of names once the loop has finished. Print the sorted list of names, one name per line in LastName, FirstName form.
4. Observe the sorted list of names, are they sorted alphabetically by first name or last name? Leave your answer in a comment within the Python file you submit. Also, how do you think we could potentially modify the program to sort by last name instead of first (or first name instead of last, whichever is opposite of what you observed)? Leave your answer in a comment within the Python file you submit.
5. At the top of your file must be a comment that contains a brief explanation of how you determined which portion of each name was the first name vs. the last name.
In: Computer Science
In: Computer Science
TITLE
Updating Accounts Using Doubly Linked List
TOPICS
Doubly Linked List
DESCRIPTION
General
Write a program that will update bank accounts stored in a master file using updates from a transaction file. The program will maintain accounts using a doubly linked list.
The input data will consist of two text files: a master file and a transaction file. See data in Test section below. The master file will contain only the current account data. For each account, it will contain account id, first name, last name and the current balance. The transaction file will contain updates. Each update will consist of an account id, first name, last name and a plus or minus balance update. A plus update value will indicates a deposit and minus a withdrawal.
Building List
At the beginning, the program will input account data from the master file and build a doubly linked list. Each node in the list will contain data relating to one account and the whole list will be kept sorted by account id.
For building the list, the program will input account information from the master file one account at a time. It will create a node containing the account data and add it to the list at the appropriate position in the list so that the list will remain sorted in ascending order by account id.
Updating List
After the list is built as described above, the program will read updates from the Update file and update the list accordingly. It will input one account update at a time and update the list using this update before inputting the next update.
For moving from account to account during updates, it will use a global cursor. For each update, it will determine whether the target account is in the forward or backward direction from the current position. It will then move the cursor in the appropriate direction using either the forward or backward links of the doubly linked list. It will move the cursor till it reaches the target account or an account past the target account (in the case that the target account does not exist).
Depending upon the account id provided in the update, the program will do one of the following:
· If the account specified in the update is not found in the list, it will create a new account, initialize its fields with the values in the update and insert it in the list at appropriate position so that the list will remain sorted in ascending order by account id.
· If the account specified in the update is found in the list, it will update its current balance according to the plus or minus value specified in the update (i.e. it will add a plus value and subtract a minus value).
· On completing the update, if an account balance becomes 0 or negative, the program will delete that account.
Logging Update
Before starting any updates, the program will log the following to the log file:
· It will log the contents of the whole doubly linked list.
Then for each update, it will log the following information to the log file:
· Before the update, it will log a line containing the contents of the update to be performed.
· After the update, it will log the contents of the whole doubly linked list.
For logging the contents of an update, it will output one line of text containing account id, account name and the update value.
For logging the contents of the whole doubly linked list, it will output one line for each account containing account id, account name and account balance.
For the contents of a sample log file, see the Sample Log File section. It contains the partial contents of a sample log file.
New Master File
When all updates are completed, the program will save the contents of the updated linked list to a new master file. (Do not save the link values).
IMPLEMENTATION
Implement the account list using a Circular Doubly Linked List with a Dummy Node.
Account Id
Use a long for account id.
Circular Doubly Linked List With A Dummy Node
Implement the list for keeping the account as a Circular Doubly Linked List with a Dummy Node.
In a Circular Dummy Node implementation of a doubly linked list, the head points to a dummy node. The dummy node looks like any other node but its data fields do not contain any useful values. It’s forward link points to the first node in the list and back link points to the last node in the list. Initially, in an empty list, both its forward and back links points to itself (i.e. to the dummy node).
For doing a sorted insertion in such a list, do the following. Point the insertion pointer to the first real node. Move the insertion pointer forward as needed, till you reach the Point Of Insertion Node. (The Point Of Insertion Node is that node before which you intend to insert the new node) After the insertion pointer is at the Point of Insertion Node, insert the new node just before it. In performing the insertion, only the links in the Point Of Insertion node and the links in the node that is one before the Point Of Insertion node will change. Links in other nodes will not change.
Because of the presence of a dummy node and the circular nature of the list, in all insert cases, there will always exist a node (either a real or dummy node ) before the Point of Insertion node. It will be the links of this node and that of the Point of Insertion node that will change. Therefore, the code for inserting a new node will be the same in all cases including: empty list, head insertion, mid insertion and end insertion. For each of these cases, the point of insertion node and the node prior to the Point of Insertion Node are listed below:
· For an empty list, both point of insertion node and the node prior to the point Of insertion node are the same namely the dummy node.
· For a head insertion, the point of insertion node is the first node and the node prior to the point of insertion node is the dummy node.
· For a mid insertion, the point of insertion node is a real node before which the new node is being inserted and the node prior to the point of insertion node is another real node after which the new node is being inserted.
· For end insertion, the point of insertion node is the dummy node and the node prior to the point of insertion node is a real node after which the new node is being inserted.
In all of the above case, both the point of insertion node and the node prior to the point of insertion node are identical in format and have the same format as all other nodes. Therefore, the code for inserting a node in all of the above cases is the same.
Doubly Linked List Class
Create a class to encapsulate circular doubly linked list with a dummy node and to provide the following.
Global Cursor
Provide a global cursor (pointer) that points to a working account. This cursor will be used during updates.
Building Initial List
Provide a method for building a linked list while reading from the master file. The algorithm for this method is provided later below.
Updating List
Provide a method for performing updates while reading from the transaction file. For doing an update, move the global cursor forwards or backwards as needed from account to account till you either reach the target node or a node past that node. Then carry out the required update (update, insert or delete). The algorithm for this method is provided later below
TESTING
Test Run 1
Use the following file contents for a test run.
Master File Contents (master.txt)
27183 Teresa Wong 1234.56
12345 Jeff Lee 211.22
31456 Jack Smith 1200.00
14142 James Bond 1500.00
31623 Norris Hunt 1500.00
10203 Mindy Ho 2000.00
20103 Ed Sullivan 3000.00
30102 Ray Baldwin 3824.36
30201 Susan Woo 9646.75
22345 Norma Patel 2496.24
Transaction File Contents (tran.txt)
31623 Norris Hunt -1500.00
20301 Joe Hammil +500.00
31416 Becky Wu +100.00
10203 Mindy Ho -2000.00
14142 James Bond +1500.00
27183 Teresa Wong -1234.56
10101 Judy Malik +1000.00
31416 Becky Wu +100.00
32123 John Doe +900.00
10101 Judy Malik -200.00
22222 Joanne Doe +2750.02
SAMPLE LOG FILE
For a sample, the contents of the log file at the end of completing the first and the second update are presented below:
List At Start:
10203 Mindy Ho 2000
12345 Jeff Lee 211.22
14142 James Bond 1500
20103 Ed Sullivan 3000
22345 Norma Patel 2496.24
27183 Teresa Wong 1234.56
30102 Ray Baldwin 3824.36
30201 Susan Woo 9646.75
31456 Jack Smith 1200
31623 Norris Hunt 1500
Update #1
31623 Norris Hunt -1500
List After Update #1:
10203 Mindy Ho 2000
12345 Jeff Lee 211.22
14142 James Bond 1500
20103 Ed Sullivan 3000
22345 Norma Patel 2496.24
27183 Teresa Wong 1234.56
30102 Ray Baldwin 3824.36
30201 Susan Woo 9646.75
31456 Jack Smith 1200
Update #2
20301 Joe Hammil +500.00
List After Update #2:
10203 Mindy Ho 2000
12345 Jeff Lee 211.22
14142 James Bond 1500
20103 Ed Sullivan 3000
20301 Joe Hammil +500.00
22345 Norma Patel 2496.24
27183 Teresa Wong 1234.56
30102 Ray Baldwin 3824.36
30201 Susan Woo 9646.75
31456 Jack Smith 1200
SUBMIT
Submit the following:
· Source code
· Contents of file log.txt
· Contents of the updated new master file.
Below is the hint
#include <iostream>
#include <string>
using namespace std;
struct NodeType;
typedef NodeType * NodePtr;
struct RecType
{
long id;
string fname;
string lname;
double amount;
};
struct NodeType
{
long id;
string fname;
string lname;
double amount;
NodePtr flink;
NodePtr blink;
};
class AccountList
{
private:
NodePtr head;
NodePtr cursor;
public:
AccountList ( );
void addAccountSorted (RecType rec);
void updateAccount (RecType rec);
void display(ofstream &lfstream);
};
AccountList::AccountList ( )
{
head = new NodeType;
head->id = -1;
head->fname = "";
head->lname = "";
head->amount= -999.999;
head->flink=head;
head->blink=head;
cursor = head;
}
void AccountList::addAccountSorted(RecType rec)
{
//Creat the new node and fill it in.
NodePtr newPtr = new NodeType;
newPtr->id = rec.id;
newPtr->fname = rec.fname;
newPtr->lname = rec.lname;
newPtr->amount = rec.amount;
newPtr->flink = NULL;
newPtr->blink = NULL;
//find the Node of point of insertion
NodePtr cur, prev;
for (cur=head->flink; cur!=head; cur=cur->flink)
{
if (rec.id < cur->id)
break;
}
//set prev
prev = cur->blink;
//update the two forward links
newPtr->flink=prev->flink;
prev->flink = newPtr;
//update the two backward links
newPtr->blink = cur->blink;
cur->blink = newPtr;
}
void AccountList::updateAccount(RecType rec)
{
//move the cursor forward if at dummy node
if (cursor == head)
cursor = cursor->flink;
//cursor is at the target node. do not move it
if (cursor->id == rec.id)
{
//update the account
//if the account became zero or negative
//delete the node and move the cursor forward
}
else if (cursor->id < rec.id)
{
while (cursor != head)
{
if (cursor->id >= rec.id)
break;
cursor = cursor->flink;
}
if (cursor->id == rec.id)
{
//update the account
//if the account became zero or negative
//delete the node and move the cursor forward.
}
else
{
//insert the node prior to where cursor is.
}
}
else
{
while (cursor != head )
{
if (cursor->id <= rec.id)
break;
cursor = cursor->blink;
}
if (cursor->id == rec.id)
{
//update the account
//if the account became zero or negative
//delete the node and move the cursor forward.
}
else
{
//first move the cursor forward by one
//This will make it point to the point of insertion node.
//Then insert the node prior to where cursor is.
}
}
}
//This method receives an ofstream opened for the log file
//as a reference parameter and uses it to write the contents
//of the doubly linked list to the log file.
//This method can be used while performing updates.
void AccountList::display(ofstream & lfout)
{
for(NodePtr cur = head->flink; cur!=head; cur=cur->flink)
lfout << cur->id << " " << cur->fname << " " << " " << cur->lname << " "
<< cur->amount << endl;
}
void main ( )
{
ofstream lfout ("C:\\log.txt");
}In: Computer Science
How would I make a bubble sort and an optimized bubble sort with the code given? I also need to implement a timer into each sort and display runtime with the sorts.
NODE.H
_______________________________________________________________________________________________________
/* node.h */
/*
two classes 1: node.h 2. singlylinkedlist.h
nod1 (value + pointer) ---> node2 ---> node3 ---> ||||
<--- node.h
^
| singlylinkedlist
----------------*node head;
*/
#ifndef NODE_H
#define NODE_H
#include <iostream>
using namespace std;
class Node {
friend class singlyLinkedList;
public:
Node();
Node (int value);
~Node();
int displayValue();
private:
int value;
Node* next;
};
#endif
SINGLYLINKEDLIST.H
_______________________________________________________________________________________________________
/* singlylinkedlist.h */
#ifndef SINGLY_LINKED_LIST_H
#define SINGLY_LINKED_LIST_H
#include <iostream>
using namespace std;
#include "node.h"
class singlyLinkedList
{
public:
singlyLinkedList();
~singlyLinkedList();
void addFront(Node* newNode);
void displaySinglyLinkedList();
void bubbleSort();
void optimizedBubbleSort();
private:
Node *head;
};
#endif
NODE.CPP
_______________________________________________________________________________________________________
/* node.cpp */
#include <iostream>
using namespace std;
#include "node.h"
Node::Node()
{
value = 0;
next = NULL;
}
Node::Node(int v)
{
value = v;
next = NULL;
}
Node::~Node()
{
}
int Node::displayValue()
{
return value;
}
SINGLYLINKEDLIST.CPP
_______________________________________________________________________________________________________
/* singlylinkedlist */
#include <iostream>
using namespace std;
#include "singlylinkedlist.h"
singlyLinkedList::singlyLinkedList()
{
head = NULL;
}
singlyLinkedList::~singlyLinkedList()
{
}
void singlyLinkedList::addFront(Node *newNode)
{
if (head == NULL)
head = newNode;
else
{
// Add code here!
/* node1-->||| (before) what is
after node2 --> node1 -->||| */
newNode->next = head;
head = newNode;
};
}
void singlyLinkedList::displaySinglyLinkedList()
{
Node *tempHead;
tempHead = head;
while (tempHead != NULL)
{
cout << tempHead->value
<< " ";
tempHead = tempHead->next;
}
cout << endl;
}
void singlyLinkedList:: bubbleSort()
{
// Your Code Starts Here!
// Please display the runtime for this task before
exit
// start clock
// ....
// stop the clock
// display the (stop-clock - start-clock
}
void singlyLinkedList:: optimizedBubbleSort()
{
// Your Code Starts Here!
// Please display the runtime for this task before
exit
// start clock
// ....
// stop the clock
// display the (stop-clock - start-clock
}
MAIN.CPP
_______________________________________________________________________________________________________
// In Project 1, your are required to implement the code for
bubble sort and optimized bubble sort
// using the data structure of sinngly linked list (nothing
else).
/* Bubble Sort:
for (int i = 0; i < n; i++)
for (int j = i; j < n; j++)
{
if (A[i] > A[j])
{
temp = A[i];
A[i] = A[j];
A[j] = temp;
}
}
}
*/
/* Optimized Bubble Sort:
flag = 0;
for (int i = 0; i < n; i++)
for (int j = 0; j < n-1; j++)
{
if (A[j] > A[j+1])
{
temp = A[j];
A[j] = A[j+1];
A[j+1] = temp;
flag = 1; // if swapping occurs
}
}
if (flag == 0)
break; // the input list already sorted, no need for the inner
loop
}
*/
// The main program will create 10 nodes for storing a sequence
of 10 integers
// bubble Sort() will be invoked to sort these 10 integers
// then optimizedBubbleSort will be invoked to sort the sorted
sequence (degenerate case) again
// You will find out optimizedBubbleSort will take O(1) running
time to do the sorting
// unlike bubbleSort taking O(n^2) run time to sort a sorted
sequence.
// Lastly, in order to see the differences of bubbleSort and
optimizedBubbleSort,
// also implement timer into both code, after the sorting, print
out the runtime
// of sorting the sequence. For bubbleSort, no matter sorted or
unsorted, it always
// take O(n^2) runtime. But for optimizedBubbleSort, for unsorted
sequence, like
// bubbleSort, it takes O(n^2) runtime. However, for a sorted
sequence,
// optimizedBubbleSort will take O(1) only. For a sorted sequence
as a
// degenerate case, optimizedBubbleSort is more efficienct than
bubbleSort
// for sorting a sequence of integers.
// You MUST use this provided code for Project 1
#include <iostream>
using namespace std;
#include "node.h"
#include "singlylinkedlist.h"
int main()
{
/* create 10 nodes */
Node *n1 = new Node(23); // objects creation (class
instantiation)
Node *n2 = new Node(-20);
Node *n3 = new Node(7);
Node *n4 = new Node(174);
Node *n5 = new Node(56);
Node *n6 = new Node(-98);
Node *n7 = new Node(101);
Node *n8 = new Node(46);
Node *n9 = new Node(31);
Node *n10 = new Node(5);
/* create singly linked list */
singlyLinkedList *sLL;
sLL = new singlyLinkedList();
/* insert 10 nodes into the singly linked list in
First in, Last out manner */
sLL->addFront(n1);
sLL->addFront(n2);
sLL->addFront(n3);
sLL->addFront(n4);
sLL->addFront(n5);
sLL->addFront(n6);
sLL->addFront(n7);
sLL->addFront(n8);
sLL->addFront(n9);
sLL->addFront(n10);
/* display the contents of the 10 nodes in the
singly linked list */
sLL->displaySinglyLinkedList();
/* The following code will act it is supposed to
do
when you are successfully implment bubbleSort
and
optimizedBubbleSort. */
sLL->bubbleSort(); // bubble sort the unsorted
sequence
sLL->displaySinglyLinkedList(); // a sorted
sequence displayed
sLL->optimizedBubbleSort(); // optimized bubble
sort the already sorted sequence (a degenerate case)
// the runtime should show O(1), it means time should
be musch less than runtime
// of bubbleSort()
sLL->displaySinglyLinkedList(); // a sorted
sequence displayed
system("PAUSE");
return 0;
}
In: Computer Science
The FBI wants to determine the effectiveness of their 10 Most Wanted list. To do so, they need to find out the fraction of people who appear on the list that are actually caught. Step 2 of 2 : Suppose a sample of 871 suspected criminals is drawn. Of these people, 628 were not captured. Using the data, construct the 99% confidence interval for the population proportion of people who are captured after appearing on the 10 Most Wanted list. Round your answers to three decimal places.
In: Statistics and Probability