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 @@
--- {}

View file

@ -1,21 +1,15 @@
with Ada.Containers.Ordered_Sets;
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Containers.Ordered_Sets, Ada.Text_IO;
use Ada.Text_IO;
procedure Unique_Set is
package Int_Sets is new Ada.Containers.Ordered_Sets(Integer);
use Int_Sets;
Nums : array (Natural range <>) of Integer := (1,2,3,4,5,5,6,7,1);
Unique : Set;
Set_Cur : Cursor;
Success : Boolean;
procedure Duplicate is
package Int_Sets is new Ada.Containers.Ordered_Sets (Integer);
Nums : constant array (Natural range <>) of Integer := (1,2,3,4,5,5,6,7,1);
Unique : Int_Sets.Set;
begin
for I in Nums'range loop
Unique.Insert(Nums(I), Set_Cur, Success);
end loop;
Set_Cur := Unique.First;
loop
Put_Line(Item => Integer'Image(Element(Set_Cur)));
exit when Set_Cur = Unique.Last;
Set_Cur := Next(Set_Cur);
end loop;
end Unique_Set;
for n of Nums loop
Unique.Include (n);
end loop;
for e of Unique loop
Put (e'img);
end loop;
end Duplicate;

View file

@ -1,10 +1,6 @@
integer a;
index x;
list l;
l = list(8, 2, 1, 8, 2, 1, 4, 8);
for (, a in l) {
for (, integer a in list(8, 2, 1, 8, 2, 1, 4, 8)) {
if ((x[a] += 1) == 1) {
o_(" ", a);
}

View file

@ -1,13 +1,13 @@
import extensions.
import system'collections.
import system'routines.
import extensions;
import system'collections;
import system'routines;
program =
[
var nums := (1,1,2,3,4,4).
dictionary unique := Dictionary new.
public program()
{
var nums := new int[]{1,1,2,3,4,4};
auto unique := new Map<int, int>();
nums forEach(:n)[ unique[n] := n ].
nums.forEach:(n){ unique[n] := n };
console printLine(unique).
].
console.printLine(unique.MapValues.asEnumerable())
}

View file

@ -0,0 +1,24 @@
/**
Remove duplicate elements, in Neko
*/
var original = $array(1, 2, 1, 4, 5, 2, 15, 1, 3, 4)
/* Create a table with only unique elements from the array */
var dedup = function(a) {
var size = $asize(a)
var hash = $hnew(size)
while size > 0 {
var v = a[size - 1]
var k = $hkey(v)
$hset(hash, k, v, null)
size -= 1
}
return hash
}
/* Show the original list and the unique values */
$print(original, "\n")
var show = function(k, v) $print(v, " ")
$hiter(dedup(original), show)
$print("\n")

View file

@ -0,0 +1,22 @@
from itertools import (groupby)
# nubByKey :: (a -> b) -> [a] -> [a]
def nubByKey(k, xs):
return list(list(v)[0] for _, v in groupby(sorted(xs, key=k), key=k))
xs = [
'apple', 'apple',
'ampersand', 'aPPLE', 'Apple',
'orange', 'ORANGE', 'Orange', 'orange', 'apple'
]
for k in [
id, # default case sensitive uniqueness
lambda x: x.lower(), # case-insensitive uniqueness
lambda x: x[0], # unique first character (case-sensitive)
lambda x: x[0].lower(), # unique first character (case-insensitive)
]:
print (
nubByKey(k, xs)
)

View file

@ -0,0 +1,38 @@
# nubByEq :: (a -> a -> Bool) -> [a] -> [a]
def nubByEq(eq, xs):
def go(yys, xxs):
if yys:
y = yys[0]
ys = yys[1:]
return go(ys, xxs) if (
elemBy(eq, y, xxs)
) else (
[y] + go(ys, [y] + xxs)
)
else:
return []
return go(xs, [])
# elemBy :: (a -> a -> Bool) -> a -> [a] -> Bool
def elemBy(eq, x, xs):
if xs:
return eq(x, xs[0]) or elemBy(eq, x, xs[1:])
else:
return False
xs = [
'apple', 'apple',
'ampersand', 'aPPLE', 'Apple',
'orange', 'ORANGE', 'Orange', 'orange', 'apple'
]
for eq in [
lambda a, b: a == b, # default case sensitive uniqueness
lambda a, b: a.lower() == b.lower(), # case-insensitive uniqueness
lambda a, b: a[0] == b[0], # unique first char (case-sensitive)
lambda a, b: a[0].lower() == b[0].lower(), # unique first char (any case)
]:
print (
nubByEq(eq, xs)
)

View file

@ -0,0 +1,14 @@
# nubBy :: (a -> a -> Bool) -> [a] -> [a]
def nubBy(p, xs):
def go(xs):
if xs:
x = xs[0]
return [x] + go(
list(filter(
lambda y: not p(x, y),
xs[1:]
))
)
else:
return []
return go(xs)

View file

@ -0,0 +1,2 @@
items <- c(1,2,3,2,4,3,2)
unique (items)

View file

@ -3,10 +3,9 @@ $= '2 3 5 7 11 13 17 19 cats 222 -100.2 +11 1.1 +7 7. 7 5 5 3 2 0 4.4 2' /*it
say 'original list:' $
say right( words($), 17, '') 'words in the original list.'
z=; @.= /*initialize the NEW list & index list.*/
do j=1 for words($); y=word($,j) /*process the words (items) in the list*/
if @.y=='' then z=z y; @.y=. /*Not duplicated? Add to Z list,@ array*/
do j=1 for words($); y= word($, j) /*process the words (items) in the list*/
if @.y=='' then z= z y; @.y= . /*Not duplicated? Add to Z list,@ array*/
end /*j*/
say
say 'modified list:' space(z)
say 'modified list:' space(z) /*stick a fork in it, we're all done. */
say right( words(z), 17, '') 'words in the modified list.'
/*stick a fork in it, we're all done. */

View file

@ -2,11 +2,10 @@
$= '2 3 5 7 11 13 17 19 cats 222 -100.2 +11 1.1 +7 7. 7 5 5 3 2 0 4.4 2' /*item list.*/
say 'original list:' $
say right( words($), 17, '') 'words in the original list.'
#=words($) /*process all the words in the list. */
do j=# by -1 for #; y=word($, j) /*get right-to-Left. */
_=wordpos(y, $, j + 1); if _\==0 then $=delword($,_,1) /*Dup? Then delete it*/
end /*j*/
#= words($) /*process all the words in the list. */
do j=# by -1 for #; y= word($, j) /*get right-to-Left. */
_= wordpos(y, $, j + 1); if _\==0 then $= delword($, _, 1) /*Dup? Then delete it*/
end /*j*/
say
say 'modified list:' space($)
say 'modified list:' space($) /*stick a fork in it, we're all done. */
say right( words(z), 17, '') 'words in the modified list.'
/*stick a fork in it, we're all done. */

View file

@ -1,12 +1,11 @@
/*REXX program removes any duplicate elements (items) that are in a list (using 2 lists)*/
old = '2 3 5 7 11 13 17 19 cats 222 -100.2 +11 1.1 +7 7. 7 5 5 3 2 0 4.4 2'
say 'original list:' old
say right( words(old), 17, '') 'words in the original list.'
say right( words(old), 17, '') 'words in the original list.'
new= /*start with a clean (list) slate. */
do j=1 for words(old); _=word(old, j) /*process the words in the OLD list. */
if wordpos(_,new)==0 then new=new _ /*Doesn't exist? Then add word to NEW.*/
end /*j*/
do j=1 for words(old); _= word(old, j) /*process the words in the OLD list. */
if wordpos(_, new)==0 then new= new _ /*Doesn't exist? Then add word to NEW.*/
end /*j*/
say
say 'modified list:' space(new)
say right( words(new), 17, '') 'words in the modified list.'
/*stick a fork in it, we're all done. */
say 'modified list:' space(new) /*stick a fork in it, we're all done. */
say right( words(new), 17, '') 'words in the modified list.'