Add tasks for all the new languages
This commit is contained in:
parent
9dc3c2bb62
commit
bba7bfd280
13208 changed files with 134745 additions and 0 deletions
21
Task/CSV-data-manipulation/ECL/csv-data-manipulation.ecl
Normal file
21
Task/CSV-data-manipulation/ECL/csv-data-manipulation.ecl
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
// Assumes a CSV file exists and has been sprayed to a Thor cluster
|
||||
MyFileLayout := RECORD
|
||||
STRING Field1;
|
||||
STRING Field2;
|
||||
STRING Field3;
|
||||
STRING Field4;
|
||||
STRING Field5;
|
||||
END;
|
||||
|
||||
MyDataset := DATASET ('~Rosetta::myCSVFile', MyFileLayout,CSV(SEPARATOR(',')));
|
||||
|
||||
MyFileLayout Appended(MyFileLayout pInput):= TRANSFORM
|
||||
SELF.Field1 := pInput.Field1 +'x';
|
||||
SELF.Field2 := pInput.Field2 +'y';
|
||||
SELF.Field3 := pInput.Field3 +'z';
|
||||
SELF.Field4 := pInput.Field4 +'a';
|
||||
SELF.Field5 := pInput.Field5 +'b';
|
||||
END ;
|
||||
|
||||
MyNewDataset := PROJECT(MyDataset,Appended(LEFT));
|
||||
OUTPUT(myNewDataset,,'~Rosetta::myNewCSVFile',CSV,OVERWRITE);
|
||||
|
|
@ -0,0 +1,21 @@
|
|||
;; CSV -> LISTS
|
||||
(define (csv->row line) (map (lambda(x) (or (string->number x) x)) (string-split line ",")))
|
||||
(define (csv->table csv) (map csv->row (string-split csv "\n")))
|
||||
|
||||
;; LISTS -> CSV
|
||||
(define (row->csv row) (string-join row ","))
|
||||
(define (table->csv header rows)
|
||||
(string-join (cons (row->csv header) (for/list ((row rows)) (row->csv row))) "\n"))
|
||||
|
||||
|
||||
(define (task file)
|
||||
(let*
|
||||
((table (csv->table file))
|
||||
(header (first table))
|
||||
(rows (rest table)))
|
||||
|
||||
(table->csv
|
||||
(append header "SUM") ;; add last column
|
||||
(for/list ((row rows)) (append row (apply + row))))))
|
||||
|
||||
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
(define file.csv #<<
|
||||
C1,C2,C3,C4,C5
|
||||
1,5,9,13,17
|
||||
2,6,10,14,18
|
||||
3,7,11,15,19
|
||||
4,8,12,16,20
|
||||
>>#)
|
||||
|
||||
(task file.csv)
|
||||
|
||||
→ "C1,C2,C3,C4,C5,SUM
|
||||
1,5,9,13,17,45
|
||||
2,6,10,14,18,50
|
||||
3,7,11,15,19,55
|
||||
4,8,12,16,20,60"
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
' FB 1.05.0 Win64
|
||||
|
||||
Open "manip.csv" For Input As #1 ' existing CSV file
|
||||
Open "manip2.csv" For Output As #2 ' new CSV file for writing changed data
|
||||
|
||||
Dim header As String
|
||||
Line Input #1, header
|
||||
header += ",SUM"
|
||||
Print #2, header
|
||||
|
||||
Dim As Integer c1, c2, c3, c4, c5, sum
|
||||
|
||||
While Not Eof(1)
|
||||
Input #1, c1, c2, c3, c4, c5
|
||||
sum = c1 + c2 + c3 + c4 + c5
|
||||
Write #2, c1, c2, c3, c4, c5, sum
|
||||
Wend
|
||||
|
||||
Close #1
|
||||
Close #2
|
||||
45
Task/CSV-data-manipulation/FunL/csv-data-manipulation.funl
Normal file
45
Task/CSV-data-manipulation/FunL/csv-data-manipulation.funl
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
import io.{lines, PrintWriter}
|
||||
|
||||
data Table( header, rows )
|
||||
|
||||
def read( file ) =
|
||||
l = lines( file )
|
||||
|
||||
def next = vector( l.next().split(',') )
|
||||
|
||||
if l.isEmpty() then
|
||||
return Table( vector(), [] )
|
||||
|
||||
header = next()
|
||||
rows = seq()
|
||||
|
||||
while l.hasNext()
|
||||
rows += next()
|
||||
|
||||
Table( header, rows.toList() )
|
||||
|
||||
def write( table, out ) =
|
||||
w = if out is String then PrintWriter( out ) else out
|
||||
|
||||
w.println( table.header.mkString(',') )
|
||||
|
||||
for r <- table.rows
|
||||
w.println( r.mkString(',') )
|
||||
|
||||
if out is String
|
||||
w.close()
|
||||
|
||||
def updateRow( header, row, updates ) =
|
||||
r = dict( (header(i), row(i)) | i <- 0:header.length() )
|
||||
updates( r )
|
||||
vector( r(f) | f <- header )
|
||||
|
||||
def update( table, updates ) =
|
||||
Table( table.header, (updateRow(table.header, r, updates) | r <- table.rows).toList() )
|
||||
|
||||
def addColumn( table, column, updates ) =
|
||||
Table( table.header + [column], (updateRow(table.header + [column], r + [null], updates) | r <- table.rows).toList() )
|
||||
|
||||
t = addColumn( read('test.csv'), 'SUM', r -> r('SUM') = sum(int(v) | (_, v) <- r if v != null) )
|
||||
write( t, 'test_out.csv' )
|
||||
write( t, System.out )
|
||||
|
|
@ -0,0 +1,99 @@
|
|||
----------------------------------------
|
||||
-- Simplified CSV parser (without escape character support etc.).
|
||||
-- First line is interrepted as header with column names.
|
||||
-- @param {string} csvStr
|
||||
-- @param {string} [sep=","] - single char as string
|
||||
-- @param {string} [eol=RETURN]
|
||||
-- @return {propList}
|
||||
----------------------------------------
|
||||
on parseSimpleCSVString (csvStr, sep, eol)
|
||||
if voidP(sep) then sep=","
|
||||
if voidP(eol) then eol = RETURN
|
||||
lines = explode(eol, csvStr)
|
||||
if lines.getLast()="" then lines.deleteAt(lines.count)
|
||||
res = [:]
|
||||
res[#header] = explode(sep, lines[1])
|
||||
res[#data] = []
|
||||
cnt = lines.count
|
||||
repeat with i = 2 to cnt
|
||||
res[#data].append(explodeBySingleChar(sep, lines[i]))
|
||||
end repeat
|
||||
return res
|
||||
end
|
||||
|
||||
----------------------------------------
|
||||
-- Simplified CSV creater (without escape character support etc.).
|
||||
-- @param {propList} csvData
|
||||
-- @param {string} [sep=","]
|
||||
-- @param {string} [eol=RETURN]
|
||||
-- @return {string}
|
||||
----------------------------------------
|
||||
on createSimpleCSVString (csvData, sep, eol)
|
||||
if voidP(sep) then sep=","
|
||||
if voidP(eol) then eol = RETURN
|
||||
res = ""
|
||||
put implode(sep, csvData[#header])&eol after res
|
||||
cnt = csvData[#data].count
|
||||
repeat with i = 1 to cnt
|
||||
put implode(sep, csvData[#data][i])&eol after res
|
||||
end repeat
|
||||
return res
|
||||
end
|
||||
|
||||
----------------------------------------
|
||||
-- Explodes string into list
|
||||
-- @param {string} delim
|
||||
-- @param {string} str
|
||||
-- @return {list}
|
||||
----------------------------------------
|
||||
on explode (delim, str)
|
||||
if delim.length=1 then return explodeBySingleChar(delim, str)
|
||||
l = []
|
||||
if voidP(str) then return l
|
||||
dl = delim.length
|
||||
repeat while true
|
||||
pos = offset(delim, str)
|
||||
if pos=0 then exit repeat
|
||||
l.add(str.char[1..pos-1])
|
||||
delete char 1 to pos+dl-1 of str
|
||||
end repeat
|
||||
if pos=0 then pos = 1-dl
|
||||
l.add(str.char[pos+dl..str.length])
|
||||
return l
|
||||
end
|
||||
|
||||
----------------------------------------
|
||||
-- Explode string into list based on single char delimiter
|
||||
-- (uses Lingo's build-in 'item' support, therefor faster)
|
||||
-- @param {string} delim
|
||||
-- @param {string} str
|
||||
-- @return {list}
|
||||
----------------------------------------
|
||||
on explodeBySingleChar (delim, str)
|
||||
l = []
|
||||
if voidP(str) then return l
|
||||
od = _player.itemDelimiter
|
||||
_player.itemDelimiter = delim
|
||||
cnt = str.item.count
|
||||
repeat with i = 1 to cnt
|
||||
l.add(str.item[i])
|
||||
end repeat
|
||||
_player.itemDelimiter = od
|
||||
return l
|
||||
end
|
||||
|
||||
----------------------------------------
|
||||
-- Implodes list into string
|
||||
-- @param {string} delim
|
||||
-- @param {list} l
|
||||
-- @return {string}
|
||||
----------------------------------------
|
||||
on implode (delim, l)
|
||||
str = ""
|
||||
cnt = l.count
|
||||
repeat with i = 1 to cnt
|
||||
put l[i]&delim after str
|
||||
end repeat
|
||||
delete char (str.length-delim.length+1) to str.length of str
|
||||
return str
|
||||
end
|
||||
|
|
@ -0,0 +1,44 @@
|
|||
sep = ","
|
||||
eol = numtochar(10)
|
||||
|
||||
-- load CSV string from file
|
||||
fn = _movie.path & "file.csv"
|
||||
fp = xtra("fileIO").new()
|
||||
fp.openFile(fn, 1)
|
||||
csvStr = fp.readFile()
|
||||
fp.closeFile()
|
||||
|
||||
-- parse CSV string into propList
|
||||
csvData = parseSimpleCSVString(csvStr, sep, eol)
|
||||
|
||||
-- add SUM column
|
||||
csvData[#header].append("SUM")
|
||||
repeat with row in csvData[#data]
|
||||
sum = 0
|
||||
repeat with cell in row
|
||||
sum = sum+integer(cell)
|
||||
end repeat
|
||||
row.append(sum)
|
||||
end repeat
|
||||
|
||||
-- create CSV string from updated propList
|
||||
csvString = createSimpleCSVString(csvData, sep, eol)
|
||||
|
||||
-- save CSV string to file
|
||||
fn = _movie.path & "file.csv"
|
||||
fp.openFile(fn, 2)
|
||||
if not fp.status() then fp.delete()
|
||||
fp.createFile(fn)
|
||||
fp.openFile(fn, 2)
|
||||
fp.writeString(csvString)
|
||||
fp.closeFile()
|
||||
|
||||
-- show the CSV string
|
||||
put csvString
|
||||
|
||||
-- "C1,C2,C3,C4,C5,SUM
|
||||
1,5,9,13,17,45
|
||||
2,6,10,14,18,50
|
||||
3,7,11,15,19,55
|
||||
4,8,12,16,20,60
|
||||
"
|
||||
25
Task/CSV-data-manipulation/Nim/csv-data-manipulation.nim
Normal file
25
Task/CSV-data-manipulation/Nim/csv-data-manipulation.nim
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
import strutils, streams
|
||||
|
||||
let
|
||||
csv = newFileStream("data.csv", fmRead)
|
||||
outf = newFileStream("data-out.csv", fmWrite)
|
||||
|
||||
var lineNumber = 1
|
||||
|
||||
while true:
|
||||
if atEnd(csv):
|
||||
break
|
||||
var line = readLine(csv)
|
||||
|
||||
if lineNumber == 1:
|
||||
line.add(",SUM")
|
||||
else:
|
||||
var tmp = 0
|
||||
for n in split(line, ","):
|
||||
tmp += parseInt(n)
|
||||
line.add(",")
|
||||
line.add($tmp)
|
||||
|
||||
outf.writeLn($line)
|
||||
|
||||
inc lineNumber
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
# Read
|
||||
var csvfile = %f'data.csv';
|
||||
var fh = csvfile.open_r;
|
||||
var header = fh.line.trim_end.split(',');
|
||||
var csv = fh.lines.map { .trim_end.split(',').map{.to_num} };
|
||||
fh.close;
|
||||
|
||||
# Write
|
||||
var out = csvfile.open_w;
|
||||
out.say([header..., 'SUM'].join(','));
|
||||
csv.each { |row| out.say([row..., row.sum].join(',')) };
|
||||
out.close;
|
||||
|
|
@ -0,0 +1,24 @@
|
|||
var csv = require('Text::CSV').new(
|
||||
Hash(eol => "\n")
|
||||
);
|
||||
|
||||
# Open
|
||||
var csvfile = %f'data.csv';
|
||||
var fh = csvfile.open_r;
|
||||
|
||||
# Read
|
||||
var rows = [];
|
||||
var header = csv.getline(fh);
|
||||
while (var row = csv.getline(fh)) {
|
||||
rows.append(row.map{.to_num});
|
||||
}
|
||||
|
||||
# Process
|
||||
header.append('SUM');
|
||||
rows.each { |row| row.append(row.sum) };
|
||||
|
||||
# Write
|
||||
var out = csvfile.open_w;
|
||||
[header, rows...].each { |row|
|
||||
csv.print(out, row);
|
||||
};
|
||||
42
Task/CSV-data-manipulation/Ursa/csv-data-manipulation.ursa
Normal file
42
Task/CSV-data-manipulation/Ursa/csv-data-manipulation.ursa
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
#
|
||||
# csv data manipulation
|
||||
#
|
||||
|
||||
# declare a string stream to hold lines
|
||||
decl string<> lines
|
||||
|
||||
# open the file specified on the command line, halting
|
||||
# execution if they didn't enter one. it will be created if
|
||||
# it doesn't exist yet
|
||||
decl file f
|
||||
if (< (size args) 2)
|
||||
out "error: please specify a csv file" endl console
|
||||
stop
|
||||
end if
|
||||
f.create args<1>
|
||||
f.open args<1>
|
||||
|
||||
# read in all lines from the file
|
||||
set lines (f.readlines)
|
||||
|
||||
# append sum column to header
|
||||
set lines<0> (+ lines<0> ",SUM")
|
||||
|
||||
# determine sums and append them
|
||||
decl int i sum
|
||||
for (set i 1) (< i (size lines)) (inc i)
|
||||
set sum 0
|
||||
for (decl int j) (< j (size (split lines<i> ","))) (inc j)
|
||||
set sum (int (+ sum (int (split lines<i> ",")<j>)))
|
||||
end for
|
||||
set lines<i> (+ lines<i> (+ "," sum))
|
||||
end for
|
||||
|
||||
# delete the file, then create it again
|
||||
f.delete args<1>
|
||||
f.create args<1>
|
||||
|
||||
# output all lines to the file
|
||||
for (set i 0) (< i (size lines)) (inc i)
|
||||
out lines<i> endl f
|
||||
end for
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
CLOSE DATABASES ALL
|
||||
SET SAFETY OFF
|
||||
MODIFY FILE file1.csv NOEDIT
|
||||
*!* Create a cursor with integer columns
|
||||
CREATE CURSOR tmp1 (C1 I, C2 I, C3 I, C4 I, C5 I)
|
||||
APPEND FROM file1.csv TYPE CSV
|
||||
SELECT C1, C2, C3, C4, C5, C1+C2+C3+C4+C5 As sum ;
|
||||
FROM tmp1 INTO CURSOR tmp2
|
||||
COPY TO file2.csv TYPE CSV
|
||||
MODIFY FILE file2.csv NOEDIT IN SCREEN
|
||||
SET SAFETY ON
|
||||
15
Task/CSV-data-manipulation/jq/csv-data-manipulation.jq
Normal file
15
Task/CSV-data-manipulation/jq/csv-data-manipulation.jq
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
# Omit empty lines
|
||||
def read_csv:
|
||||
split("\n")
|
||||
| map(if length>0 then split(",") else empty end) ;
|
||||
|
||||
# add_column(label) adds a summation column (with the given label) to
|
||||
# the matrix representation of the CSV table, and assumes that all the
|
||||
# entries in the body of the CSV file are, or can be converted to,
|
||||
# numbers:
|
||||
def add_column(label):
|
||||
[.[0] + [label],
|
||||
(reduce .[1:][] as $line
|
||||
([]; ($line|map(tonumber)) as $line | . + [$line + [$line|add]]))[] ] ;
|
||||
|
||||
read_csv | add_column("SUM") | map(@csv)[]
|
||||
Loading…
Add table
Add a link
Reference in a new issue