Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

INGI1131 (Oz 2) - Ajout de solutions d'APE 2-4-5 #813

Merged
merged 4 commits into from
May 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/q6/oz-INGI1131/exercises/APE2/q1.oz
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
declare
proc {Max L Z}
proc {MaxLoop L M R}
case L of nil then R = M
[] H|T then
if M > H then {MaxLoop T M R}
else {MaxLoop T H R} end
end
end
in
if L == nil then Z = error
else {MaxLoop L.2 L.1 Z} end
end

{Browse {Max [4 2 7 8 6]}}
40 changes: 40 additions & 0 deletions src/q6/oz-INGI1131/exercises/APE2/q10.oz
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
local
fun {Flatten X}
fun {DoFlatten Xs Start End}
case Xs
of X|Xr then S S1 in
if {DoFlatten X S S1} then
S = Start
{DoFlatten Xr S1 End}
else S2 in
Start = X|S2
{DoFlatten Xr S2 End}
end
[] nil then
Start = End
true
else false
end
end
Start
in
if {DoFlatten X Start nil} then Start
else X
end
end
in
{Browse {Flatten [a [b [c d]] e [[[f]]]]}} % -> [a b c d e f]
end

%%% Simpler, but less optimized version %%%
local
fun {Flatten X}
case X
of H|T then {Append {Flatten H} {Flatten T}}
[] nil then nil
else [X]
end
end
in
{Browse {Flatten [a [b [c d]] e [[[f]]]]}} % -> [a b c d e f]
end
11 changes: 11 additions & 0 deletions src/q6/oz-INGI1131/exercises/APE2/q11.oz
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
declare
proc{Fact N R}
if N==1 then R = [N]
else
Out = {Fact N-1}
Jimvy marked this conversation as resolved.
Show resolved Hide resolved
in
R = N*Out.1|Out
end
end

{Browse {Fact 4}} %-> [24 6 2 1]
19 changes: 19 additions & 0 deletions src/q6/oz-INGI1131/exercises/APE2/q12.oz
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
declare
fun {DictionaryFilter D F}
case D
of leaf then nil
[] dict(key:Key info:Info left:Left right:Right) then
DicL = {DictionaryFilter Left F}
DicR = {DictionaryFilter Right F}
in
if({F Info})then
{Append DicL Key#Info|DicR}
else
{Append DicL DicR}
end
end
end

Class=dict(key:10 info:person('Christian'19) left:dict(key:7 info:person('Denys'25) left:leaf right:dict(key:9 info:person('David'7) left:leaf right:leaf)) right:dict(key:18 info:person('Rose'12) left:dict(key:14 info:person('Ann'27) left:leaf right:leaf) right:leaf))

{Browse {DictionaryFilter Class fun {$ Info}Info.2>20 end}} %->[7#person('Denys' 25) 14#person('Ann' 27)]
12 changes: 12 additions & 0 deletions src/q6/oz-INGI1131/exercises/APE2/q2.oz
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
%a
declare
fun {Sum N M}
if N == 0 then M
else {Sum N-1 M+N}
end
end

{Browse {Sum 8 0}}
%b

%c
33 changes: 33 additions & 0 deletions src/q6/oz-INGI1131/exercises/APE2/q4.oz
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
%a
define
fun {ForAllTail Xs P}
case Xs of nil then skip
[] H|T then
{P Xs}
{ForAllTail T P}
end

%b
fun {GetElementsInOrder Tree}
if Tree.left == nil && Tree.right == nil then [Tree.info]
elseif Tree.left == nil then Tree.info|{GetElementsInOrder Tree.right}
elseif Tree.right == nil then Append{{GetElementsInOrder Tree.left} [Tree.info]}
else Append{Append{{GetElementsInOrder Tree.left} [Tree.info]} {GetElementsInOrder Tree.right}
end
end

%(plus court)
fun {GetElementsInOrder Tree}
fun{Loop T Li}
case T
of nil then Li
[] tree(info:I left:L right:R) then
{Loop R {Append {Loop L Li} [I]}}
end
end
in
{Loop Tree nil}
end

Tree = tree(info:10 left:tree(info:7 left:nil right:tree(info:9 left:nil right:nil)) right:tree(info:18 left:tree(info:14 left:nil right:nil) right:nil))
{Browse {GetElementsInOrder Tree}} %-> [7 9 10 14 18]
10 changes: 10 additions & 0 deletions src/q6/oz-INGI1131/exercises/APE2/q5.oz
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
%a
local Fib in
fun{Fib N}
if N<2 then 1
else {Fib N-1}+{Fib N-2}
Jimvy marked this conversation as resolved.
Show resolved Hide resolved
end
end
end

%b
32 changes: 32 additions & 0 deletions src/q6/oz-INGI1131/exercises/APE2/q6.oz
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
declare
fun {Add A B}
fun {AddDigits A B C}
SUM = A+B+C
OUT = output(sum:_ carry:_)
in
case SUM
of 0 then OUT.sum=0 OUT.carry=0
[] 1 then OUT.sum=1 OUT.carry=0
[] 2 then OUT.sum=0 OUT.carry=1
[] 3 then OUT.sum=1 OUT.carry=1
end
OUT
end

fun {Loop A B}
Prev
Addition
in
if A==nil then output(r:nil carry:0)
else
Prev = {Loop A.2 B.2}
Addition = {AddDigits A.1 B.1 Prev.carry}
output(r:Addition.sum|Prev.r carry:Addition.carry)
end
end
Addition = {Loop A B}
in
Addition.carry|Addition.r
end

{Browse {Add [1 1 0 1 1 0] [0 1 0 1 1 1]}} % -> [1 0 0 1 1 0 1]
17 changes: 17 additions & 0 deletions src/q6/oz-INGI1131/exercises/APE2/q7-8-9.oz
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
declare
%7
fun {Filter L F}
if L==nil then nil
elseif {F L.1} then L.1|{Filter L.2 F}
else {Filter L.2 F}
end
end

%8
fun {IsEven A}
A mod 2 == 0
end

{Browse {Filter [1 2 3 4 6 7] IsEven}}

%9
24 changes: 24 additions & 0 deletions src/q6/oz-INGI1131/exercises/APE4/q5.oz
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
declare
proc{MapRecord Done R1 F R2}
A={Record.arity R1}
proc{Loop L X}
case L of nil then X=unit
[] H|T then
XTail
in
thread R2.H={F R1.H}end
{Loop T XTail}
{Wait R2.H}
{Wait XTail}
X=unit
end
end
in
R2={Record.make {Record.label R1} A}
{Loop A Done}
end

Done
R = {MapRecord Done '#'(a:1 b:2 c:3 d:4 e:5 f:6 g:7) fun {$ X} {Delay 1000} 2*X end}
{Wait Done}
{Show R}
10 changes: 0 additions & 10 deletions src/q6/oz-INGI1131/exercises/APE5/q3.oz

This file was deleted.

9 changes: 9 additions & 0 deletions src/q6/oz-INGI1131/exercises/APE5/q3a.oz
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
%a
declare
L1 L2 F
L1 = [1 2 3]
F = fun{$ X} {Delay 200} X*X end
X
thread L2 = {Map L1 F} X=unit end
{Wait X}
{Show L2}
10 changes: 10 additions & 0 deletions src/q6/oz-INGI1131/exercises/APE5/q3b.oz
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
%b
declare
L1 L2 L3 L4
X1 X2 X3
L1= [1 2 3]
thread L2={Map L1 fun{$ X} {Delay 200} X*X end} X1=unit end
thread L3={Map L1 fun{$ X} {Delay 200} 2*X end} X2=unit end
thread L4={Map L1 fun{$ X} {Delay 200} 3*X end} X3=unit end
{Wait X1} {Wait X2} {Wait X3}
{Show L2#L3#L4}
Loading