how to use python to read from stdin and print stderr in bash
In: Computer Science
Regarding the lecture on HIT Overview, how do you distinguish between Operation System software, Utility software and Application software?
In: Computer Science
The object is to make this client code work:
'''
>>> s = Stack()
>>> s.push('apple')
>>> s
Stack(['apple'])
>>> s.push('pear')
>>> s.push('kiwi')
>>> s
Stack(['apple', 'pear', 'kiwi'])
>>> top = s.pop()
>>> top
'kiwi'
>>> s
Stack(['apple', 'pear'])
>>> len(s)
2
>>> s.isEmpty()
False
>>> s.pop()
'pear'
>>> s.pop()
'apple'
>>> s.isEmpty()
True
>>> s = Stack(['apple', 'pear', 'kiwi'])
>>> s = Stack(['apple', 'pear', 'kiwi'])
>>> s[0]
'apple'
>>>
'''
use python3.7
>>> s = Stack()
>>> s.push('apple')
>>> s
Stack(['apple'])
>>> s.push('pear')
>>> s.push('kiwi')
>>> s
Stack(['apple', 'pear', 'kiwi'])
>>> top = s.pop()
>>> top
'kiwi'
>>> s
Stack(['apple', 'pear'])
>>> len(s)
2
>>> s.isEmpty()
False
>>> s.pop()
'pear'
>>> s.pop()
'apple'
>>> s.isEmpty()
True
>>> s = Stack(['apple', 'pear', 'kiwi'])
>>> s = Stack(['apple', 'pear', 'kiwi'])
>>> s[0]
'apple'
>>>
check that Stacks constructed without a list remain
distinct
if you receive an error on s2 then you probably need to use a
default argument of None in __init__
(see the Queue class developed in class for an example)
>>> s = Stack()
>>> s.push('apple')
>>> s
Stack(['apple'])
>>> s2 = Stack()
>>> s2.push('pear')
>>> s2 # if this fails - see the TEST file for
explanation
Stack(['pear'])
In: Computer Science
Use while loop for the num inputs
#include
#include
using namespace std;
int main()
{
long long int digit;
long long int num1, num2, num3, num4, num5;
int ave;
cout << "Enter a 10 digit number: ";
cin >> digit;
num5 = digit %100;
digit = digit / 100;
num4 = digit %100;
digit = digit / 100;
num3 = digit %100;
digit = digit / 100;
num2 = digit %100;
digit = digit / 100;
num1 = digit %100;
digit = digit / 100;
cout << num1 << endl;
cout << num2 << endl;
cout << num3 << endl;
cout << num4 << endl;
cout << num5 << endl;
ave = (double)(num1 + num2 + num3 + num4 + num5) /
5;
cout << "Avg: " << ave <<
endl;
switch(ave/10){
case 9:
cout <<
"Grade: A";
break;
case 8:
cout <<
"Grade: B";
break;
case 7:
cout <<
"Grade: C";
break;
case 6:
cout <<
"Grade: D";
break;
default:
cout <<
"Grade: F";
break;
}
return 0;
}
In: Computer Science
One common use of this class is to parse comma-separated integers from a string (e.g., "23,4,56").
stringstream ss("23,4,56");
char ch;
int a, b, c;
ss >> a >> ch >> b >> ch >> c; // a = 23, b = 4, c = 56
You have to complete the function vector parseInts(string str). str will be a string consisting of comma-separated integers, and you have to return a vector of int representing the integers.
Note If you want to know how to push elements in a vector, solve the first problem in the STL chapter.
Input Format
The first and only line consists of n integers separated by commas.
Output Format
Print the integers after parsing it.
P.S.: I/O will be automatically handled. You need to
complete the function only.
Sample Input
23,4,56
Sample Output
23 4 56
How do I input a string to then convert the string into a integer vector.
In: Computer Science
Python program to simulate breaking a unit-length pencil into two places. Repeat the experiment 100,000 times and determine the average size of the smallest, middle-size, and the largest pieces. Record the estimate of the average sizes in your program
Please use comments to better explain what is happening in the code
In: Computer Science
Read a text file into arrays and output - Java Program
------------------------------------------------------------------------------
I need to have a java program read an "input.txt" file (like below) and store the information in an respective arrays. This input.txt file will look like below and will be stored in the same directory as the java file. The top line of the text represents the number of people in the group for example. the lines below it each represent the respective persons preferences in regards to the other people in the group. I would then like the java program to output as a string each array and its elements.
Example of "input.txt" that would be read
4
2 3 4
1 3 4
1 2 4
1 2 3
As you can see the top line says there are 4 people in this group and person #1 which is line #2 prefers to be in a group with persons 2, 3, and 4 respectively. So for Person #1 it would be an Array with 3 elements being 2, 3, and 4 in that order.
The java program in this case would read the .txt file. create 4 arrays based on line #1 saying there are 4 people and then it would output as a string the 4 arrays and their respective elements in order,
Example Output:
Person #1 prefers 2, then 3, then 4
Person #2 prefers 1, then 3, then 4
.
.
.
The java program should be able to work with text files that have different numbers of people, it should create # of arrays based on the number on line 1.
In: Computer Science
In java, Write a program that reads a data file containing final exam scores for a class and determines the number of passing scores (those greater than 60) and the number of failing scores (those less than 60). The average score as well as the range of scores should also be determined.
The program should request the name of the data file from the end user. The file may contain any number of scores. Using a loop, read and process each score until the end of file is encountered. Once all data has been processed, display the statistics.
The input file should consist of integer values between 1 and 100. We will assume that the data file contains only valid values. The data file can be created using jGrasp by using File | New | Plain Text. The file should be stored in the same folder as the .java file.
Hint: The smallest and largest values can be determined as the data is read if the variables are initialized to opposite values. For example, the variable holding the largest score is initialized to 0, and then reset as needed after each data item is read.
The Scanner class should be used to read the file. End user input/output may be accomplished using either a) Scanner & print methods, OR (b) JOptionPane methods. All end user input and output must use the same I/O technique.
The throws clause should be added to the main method header:
public static void main(String[] args) throws IOException
Example run 1:
Enter the file name to be read: scores1.dat
15 scores were read:
14 passing scores
1 failing scores
The score ranges from 50 to 100
The average is 79.80
Example run 2:
Enter the file name to be read: scores2.dat
20 scores were read:
17 passing scores
3 failing scores
The scores ranges from 52 to 99
In: Computer Science
In: Computer Science
Program coding for C#
(Visual Studio)
to move block up and down left to right.
Simple harmonic motion
In: Computer Science
You have just purchased a stereo system that cost $1,000 on the following credit plan: no down payment, an interest rate of 18% per year (and hence 1.5% per month), and monthly payments of $50. The monthly payment of $50 is used to pay the interest, and whatever is left is used to pay part of the remaining debt. Hence, the first month you pay 1.5% of $1,000 in interest. That is $15 in interest. So, the remaining $35 is deducted from your debt, which leaves you with a debt of $965.00. The next month, you pay interest of 1.5% of $965.00, which is $14.48. Hence, you can deduct $35.52 (which is $50 – $14.48) from the amount you owe. Write a program that tells you how many months it will take you to pay off the loan, as well as the total amount of interest paid over the life of the loan. Use a loop to calculate the amount of interest and the size of the debt after each month. (Your final program need not output the monthly amount of interest paid and remaining debt, but you may want to write a preliminary version of the program that does output these values.) Use a variable to count the number of loop iterations and hence, the number of months until the debt is zero. You may want to use other variables as well. The last payment may be less than $50 if the debt is small, but do not forget the interest. If you owe $50, your monthly payment of $50 will not pay off your debt, although it will come close. One month’s interest on $50 is only 75 cents.
In: Computer Science
In: Computer Science
Please explain answer.
data in address 0x00000004: 0x15
Get 4 bytes of data from address 0x00000004, assume Little-Endian byte ordering in memory
In: Computer Science
Java - Text File to Arrays and output
------------------------------------------------------------------------------
I need to have a java program read an "input.txt" file (like below) and store the information in an respective arrays. This input.txt file will look like below and will be stored in the same directory as the java file. The top line of the text represents the number of people in the group for example. the lines below it each represent the respective persons preferences in regards to the other people in the group. I would then like the java program to output as a string each array and its elements.
Example of "input.txt" that would be read
4
2 3 4
1 3 4
1 2 4
1 2 3
As you can see the top line says there are 4 people in this group and person #1 which is line #2 prefers to be in a group with persons 2, 3, and 4 respectively. So for Person #1 it would be an Array with 3 elements being 2, 3, and 4 in that order.
The java program in this case would read the .txt file. create 4 arrays based on line #1 saying there are 4 people and then it would output as a string the 4 arrays and their respective elements in order,
Example Output:
Person #1 prefers 2, then 3, then 4
Person #2 prefers 1, then 3, then 4
.
.
.
The java program should be able to work with text files that have different numbers of people, it should create # of arrays based on the number on line 1.
In: Computer Science
complete java binary search tree
/*
* Complete the printInLevelOrder() method
* Complete the visuallyIdentical(A4BST rhs) method
* No other methods/variables should be added/modified
*/
public class A4BST<E extends Comparable<? super E>>
{
/*
* Grading:
* Correctly prints values in level order - 1.5pt
* Runs in O(N) - 1.5pt
*/
public String printInLevelOrder()
{
String content = "";
/*
* Add items from the tree to
content one level at a time
* No line breaks are required
between levels
* Ensure method runs in O(N) - does
not revisit any node
*/
return content;
}
/*
* Grading:
* Correctly compares the structure of both trees -
3pts
*/
public boolean visuallyIdentical(A4BST rhs)
{
/*
* Check if the structure of the
local tree and the rhs tree are identical
* This means they have the same
left/right connections
* This means there are no extra
connections in either tree
* The values at each position do
not need to match, only the structure of the tree
* Think about if you drew both
trees on paper, would they visually look the same (besides
values)
*/
return false;
}
private Node root;
public A4BST()
{
root = null;
}
public String printTree()
{
return printTree(root);
}
private String printTree(Node current)
{
String content = "";
if(current != null)
{
content +=
"Current:"+current.data.toString();
if(current.left
!= null)
{
content += "; Left
side:"+current.left.data.toString();
}
if(current.right
!= null)
{
content += "; Right
side:"+current.right.data.toString();
}
content+="\n";
content+=printTree(current.left);
content+=printTree(current.right);
}
return content;
}
public String printInOrder()
{
return printInOrder(root);
}
private String printInOrder(Node current)
{
String content = "";
if(current != null)
{
content +=
printInOrder(current.left);
content +=
current.data.toString()+",";
content +=
printInOrder(current.right);
}
return content;
}
public boolean contains(E val)
{
Node result = findNode(val,
root);
if(result != null)
return
true;
else
return
false;
}
private Node findNode(E val, Node current)
{
//base cases
if(current == null)
return
null;
if(current.data.equals(val))
return
current;
//recursive cases
int result =
current.data.compareTo(val);
if(result < 0)
return
findNode(val, current.right);
else
return
findNode(val, current.left);
}
public E findMin()
{
Node result = findMin(root);
if(result == null)
return
null;
else
return
result.data;
}
private Node findMin(Node current)//used in findMin
and delete
{
while(current.left != null)
{
current =
current.left;
}
return current;
}
public E findMax()
{
Node current = root;
while(current.right != null)
{
current =
current.right;
}
return current.data;
}
public void insert(E val)
{
root = insertHelper(val,
root);
}
public Node insertHelper(E val, Node current)
{
if(current == null)
{
return new
Node(val);
}
int result =
current.data.compareTo(val);
if(result < 0)
{
current.right =
insertHelper(val, current.right);
}
else if(result > 0)
{
current.left =
insertHelper(val, current.left);
}
else//update
{
current.data =
val;
}
return current;
}
public void remove(E val)
{
root = removeHelper(val,
root);
}
private Node removeHelper(E val, Node current)
{
if(current.data.equals(val))
{
if(current.left
== null && current.right == null)//no children
{
return null;
}
else
if(current.left != null && current.right != null)//two
children
{
Node result = findMin(current.right);
result.right = removeHelper(result.data,
current.right);
result.left = current.left;
return result;
}
else//one
child
{
return (current.left != null)? current.left :
current.right;
}
}
int result =
current.data.compareTo(val);
if(result < 0)
{
current.right =
removeHelper(val, current.right);
}
else if(result > 0)
{
current.left =
removeHelper(val, current.left);
}
return current;
}
private class Node
{
E data;
Node left, right;
public Node(E d)
{
data = d;
left =
null;
right =
null;
}
}
}
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
public class A4Driver {
public static void main(String[] args) {
A4BST<Integer> tree1 = new
A4BST<>();
tree1.insert(5);
tree1.insert(3);
tree1.insert(1);
tree1.insert(2);
tree1.insert(9);
tree1.insert(10);
tree1.insert(25);
A4BST<Integer> tree2 = new
A4BST<>();
tree2.insert(8);
tree2.insert(5);
tree2.insert(1);
tree2.insert(3);
tree2.insert(15);
tree2.insert(20);
tree2.insert(25);
A4BST<Integer> tree3 = new
A4BST<>();
tree3.insert(1);
tree3.insert(2);
tree3.insert(3);
tree3.insert(5);
tree3.insert(9);
tree3.insert(10);
tree3.insert(25);
System.out.println(tree1.printInLevelOrder());//5, 3, 9, 1, 10, 2,
25
System.out.println(tree2.printInLevelOrder());//8, 5, 15, 1, 20, 3,
25
System.out.println(tree3.printInLevelOrder());//1, 2, 3, 4, 5, 9,
10, 25
System.out.println(tree1.visuallyIdentical(tree2));//true
System.out.println(tree1.visuallyIdentical(tree3));//false
}
}
In: Computer Science