computer-science

ValueError: invalid literal for int() with base 10: '-' m[i][i] = int(d[i])

只谈情不闲聊 提交于 2019-12-24 19:36:59
问题 Im working on the code the finding the maximum evaluation (ignoring precedence) such as 2 5+ 3*2 -1 + -4 * -3 Output 16 40000000000 I have the following code which returns the following error: ValueError: invalid literal for int() with base 10: '-' m[i][i] = int(d[i]) File Note: I have looked through the other questions with a similar error, but this is different from each of them. Im using Python 3and it docent work. def get_maximum_value(expression): expression="".join(expression.split())

Ruby Regex for repeated numbers in a string

流过昼夜 提交于 2019-12-24 18:49:25
问题 If i have a string like "123123123" - Here 123 is repeated 3 times. 1. So how can i get only "123" in ruby? 2. So if the string is "12312312" - Here 123 is repeated 2 times and then just 12 , so here still i need to get "123" . 3. Even if string is 99123123123 , still i need to get 123 . Is this possible in Ruby Regex? EDIT: I want this to solve Project Euler Problem 26 . So here 123 can be anything. All i want is to extract 1 number of at-least 2 repeated numbers. 回答1: This regex will detect

When is type checking for type argument performed?

浪子不回头ぞ 提交于 2019-12-24 09:48:32
问题 Here is some C++ template code from Programming Language Pragmatics, by Scott template<typename T> class chooser { public: virtual bool operator()(const T& a, const T& b) = 0; }; template<typename T, typename C> class arbiter { T* best_so_far; C comp; public: arbiter() { best_so_far = nullptr; } void consider(T* t) { if (!best_so_far || comp(*t, *best_so_far)) best_so_far = t; } T* best() { return best_so_far; } }; class case_sensitive : chooser<string> { public: bool operator()(const string&

How to generate a Control Flow Graph from Assembly?

≯℡__Kan透↙ 提交于 2019-12-24 09:48:31
问题 For context, I'm attempting to write a decompiler from AVM2 (ActionScript Virtual machine 2) bytecode/assembly to high-level ActionScript 3 code. As far as I am aware, this requires me to analyze the assembly and generate resulting Control Flow Graph from this, in order to deduce structures such as loops, and conditional branching (if/else). Given some assembly like: 0 getlocal0 1 pushscope 2 findpropstrict {, private, }::trace 4 pushstring "one" 6 callproperty {, private, }::trace (1) 9 pop

generate top k values

≯℡__Kan透↙ 提交于 2019-12-24 09:01:22
问题 I have a problem and I want to make sure if I am doing it most efficiently. I have an array A of float values of size N. The values are all between 0 and 1. I have to find top k values which can be a product of a maximum of three numbers from A. So, the top-k list can have individual numbers from A, product of two numbers or product of three numbers from A. So, this is how I am doing it now. I can get top-k numbers in desecding order in O(Nlogk) time. I then create a max-heap and initialize

Could an iterative function call itself?

て烟熏妆下的殇ゞ 提交于 2019-12-24 01:01:55
问题 When viewing the below MIT 6.001 course video, at 28:00 the instructor marks this algorithm as iteration. But, at 30.27 he says both this and the actual "recursive" algorithms are recursive. The function is calling itself with a base case, so how is this iteration? https://www.youtube.com/watch?v=dlbMuv-jix8&list=PLE18841CABEA24090&index=2 private int iterativeSum(int x, int y) { System.out.println("x "+x+" y "+y); if(x == 0) { return y; } return iterativeSum(--x, ++y); } 回答1: He seems to be

What kind of address instruction does the x86 cpu have?

送分小仙女□ 提交于 2019-12-24 00:54:37
问题 I learned about one address, two address, and three address instruction, but now I'd like to know, what kind of address instruction does x86 use? 回答1: x86 is a register machine, where at most 1 operand for any instruction can be an explicit memory address instead of a register, using an addressing mode like [rdi + rax*4] . (There are instruction which can have 2 memory operands with one or both being implicit, though: What x86 instructions take two (or more) memory operands?) Typical x86

Quick Way to Find the Largest Array in a Multidimensional Array?

本秂侑毒 提交于 2019-12-23 19:46:46
问题 Situation: I have a multidimensional array with a variable number of elements. e.g. array(N) { 0 => array(3) { ... }, 1 => array(8) { ... }, 2 => array(1) { ... }, ... M => array(12) { ... }, ... N-1 => array(7) { ... } } And I would like to find the maximum number of elements in this sub-array (in the example above, it would be 12). A straightforward solution would be an O(N) linear search. <?php function max_length($2d_array) { $max = 0; foreach($2d_array as $child) { if(count($child) >

Single-source shortest path with distance and weight for each edge

旧巷老猫 提交于 2019-12-23 19:15:46
问题 Suppose there's an undirected graph and each edge that connects any two nodes has two weights (i.e. distance and cost). I want to get the shortest path, but also make sure I do not go beyond a certain cost. I've tried implementing Djikstra's and simply backtracking (for lack of a better term) if I exceed the cost, until I traverse the entire graph. However, I'm looking for a faster solution than this. I've also attempted to use a function that creates a weight given the distance and cost of

How can I use domain knowledge to improve this algorithm? Ugh, the weights

风流意气都作罢 提交于 2019-12-23 17:14:40
问题 I am having balancing trouble. I feel like I'm missing something here.. This problem equates to the following situation: On a table are a scattering of weights of various mass. In your hand are a few weights of various mass. If there is a combination of weights on the table that have a mass matching one in your hand, you can take those weights from the table, because they weigh the same as the single weight in your hand. You can drop the whole thing, including the weight from your hand, into