September Morn Update

This commit is contained in:
Ingy döt Net 2019-09-12 10:33:56 -07:00
parent 4e2d22a71d
commit aac6731f2c
6856 changed files with 141342 additions and 21127 deletions

View file

@ -0,0 +1,77 @@
function FBString(lon, cad$)
# Definimos la función String en BASIC256
cadena$ = ""
for a = 1 to lon
cadena$ += cad$
next a
return cadena$
end function
function RLDecode(i$)
rCount$ = "" : outP$ = ""
for Loop0 = 1 to length(i$)
m$ = mid(i$, Loop0, 1)
begin case
case m$ = "0"
rCount$ += m$
case m$ = "1"
rCount$ += m$
case m$ = "2"
rCount$ += m$
case m$ = "3"
rCount$ += m$
case m$ = "4"
rCount$ += m$
case m$ = "5"
rCount$ += m$
case m$ = "6"
rCount$ += m$
case m$ = "7"
rCount$ += m$
case m$ = "8"
rCount$ += m$
case m$ = "9"
rCount$ += m$
else
if length(rCount$) then
outP$ += FBString(int(rCount$), m$)
rCount$ = ""
else
outP$ += m$
end if
end case
next Loop0
RLDecode = outP$
end function
function RLEncode(i$)
outP$ = ""
tmp1 = mid(i$, 1, 1)
tmp2 = tmp1
rCount = 1
for Loop0 = 2 to length(i$)
tmp1 = mid(i$, Loop0, 1)
if tmp1 <> tmp2 then
outP$ += string(rCount) + tmp2
tmp2 = tmp1
rCount = 1
else
rCount += 1
end if
next Loop0
outP$ += replace(string(rCount)," ", "")
outP$ += tmp2
RLEncode = outP$
end function
input "Type something: ", initial
encoded$ = RLEncode(initial)
decoded$ = RLDecode(encoded$)
print initial
print encoded$
print decoded$
end

View file

@ -3,7 +3,7 @@
typedef struct stream_t stream_t, *stream;
struct stream_t {
/* get funciton is supposed to return a byte value (0-255),
/* get function is supposed to return a byte value (0-255),
or -1 to signify end of input */
int (*get)(stream);
/* put function does output, one byte at a time */

View file

@ -1,63 +1,66 @@
import system'text.
import system'routines.
import extensions.
import system'text;
import system'routines;
import extensions;
import extensions'text;
singleton compressor
{
literal compress(literal s)
[
type<TextBuilder> tb := TextBuilder new.
int count := 0.
char current := s[0].
s forEach(:ch)
[
string compress(string s)
{
auto tb := new TextBuilder();
int count := 0;
char current := s[0];
s.forEach:(ch)
{
if (ch == current)
[
{
count += 1
];
[
tb writeFormatted("{0}{1}",count,current).
count := 1.
}
else
{
tb.writeFormatted("{0}{1}",count,current);
count := 1;
current := ch
]
].
}
};
tb writeFormatted("{0}{1}",count,current).
tb.writeFormatted("{0}{1}",count,current);
^ tb
]
}
literal decompress(literal s)
[
type<TextBuilder> tb := TextBuilder new.
char current := $0.
var a := String new.
s forEach(:ch)
[
current := ch.
if (current isDigit)
[
a append(ch)
];
[
int count := a toInt.
a clear.
string decompress(string s)
{
auto tb := new TextBuilder();
char current := $0;
var a := new StringWriter();
s.forEach:(ch)
{
current := ch;
if (current.isDigit())
{
a.append(ch)
}
else
{
int count := a.toInt();
a.clear();
tb fill(current,count).
]
].
tb.fill(current,count)
}
};
^ tb
]
}
}
program =
[
var s := "WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWBWWWWWWWWWWWWWW".
public program()
{
var s := "WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWBWWWWWWWWWWWWWW";
s := compressor compress(s).
console printLine(s).
s := compressor.compress(s);
console.printLine(s);
s := compressor decompress(s).
console printLine(s).
].
s := compressor.decompress(s);
console.printLine(s)
}

View file

@ -0,0 +1,18 @@
(defun run-length-encode (str_arg)
"Return the run length encoding of a string argument STR_ARG."
(let ((dalist (string-to-list str_arg)) (letters) (frequency))
(dolist (element dalist)
(if (not (equal (car letters) element))
(progn
(setq letters (cons element letters))
(setq frequency (cons 1 frequency)))
(setq frequency (cons (+ (car frequency ) 1) (cdr frequency)))))
(apply 'concat
(reverse
(mapcar*
(lambda (x y) (concat (number-to-string x)
(char-to-string y)))
frequency letters)))))

View file

@ -0,0 +1,54 @@
Dim As String initial, encoded, decoded
Function RLDecode(i As String) As String
Dim As Long Loop0
dim as string rCount, outP, m
For Loop0 = 1 To Len(i)
m = Mid(i, Loop0, 1)
Select Case m
Case "0" To "9"
rCount += m
Case Else
If Len(rCount) Then
outP += String(Val(rCount), m)
rCount = ""
Else
outP += m
End If
End Select
Next
RLDecode = outP
End Function
Function RLEncode(i As String) As String
Dim As String tmp1, tmp2, outP
Dim As Long Loop0, rCount
tmp1 = Mid(i, 1, 1)
tmp2 = tmp1
rCount = 1
For Loop0 = 2 To Len(i)
tmp1 = Mid(i, Loop0, 1)
If tmp1 <> tmp2 Then
outP += Ltrim(Rtrim(Str(rCount))) + tmp2
tmp2 = tmp1
rCount = 1
Else
rCount += 1
End If
Next
outP += Ltrim(Rtrim(Str(rCount)))
outP += tmp2
RLEncode = outP
End Function
Input "Type something: ", initial
encoded = RLEncode(initial)
decoded = RLDecode(encoded)
Print initial
Print encoded
Print decoded
End

View file

@ -1,12 +1,18 @@
<?php
function encode($str) {
return preg_replace('/(.)\1*/e', 'strlen($0) . $1', $str);
function encode($str)
{
return preg_replace_callback('/(.)\1*/', function ($match) {
return strlen($match[0]) . $match[1];
}, $str);
}
function decode($str) {
return preg_replace('/(\d+)(\D)/e', 'str_repeat($2, $1)', $str);
function decode($str)
{
return preg_replace_callback('/(\d+)(\D)/', function($match) {
return str_repeat($match[2], $match[1]);
}, $str);
}
echo encode('WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWBWWWWWWWWWWWWWW'), "\n";
echo decode('12W1B12W3B24W1B14W'), "\n";
echo encode('WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWBWWWWWWWWWWWWWW'), PHP_EOL;
echo decode('12W1B12W3B24W1B14W'), PHP_EOL;
?>

View file

@ -0,0 +1,34 @@
|compress decompress|
compress := [:string |
String streamContents:[:out |
|count prev|
count := 0.
(string,'*') "trick to avoid final run handling in loop"
inject:nil
into:[:prevChar :ch |
ch ~= prevChar ifTrue:[
count = 0 ifFalse:[
count printOn:out.
out nextPut:prevChar.
count := 0.
].
].
count := count + 1.
ch
]
]
].
decompress := [:string |
String streamContents:[:out |
string readingStreamDo:[:in |
[in atEnd] whileFalse:[
|n ch|
n := Integer readFrom:in.
ch := in next.
out next:n put:ch.
]
]
].
].

View file

@ -0,0 +1,5 @@
compress value:'WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWBWWWWWWWWWWWWWW'
-> '12W1B12W3B24W1B14W'
decompress value:'12W1B12W3B24W1B14W'
-> 'WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWBWWWWWWWWWWWWWW'

View file

@ -0,0 +1,4 @@
compress := [:string |
String streamContents:[:out |
string asRunArray runsDo:[:count :char |
count printOn:out. out nextPut:char]]].