Created
March 28, 2017 18:26
-
-
Save wilzbach/d2759ff6c701e43475f54d830a867a37 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env rdmd | |
import std.algorithm, std.array, std.conv, std.mmfile, std.range, std.stdio, std.bigint; | |
void main(string[] args) | |
{ | |
if (args.length < 2) | |
{ | |
writeln("No input file given."); | |
return; | |
} | |
auto inFile = File(args[1]); | |
auto columns = new BigInt[inFile.readln.splitter(",").walkLength]; | |
long rows; | |
foreach(line; inFile.byLine) | |
{ | |
foreach (i, el; line.splitter(',').map!(to!int).enumerate) | |
{ | |
foreach (column; args[2..$]) | |
columns[i] += el; | |
} | |
rows++; | |
} | |
foreach (column; args[2..$]) | |
{ | |
auto colParts = column.split("="); | |
auto res = columns[colParts[0].to!size_t]; | |
switch (colParts[1]) | |
{ | |
case "MEAN": | |
writefln("%s[%s]: %s", colParts[0], colParts[1], res.toLong / double(rows)); | |
break; | |
case "SUM": | |
writefln("%s[%s]: %s", colParts[0], colParts[1], res); | |
break; | |
default: | |
writefln("%s is not defined!: ", colParts[1]); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env rdmd | |
import std.algorithm; | |
import std.array; | |
import std.conv; | |
import std.mmfile; | |
import std.range; | |
import std.stdio; | |
import std.bigint; | |
void main(string[] args) | |
{ | |
if (args.length < 2) | |
{ | |
writeln("No input file given."); | |
return; | |
} | |
scope mmFile = new MmFile(args[1]); | |
auto file = splitter(cast(string)mmFile[0..mmFile.length], '\n').filter!"!a.empty"; | |
// read header | |
auto p = splitter(file.front, ',').array; | |
file.popFront(); | |
auto columns = new BigInt[p.length]; | |
long rows; | |
foreach(line; file) | |
{ | |
foreach (i, el; line.splitter(',').map!(to!int).enumerate) | |
{ | |
columns[i] += el; | |
} | |
rows++; | |
} | |
foreach (column; args[2..$]) | |
{ | |
auto colParts = column.split("="); | |
auto res = columns[colParts[0].to!size_t]; | |
switch (colParts[1]) | |
{ | |
case "MEAN": | |
writefln("%s[%s]: %s", colParts[0], colParts[1], res.toLong / double(rows)); | |
break; | |
case "SUM": | |
writefln("%s[%s]: %s", colParts[0], colParts[1], res); | |
break; | |
default: | |
writeln("Not found", colParts[1]); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env rdmd | |
import std.algorithm; | |
import std.array; | |
import std.conv; | |
import std.mmfile; | |
import std.range; | |
import std.stdio; | |
import std.bigint; | |
import std.variant; | |
void main(string[] args) | |
{ | |
if (args.length < 2) | |
{ | |
writeln("No input file given."); | |
return; | |
} | |
auto inFile = File(args[1]); | |
BigInt[] columns; | |
columns.length = inFile.readln.splitter(",").walkLength; | |
class Counter | |
{ | |
this(){} | |
size_t column; | |
this(size_t column) | |
{ | |
this.column = column; | |
} | |
abstract void add(int x); | |
} | |
class AVGCounter : Counter | |
{ | |
this(size_t column) { super(column); } | |
long n; | |
long k; | |
long ex; | |
long ex2; | |
double result() | |
{ | |
return k + ex / n; | |
} | |
override void add(int x) | |
{ | |
if (n == 0) | |
k = x; | |
n++; | |
ex += x - k; | |
ex2 += (x - k) * (x - k); | |
} | |
override string toString() | |
{ | |
import std.format; | |
return format("%.4g", result); | |
} | |
} | |
class SumCounter : Counter | |
{ | |
this(size_t column) { super(column); } | |
BigInt result; | |
override void add(int t) | |
{ | |
result += t; | |
} | |
override string toString() | |
{ | |
return result.to!string; | |
} | |
} | |
Counter[] counters; | |
foreach (column; args[2..$]) | |
{ | |
auto colParts = column.split("="); | |
auto col = colParts[0].to!size_t; | |
switch (colParts[1]) | |
{ | |
case "MEAN": | |
counters ~= new AVGCounter(col); | |
break; | |
case "SUM": | |
counters ~= new SumCounter(col); | |
break; | |
default: | |
} | |
} | |
foreach(line; inFile.byLine) | |
{ | |
auto cols = line.splitter(',').map!(to!int); | |
size_t column; | |
foreach (counter; counters) | |
{ | |
while (column < counter.column) | |
{ | |
cols.popFront; | |
column++; | |
} | |
counter.add(cols.front); | |
} | |
} | |
foreach (counter; counters) | |
{ | |
writefln("%d: %s", counter.column, counter); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment