computer-science

Why are loops executed one more time than the loop body?

喜夏-厌秋 提交于 2019-12-07 15:24:40
问题 A quote from an Algorithms textbook: "When a for or while loop exits in the usual way (i.e., due to the test in the loop header), the test is executed one time more than the loop body." So, for example, a for loop that begins with for j=1 to 3 will be executed not 3 times, but 4 times! Question: Why would such a loop be executed 4 times and not 3 times? By my reasoning: When j = 1, the loop is executed. When j = 2, the loop is executed. When j = 3, the loop is executed. When j = 4, the loop

Understanding regex string matching using Dynamic Programming

风格不统一 提交于 2019-12-07 11:26:31
问题 I came across this problem that asks you to implement a regular expression matcher with support for '.' and '*', where '.' Matches any single character. '*' Matches zero or more of the preceding element. isMatch("aa","a") → false isMatch("aa","aa") → true isMatch("aaa","aa") → false isMatch("aa", "a*") → true isMatch("aa", ".*") → true isMatch("ab", ".*") → true isMatch("aab", "c*a*b") → true While I was able to solve this in a linear fashion, I came across lots of solutions that used DP,

What's the fastest heuristic algorithm to split students into groups?

…衆ロ難τιáo~ 提交于 2019-12-07 07:16:31
问题 I have X number of students, where X is a multiple of 6. I now want to split up the students into groups of 6. I have a function that measures how "good" a group of 6 is (lets say it's a black box that runs in constant time for now). By splitting up the students, and then calling my function on each group to measure it's goodness, and then summing up the goodness of each group, I'm able to measure how "good" a certain set of groups is. I'm trying to create an algorithm that will group the

C-like language without NULL?

一笑奈何 提交于 2019-12-07 02:26:43
问题 Hi I was recently watching an old video about how null pointers were a billion dollar mistake. He points out both C# and java as they have run-time checks but don't completely eliminate it enough and this is somewhat understandable. He also points out the C at one point which he feels so sure of is a great problem. I get that null terminated strings, arrays with no length and a few other things are bad (billions of dollars on buffer overflow exploits) but to completely remove null? Some

Data Structure for Storing Sparse Matrices

ε祈祈猫儿з 提交于 2019-12-07 00:55:30
问题 I need to do some mathematics operations on sparse matrices. I noticed that using arrays may not be the most efficient way to utilize my memory, especially since the matrices may have over 200 rows. I have considered using a linked list too, but I'm not sure if that'll be better. Is there any suitable data structure [approach] to this situation. 回答1: How many "over 200 rows"? How sparse? A 1000x1000 matrix of doubles is still less than 8MB, which is not something I'd worry about unless you

What does qualify mean?

让人想犯罪 __ 提交于 2019-12-07 00:22:45
问题 When reading articles, manuals, etc... about programming, i always come across the word qualified . like in java the fully qualified class name would be com.example.Class. Reading this article, defines the scope resolution operator :: in C++ as being used to Qualify hidden names so you can still use them. Is there a definition for this ? Beccause it seems to be used in a different context each time. 回答1: In computer programming, a fully qualified name is an unambiguous name that specifies

Create a java program to solve quadratic equations

江枫思渺然 提交于 2019-12-06 14:47:42
问题 This question was migrated from Mathematics Stack Exchange because it can be answered on Stack Overflow. Migrated 6 years ago . Solving a quadratic equation I have the following written down so far. I am not sure on how to introduce the second method public static void main(string args[]){ } public static double quadraticEquationRoot1(int a, int b, int c) (){ } if(Math.sqrt(Math.pow(b, 2) - 4*a*c) == 0) { return -b/(2*a); } else { int root1, root2; root1 = (-b + Math.sqrt(Math.pow(b, 2) - 4*a

Break text evenly into certain number of lines

↘锁芯ラ 提交于 2019-12-06 11:29:42
问题 There is a linear time algorithm (or quadratic time algorithm by Knuth & Plass) for breaking text evenly into lines of maximum width. It uses SMAWK and "evenly" means: http://en.wikipedia.org/wiki/Word_wrap#Minimum_raggedness Is there an algorithm or a concave cost function for algorithm above which would take into account the number of lines I would like the text break into, instead of the maximum line width? In other words, I'm looking for a line breaking (or paragraph formation, or word

Number of ways to create an AVL Tree with n nodes and L leaf node

橙三吉。 提交于 2019-12-06 11:27:41
I'd like to know number of ways I can create a Balanced Binary Tree with n nodes and L leaf nodes . also I know that n must be ( 2*L - 1 ) . A balanced binary tree is a tree such that given any node, the two subtrees of that node has their height differing by at most one. So the number of nodes is not necessarily 2^L -1. If a tree has 2^L-1 nodes, then it is by definition, a full binary tree. So to answer your question.. If order does matter.. there are (n choose 1) ways (or n ways) to choose the top node. Then since order does matter, there are (n-1 choose 2) choices to choose the children of

Reason for end around carry to do [closed]

删除回忆录丶 提交于 2019-12-06 11:10:46
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 7 years ago . I know that the r-1 complement for r-base number should do end around carry if the highest bit has carry. But I cannot figure out why it should do it. I merely can think about it is the reason may be about the two representations for zero. ex: 1 1 1 0 (-1) 0 1 0 1 (+5) =============== 10 0 1 1 =====>(0 1 0 0) I