Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
|
|
@ -0,0 +1,2 @@
|
|||
items = [1, 2, 3, 'a', 'b', 'c', 2, 3, 4, 'b', 'c', 'd']
|
||||
unique = list(set(items))
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
items = [1, 2, 3, 'a', 'b', 'c', 2, 3, 4, 'b', 'c', 'd']
|
||||
unique = []
|
||||
helperset = set()
|
||||
for x in items:
|
||||
if x not in helperset:
|
||||
unique.append(x)
|
||||
helperset.add(x)
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
import itertools
|
||||
items = [1, 2, 3, 'a', 'b', 'c', 2, 3, 4, 'b', 'c', 'd']
|
||||
unique = [k for k,g in itertools.groupby(sorted(items))]
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
items = [1, 2, 3, 'a', 'b', 'c', 2, 3, 4, 'b', 'c', 'd']
|
||||
unique = []
|
||||
for x in items:
|
||||
if x not in unique:
|
||||
unique.append(x)
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
from collections import OrderedDict as od
|
||||
|
||||
print(list(od.fromkeys([1, 2, 3, 'a', 'b', 'c', 2, 3, 4, 'b', 'c', 'd']).keys()))
|
||||
|
|
@ -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)
|
||||
)
|
||||
|
|
@ -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)
|
||||
)
|
||||
|
|
@ -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)
|
||||
Loading…
Add table
Add a link
Reference in a new issue