theory

Way to go from recursion to iteration

我只是一个虾纸丫 提交于 2020-01-03 05:55:11
问题 I've used recursion quite a lot on my many years of programming to solve simple problems, but I'm fully aware that sometimes you need iteration due to memory/speed problems. So, sometime in the very far past I went to try and find if there existed any "pattern" or text-book way of transforming a common recursion approach to iteration and found nothing. Or at least nothing that I can remember it would help. Are there general rules? Is there a "pattern"? 回答1: Usually, I replace a recursive

integer division properties

时光总嘲笑我的痴心妄想 提交于 2020-01-02 02:54:32
问题 does the following integer arithmetic property hold? (m/n)/l == m/(n*l) At first I thought I knew answer (does not hold), but now am not sure. Does it hold for all numbers or only for certain conditions, i.e. n > l ? the question pertains to computer arithmetic, namely q = n/m, q*m != n , ignoring overflow. 回答1: Case1 assume m = kn+b (b<n), left = (m/n)/l = ((kn+b)/n)/l = (k+b/n)/l = k/l (b/n=0, because b<n) right = (kn+b)/(n*l) = k/l + b/(n*l) = k/l (b/(n*l)=0, because b<n) => left = right

Why and when is necessary to rebuild indexes in MongoDB?

拜拜、爱过 提交于 2019-12-31 17:58:43
问题 Been working with MongoDB for a while and today I had a doubt while discussing with a colleague. The thing is that when you create an index in MongoDB, the collection is processed and the index is built. The index is updated within insertion and deletion of documents so I don't really see the need to run a rebuild index operation (which drops the index and then rebuild it). According to MongoDB documentation: Normally, MongoDB compacts indexes during routine updates. For most users, the

Why can the 'as' operator not be used to parse non-nullable value types?

元气小坏坏 提交于 2019-12-31 01:22:07
问题 Every Developer has his/her own standards. Some developers like to <type>.TryParse() , some developers like to cast using (type)object; , and some developers love using the keywords instead. I noticed a hiccup with the 'as' operator - you cannot use it to perform conversions between non-nullable value types. I read the documentation on MSDN on the as Keyword and they also explain it as "You can use the as operator to perform certain types of conversions between compatible reference types or

Does a good use case exist for skip() on parallel streams?

不羁的心 提交于 2019-12-30 12:34:51
问题 EDITED on September, 2015 When I initially asked this question on February, 2015, the behaviour reported in the linked question was counter-intuitive, though kind of allowed by the specification (despite some little inconsistencies in the docs). However, Tagir Valeev asked a new question on June, 2015, where I think he clearly demonstrated that the behaviour reported in this question was actually a bug . Brain Goetz answered his question, and admitted that it was a bug to not stop the back

How do I mimic access modifiers in JavaScript with the Prototype library?

≡放荡痞女 提交于 2019-12-30 11:24:10
问题 I've been working with the prototype library for some time now and occasionally find myself wishing I had multiple access levels (public, private, and protected). The closest I've come so far is the following: SampleBase = Class.create({ /* virtual public constructor */ initialize: function(arg1, arg2) { // private variables var privateVar1, privateVar2; // private methods var privateMethod1 = function() { } function privateMethod2() { } // public (non virtual) methods this.publicNonVirtual1

Are NSStrings stored on the heap or on the stack and what is a good way to initialize one

只愿长相守 提交于 2019-12-30 03:16:09
问题 I have 2 new questions: 1) Consider this line: NSString *myString = [[NSString alloc] initWithString: @"Value"]; There were two things I learned, but I would like confirmation: As I learned, the "alloc" message indicates that the instance of NSString will be stored in the "heap" memory. I understood also that primitive variables such as "chars" are stored in the "stack" memory. Does this mean that: the instance of NSString is stored in the heap memory; AND that this object has an iVar pointer

When does Big-O notation fail?

孤者浪人 提交于 2019-12-29 11:35:13
问题 What are some examples where Big-O notation[1] fails in practice? That is to say: when will the Big-O running time of algorithms predict algorithm A to be faster than algorithm B, yet in practice algorithm B is faster when you run it? Slightly broader: when do theoretical predictions about algorithm performance mismatch observed running times? A non-Big-O prediction might be based on the average/expected number of rotations in a search tree, or the number of comparisons in a sorting algorithm

What are the differences between Hashmap vs Hashtable in theory?

ぐ巨炮叔叔 提交于 2019-12-29 08:11:54
问题 Are there are differences between hashmap and hashtable in theory? I don't mean in the concrete definitions given in Java (or the implementation), but in theory. Isn't a hashtable a map that uses hashing ... hence a hashmap? 回答1: According to Wikipedia, they are the same: In computing, a hash table (hash map) is a data structure used to implement an associative array (...) According to Wikibooks, it's the same: A hash table, or a hash map, is a data structure that associates keys with values.

Make java methods visible to only specific classes

不想你离开。 提交于 2019-12-29 00:48:13
问题 I have a manager class that is responsible for managing Objects of a certain kind. To do so it needs to manipulate these Objects, but these Objects have no relation to the manager whatsoever, so design technically, they are in separate packages "project.managers" and "project.objects" . The important thing is that the Objects in question should only be manipulated by the managers and nowhere else, but need to be accessible by every other class in the project. As such I'd like to have the