in-place

Is Quicksort in-place or not? [duplicate]

Deadly 提交于 2019-12-03 03:37:54
问题 This question already has answers here : Is imperative Quicksort in situ (in-place) or not? (2 answers) Closed 5 years ago . So the space efficiency of Quicksort is O(log(n)). This is the space required to maintain the call stack. Now, according to the Wikipedia page on Quicksort, this qualifies as an in-place algorithm, as the algorithm is just swapping elements within the input data structure. According to this page however, the space Efficiency of O(log n) disqualifies Quicksort from being

Difference between a -= b and a = a - b in Python

∥☆過路亽.° 提交于 2019-12-03 01:46:51
问题 I have recently applied this solution for averaging every N rows of matrix. Although the solution works in general I had problems when applied to a 7x1 array. I have noticed that the problem is when using the -= operator. To make a small example: import numpy as np a = np.array([1,2,3]) b = np.copy(a) a[1:] -= a[:-1] b[1:] = b[1:] - b[:-1] print a print b which outputs: [1 1 2] [1 1 1] So, in the case of an array a -= b produces a different result than a = a - b . I thought until now that

How to master in-place array modification algorithms?

白昼怎懂夜的黑 提交于 2019-12-02 14:10:49
I am preparing for a software job interview, and I have trouble with in-place array modifications. For example, in the out-shuffle problem you interleave two halves of an array, so that 1 2 3 4 5 6 7 8 would become 1 5 2 6 3 7 4 8. This question asks for a constant-memory solution (and linear-time, although I'm not sure that's even possible). First I thought a linear algorithm is trivial, but then I couldn't work it out. Then I did find a simple O(n^2) algorithm but it took me a long time. And I still don't find a faster solution. I remember also having trouble solving a similar problem from

Replacing words in text file using a dictionary

喜夏-厌秋 提交于 2019-12-01 04:28:28
I'm trying to open a text file and then read through it replacing certain strings with strings stored in a dictionary. Based on answers to How do I edit a text file in Python? I could pull out the dictionary values before doing the replacing, but looping through the dictionary seems more efficient. The code doesn't produce any errors, but also doesn't do any replacing. import fileinput text = "sample file.txt" fields = {"pattern 1": "replacement text 1", "pattern 2": "replacement text 2"} for line in fileinput.input(text, inplace=True): line = line.rstrip() for i in fields: for field in fields

why is in place merge sort not stable?

偶尔善良 提交于 2019-11-30 16:12:34
The implementation below is stable as it used <= instead of < at line marked XXX. This also makes it more efficient. Is there any reason to use < and not <= at this line? /** class for In place MergeSort **/ class MergeSortAlgorithm extends SortAlgorithm { void sort(int a[], int lo0, int hi0) throws Exception { int lo = lo0; int hi = hi0; pause(lo, hi); if (lo >= hi) { return; } int mid = (lo + hi) / 2; /* * Partition the list into two lists and sort them recursively */ sort(a, lo, mid); sort(a, mid + 1, hi); /* * Merge the two sorted lists */ int end_lo = mid; int start_hi = mid + 1; while (

Pandas: drop columns with all NaN's

别说谁变了你拦得住时间么 提交于 2019-11-30 15:12:26
I realize that dropping NaN s from a dataframe is as easy as df.dropna but for some reason that isn't working on mine and I'm not sure why. Here is my original dataframe: fish_frame1: 0 1 2 3 4 5 6 7 0 #0915-8 NaN NaN NaN NaN NaN NaN NaN 1 NaN NaN NaN LIVE WGT NaN AMOUNT NaN TOTAL 2 GBW COD NaN NaN 2,280 NaN $0.60 NaN $1,368.00 3 POLLOCK NaN NaN 1,611 NaN $0.01 NaN $16.11 4 WHAKE NaN NaN 441 NaN $0.70 NaN $308.70 5 GBE HADDOCK NaN NaN 2,788 NaN $0.01 NaN $27.88 6 GBW HADDOCK NaN NaN 16,667 NaN $0.01 NaN $166.67 7 REDFISH NaN NaN 932 NaN $0.01 NaN $9.32 8 GB WINTER FLOUNDER NaN NaN 145 NaN $0

link_to with jquery params in rails

孤街浪徒 提交于 2019-11-30 13:23:06
问题 I want to make an in-place search in my rails app. I used button_to_remote with prototype, but now I'm using JQuery, so I changed to link_to. This is my code: <%= link_to "Search", {:action => "geocode", :with => "'address='+$('#{address_helper_id}').value"}, :method => :post, :remote => true %> I want to pass the address textfield to my controller, but the output is not what I expected. /mycontroller/geocode?with=%27address%3D%27%2B%24%28%27record_location___address%27%29.value How do I

Algorithm to apply permutation in constant memory space

谁都会走 提交于 2019-11-30 13:02:01
问题 I saw this question is a programming interview book, here I'm simplifying the question. Assume you have an array A of length n , and you have a permutation array P of length n as well. Your method will return an array where elements of A will appear in the order with indices specified in P . Quick example: Your method takes A = [a, b, c, d, e] and P = [4, 3, 2, 0, 1] . then it will return [e, d, c, a, b] . You are allowed to use only constant space (i.e. you can't allocate another array,

Algorithm to apply permutation in constant memory space

妖精的绣舞 提交于 2019-11-30 06:54:46
I saw this question is a programming interview book, here I'm simplifying the question. Assume you have an array A of length n , and you have a permutation array P of length n as well. Your method will return an array where elements of A will appear in the order with indices specified in P . Quick example: Your method takes A = [a, b, c, d, e] and P = [4, 3, 2, 0, 1] . then it will return [e, d, c, a, b] . You are allowed to use only constant space (i.e. you can't allocate another array, which takes O(n) space). Ideas? zw324 There is a trivial O(n^2) algorithm, but you can do this in O(n). E.g

What are the best options for Rich Text Editing in Rails?

前提是你 提交于 2019-11-30 03:48:48
I'd like to use Rich Text Editing in place on forms in order to let admins change instructions. What are the best options for doing this? [To be more clear - the admins are non-technical but may want to control some formatting without using markup or with as little markup as possible. What I'd like is for them to be able to edit inline all AJAXy with an RTE featuring some formatting controls and then submit and be able to see what the instructions will look like to the end user without changing pages. With Regards to plugins specifically, what I'd like to know is which Rich Test Editing