To print follow like this from text array - arrays

Print, for each data set that is input, the average to four decimal places. This average should be preceded each time by “For Competitor #X, the average score is ”, where X denotes the competitor’s position (starting with 1) in the input file.
Output to screen for above input file:
For Competitor #1, the average is 5.8625
For Competitor #2, the average is 0.0000
For Competitor #3, the average is 1.0000
file:///C:/Users/tram/Downloads/gym.PNG
go this link this is my code and it printed.
For Competitor #0, the average is 0
For Competitor #0, the average is 0
For Competitor #0, the average is 0

You don't mention in which language do you want it.
In javascript you have:
var num=5.11234123
num.toFixed(4) // "5.1123"

Related

Extracting numbers from a series of text files

I have 12 files all with the same format:
Statistics Information
Line: 4
Fiducial range: 156 to 364
Number of items: 209
Number of dummies: 0
Minimum value: -0.08983668870989447
Maximum value: 0.059795797205623558
Mean value: -0.00884060126461031
Standard deviation: 0.03707261357656038
Arithmetic sum: -1.8476856643035546
Each file is for a manoeuvre in a specific direction, North (Pitch, roll, Yaw), South (Pitch, roll, Yaw), East (Pitch, roll, Yaw) and West (Pitch, roll, Yaw).
I want to cycle through each of these text files and store the number for each minimum, maximum and mean value for each file. Then export them in a table:
NORTH
Pitch
Roll
Yaw
Min
-0.08983668870989447
Max
0.059795797205623558
Mean
-0.00884060126461031
South
Pitch
Roll
Yaw
Min
Max
Mean
et cetera
So far I have managed to list the different files and then extract the first line:
import glob
txt_files = glob.glob("*.txt")
def read_first_line(txt_files):
with open(txt_files, 'rt') as fd:
first_line = fd.readline()
return first_line
output_strings = map(read_first_line, txt_files) # apply read first line function all text files
print(txt_files)
output_content = "".join(sorted(output_strings))
output_content # as a string
print(output_content) # print as formatted
with open('outfile.txt', 'wt') as fd:
fd.write(output_content)

formatting arrays with numbers and characters

I need help turning a Decay.txt file into an array, the first 1-3 and 5th columns are numbers, the third column is "time elapsed" in integers, but the 4th column is a unit of time (milliseconds, Months, Days) but its spelled out with characters. i cant get this mixed array (numbers and characters) to transfer over to matlab
ideally id like to take the unit of time (4th column) change it to a seconds value, (i.e. hour becomes 3600 seconds) then multiply it by the number in the third column and have a final 4 column array where the 3rd column is simply the time elapsed in seconds
anyone know how to do either of these things?
ive tried
Decay = fopen('Decay.txt','r');
B = fscanf(Decay,'%f',[5 inf]);
which stops and has an error as soon as it hits the 4th column
and
Decay = fopen('Decay.txt','r');
B = fscanf(Decay,'%s',[5 inf]);
but this just creates a 5x10000 column where every single number, decimal, and letter is on its own in its own cell of the array
Your first example
Decay = fopen('Decay.txt','r');
B = fscanf(Decay,'%f',[5 inf]);
Breaks because it can't scan the fourth column (a string) as a number (%f). Your second example doesn't have numbers because you're scanning everything as a string (%s).
The correct specifier for your format should be
'%f %f %f %s %f'
However, if you call fscanfwith it, as per documentation:
If formatSpec contains a combination of numeric and character specifiers, then A is numeric, of class double, and fscanf converts each text characters to its numeric equivalent. This occurs even when formatSpec explicitly skips all numeric fields (for example, formatSpec is '%*d %s').
So this input file:
50 1.2 99 s 0
6.42 1.2 3.11 min 1
22 37 0.01 h 2
Has this (undesired) output:
>> fscanf(Decay, "%f %f %f %s %f", [5, inf])
ans =
50.0000 6.4200 110.0000 104.0000
1.2000 1.2000 1.0000 2.0000
99.0000 3.1100 22.0000 0
115.0000 109.0000 37.0000 0
0 105.0000 0.0100 0
That happens because a matrix in MATLAB can't have multiple data of different types. So, your best bet is scanning into a cell array, which can have any type inside.
B = textscan(Decay, "%f %f %f %s %f")
Returns a cell array with the appropriate types. You can use this output to convert the time data into the same unit and build your vectors/matrix. Columns 1, 2, 3 and 5 are trivial to do, just by accessing the cell B{n} for each n.
Column 4 is a cell array of cells. In each internal cell, there's the string you have. You need to apply a conversion from string to the number you need. For my example, such function would look like:
function scale = DecayScale(unit)
switch(unit)
case 's'
scale = 1;
case 'min'
scale = 60;
case 'h'
scale = 3600;
otherwise
throw('Number format not recognized');
end
end
Which you could then apply to the 4th column like:
timeScale = cellfun(#DecayScale, B{4})
And get the final time as:
timeColumn = B{3} .* timeScale

how to calculate vwap price for a specified notional amount in kdb?

I have the following example table in kdb.
mkt:([] date:2018.05.05 2018.05.05 2018.05.05 2018.05.05; time:2018.05.05D01:30:00 2018.05.05D01:30:01 2018.05.05D01:30:01 2018.05.05D01:30:02; bid:((1.2110 1.21105);(1.2111 1.2109 1.2112);(1.2111 1.2109);(1.2110 1.21105)); bidSize:((3000000 1000000);(500000 1000000 1000000);(1000000 2000000);(1000000 1000000)); ask:((1.2111 1.21115);(1.2112 1.2110 1.2113);(1.2112 1.2110);(1.2111 1.21115)); askSize:((3000000 1000000);(500000 1000000 1000000);(1000000 2000000);(1000000 1000000)))
I have this as a solution but the numbers won't be the most accurate.
table:select date, time, bid:{x wavg y}'[bidSize;bid], bidSize: sum each bidSize, ask:{x wavg y}'[askSize;ask], askSize:sum each askSize from mkt
table: update cumulBidSize: sums bidSize, cumulAskSize: sums askSize from table
1#select from table where cumlBidSize>=5000000
I want to be able to have a function when given a specific size (e.g. 5000000), calculate the vwap for that size. How am I able to cycle through the data in the cell until i get 5000000?
You can use this to calculate the avg bid only for rows where the total bidSize is greater than or equal to a specified size:
{[x]select {x wavg y}'[sum each bidSize;bid] from mkt where x<=sum each bidSize}[4000000]
You can use this if you want to calculate the average price for only prices with an associate size greater than or equal to a specified size:
{select bidSize wavg bid from ungroup mkt where bidSize>=x}[2000000]
Hope this helps,
James

SAS code is giving error for Do-While loop

I need to print the fuel consumption and mileage of the car in SAS code. if given that mileage is 20 miles per gallon.
It should stop generating output if fuel reaches to 10 gallon OR car travel 250 miles.
My code:
data milage;
fuel=1;
do while (fuel<11);
miles = fuel*20;
fuel+1;
output;
end;
run;
My output:
Code Output
The output for fuel needs to be started from 1 for first 20 miles which incorrect in my code. Any suggestion on what I am missing here.
Thanks!!
Add an explicit OUTPUT for the first line or start at 0 instead. If
you start at 0, make sure the order of the fuel and miles
calculation are correct.
Change your loop condition to be <10 and add in the MILES criteria
as well. In this case you're only looping if fuel<10 AND the miles
lt 250. Make sure the boundaries are what you want.
data milage;
fuel=0; miles=0;
do while (fuel<10 and miles lt 250);
fuel+1;
miles = fuel*20;
output;
end;
run;

Solr Facet Index Sorting not working for numerics

I'm using Solr for offering faceted autosuggestion.I'm using latest version of Solr.
Here is an example what I need to do.
The suggestion order should be
grade 1
grade 2
grade 3
grade 4
...
...
grade 10
But now it look like this...
grade 1
grade 10
grade 11
grade 12
grade 2
grade 3
...
...
I am using facet.sort=index .
Any idea how to do this??Please advise..
Index as 001, 002, 003, ... 999 and remove the leading zeroes in your front end.
Word 'grade' is useless to index. If you index the actual numeric grade, e.g., 1,2,3,...,10, then you can sort it. And if you make its type double, by default it is sorted numerically. Like the example below:
/select?q=*%3A*&fq=ZIP_code%3D94602&fq=Year%3A[1999+TO+2013]&fq=NOT+Causes_of_Death:OTH&fl=Year,Causes_of_Death,Count&wt=csv&indent=true&rows=200&sort=Count+desc
Year,Causes_of_Death,Count
2000,HTD,68
2002,HTD,64
1999,CAN,61
1999,HTD,60
2005,CAN,60
2001,CAN,57
Hope this helps.

Resources