Sync
This commit is contained in:
parent
6f050a029e
commit
776bba907c
3887 changed files with 59894 additions and 7280 deletions
51
Task/Gray-code/Component-Pascal/gray-code.component
Normal file
51
Task/Gray-code/Component-Pascal/gray-code.component
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
MODULE GrayCodes;
|
||||
IMPORT StdLog,SYSTEM;
|
||||
|
||||
PROCEDURE Encode*(i: INTEGER; OUT x: INTEGER);
|
||||
VAR
|
||||
j: INTEGER;
|
||||
s,r: SET;
|
||||
BEGIN
|
||||
s := BITS(i);j := MAX(SET);
|
||||
WHILE (j >= 0) & ~(j IN s) DO DEC(j) END;
|
||||
r := {};IF j >= 0 THEN INCL(r,j) END;
|
||||
WHILE j > 0 DO
|
||||
IF ((j IN s) & ~(j - 1 IN s)) OR (~(j IN s) & (j - 1 IN s)) THEN INCL(r,j-1) END;
|
||||
DEC(j)
|
||||
END;
|
||||
x := SYSTEM.VAL(INTEGER,r)
|
||||
END Encode;
|
||||
|
||||
PROCEDURE Decode*(x: INTEGER; OUT i: INTEGER);
|
||||
VAR
|
||||
j: INTEGER;
|
||||
s,r: SET;
|
||||
BEGIN
|
||||
s := BITS(x);r:={};j := MAX(SET);
|
||||
WHILE (j >= 0) & ~(j IN s) DO DEC(j) END;
|
||||
IF j >= 0 THEN INCL(r,j) END;
|
||||
WHILE j > 0 DO
|
||||
IF ((j IN r) & ~(j - 1 IN s)) OR (~(j IN r) & (j - 1 IN s)) THEN INCL(r,j-1) END;
|
||||
DEC(j)
|
||||
END;
|
||||
i := SYSTEM.VAL(INTEGER,r);
|
||||
END Decode;
|
||||
|
||||
|
||||
PROCEDURE Do*;
|
||||
VAR
|
||||
grayCode,binCode: INTEGER;
|
||||
i: INTEGER;
|
||||
BEGIN
|
||||
StdLog.String(" i ");StdLog.String(" bin code ");StdLog.String(" gray code ");StdLog.Ln;
|
||||
StdLog.String("---");StdLog.String(" ----------------");StdLog.String(" ---------------");StdLog.Ln;
|
||||
FOR i := 0 TO 32 DO;
|
||||
Encode(i,grayCode);Decode(grayCode,binCode);
|
||||
StdLog.IntForm(i,10,3,' ',FALSE);
|
||||
StdLog.IntForm(binCode,2,16,' ',TRUE);
|
||||
StdLog.IntForm(grayCode,2,16,' ',TRUE);
|
||||
StdLog.Ln;
|
||||
END
|
||||
END Do;
|
||||
|
||||
END GrayCodes.
|
||||
|
|
@ -1,11 +1,11 @@
|
|||
import std.stdio, std.algorithm, std.range;
|
||||
|
||||
string[] g(in uint n) /*pure nothrow*/ {
|
||||
return n ? g(n - 1).map!q{'0' ~ a}().array() ~
|
||||
g(n - 1).retro().map!q{'1' ~ a}().array()
|
||||
string[] g(in uint n) pure nothrow {
|
||||
return n ? g(n - 1).map!q{'0' ~ a}.array ~
|
||||
g(n - 1).retro.map!q{'1' ~ a}.array
|
||||
: [""];
|
||||
}
|
||||
|
||||
void main() {
|
||||
writeln(g(4));
|
||||
4.g.writeln;
|
||||
}
|
||||
|
|
|
|||
18
Task/Gray-code/Icon/gray-code.icon
Normal file
18
Task/Gray-code/Icon/gray-code.icon
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
link bitint
|
||||
|
||||
procedure main()
|
||||
every write(right(i := 0 to 10,4),":",right(int2bit(i),10)," -> ",
|
||||
right(g := gEncode(i),10)," -> ",
|
||||
right(b := gDecode(g),10)," -> ",
|
||||
right(bit2int(b),10))
|
||||
end
|
||||
|
||||
procedure gEncode(b)
|
||||
return int2bit(ixor(b, ishift(b,-1)))
|
||||
end
|
||||
|
||||
procedure gDecode(g)
|
||||
b := g[1]
|
||||
every i := 2 to *g do b ||:= if g[i] == b[i-1] then "0" else "1"
|
||||
return b
|
||||
end
|
||||
30
Task/Gray-code/Mercury/gray-code-1.mercury
Normal file
30
Task/Gray-code/Mercury/gray-code-1.mercury
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
:- module gray.
|
||||
|
||||
:- interface.
|
||||
:- import_module int.
|
||||
|
||||
:- type gray.
|
||||
|
||||
% VALUE conversion functions
|
||||
:- func gray.from_int(int) = gray.
|
||||
:- func gray.to_int(gray) = int.
|
||||
|
||||
% REPRESENTATION conversion predicate
|
||||
:- pred gray.coerce(gray, int).
|
||||
:- mode gray.coerce(in, out) is det.
|
||||
:- mode gray.coerce(out, in) is det.
|
||||
|
||||
:- implementation.
|
||||
|
||||
:- import_module list.
|
||||
|
||||
:- type gray
|
||||
---> gray(int).
|
||||
|
||||
gray.from_int(X) = gray(X `xor` (X >> 1)).
|
||||
|
||||
gray.to_int(gray(G)) = (G > 0 -> G `xor` gray.to_int(gray(G >> 1))
|
||||
; G).
|
||||
gray.coerce(gray(I), I).
|
||||
|
||||
:- end_module gray.
|
||||
40
Task/Gray-code/Mercury/gray-code-2.mercury
Normal file
40
Task/Gray-code/Mercury/gray-code-2.mercury
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
:- module gray_test.
|
||||
|
||||
:- interface.
|
||||
|
||||
:- import_module io.
|
||||
|
||||
:- pred main(io::di, io::uo) is det.
|
||||
|
||||
:- implementation.
|
||||
|
||||
:- import_module gray.
|
||||
:- import_module int, list, string.
|
||||
|
||||
:- pred check_conversion(list(int)::in, list(gray)::out) is semidet.
|
||||
:- pred display_lists(list(int)::in, list(gray)::in, io::di, io::uo) is det.
|
||||
:- pred display_record(int::in, gray::in, io::di, io::uo) is det.
|
||||
|
||||
main(!IO) :-
|
||||
Numbers = 0..31,
|
||||
( check_conversion(Numbers, Grays) ->
|
||||
io.format("%8s %8s %8s\n", [s("Number"), s("Binary"), s("Gray")], !IO),
|
||||
io.format("%8s %8s %8s\n", [s("------"), s("------"), s("----")], !IO),
|
||||
display_lists(Numbers, Grays, !IO)
|
||||
|
||||
; io.write("Either conversion or back-conversion failed.\n", !IO)).
|
||||
|
||||
check_conversion(Numbers, Grays) :-
|
||||
Grays = list.map(gray.from_int, Numbers),
|
||||
Numbers = list.map(gray.to_int, Grays).
|
||||
|
||||
display_lists(Numbers, Grays, !IO) :-
|
||||
list.foldl_corresponding(display_record, Numbers, Grays, !IO).
|
||||
|
||||
display_record(Number, Gray, !IO) :-
|
||||
gray.coerce(Gray, GrayRep),
|
||||
NumBin = string.int_to_base_string(Number, 2),
|
||||
GrayBin = string.int_to_base_string(GrayRep, 2),
|
||||
io.format("%8d %8s %8s\n", [i(Number), s(NumBin), s(GrayBin)], !IO).
|
||||
|
||||
:- end_module gray_test.
|
||||
28
Task/Gray-code/PHP/gray-code.php
Normal file
28
Task/Gray-code/PHP/gray-code.php
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @author Elad Yosifon
|
||||
*/
|
||||
|
||||
/**
|
||||
* @param int $binary
|
||||
* @return int
|
||||
*/
|
||||
function gray_encode($binary){
|
||||
return $binary ^ ($binary >> 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $gray
|
||||
* @return int
|
||||
*/
|
||||
function gray_decode($gray){
|
||||
$binary = $gray;
|
||||
while($gray >>= 1) $binary ^= $gray;
|
||||
return $binary;
|
||||
}
|
||||
|
||||
for($i=0;$i<32;$i++){
|
||||
$gray_encoded = gray_encode($i);
|
||||
printf("%2d : %05b => %05b => %05b : %2d \n",$i, $i, $gray_encoded, $gray_encoded, gray_decode($gray_encoded));
|
||||
}
|
||||
10
Task/Gray-code/Prolog/gray-code-1.pro
Normal file
10
Task/Gray-code/Prolog/gray-code-1.pro
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
to_gray(N, G) :-
|
||||
N0 is N >> 1,
|
||||
G is N xor N0.
|
||||
|
||||
from_gray(G, N) :-
|
||||
( G > 0
|
||||
-> S is G >> 1,
|
||||
from_gray(S, N0),
|
||||
N is G xor N0
|
||||
; N is G ).
|
||||
31
Task/Gray-code/Prolog/gray-code-2.pro
Normal file
31
Task/Gray-code/Prolog/gray-code-2.pro
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
:- use_module(library(apply)).
|
||||
|
||||
to_gray(N, G) :-
|
||||
N0 is N >> 1,
|
||||
G is N xor N0.
|
||||
|
||||
from_gray(G, N) :-
|
||||
( G > 0
|
||||
-> S is G >> 1,
|
||||
from_gray(S, N0),
|
||||
N is G xor N0
|
||||
; N is G ).
|
||||
|
||||
make_num(In, Out) :-
|
||||
atom_to_term(In, Out, _),
|
||||
integer(Out).
|
||||
|
||||
write_record(Number, Gray, Decoded) :-
|
||||
format('~w~10|~2r~10+~2r~10+~2r~10+~w~n',
|
||||
[Number, Number, Gray, Decoded, Decoded]).
|
||||
|
||||
go :-
|
||||
setof(N, between(0, 31, N), Numbers),
|
||||
maplist(to_gray, Numbers, Grays),
|
||||
maplist(from_gray, Grays, Decodeds),
|
||||
format('~w~10|~w~10+~w~10+~w~10+~w~n',
|
||||
['Number', 'Binary', 'Gray', 'Decoded', 'Number']),
|
||||
format('~w~10|~w~10+~w~10+~w~10+~w~n',
|
||||
['------', '------', '----', '-------', '------']),
|
||||
maplist(write_record, Numbers, Grays, Decodeds).
|
||||
go :- halt(1).
|
||||
Loading…
Add table
Add a link
Reference in a new issue