language-agnostic

Is encapsulation possible without OOP?

放肆的年华 提交于 2019-12-24 08:09:15
问题 I was asked a question in an interview: if encapsulation is possible without OOP, e.g. in a procedural language? 回答1: Bob Martin has stated that encapsulation is not only possible without OOP, it was better before OOP came along. Here is an excerpt from a talk he gave in 2014 at Yale School of Management. We had perfect encapsulation. In C, all you had to do was forward declare your functions and your data structures. You didn't have to implement them. You would forward declare them in a

Print a simply linked list backwards with no recursion, in two passes at most, using constant extra memory, leaving it intact

戏子无情 提交于 2019-12-24 06:49:22
问题 You must print a simply linked list backwards: Without recursion With constant extra memory In linear time Leaving the list intact Added Later Two passes at most 回答1: Building on sharptooth's reply, you can combine the printing and second inversion in the same pass. Edit: The "list is left intact" from a single-threaded view because the post-condition equals the pre-condition. Edit 2: Not sure how I got the answer, but I'll take it since I've hit the rep cap for the day. I gave sharptooth a

'Live orders' view on website - Advice needed!

怎甘沉沦 提交于 2019-12-24 06:03:28
问题 I'm just after a bit of advice - I am building an online store (using php/mysql) and at present can view current orders by clicking through to the relevant section and applying filters etc. An email is also sent to my inbox. Because of the nature of the business (orders need to be prepared and dispatched within half hour), I would like to have a page on my website that displays orders 'live' as they come through the website, that I can manage and mark off as they are fulfilled. What would be

How is this Huffman Table created?

会有一股神秘感。 提交于 2019-12-24 04:02:10
问题 I have a table that shows the probability of an event happening. I'm fine with part 1, but part 2 is not clicking with me. I'm trying to get my head around how the binary numbers are derived in part 2? I understand 0 is assigned to the largest probability and we work back from there, but how do we work out what the next set of binary numbers is? And what do the circles around the numbers mean/2 shades of grey differentiate? It's just not clicking. Maybe someone can explain it in a way that

Best way to keep the user-interface up-to-date?

房东的猫 提交于 2019-12-24 01:54:26
问题 This question is a refinement of my question Different ways of observing data changes. I still have a lot of classes in my C++ application, which are updated (or could be updated) frequently in complex mathematical routines and in complex pieces of business logic. If I go for the 'observer' approach, and send out notifications every time a value of an instance is changed, I have 2 big risks: sending out the notifications itself may slow down the applications seriously if user interface

Algorithm for highest value inside budget

浪子不回头ぞ 提交于 2019-12-24 01:23:59
问题 I wasn't entirely sure the best way to ask this question (or do the research to see if it has been previously answered). Given a data set where each entry has a Point value and a Dollar value, I'm looking to generate a list of length N entries that yields the highest aggregate Point value whilst staying within budget B. Example data set: Item Points Dollars Apple 3.0 $1.00 Pear 2.5 $0.75 Peach 2.8 $0.88 And with this (small) data set, say my budget (B) is $2.25, and list length (N) must be 2.

How do you find the minimal set of swaps that will sort a known list?

妖精的绣舞 提交于 2019-12-24 00:52:31
问题 It is known that the problem of sorting an unknown lists can't be done in less than N * log(N) steps in average. But what about the problem of finding the optimal sorting for a known list ? That is, suppose you have the following list: [1,3,2,7,4] On that case, only 2 swaps leave it sorted: swap 1 2 swap 3 4 Which is much less than 5 * log 5 . How do I find the minimal set of swaps that will leave a specific list sorted? Note: this question is very similar to my previous one, except without

How to deal with merged objects/resources in a web app and in a RESTful API?

怎甘沉沦 提交于 2019-12-24 00:49:47
问题 I have a web app that has lots of pages with a URL of the form /object_type/object_id/ which show this objects details. I also have a RESTful API, returning a JSON/XML representation of my objects (under /api/object_type/object_id/ ). (My objects are Laws and Bills, but I guess this question is rather generic). From time to time, I discover that 2 or more of my objects are actually duplicates, and describe the same object in the real world. Lets say they were /Bill/111/ and /Bill/222/ .

Octree neighbour search

人盡茶涼 提交于 2019-12-23 22:45:05
问题 I have an octree, that stores a voxel-based fluid. When I simulate the fluid, I need to access the leaves around the current node, how can I implement such a search? You can assume the node stores a pointer to its parent node.(Perhaps other data is needed?) 回答1: Assuming each octree node also holds its 3D index[1] in the octree (and its depth). Generate the 3D indices for the neighbor nodes of the query node (simply increment/decrement the 3D index of the query node in each dimension). For

Generating Fixtures from a list of n

浪子不回头ぞ 提交于 2019-12-23 21:55:25
问题 Suppose I have N teams and what to generate a fixture list, where every team plays every other team, what is the best practice for this. Is there a known algorithm that does this nicely? Efficiency isn't really a necessity as this only needs to be generated once a season. To be more specific, I'll start with some definitions: I have N teams... T_1, T_2, ... , T_n. If N is odd, include a 'ghost' team to make the amount of teams even. A set of fixtures for a week is a set of N/2 pairs, with no