Raphael Collet wrote :
Thanks for the effort, Chris.

I have reviewed the first ten, and have proposed alternative versions, which I personally find simpler. I have also fixed the solutions #7 and #8, which were not correct (see history for #8).

I'll have a look to the other ones later, and I will probably extend the list of solutions... ;-)

Cheers,
raph

Here are a few ones I did some time ago.

Yves
On Thu, Dec 4, 2008 at 11:05 PM, <[EMAIL PROTECTED] <mailto:[EMAIL PROTECTED]>> wrote:

    I see the stubs were put on the wiki a while back.  I took the
    liberty of
    going through the first 25, but got stuck on 26 for now.



    
http://www.codepoetics.com/wiki/index.php?title=Topics:99_Problems_in_other_languages:Oz



    Chris


------------------------------------------------------------------------

_________________________________________________________________________________
mozart-users mailing list                               
[email protected]
http://www.mozart-oz.org/mailman/listinfo/mozart-users

%1-10
fun{Last L}
   case L
   of [X] then X
   [] _|T then {Last T}
   end
end
fun{LastButOne L}
   case L
   of [X _] then X
   [] _|T then {LastButOne T}
   end
end
fun{ElementAt L N}
   if N==1 then L.1
   else {ElementAt L.2 N-1}
   end
end
fun{Length L}
   fun{Loop L A}
      case L
      of nil then A
      [] _|T then {Loop T A+1}
      end
   end
in
   {Loop L 0}
end
fun{Reverse L}
   fun{Loop L A}
      case L
      of nil then A
      [] H|T then {Loop T H|A}
      end
   end
in
   {Loop L nil}
end
fun{IsPalindrome L}
   L=={Reverse L}
end
fun{Flatten L}
   fun{Loop L A}
      case L
      of nil then A
      [] H|T then {Loop H {Loop T}}
      else L
      end
   end
in
   {Loop L nil}
end
fun{Compress L}
   fun{Loop L X}
      case L
      of nil then nil
      [] !X|T then {Loop T X}
      [] H|T then H|{Loop H T}
      end
   end
in
   {Loop L {NewName}}
end
fun{Pack L}
   case L
   of nil then nil
   [] H|T then
      R
      fun{Loop L X}
         case L
         of nil then R=nil nil
         [] !X|T then X|{Loop T}
         else R=L nil
         end
      end
   in
      {Loop T H}|{Pack R}
   end
end
fun{Encode L}
   fun{Loop L}
      case L
      of nil then nil
      [] H|T then [{Length H} H.1]|{Loop T}
      end
   end
in
   {Loop {Pack L}}
end
%11-20
fun{EncodeMod L}
   fun{Loop L}
      case L
      of nil then nil
      [] [1 X]|T then X|{Loop T}
      [] H|T then H|{Loop T}
      end
   end
in
   {Loop {Encode L}}
end
fun{DecodeMod L}
   case L
   of nil then nil
   [] [1 Y]|T then Y|{DecodeMod T}
   [] [X Y]|T then Y|{DecodeMod [X-1 Y]|T}
   [] H|T then H|{DecodeMod T}
   end
end
fun{EncodeDirect L}
   fun{Loop L X C}
      case L
      of !X|T then
         {Loop T X C+1}
      else
         [C X]|{EncodeDirect L}
      end
   end
in
   case L
   of nil then nil
   [] H|!H|T then {Loop T H 2}
   [] H|T then H|{EncodeDirect T}
   end
end
fun{Duplicate L}
   case L
   of nil then nil
   [] H|T then H|H|{Duplicate T}
   end
end
fun{Replicate L N}
   case L
   of nil then nil
   [] H|T then
      fun{Loop I}
         if I==0 then
            {Replicate T N}
         else
            H|{Loop I-1}
         end
      end
   in
      {Loop N}
   end
end
fun{Drop L N}
   fun{Loop L I}
      case L
      of nil then nil
      [] _|T andthen I==N then
         {Loop T 1}
      [] H|T then
         H|{Loop T I+1}
      end
   end
in
   {Loop L 1}
end
fun{Split L N}
   R
   fun{Loop L I}
      if I==0 then R=L nil
      else L.1|{Loop L.2 I-1}
      end
   end
in
   [{Loop L N} R]
end
fun{Slice L I K}
   if I==1 then
      fun{Loop L J}
         if J==0 then nil
         else L.1|{Loop L.2 J-1}
         end
      end
   in
      {Loop L K}
   else
      {Slice L.2 I-1 K-1}
   end
end
fun{Rotate L N}
   if N<0 then
      L2={Length L} in
      {Rotate L L2+(N mod L2)}
   else
      R
      fun{Loop1 L T}
         case L
         of nil then T
         [] H|T then H|{Loop1 T}
         end
      end
      fun{Loop2 L N}
         if N==0 then R=L nil
         else L.1|{Loop2 L.2 N-1}
         end
      end
      in
      {Loop1 R {Loop2 L N}}
   end
end
fun{RemoveAt N L ?X}
   if N==1 then X=L.1 L.2
   else L.1|{RemoveAt N-1 L.2 X}
   end
end
%21-28
fun{InsertAt X L N}
   if N==1 then X|L
   else L.1|{InsertAt X L.2 N-1}
   end
end
fun{Range X Y}
   if X==Y then [X]
   else X|{Range X+1 Y}
   end
end
fun{RndSelect L N}
   fun{Loop L LL N}
      if N==0 then nil
      else
         X={OS.rand} mod LL
         NL
      in
         {RemoveAt X L $ NL}|{Loop NL LL-1 N-1}
      end
   end
in
   {Loop L {Length L} N}
end
fun{DiffSelect N M}
   fun lazy {LRange X Y}
      if X==Y then [X]
      else X|{LRange X+1 Y}
      end
   end
   fun lazy {LRemoveAt N L ?X}
      if N==1 then X=L.1 L.2
      else L.1|{LRemoveAt N-1 L.2 X}
      end
   end
   fun{Loop L LL N}
      if N==0 then nil
      else
         X={OS.rand} mod LL
         NL
      in
         {RemoveAt X L $ NL}|{Loop NL LL-1 N-1}
         end
   end
in
   {Loop {LRange 1 M} M N}
end
fun{RndPermu L}
   {RndSelect L {Length L}}
end
fun{Combinations N L}
   fun{Loop N L A Xs}
      if N==0 then A|Xs
      else
         case L
         of nil then Xs
         [] H|T then
            {Loop N T A {Loop N-1 T H|A Xs}}
         end
      end
   end
in
   {Loop N L nil nil}
end
fun{Group L Ns}
   fun{Reverse L A}
      case L
      of nil then A
      [] H|T then {Reverse T H|A}
      end
   end
   fun{Append L1 L2}
      case L1
      of nil then L2
      [] H|T then H|{Append T L2}
      end
   end
   fun{Loop Ns L R A A2 Xs}
      case Ns
      of nil then
         A2|Xs
      [] N|Nr then
         if N==0 then
            {Loop Nr {Append L R} nil nil A|A2 Xs}
         else
            case L
            of nil then Xs
            [] H|T then
               {Loop Ns T H|R A A2 {Loop N-1|Nr T R H|A A2 Xs}}
            end
         end
      end
   end
in
   {Loop {Reverse Ns nil} L nil nil nil nil}
end
fun{LSort Xss}
   {List.map
    {List.sort
     {List.map Xss
      fun{$Xs}{Length Xs}#Xs end
     }
     fun{$ L1#_ L2#_}L1<L2 end
    }
    fun{$ _#Xs}Xs end
   }
end
fun{LFSort Xss}
   B C X in
   {List.foldL
    {List.sort
     {List.foldL
      {List.sort
       {List.map Xss
        fun{$Xs}{Length Xs}#Xs end
       }
       fun{$ L1#_ L2#_}L1<L2 end
      }
      fun{$ PL#R#B#E#C L#Xs}
         if L==PL then
            NE in
            E=Xs|NE
            PL#R#B#NE#C+1
         else
            NR NB NE in
            E=nil
            R=(C#B)|NR
            NB=Xs|NE
            L#NR#NB#NE#1
         end
      end
      0#$#B#B#0
      _#[C#X]#X#nil#C
     }
     fun{$ F1#_ F2#_}F1<F2 end
    }
    proc{$ $ _#Xs T}
       {List.append Xs T}
    end
    $
    nil
   }
end
_________________________________________________________________________________
mozart-users mailing list                               
[email protected]
http://www.mozart-oz.org/mailman/listinfo/mozart-users

Reply via email to