started at ; stage 2 at ; ended at
phonebooks are a thing of the past. implement an associative array. programs can be written in python, haskell, ocaml, or f#.
an associative array, also known as a map or dictionary and often implemented as a hash table or search tree, is a type of collection that associates unique keys with values, allowing fast lookup based on these keys.
the required interface this round consists of 3 functions: creating a new object, looking up an entry and inserting a new entry. if lookup fails, return a special error value (detailed in the APIs section). inserting should overwrite any existing entry in the array.
keys are guaranteed to be equatable. you may also impose other restrictions on the key; for example, a hash table may require that the keys are hashable, or a tree may require they can be ordered.
APIs:
Entry
. Entry()
should create a new empty array with methods get(key, default=None)
and insert(key, value)
. get
returns default
if the key is missing.Entry k a
and functions empty :: Entry k a
, lookup :: k -> Entry k a -> Maybe a
, and insert :: k -> a -> Entry k a -> Entry k a
.Map
. Entry.Make(M)
should have definitions type key = M.t
, type 'a t
, val empty : 'a t
, val find_opt : key -> 'a t -> 'a option
, and val add : key -> 'a -> 'a t -> 'a t
.Entry<'Key, 'Value>
. Entry()
should construct a new empty array with methods e.TryFind(key: 'Key) : 'Value option
and e.Add(key: 'Key, value: 'Value) : Entry<'Key, 'Value>
.you can download all the entries
written by Olivia
submitted at
5 likes
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | "https://cg.esolangs.gay/24";API:"Entry";t=lambda*a,**f:type("",(),dict(a)|f);( l:=lambda*k:lambda*f:lambda*a:f[0](**dict(zip(k,a))));i=l("x")(lambda**_:_["x"] );c=l(*"fg")(lambda**_:lambda*a:_["f"](_["g"](*a)));C=lambda *f:__import__("f"+ "unctools").reduce(c,f,i);d=lambda**f:__import__( "datacla" "sses").dataclass(t (("__annotations__", dict(filter(C( bool,getattr(1 ,"__" "sub__"),callable,i, __import__("operator") .itemgetter (1)),f.items())) ), **f));s=l("n")(lambda **_:l("f")(lambda**___: lambda*aa :___["f"](*aa[_["n" ]:])) );D=l(*"n","f" )(lambda**_:i( lambda*d :''or (lambda*a:_[ "f"](*[* a,*d ][:_["n"]]))));( g:=l(*"nk","d")( lambda* __ ,**_:(_[ "d"]) if(_ ["n" ]==None)else(g(_[ "n"].l,_["k"],_[0 or"d"]) if((_[ "" "k"]<_[ '' "n" ].k))else(g(_["n"] .r,_["k"],_["\x64" ])if(( _["n"].k <_["k"]) + 10 ==0xa)else(_["n"].v) ))));u=l(*"spnikv"* int(i (1))) (lambda** _:setattr((_["p"])or(_ ["s"]),"rl"[_["i"]], N(_[ " k".strip ( "\40")],_["v"]))if(_["n"] ==None)else(u(_["s"],_[ "" "n"],_["n" ].l,1,_["k"],_["v"])if(_["\ k"]<_["""n"""""].k)else(u (_ ["\s"],_[ "n"],_["n"].r,False,_["k"],_ ["v"])if(_["n"].k<_["k"]or( _[ "n"].k <_["n"].k))else(setattr(_["n"] ,"v",_["v"])))));N=d(k="??",v = "!!" ,l=None,r=None);Entry=d(r=None, get=D(3,l(*"skd")(lambda**_:g(_ [ "" "s"].r,_["k"],_["d"],)))(None,), insert=l("s",*"kv")(lambda**_:u(_ ["s"],None,_["s"].r,False,_["k"], _["v"])),__str__=l("s")(lambda**_ :"".join(map(str,filter(None,(((( _["s"].r,)))))))));N.__str__=l("s") (lambda**_:"".join(map(["({})","{" "}","({})"].__getitem__,J:=list(map ((K:=list(map(getattr(_["s"],"__ge" "tattribute__"),"lvr"))).index,i(i( i( i (filter)(None,K)))))))).format (*map(K.__getitem__,J)));HEY_THERE, ="LOST_EVERYTHING_YOU_LOVED?",;( __annotations__:=t(__setitem__= s (1)(l("k","v")(lambda**_:globals(). __setitem__(_["k"],_["v"]))))());[ *WELL_DO_I_HAVE_A_DEAL_FOR_YOU,]="\ \U0001F4C8";YOU_LITTLE_SPONGE:"JUST COMMENT_ON_THIS_ENTRY_SAYING_THIS:" '''i'm...a,BIG-SHOT''';__debug__== False==print("run","unoptimised"); JUST_DO_THAT_ONE_THING,AND,VOILA, *_="EVERYTHING_WILL_BE_SOLVED","\ THEY_WILL_LAUGH_AT_YOUR_JOKES ","YOU_WILL_NOT_BE_A_FAILURE"; |
written by LyricLy
submitted at
3 likes
1 2 3 4 5 6 7 8 9 10 11 12 13 | {-# LANGUAGE RankNTypes #-} module Entry where import Johnson type Entry k a = k -> (forall b. b -> (a -> b) -> b) round and round the merry go = round empty = sonj lookup = johnso johnso Just . johnso johnso Nothing . johnso ($) insert k v m = ohnsonj (\x -> if x == k then const else const id) v m |
1 2 3 4 5 6 7 8 | module Johnson where j o hns = o n j ohnson johnson = johnson j ohnson _ johnso _ njohnson = johnso njohnson johnso n john sonj = ohnson j (ohnson johnso n john sonj) ohnson john sonj = ohnson () j ohnson j ohnsonj oh ns on j = ohnson j (ohnson j (ohnson j oh (ns, on) j) () (ohnson (j, oh) n (sonj, oh) ns)) (on, j) (ohnson (j, oh, ns) on () j) |
written by olus2000
submitted at
2 likes
Oh for fucks sake. Feature request: preview which files I uploaded before stage 2 starts.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | (* // Code by SoundOfSpouting#6980 (UID: 151149148639330304) *) (* The constructor is `Entry` instead of `Entry()`, otherwise as required *) type Color = Red | Black type Entry<'Key, 'Value when 'Key: comparison> = | Empty | Tree of smaller: Entry<'Key, 'Value> * key: 'Key * value: 'Value * bigger: Entry<'Key, 'Value> * color: Color member this.TryFind (key: 'Key) : 'Value option = match this with | Empty -> None | Tree (s, k, v, b, _) -> if k = key then Some v else if key > k then b.TryFind key else s.TryFind key member this.Add (key : 'Key) (value: 'Value) : Entry<'Key, 'Value> = match this.ChildAdd key value with | Tree (s, k, v, b, Red) -> Tree (s, k, v, b, Black) | other -> other member private this.ChildAdd (key : 'Key) (value: 'Value) : Entry<'Key, 'Value> = match this with | Empty -> Tree (Empty, key, value, Empty, Red) | Tree (s, k, v, b, c) -> if k = key then Tree (s, key, value, b, c) else ( if key > k then Tree (s, k, v, b.ChildAdd key value, c) else Tree (s.ChildAdd key value, k, v, b, c) ).rebalance member private this.rebalance : Entry<'Key, 'Value> = match this with | Tree (Tree (Tree (a, x, xv, b, Red), y, yv, c, Red), z, zv, d, Black) | Tree (Tree (a, x, xv, Tree (b, y, yv, c, Red), Red), z, zv, d, Black) | Tree (a, x, xv, Tree (Tree (b, y, yv, c, Red), z, zv, d, Red), Black) | Tree (a, x, xv, Tree (b, y, yv, Tree (c, z, zv, d, Red), Red), Black) -> Tree (Tree (a, x, xv, b, Black), y, yv, Tree(c, z, zv, d, Black), Red) | other -> other |
1 | -- // Code by SoundOfSpouting#6980 (UID: 151149148639330304)
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | (* // Code by SoundOfSpouting#6980 (UID: 151149148639330304) *) module type OrderedType = sig type t val compare: t -> t -> int end module type S = sig type key type +'a t val empty: 'a t val find_opt: key -> 'a t -> 'a option val add: key -> 'a -> 'a t -> 'a t end module Make(Ord: OrderedType) = struct type key = Ord.t type 'a t = (key * 'a) list let empty = [] let rec find_opt x = function [] -> None | (k, v) :: r -> let c = Ord.compare x k in if c = 0 then Some v else find_opt x r let add x data assoc = (x, data) :: assoc end |
1 2 3 4 5 | # // Code by SoundOfSpouting#6980 (UID: 151149148639330304) class Entry(dict): def insert(self, key, val): self[key] = val |
written by pii
submitted at
3 likes
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 | import os, json, ntpath, shutil class Node(): def __init__(self, key, data, path): self.key = key # must be a string self.data = data self.path = path def getLeft(self): if os.path.isdir(self.path + "left/"): p = self.path + "left/" f = [x for x in os.listdir(p) if x.endswith(".json")][0] obj = json.load(open(f)) node = Node(obj["key"], obj["data"], obj["path"]) return node return None def getRight(self): if os.path.isdir(self.path + "right/"): p = self.path + "right/" f = [x for x in os.listdir(p) if x.endswith(".json")][0] obj = json.load(open(f)) node = Node(obj["key"], obj["data"], obj["path"]) return node return None def getParent(self): p = self.path.split('/')[1:-1] if p[-1] != "tree": p = f"/{'/'.join(p[:-1])}/" f = [x for x in os.listdir(p) if x.endswith(".json")][0] obj = json.load(open(f)) node = Node(obj["key"], obj["data"], obj["path"]) return node return None def write(self): if not os.path.isdir(self.path): os.makedirs(self.path, exist_ok=True) f = open(self.path + self.key + ".json", "w+") payload = {"path": self.path, "key": self.key, "data": self.data} json.dump(payload, f) f.close() class Entry: def __init__(self): pass def get(self, key, default=None): path = os.path.dirname(os.path.realpath(__file__)) + "/tree/" while 1: if not os.path.isdir(path): break else: data = path + [x for x in os.listdir(path) if x.endswith(".json")][0] fkey = ntpath.basename(data)[:-5] if fkey == key: obj = json.load(open(data)) return obj["data"] elif key >= fkey: path += "right/" else: path += "left/" return default def insert(self, key, value): path = os.path.dirname(os.path.realpath(__file__)) + "/tree/" while 1: if not os.path.isdir(path) or not len(ld := os.listdir(path)): node = Node(key, value, path) node.write() return self else: data = path + [x for x in ld if x.endswith(".json")][0] fkey = ntpath.basename(data)[:-5] if fkey == key: node = Node(key, value, path) node.write() return self elif key >= fkey: path += "right/" else: path += "left/" def clean(self): # erases the "memory" path = os.path.dirname(os.path.realpath(__file__)) + "/tree/" shutil.rmtree(path) os.mkdir(path) |
1 | cg: couldn't decode file contents |
written by BeatButton
submitted at
2 likes
1 2 3 4 5 6 7 8 9 | Entry = type( 'Entry', (), dict( __init__=lambda self: (setattr(self, 'entries', list()), setattr(self, 'keys', dict())) and None, get=lambda self, key, default=None: self.entries.__getitem__(self.keys.__getitem__(key)) if key in self.keys else default, insert=lambda self, key, value: (lambda: self.entries.__setitem__(self.keys[key], value), lambda: (self.keys.__setitem__(key, len(self.entries)), self.entries.append(value)) and None)[0 if key in self.keys else 1](), ), ) |
written by razetime
submitted at
1 like
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | class Entry: def __init__(self): self.keys = [] self.values = [] def get(self, key, default=None): for i in range(len(self.keys)): if len(str(self.keys[i])) == len(str(key)): if self.keys[i] == key: return self.values[i] return default def insert(self, key, value): if key in self.keys: self.values[self.keys.index(key)] = value else: self.keys.append(key) self.values.append(value) |
written by Olive
submitted at
2 likes
1 2 3 4 5 6 7 8 9 10 11 12 | class Entry: '''An associative array. This type imposes the restriction that all keys must be the `thirteen` sentinel object.''' thirteen = object() def __init__(self): self.maybe = False def get(self, key, default=None): return self.value if self.maybe and key is self.thirteen else default def insert(self, key, value): if key is not self.thirteen: raise KeyError self.maybe = True self.value = value |
written by soup girl
submitted at
1 like
1 2 3 4 5 6 7 8 9 10 11 12 | module Entry where import Prelude hiding (lookup) data Entry by lyric = The | Lyric by lyric (Entry by lyric) lookup at The = Nothing lookup to (Lyric is not lyric) = if is == to then Just not else lookup to lyric empty = The insert = Lyric |
i'm...a,BIG-SHOT
i'm...a,BIG-SHOT
i'm...a,BIG-SHOT
i'm...a,BIG-SHOT
i'm...a,BIG-SHOT
i'm...a,BIG-SHOT
i'm...a,BIG-SHOT
post a comment