LINQ group results - entity-framework

I've spent the whole day trying to figure out how to JOIN / group my query below, can someone help me or guide me to complete the desired output? -i enclosed some of the foreign key values for easy reference)
Below is my Table Grade
rowID testID studentID Grade
1 1 1(class2011) 50
2 1 1(class2011) 90
3 2 1(class2011) 100
4 2 2(class2012) 85
--Student table
StudentID Classyear
1 2(class2011)
2 3(class2012)
3 1(class2010)
--Classyear Table
ClassYearID Desc
1 2010
2 2011
3 2012
My query below (to display the testID, failed(passing rate=80), passed, taken, Rate)
var h = list.GroupBy(a => a.testID)
.Select(a => {
int _failed = a.Count(g => g.Grade < 80);
int _passed = a.Count(g => g.Grade >= 80);
int _rate = (int)(_passed / (double)a.Count() * 100.0);
return new {
testID = a.Key,
failed = _failed,
passed = _passed,
taken = a.Count(),
rate = _rate,
};
});
The result1
testID failed passed taken Rate
1 1 1 2 50%
2 0 2 2 100%
Now here's my BIG problem: the students in my table grade above DOES NOT belong in the
same classyear. I need to group this by testID and classyear.
See below for the desired output:
testID ClassyearID failed passed taken Rate
1 1(2010) 0 0 0 0
1 2(2011) 1 1 2 50%
1 3(2012) 0 0 0 0
2 1(2010) 0 0 0 0
2 2(2011) 0 1 1 100%
2 3(2012) 0 1 1 100%
You can immediately see the difference from result 1. stats for Test2 is divided for class 2011 and class 2012.
Hope I did explain clear enough my intentions. Maybe I will post later my crappy code trying to get the desired output. Thanks

Can't you just do this?:
var h = list.GroupBy(a =>new {a.testID,a.classyear})
.Select(a => {
int _failed = a.Count(g => g.Grade < 80);
int _passed = a.Count(g => g.Grade >= 80);
int _rate = (int)(_passed / (double)a.Count() * 100.0);
return new {
testID = a.Key.testID,
classyear=a.Key.classyear,
failed = _failed,
passed = _passed,
taken = a.Count(),
rate = _rate,
};
});

Related

Count rows that meet certain requirements and then subtract that number from another count

Let's say I have data like this:
Type
OrderNumber
Priority
DeliveryMethod
Boxes
1
High
UPS
Misc
1
High
UPS
Boxes
2
Standard
InstaBox
Boxes
3
Standard
UPS
Boxes
3
Standard
UPS
Boxes
3
Standard
UPS
Boxes
4
Standard
Instabox
Boxes
5
Standard
Instabox
Boxes
5
Standard
Instabox
Boxes
6
Standard
UPS
Boxes
7
Standard
UPS
And I want to count all the so called "Private Orders". They are boxes with an unique ordernumber and a Standard priority and UPS delivery.
(There's 2 in this example, 6 and 7)
Then I want to count all the boxes with Standard priority and subtract all the Private orders. (There's 9 boxes with Standard priority, minus the 2 Private orders = 7.)
Is that possible in Tableau? I want to display the number 7 in a textbox.
For Private orders use this field
IF
{FIXED [Type], [Ordernumber] : COUNT([Ordernumber])} = 1
AND [Type] = 'Boxes' And [Priority] = 'Standard' And [DeliveryMethod] = 'UPS'
THEN 1 ELSE 0 END
For all boxes use this
Sum(
If [Type] = 'Boxes' And [Priority] = 'Standard' then 1 else 0 end
)
SO FOR FINAL OUTPUT YOU MAY USE THIS DIRECTLY say calculation2
Sum(
If [Type] = 'BOXES' And [Priority] = 'Standard' then 1 else 0 END
) -
Sum(IF
{FIXED [Type], [OrderNumber] : COUNT([OrderNumber])} = 1
AND [Type] = 'BOXES' And [Priority] = 'Standard' And [DeliveryMethod] = 'UPS'
THEN 1 ELSE 0 END)
For matching pattern change the above field to say calculation3
Sum(
If [Type] = 'BOXES' And [Priority] = 'Standard' then 1 else 0 END
) -
Sum(IF
{FIXED [Type], [OrderNumber] : COUNT([OrderNumber])} = 1
AND [Type] = 'BOXES' And [Priority] = 'Standard' And REGEXP_MATCH([DeliveryMethod], "UPS")
THEN 1 ELSE 0 END)
the data used
Results

index in a loop by using ..<

I didn't understood the concept of the For-In loop in swift 3 , can anyone explain to us it m thanks in advance
var total = 0
for i in 0..<4 {
total += i
}
print(total)
The result of total is 6 , Why ?
i=0 =>
total = 0+0 =0
i=1 =>
total = 0+1 = 1
i=2 =>
total = 1+2 = 3
i=3 =>
total = 3+3 =6
it's simply alogrithm ;-)
i never reach 4 because you said it STRICTLY inferior to 4 =)
(Do I answer your question?)
Your loop will be vary from 0 to 3 i.e. 0,1,2,3 but if you want it will vary from 0 to 4 then try this -
var total = 0
for i in 0...4 {
total += i
}
print(total)

DP Coin Change Algorithm - Retrieve coin combinations from table

To find how many ways we have of making change for the amount 4 given the coins [1,2,3], we can create a DP algorithm that produces the following table:
table[amount][coins.count]
0 1 2 3 4
-----------
(0) 1 | 1 1 1 1 1
(1) 2 | 1 1 2 2 3
(2) 3 | 1 1 2 3 4
The last position being our answer. The answer is 4 because we have the following combinations: [1,1,1,1],[2,1],[2,2],[3,1].
My question is, is it possible to retrieve these combinations from the table I just generated? How?
For completeness, here's my algorithm
func coinChange(coins: [Int], amount: Int) -> Int {
// int[amount+1][coins]
var table = Array<Array<Int>>(repeating: Array<Int>(repeating: 0, count: coins.count), count: amount + 1)
for i in 0..<coins.count {
table[0][i] = 1
}
for i in 1...amount {
for j in 0..<coins.count {
//solutions that include coins[j]
let x = i - coins[j] >= 0 ? table[i - coins[j]][j] : 0
//solutions that don't include coins[j]
let y = j >= 1 ? table[i][j-1] : 0
table[i][j] = x + y
}
}
return table[amount][coins.count - 1];
}
Thanks!
--
Solution
Here's an ugly function that retrieves the combinations, based on #Sayakiss 's explanation:
func getSolution(_ i: Int, _ j: Int) -> [[Int]] {
if j < 0 || i < 0 {
//not a solution
return []
}
if i == 0 && j == 0 {
//valid solution. return an empty array where the coins will be appended
return [[]]
}
return getSolution(i - coins[j], j).map{var a = $0; a.append(coins[j]);return a} + getSolution(i, j - 1)
}
getSolution(amount, coins.count-1)
Output:
[[1, 3], [2, 2], [1, 1, 2], [1, 1, 1, 1]]
Sure you can. We define a new function get_solution(i,j) which means all solution for your table[i][j].
You can think it returns an array of array, for example, the output of get_solution(4,3) is [[1,1,1,1],[2,1],[2,2],[3,1]]. Then:
Case 1. Any solution from get_solution(i - coins[j], j) plus coins[j] is a solution for table[i][j].
Case 2. Any solution from get_solution(i, j - 1) is a solution for table[i][j].
You can prove Case 1 + Case 2 is all possible solution for table[i][j](note you get table[i][j] by this way).
The only problem remains is to implement get_solution(i,j) and I think it's good for you to do it by yourself.
If you still got any question, please don't hesitate to leave a comment here.

Splitting Observable into windows

I am looking for Observable.Window like operator with opening window selector
As example we can take a sequence of natural numbers.
I am wondering how to split this sequence into windows so every new window starts if number is greater than 4 or window size has reached 3
Input sequence is IObservable<int>
Output sequence is IObservable<IObservable<int>>
Sequence 1 2 5 3 1 1 2 3 1 5 0
will produce windows 1 2; 5 3 1; 1 2 3; 1; 5 0
Using C# this works:
var observable =
source
.Concat(Observable.Return(-1))
.Publish(sp =>
sp.Zip(sp.Skip(1), (x0, x1) => new { x0, x1 })
.Publish(zsp =>
zsp
.Window(zsp.Where(x => x.x1 >= 4))
.Select(xs => xs.Select(x => x.x0).Window(3))
.Merge()));
I get this result:
As per my comment on the question I am unsure whether the sequence 1, 2, 3, 5 should produce the windows 1 2 3 | 5 or 1 2 3 | | 5 (see the empty window which is what Enigmativity's answer produces). My answer does not produce the empty window:
public static IObservable<IObservable<T>> Window<T>(this IObservable<T> source, Func<T, bool> predicate, int maximumWindowSize)
{
return Observable.Create<IObservable<T>>(obs =>
{
var currentWindow = new Subject<T>();
obs.OnNext(currentWindow);
var count = 0;
return source.Subscribe(x =>
{
if (count == maximumWindowSize || predicate(x))
{
count = 0;
currentWindow.OnCompleted();
currentWindow = new Subject<T>();
obs.OnNext(currentWindow);
}
currentWindow.OnNext(x);
count++;
}, obs.OnError, () =>
{
obs.OnCompleted();
currentWindow.OnCompleted();
});
});
}
This can be used like so:
var windows = source.Window(x => x > 4, 3);

Matlab HDF5: Read DIMENSION_LIST attribute

I'm trying to read HDF5 files with Matlab. I created the files in Fortran, which is only relevant in that I used h5dsattach_scale_f to attached scale datasets to each dimension of my given primary dataset. Most of my logic works well, but I'm having trouble reading the attributes of my primary dataset in order to get at the attached scales.
I start by iterating through each dataset in the file. Once I know I have my primary dataset, I iterate through its attributes with this call:
[status, index_out, SD] = H5A.iterate(dset_id, 'H5_INDEX_NAME', 'H5_ITER_NATIVE', 0, #hdf5_sds_attr_iter, SD);
That calls this function for every attribute:
function [status, SD] = hdf5_sds_attr_iter(dset_id, attr_name, info, SD)
status = 0;
disp(attr_name);
if ~strcmp(attr_name, 'DIMENSION_LIST')
return;
end
attr_id = H5A.open(dset_id, attr_name, 'H5P_DEFAULT');
space = H5A.get_space (attr_id);
[~, dims, ~] = H5S.get_simple_extent_dims(space);
info2 = H5A.get_info(attr_id);
disp(info2);
rdata = H5A.read(attr_id, 'H5ML_DEFAULT');
disp(rdata);
for i = 1:dims
disp(rdata{i});
end
H5S.close(space);
H5A.close(attr_id);
end
This is the output:
DIMENSION_LIST
3
corder_valid: 1
corder: 0
cset: 0
data_size: 48
[8x1 uint8]
[8x1 uint8]
[8x1 uint8]
184
17
0
0
0
0
0
0
32
28
0
0
0
0
0
0
240
29
0
0
0
0
0
0
If I do h5dump on the dataset, this is what that attribute looks like:
ATTRIBUTE "DIMENSION_LIST" {
DATATYPE H5T_VLEN { H5T_REFERENCE { H5T_STD_REF_OBJECT }}
DATASPACE SIMPLE { ( 3 ) / ( 3 ) }
DATA {
(0): (DATASET 1400 /beamdata scale rank 1 ),
(1): (DATASET 6512 /beamdata scale rank 2 ),
(2): (DATASET 6976 /beamdata scale rank 3 )
}
}
Since those numbers (1400, 6512, 6976) do not appear elsewhere in the dump, I don't know how to use them or the output of H5A.read (rdata) to actually get at the scale data. The Matlab HDF5 documentation is rather silent on what to do with attribute data. Does anyone know how to process attribute reference data correctly?