Look-and-say sequence: Difference between revisions
Content deleted Content added
Line 1,212: | Line 1,212: | ||
<pre>["1", "11", "21", "1211", "111221", "312211", "13112221", "1113213211"]</pre> |
<pre>["1", "11", "21", "1211", "111221", "312211", "13112221", "1113213211"]</pre> |
||
=== |
===Begginer Imperative Version=== |
||
<lang d>import std.stdio, std.conv, std.array; |
<lang d>import std.stdio, std.conv, std.array; |
||
auto lookAndSay(string s) |
auto lookAndSay(string s){ |
||
{ |
|||
auto result = appender!string; |
auto result = appender!string; |
||
auto i=0, j=i+1; |
auto i=0, j=i+1; |
||
while(i<s.length) |
while(i<s.length){ |
||
{ |
|||
while(j<s.length && s[i]==s[j]) j++; |
while(j<s.length && s[i]==s[j]) j++; |
||
result.put( text(j-i) ~ s[i] ); |
result.put( text(j-i) ~ s[i] ); |
||
Line 1,227: | Line 1,225: | ||
return result.data; |
return result.data; |
||
} |
} |
||
void main() |
void main(){ |
||
{ |
|||
auto s="1"; |
auto s="1"; |
||
for(auto i=0; i<10; i++) |
for(auto i=0; i<10; i++) |
||
(s = s.lookAndSay).writeln; |
(s = s.lookAndSay).writeln; |
||
}</lang> |
}</lang> |
||
===Fast Imperative Version=== |
===Fast Imperative Version=== |
||
<lang d>import core.stdc.stdio, std.math, std.conv, std.algorithm, std.array; |
<lang d>import core.stdc.stdio, std.math, std.conv, std.algorithm, std.array; |