Thursday, December 17, 2015

Oh say can u see..................can someone stop the run away train..................called the USA??????  Cia spooks everywhere............


Look-and-say sequence

Task
Look-and-say sequence
You are encouraged to solve this taskaccording to the task description, using any language you may know.
The Look and say sequence is a recursively defined sequence of numbers studied most notably by John Conway.
Sequence Definition
  • Take a decimal number
  • Look at the number, visually grouping consecutive runs of the same digit.
  • Say the number, from left to right, group by group; as how many of that digit there are - followed by the digit grouped.
This becomes the next number of the sequence.
An example:
  • Starting with the number 1, you have one 1 which produces 11.
  • Starting with 11, you have two 1's i.e. 21
  • Starting with 21, you have one 2, then one 1 i.e. (12)(11) which becomes 1211
  • Starting with 1211 you have one 1, one 2, then two 1's i.e. (11)(12)(21) which becomes 111221
Task description
Write a program to generate successive members of the look-and-say sequence.
See also

Contents

 [hide

[edit]Ada

with Ada.Text_IO, Ada.Strings.Fixed;
use  Ada.Text_IO, Ada.Strings, Ada.Strings.Fixed;
 
function "+" (S : String) return String is
   Item : constant Character := S (S'First);
begin
   for Index in S'First + 1..S'Last loop
      if Item /= S (Index) then
         return Trim (Integer'Image (Index - S'First), Both) & Item & (+(S (Index..S'Last)));
      end if;
   end loop;
   return Trim (Integer'Image (S'Length), Both) & Item;
end "+";
This function can be used as follows:
Put_Line (+"1");
Put_Line (+(+"1"));
Put_Line (+(+(+"1")));
Put_Line (+(+(+(+"1"))));
Put_Line (+(+(+(+(+"1")))));
Put_Line (+(+(+(+(+(+"1"))))));
Put_Line (+(+(+(+(+(+(+"1")))))));
Put_Line (+(+(+(+(+(+(+(+"1"))))))));
Put_Line (+(+(+(+(+(+(+(+(+"1")))))))));
Put_Line (+(+(+(+(+(+(+(+(+(+"1"))))))))));
Output:
11
21
1211
111221
312211
13112221
1113213211
31131211131221
13211311123113112211
11131221133112132113212221

[edit]ALGOL 68

Translation ofAda
Works withALGOL 68 version Standard - no extensions to language used
Works withALGOL 68G version Any - tested with release mk15-0.8b.fc9.i386
Works withELLA ALGOL 68 version Any (with appropriate job cards) - tested with release 1.8.8d.fc9.i386
OP + = (STRING s)STRING:
BEGIN
   CHAR item = s[LWB s];
   STRING out;
   FOR index FROM LWB s + 1 TO UPB s DO
      IF item /= s [index] THEN
         out := whole(index - LWB s, 0) + item + (+(s [index:UPB s]));
         GO TO return out
      FI
   OD;
   out := whole (UPB s, 0) + item;
   return out: out
END  # + #;
 
OP + = (CHAR s)STRING: 
  + STRING(s);
 
print ((+"1", new line));
print ((+(+"1"), new line));
print ((+(+(+"1")), new line));
print ((+(+(+(+"1"))), new line));
print ((+(+(+(+(+"1")))), new line));
print ((+(+(+(+(+(+"1"))))), new line));
print ((+(+(+(+(+(+(+"1")))))), new line));
print ((+(+(+(+(+(+(+(+"1"))))))), new line));
print ((+(+(+(+(+(+(+(+(+"1")))))))), new line));
print ((+(+(+(+(+(+(+(+(+(+"1"))))))))), new line))
Output:
11
21
1211
111221
312211
13112221
1113213211
31131211131221
13211311123113112211
11131221133112132113212221

[edit]AutoHotkey

AutoExecute:    Gui, -MinimizeBox
    Gui, Add, Edit, w500 r20 vInput, 1
    Gui, Add, Button, x155 w100 Default, &Calculate
    Gui, Add, Button, xp+110 yp wp, E&xit
    Gui, Show,, Look-and-Say sequence
Return
 
 
ButtonCalculate:    Gui, Submit, NoHide
    GuiControl,, Input, % LookAndSay(Input)
Return

No comments:

Post a Comment