June 2018 Update
This commit is contained in:
parent
ba8067c3b7
commit
22f33d4004
5278 changed files with 84726 additions and 14379 deletions
|
|
@ -0,0 +1,25 @@
|
|||
# look and say
|
||||
|
||||
dim a$(2)
|
||||
|
||||
i = 0 # input string index
|
||||
|
||||
a$[i] = "1"
|
||||
|
||||
print a$[i]
|
||||
|
||||
for n=1 to 10
|
||||
j = 1 - i # output string index
|
||||
a$[j] = ""
|
||||
k = 1
|
||||
while (k <= length(a$[i]))
|
||||
k0 = k + 1
|
||||
while ((k0 <= length(a$[i])) and (mid(a$[i], k, 1) = mid(a$[i], k0, 1)))
|
||||
k0 = k0 + 1
|
||||
end while
|
||||
a$[j] += string(k0 - k) + mid(a$[i], k, 1)
|
||||
k = k0
|
||||
end while
|
||||
i = j
|
||||
print a$[j]
|
||||
next n
|
||||
|
|
@ -1,32 +1,32 @@
|
|||
#include <string>
|
||||
#include <sstream>
|
||||
|
||||
std::string lookandsay(const std::string &s)
|
||||
{
|
||||
std::ostringstream r;
|
||||
|
||||
for (unsigned int i = 0; i != s.length(); ) {
|
||||
unsigned int new_i = s.find_first_not_of(s[i], i+1);
|
||||
if (new_i == std::string::npos)
|
||||
new_i = s.length();
|
||||
|
||||
r << new_i - i << s[i];
|
||||
i = new_i;
|
||||
}
|
||||
return r.str();
|
||||
}
|
||||
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
|
||||
std::string lookandsay(const std::string& s)
|
||||
{
|
||||
std::ostringstream r;
|
||||
|
||||
for (std::size_t i = 0; i != s.length();) {
|
||||
auto new_i = s.find_first_not_of(s[i], i + 1);
|
||||
|
||||
if (new_i == std::string::npos)
|
||||
new_i = s.length();
|
||||
|
||||
r << new_i - i << s[i];
|
||||
i = new_i;
|
||||
}
|
||||
return r.str();
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
std::string laf = "1";
|
||||
std::string laf = "1";
|
||||
|
||||
std::cout << laf << std::endl;
|
||||
for (int i = 0; i < 10; i++) {
|
||||
laf = lookandsay(laf);
|
||||
std::cout << laf << std::endl;
|
||||
}
|
||||
for (int i = 0; i < 10; ++i) {
|
||||
laf = lookandsay(laf);
|
||||
std::cout << laf << std::endl;
|
||||
}
|
||||
|
||||
return 0;
|
||||
return 0;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,28 @@
|
|||
shared void run() {
|
||||
|
||||
function lookAndSay(Integer|String input) {
|
||||
|
||||
variable value digits = if (is Integer input) then input.string else input;
|
||||
value builder = StringBuilder();
|
||||
|
||||
while (exists currentChar = digits.first) {
|
||||
if (exists index = digits.firstIndexWhere((char) => char != currentChar)) {
|
||||
digits = digits[index...];
|
||||
builder.append("``index````currentChar``");
|
||||
}
|
||||
else {
|
||||
builder.append("``digits.size````currentChar``");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return builder.string;
|
||||
}
|
||||
|
||||
variable String|Integer result = 1;
|
||||
print(result);
|
||||
for (i in 1..14) {
|
||||
result = lookAndSay(result);
|
||||
print(result);
|
||||
}
|
||||
}
|
||||
|
|
@ -1 +1 @@
|
|||
.say for '1', *.subst(/(.)$0*/, { .chars ~ .[0] }, :g) ... *;
|
||||
.say for ('1', *.subst(/(.)$0*/, { .chars ~ .[0] }, :g) ... *)[^15];
|
||||
|
|
|
|||
2
Task/Look-and-say-sequence/Q/look-and-say-sequence.q
Normal file
2
Task/Look-and-say-sequence/Q/look-and-say-sequence.q
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
las:{{raze string[count@'x],'@'[;0]x:where[differ x]_x}\[x;1#"1"]}
|
||||
las 8
|
||||
|
|
@ -1,11 +1,27 @@
|
|||
def lookAndSay(seed: BigInt) = {
|
||||
val s = seed.toString
|
||||
( 1 until s.size).foldLeft((1, s(0), new StringBuilder)) {
|
||||
case ((len, c, sb), index) if c != s(index) => sb.append(len); sb.append(c); (1, s(index), sb)
|
||||
case ((len, c, sb), _) => (len + 1, c, sb)
|
||||
} match {
|
||||
case (len, c, sb) => sb.append(len); sb.append(c); BigInt(sb.toString)
|
||||
}
|
||||
}
|
||||
import scala.annotation.tailrec
|
||||
|
||||
def lookAndSayIterator(seed: BigInt) = Iterator.iterate(seed)(lookAndSay)
|
||||
object LookAndSay extends App {
|
||||
|
||||
loop(10, "1")
|
||||
|
||||
@tailrec
|
||||
private def loop(n: Int, num: String): Unit = {
|
||||
println(num)
|
||||
if (n <= 0) () else loop(n - 1, lookandsay(num))
|
||||
}
|
||||
|
||||
private def lookandsay(number: String): String = {
|
||||
val result = new StringBuilder
|
||||
|
||||
@tailrec
|
||||
def loop(numberString: String, repeat: Char, times: Int): String =
|
||||
if (numberString.isEmpty) result.toString()
|
||||
else if (numberString.head != repeat) {
|
||||
result.append(times).append(repeat)
|
||||
loop(numberString.tail, numberString.head, 1)
|
||||
} else loop(numberString.tail, numberString.head, times + 1)
|
||||
|
||||
loop(number.tail + " ", number.head, 1)
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,17 +1,11 @@
|
|||
object Main extends App {
|
||||
|
||||
def lookAndSay(previous: List[BigInt]): Stream[List[BigInt]] = {
|
||||
|
||||
def next(num: List[BigInt]): List[BigInt] = num match {
|
||||
case Nil => Nil
|
||||
case head :: Nil => 1 :: head :: Nil
|
||||
case head :: tail =>
|
||||
val size = (num takeWhile (_ == head)).size
|
||||
List(BigInt(size), head) ::: next(num.drop(size))
|
||||
}
|
||||
val x = next(previous)
|
||||
x #:: lookAndSay(x)
|
||||
def lookAndSay(seed: BigInt) = {
|
||||
val s = seed.toString
|
||||
( 1 until s.size).foldLeft((1, s(0), new StringBuilder)) {
|
||||
case ((len, c, sb), index) if c != s(index) => sb.append(len); sb.append(c); (1, s(index), sb)
|
||||
case ((len, c, sb), _) => (len + 1, c, sb)
|
||||
} match {
|
||||
case (len, c, sb) => sb.append(len); sb.append(c); BigInt(sb.toString)
|
||||
}
|
||||
|
||||
(lookAndSay(1 :: Nil) take 10).foreach(s => println(s.mkString("")))
|
||||
}
|
||||
|
||||
def lookAndSayIterator(seed: BigInt) = Iterator.iterate(seed)(lookAndSay)
|
||||
|
|
|
|||
|
|
@ -0,0 +1,17 @@
|
|||
object Main extends App {
|
||||
|
||||
def lookAndSay(previous: List[BigInt]): Stream[List[BigInt]] = {
|
||||
|
||||
def next(num: List[BigInt]): List[BigInt] = num match {
|
||||
case Nil => Nil
|
||||
case head :: Nil => 1 :: head :: Nil
|
||||
case head :: tail =>
|
||||
val size = (num takeWhile (_ == head)).size
|
||||
List(BigInt(size), head) ::: next(num.drop(size))
|
||||
}
|
||||
val x = next(previous)
|
||||
x #:: lookAndSay(x)
|
||||
}
|
||||
|
||||
(lookAndSay(1 :: Nil) take 10).foreach(s => println(s.mkString("")))
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue