PascalABC.NET: Difference between revisions

Content deleted Content added
Ibond84 (talk | contribs)
Ibond84 (talk | contribs)
Line 57:
== Samples ==
 
=== 1. Swap the first and second halves of an array ===
{{Empty section|date=April 2016}}
<source lang="delphi">
begin
var a := ArrGen(10,i->2*i+1);
a.Println;
Assert(a.Length mod 2 = 0);
var n := a.Length div 2;
a := a.Skip(n).Concat(a.Take(n)).ToArray;
a.Println;
end.
</source>
 
=== 2. 100! ===
<source lang="delphi">
begin
var p: BigInteger := 1;
for var i:=1 to 100 do
p := p * i;
write(p);
end.
</source>
 
=== 3. Display all Fibonacci numbers less than 1000 ===
<source lang="delphi">
begin
SeqWhile(1,1,(x,y)->x+y,x->x<1000).Print;
end.
</source>
 
=== 4. Word frequence dictionary for a file ===
<source lang="delphi">
begin
var d := new Dictionary<string,integer>;
foreach var s in ReadLines('words.txt') do
foreach var word in s.ToWords do
d[word] := d.Get(word) + 1;
d.Print(NewLine);
end.
</source>
 
=== 4а. Word frequence dictionary for a file. Solution in functional style ===
<source lang="delphi">
begin
ReadLines('words.txt').SelectMany(s->s.ToWords()).GroupBy(v->v).ToDictionary(x->x.Key,x->x.Count()).Print(NewLine);
end.
</source>
 
=== 5. Parallel matrix multiplication using OpenMP directives ===
<source lang="delphi">
procedure Mult(a,b,c: array [,] of real; n: integer);
begin
{$omp parallel for}
for var i:=0 to n-1 do
for var j:=0 to n-1 do
begin
var cc := 0.0;
for var l:=0 to n-1 do
cc += a[i,l]*b[l,j];
c[i,j] := cc;
end;
end;
const n = 1000;
begin
var a := MatrixRandomReal(n,n,1,1.1);
var b := MatrixRandomReal(n,n,1,1.1);
var c := new real[n,n];
Mult(a,b,c,n);
writeln(MillisecondsDelta/1000);
end.
</source>
 
== See also ==