Re: Need a Challenge?

new topic     » goto parent     » topic index » view thread      » older message » newer message

On Thursday 16 August 2001 16:00, David Cuny wrote:

> OK, I ran the second version, and fixed a couple of bugs in it - I had
> forgotten to change house[] to matrix[House][] in some of the tests. It
> took about 1.3 hours to run, but it works correctly now.

Wow. The following pgm runs in about 24 seconds; of course, this 
is a compiled language.

MODULE zebra;

CONST	N = 5;
English = 1; Spaniard = 2; Japanese = 3; Italian = 4; Norwegian = 5;
Red = 1; Green = 2; White = 3; Yellow = 4; Blue = 5;
Painter = 1; Sculptor = 2; Diplomat = 3; Violinist = 4; Doctor = 5;
Dog = 1; Snails = 2; Fox = 3; Horse = 4; Zebra = 5;
Tea = 1; Coffee = 2; Milk = 3; Juice = 4; Water = 5;

TYPE	T = [1..N];
        ArrayT = ARRAY [1..N] OF [1..N];

VAR 
   Nat,           (* [English, Spaniard, Japanese, Italian, Norwegian] *)
   Color,	       (* [Red, Green, White, Yellow, Blue] *)
   Profession, (* [Painter, Sculptor, Diplomat, Violinist, Doctor] *)
   Pet, 	       (* [Dog, Snails, Fox, Horse, Zebra] *)
   Drink: 	       (* [Tea, Coffee, Milk, Juice, Water] *)
   ArrayT;

PROCEDURE generate(VAR x:ArrayT);
  VAR i,j,k: T;
BEGIN
  FOR i := 1 TO N DO 
    SOME j := 1 TO N DO  
      FOR k := 1 TO i-1 DO j <> x[k] END;
      x[i] = j
    END
  END
END generate;

PROCEDURE zebra(VAR Nat, Color, Profession, Pet, Drink: ArrayT);
BEGIN
  Nat[English] = Color[Red]; 		(* English = Red *)
  Nat[Spaniard] = Pet[Dog];		(* Spaniard = Dog *)
  Nat[Japanese] = Profession[Painter]; 	(* Japanese = Painter *)
  Nat[Italian] = Drink[Tea]; 		(* Italian = Tea *)
  Nat[Norwegian] = 1; 	        		(* Norwegian = 1 *)
  Color[Green] = Drink[Coffee];    		(* Green = Coffee *)
  Color[Green] = Color[White] + 1;		(* Green = White + 1 *)
  Profession[Sculptor] = Pet[Snails];	(* Sculptor = Snails *)
  Profession[Diplomat] = Color[Yellow];	(* Diplomat = Yellow *)
  Drink[Milk] = 3; 				(* Milk = 3 *)
  EITHER Nat[Norwegian] - Color[Blue] = 1 
  ORELSE Nat[Norwegian] - Color[Blue] = -1 
  END; 					(* |Norwegian - Blue| = 1 *)
  Profession[Violinist] = Drink[Juice]; (* Violinist = Juice *)
  EITHER Profession[Doctor] <> N; Pet[Fox] = Profession[Doctor] + 1
  ORELSE Profession[Doctor] <> 1; Pet[Fox] = Profession[Doctor] - 1
  END; 					(* |Doctor - Fox| = 1 *)
  EITHER Profession[Diplomat] <> N; Pet[Horse] = Profession[Diplomat] + 1
  ORELSE Profession[Diplomat] <> 1; Pet[Horse] = Profession[Diplomat] - 1
  END 					(* |Diplomat - Horse| = 1 *)
END zebra;

VAR sol: T;
    i: INTEGER;
BEGIN
  FORALL	
    generate(Nat);
    generate(Color);
    generate(Profession);
    zebra(Nat, Color, Profession, Pet, Drink);
    generate(Pet);
    generate(Drink);
  DO
    FOR i := 1 TO N DO
      IF Profession[i] = Pet[Zebra]
      THEN 
      WRITELN("Person with profession number ", Profession[i], " owns the  
zebra.");
      END;
      IF Profession[i] = Drink[Water]
      THEN 
      WRITELN("Person with profession number ", Profession[i], " drinks 
water.");
      END;

    END
  END
END zebra.

Regards,
Irv

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu