The Big Programming Thread - Page 932
Forum Index > General Forum |
Thread Rules 1. This is not a "do my homework for me" thread. If you have specific questions, ask, but don't post an assignment or homework problem and expect an exact solution. 2. No recruiting for your cockamamie projects (you won't replace facebook with 3 dudes you found on the internet and $20) 3. If you can't articulate why a language is bad, don't start slinging shit about it. Just remember that nothing is worse than making CSS IE6 compatible. 4. Use [code] tags to format code blocks. | ||
WarSame
Canada1950 Posts
| ||
Thaniri
1264 Posts
Fuck YAML. Every new software these days uses YAML config files and I have to write puppet code to feed settings into templates that will eventually be translated into YAML format cause standardization. At least its not XML. Meaningful whitespace makes me want to do hyperbolic things that I wouldn't actually do. YAML isn't human readable. JSON is. Give me braces. I don't care about your 2 space rule. I'll use 500 spaces if I want in JSON. Pro tip: I won't since JSON can be minified. But minified JSON is unreadable to humans? Then use jq to unminify it if you need to. For large scale parsing, YAML is actually slower than JSON. If I'm pushing code out to a $LARGE_NUMBER of nodes it actually adds up. What do I get in exchange? Comments? RTFM, YAML isn't meant for programming. Multi parent inheritance? I thought the big meme these days was that inheritance is Satan reincarnate. edit: Merry Christmas everyone! Wish everyone the best on their projects in the new year. | ||
TMG26
Portugal2017 Posts
| ||
Deleted User 3420
24492 Posts
Anyways back to the question Is it faster to: a.) insert into the list in order(by element 1). So, for each inserted element, I can do a O(logk) insertion where k is the number of elements currently in the list, putting it into a sorted position. Then, if there is a duplicate, It will be very nearby, so I can check nearby positions for duplicates. I could make it even faster by doing something like maintaining minimum, maximum, mean of the list or w/e. But that's getting pretty fancy. b.) just insert into the list with no regard for order and then sort it at the end(maybe radix sort). So, speed is more of a concern than memory, but both are important. I know a) will be better for memory.. but I am unsure if it would be better for speed. Any thoughts/ideas? edit: a final edit. I don't think bucket sort is feasible because I don't know about the distribution of the values. | ||
![]()
tofucake
Hyrule18982 Posts
YAML is good at what it does Why do you have to write any code to interact with yaml? Use one of the bajillion standard implementations | ||
Manit0u
Poland17203 Posts
On December 25 2017 10:36 tofucake wrote: JSON is for data transport, not human oriented configuration YAML is good at what it does Why do you have to write any code to interact with yaml? Use one of the bajillion standard implementations Seconded. YAML.parse (or whatever equivalent) will give you a hash, which makes it no different from the usual JSON.parse. What I like about YAML for config files is that it's readable, concise and has some cool features (like anchors and aliases to dry up your config).
If your nodes share common subnodes you can easily alias and reference them. Very neat if you ask me, | ||
phar
United States1080 Posts
On December 25 2017 08:46 travis wrote: Let's saying that I am making very long lists(possibly length of millions) from pairs of pseudo-random values, inserting each element one a time. In the end, I want lists without duplicates of the first value in the pairs. Which duplicate is removed is based on the value of the 2nd element. So, I can't use a set because it won't recognize that (8,31) and (8,224) are duplicates and that (8,31) should be removed because 224 > 31. I also don't think a hash is practical because, while I said the values are pseudo random, I actually will only ever get one duplicate for each respective pair. So... I would be making a huge hash table.. when memory is already becoming a problem. [quick side note, if anyone *did* know how to use a set or set like structure to accomplish this, I would be in love with them] Anyways back to the question Is it faster to: a.) insert into the list in order(by element 1). So, for each inserted element, I can do a O(logk) insertion where k is the number of elements currently in the list, putting it into a sorted position. Then, if there is a duplicate, It will be very nearby, so I can check nearby positions for duplicates. I could make it even faster by doing something like maintaining minimum, maximum, mean of the list or w/e. But that's getting pretty fancy. b.) just insert into the list with no regard for order and then sort it at the end(maybe radix sort). So, speed is more of a concern than memory, but both are important. I know a) will be better for memory.. but I am unsure if it would be better for speed. Any thoughts/ideas? edit: a final edit. I don't think bucket sort is feasible because I don't know about the distribution of the values. Does input order have to affect output order, or can input be unstable? If input can be unstable this is trivially accomplished in linear space and constant time for insertion by using a list and map. Your concerns on the size of the hash table sound unreasonable for input ~millions. | ||
dekibeki
Australia34 Posts
On December 25 2017 16:55 phar wrote: Does input order have to affect output order, or can input be unstable? If input can be unstable this is trivially accomplished in linear space and constant time for insertion by using a list and map. Your concerns on the size of the hash table sound unreasonable for input ~millions. Even if input order has to be respected, you can do the same thing. Each Pair becomes a node (<A,B>) in an intrusive doubly linked list. The map maps A -> <A,B>.
C++ example code + Show Spoiler +
You can do some fancier stuff to save memory, but that's the basic idea. Edit: For production, use the boost intrusive list, instead of the hand rolled one | ||
TMG26
Portugal2017 Posts
In order insert faster than a list! And when you detect the duplicate if it's the new value, don't save it. It it's the one already there, swap it. You can also implement an heap/tree in an array instead of dynamic memory allocation . Especilly usefull for huge ammounts of data that you already know how much size you will need. No need to storing pointers if it is all continuous memory in an array. Lists are terribly slow for your problem. Discard them. You can also use a hash table! It's even faster than the heap, if you don't need them sorted at the end. On the jash table you can use the 1st value for the hash code. Implementing the data structure for your poblem in C would be faster. But it's really simple using the generics collections in java by using you own comparator. But the overhead is bigger, and it seems that you are wrring shit around the promblem instead of actually solving he problem. You can also use haskell for a really elegant solution, but I don't remember how it would be stored in memory, and since the size is huge you should just go with C so you understand the problem and the common data structures better with performance/space. Haskell will only give knowledge in the structures not performance. Using libs you will only be writing odd glue. So: pick up C. Implement a specific Heap or Hash table depending if you need sorting or not. Use continuous memory because the size is huge, and you most likely know it first hand so you only need to allocte it once! | ||
TMG26
Portugal2017 Posts
But as I said, using a lib looks like writting shit arpund the problem rather than solving it. | ||
phar
United States1080 Posts
On December 26 2017 09:04 dekibeki wrote: Even if input order has to be respected, you can do the same thing. Each Pair becomes a node (<A,B>) in an intrusive doubly linked list. The map maps A -> <A,B>.
C++ example code + Show Spoiler +
You can do some fancier stuff to save memory, but that's the basic idea. Edit: For production, use the boost intrusive list, instead of the hand rolled one Yea it still works, I just put the caution up front because OP would have to be clear on what ordering. The replacement may need to go in the middle not the end... so that wasn't super clear from the question given. There's also the usual caution about using a linked list: if you're passing that back to a caller who is expecting constant time random access, they're gonna have a bad time, so be explicit about it (or consider returning an iter instead). | ||
graNite
Germany4434 Posts
I put many comments in the code and put the modules that were needed together so you can read it easier. It should still work, print the files of the `boardmatrix` after the initialization of all pieces and give some information of the black kingside rook. Things that I want to change: 1) `reset` function gets a `start` option which initializes all pieces. 2) I dont know how to place the pieces while creating them, thats why the placement is in the `__init__` and as a seperate place function to move them. 3) I already have functions like `kingMove`, `queenMove`, ... . Where do I put them? In the `elif` parts where I decide which kind of piece the new one is? I feel like this would make the class too big and hard to keep track of. + Show Spoiler +
| ||
WarSame
Canada1950 Posts
I would also recommend changing canCastle to hasMoved and making that a property for every piece. Also, make the colour an enumeration(i.e. 0=white, 1=black, 2=purple...) so that you can mess around with colour properties if you want. place() should be called movePiece() and should be a method of the Board. You could then decide where the piece can move based off of the type of piece. Generally if you're feeding options into a function it's a sign that things are going wrong. Have an initializeBoard() that creates a new Board and calls a fillBoard() function that has creates new pieces and deploys them according to the default layout. This should clear up your code a lot. If you're interested read Clean Code to see how to write neater and more legible code. Some of your functions are not very clear and that could help it. | ||
WarSame
Canada1950 Posts
| ||
sc-darkness
856 Posts
Still, I think I've implemented more or less the algorithm in C++ just by following steps but I still don't understand it enough. Here is the algorithm: https://en.wikipedia.org/wiki/Knuth–Morris–Pratt_algorithm Edit: It seems to be about prefixes. I've found this question: https://stackoverflow.com/questions/13792118/kmp-prefix-table Still, not the most readable algorithm I've encountered. | ||
emperorchampion
Canada9496 Posts
| ||
WarSame
Canada1950 Posts
| ||
Blisse
Canada3710 Posts
On December 31 2017 09:35 sc-darkness wrote: It's late night, but I find this string matching algorithm hard to follow. I've got some of its core ideas but still no clue what each value from KMP table means. Maybe it's name of variables that make it hard to follow, but I find examples easier. Or maybe I'm not smart enough. Still, I think I've implemented more or less the algorithm in C++ just by following steps but I still don't understand it enough. Here is the algorithm: https://en.wikipedia.org/wiki/Knuth–Morris–Pratt_algorithm Edit: It seems to be about prefixes. I've found this question: https://stackoverflow.com/questions/13792118/kmp-prefix-table Still, not the most readable algorithm I've encountered. I usually google search for a bunch of sources until I find one with the language that makes sense. can be videos, blogs, coding challenges, anything. wiki's not my favourite source for algos, only algo i use wiki is mergesort. knp is really straightforward. if you're looking for a pattern in a text, once you've seen some part of the pattern, how many characters can you skip?
text[0] -> pattern[0], good text[1] -> pattern[1], good text[2] -> pattern[2], good text[3] -> pattern[3], good text[4] -> pattern[4], good text[5] -> pattern[5], bad naively, we would go, text[1] -> pattern[0], text[2] -> pattern[1]. can we do better? notice that we've seen a repeated AA pattern already. in fact, pattern[0..1] == pattern[3..4]. so instead of checking text[5] == pattern[5], we can retry with text[5] == pattern[2], because we know pattern[0..1] == pattern[3..4]. text[5] -> pattern[2], good text[6] -> pattern[3], good text[7] -> pattern[4], good text[8] -> pattern[5], bad etc. to determine where we jump to, we build a table of the longest proper prefix which is also a suffix
table[0] -> pattern[0..0] -> A -> 0, 'proper prefix' excludes the entire word table[1] -> pattern[0..1] -> AA -> 1, A is a prefix and a suffix table[2] -> pattern[0..2] -> AAB -> 0 table[3] -> pattern[0..3] -> AABA -> 1, A is a prefix and a suffix table[4] -> pattern[0..4] -> AABAA -> 2, AA is a prefix and a suffix ... table[9] -> pattern[0..9] -> AABAACAABA -> 4, AABA is the longest prefix and suffix happy new year! | ||
sc-darkness
856 Posts
Does anyone know what happens to memory in the following scenario?
In my case, memory wasn't the same as it was initially. Slightly higher (e.g. 0.5-1 MB but not the same). Language I use is C++. Steps are as described above. The only thing I did for step 2 and 3 was to use std::list<std::string> with 1 million elements, then I cleared list. Is Task Manager not reliable? Is OS doing something funny? I noticed the same effect when I used my own linked list implementation (it's only for learning purposes, I know std::list is better). Edit: It turns out 'delete' doesn't really release memory immediately. Maybe it's up to the operating system. | ||
Excludos
Norway7969 Posts
On January 02 2018 09:49 sc-darkness wrote: Thank you Blisse! I appreciate your help! Does anyone know what happens to memory in the following scenario?
In my case, memory wasn't the same as it was initially. Slightly higher (e.g. 0.5-1 MB but not the same). Language I use is C++. Steps are as described above. The only thing I did for step 2 and 3 was to use std::list<std::string> with 1 million elements, then I cleared list. Is Task Manager not reliable? Is OS doing something funny? I noticed the same effect when I used my own linked list implementation (it's only for learning purposes, I know std::list is better). Edit: It turns out 'delete' doesn't really release memory immediately. Maybe it's up to the operating system. That depends entirely on the language you're using. C and C++ should delete it immediately (unless you're using something like Qt which runs its own "delete later"). Meanwhile Java runs garbage collection, which means it deletes the objects whenever it feels like it. | ||
| ||