▲ | bluGill 5 days ago | ||||||||||||||||||||||||||||
I could probably do it - but you wouldn't like it. My dictionaries would be a linked-list, looking for a key becomes a linear search... (if you gave me C++ I'd use std::map) I'm assuming you will allow me to use the C standard library, if I have to implement strlen or malloc in that 500 lines of C I'm not sure I can pull that off. 500 lines is aggressive, but IOCCC gives me plenty of tricks to get the line count down and the language isn't that big. I'm also going to assume 100% valid python code is fed in, if there is a bug or error of any sort that is undefined behavior. Note that most of what makes python great isn't the language, it is the library. I believe that large parts of the python library are also written in C (for speed), and thus you won't be able to use my 500 line python compiler for anything useful because you won't have any useful libraries. | |||||||||||||||||||||||||||||
▲ | dekhn 5 days ago | parent | next [-] | ||||||||||||||||||||||||||||
A hash table in C is about 30 lines of code, so I don't think you have to stick to linked lists for dictionaries. | |||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||
▲ | jlokier 4 days ago | parent | prev [-] | ||||||||||||||||||||||||||||
If you can search (and do other operations) on a linked list, and the lists are long enough to matter, you can trivially speed it up to a fixed-size hash table with hardly any code changes. This:
becomes:
This:
becomes:
etc.A simple hash(), not high quality but good enough for non-adversarial inputs, can be a one-liner. A good dictionary does more than this of course, especially dynamic sizing, but this simple change can radically speed up simple linked list code when the lists are long, you don't have a hash table implemention you can use, and you want to keep the code change very small. The same principle can be used with other things than lists too. | |||||||||||||||||||||||||||||
|