algorithm

Algorithm to print all valid combations of n pairs of parenthesis

旧巷老猫 提交于 2021-02-04 16:17:32
问题 I'm working on the problem stated in the question statement. I know my solution is correct (ran the program) but I'm curious as to whether or not I'm analyzing my code (below) correctly. def parens(num) return ["()"] if num == 1 paren_arr = [] parens(num-1).each do |paren| paren_arr << paren + "()" unless "()#{paren}" == "#{paren}()" paren_arr << "()#{paren}" paren_arr << "(#{paren})" end paren_arr end parens(3), as an example, will output the following: ["()()()", "(()())", "(())()", "()(())

Python pandas - how to group close elements

倾然丶 夕夏残阳落幕 提交于 2021-02-04 16:15:22
问题 I have a dataframe where I need to group elements with distance of no more than 1. For example, if this is my df: group_number val 0 1 5 1 1 8 2 1 12 3 1 13 4 1 22 5 1 26 6 1 31 7 2 7 8 2 16 9 2 17 10 2 19 11 2 29 12 2 33 13 2 62 So I need to group both by the group_number and val where the values of val are smaller than or equal to 1. So, in this example, lines 2 and 3 would group together, and also lines 8 and 9 would group together. I tried using diff or related functions, but I didn't

What is the best data structure for text auto completion?

你离开我真会死。 提交于 2021-02-04 15:57:06
问题 I have a long list of words and I want to show words starting with the text entered by the user. As user enters a character, application should update the list showed to user. It should be like AutoCompleteTextView on Android. I am just curious about the best data structure to store the words so that the search is very fast. 回答1: A trie could be used . http://en.wikipedia.org/wiki/Trie https://stackoverflow.com/search?q=trie A nice article - http://www.sarathlakshman.com/2011/03/03

Python algorithm of counting occurrence of specific word in csv

给你一囗甜甜゛ 提交于 2021-02-04 15:34:08
问题 I've just started to learn python. I'm curious about what are the efficient ways to count the occurrence of a specific word in a CSV file, other than simply use for loop to go through line by line and read. To be more specific, let's say I have a CSV file contain two columns, "Name" and "Grade", with millions of records. How would one count the occurrence of "A" under "Grade"? Python code samples would be greatly appreciated! 回答1: Basic example, with using csv and collections.Counter (Python

Permute a vector such that an element can't be in the same place

隐身守侯 提交于 2021-02-04 14:36:19
问题 I want to permute a vector so that an element can't be in the same place after permutation, as it was in the original. Let's say I have a list of elements like this: AABBCCADEF A valid shuffle would be: BBAADEFCCA But these would be invalid: B A ACFEDCAB or BCA B FEDCAB The closest answer I could find was this: python shuffle such that position will never repeat. But that's not quite what I want, because there are no repeated elements in that example. I want a fast algorithm that generalizes

Algorithm Time Complexity: i/=2 in loops

筅森魡賤 提交于 2021-02-04 14:08:54
问题 int fun(int n) { int count = 0; for (int i = n; i > 0; i /= 2) for (int j = 0; j < i; j++) count += 1; return count; } I'm a very new to the time complexities calculations. For this algorithm, I get the answer to be O(nlogn), but the answer is apparently O(n). My logic was the outer loop has an exponential decline and will occur log_base2_(N) times. The inner loop will run a total of N times as it becomes a geometric sum (first iteration it is N/2 times, then N/4, then N/8 ...). If I put

Algorithm Time Complexity: i/=2 in loops

断了今生、忘了曾经 提交于 2021-02-04 14:06:44
问题 int fun(int n) { int count = 0; for (int i = n; i > 0; i /= 2) for (int j = 0; j < i; j++) count += 1; return count; } I'm a very new to the time complexities calculations. For this algorithm, I get the answer to be O(nlogn), but the answer is apparently O(n). My logic was the outer loop has an exponential decline and will occur log_base2_(N) times. The inner loop will run a total of N times as it becomes a geometric sum (first iteration it is N/2 times, then N/4, then N/8 ...). If I put

Algorithm Time Complexity: i/=2 in loops

与世无争的帅哥 提交于 2021-02-04 14:05:11
问题 int fun(int n) { int count = 0; for (int i = n; i > 0; i /= 2) for (int j = 0; j < i; j++) count += 1; return count; } I'm a very new to the time complexities calculations. For this algorithm, I get the answer to be O(nlogn), but the answer is apparently O(n). My logic was the outer loop has an exponential decline and will occur log_base2_(N) times. The inner loop will run a total of N times as it becomes a geometric sum (first iteration it is N/2 times, then N/4, then N/8 ...). If I put

how to read last n lines from a file in C

∥☆過路亽.° 提交于 2021-02-04 13:44:12
问题 Its a microsoft interview question. Read last n lines of file using C (precisely) Well there could be so many ways to achieve this , few of them could be : -> Simplest of all, in first pass , count the number of lines in the file and in second pass display the last n lines. -> Or may be maintain a doubly linked-list for every line and display the last n lines by back traversing the linkedlist till nth last node. -> Implement something of sort tail -n fname -> In order to optimize it more we

Filter a json data by another array in underscore.js

耗尽温柔 提交于 2021-02-04 08:40:16
问题 I have a search field and I want to add some complex functionality using underscore.js. Sometimes users search for a whole "sentence" like "Samsung galaxy A20s ultra". I want to filter JSON data using any of the words in the search string and sort by results that contain more of the words. Sample data: var phones = [ {name: "Samsung A10s", id: 845}, {name: "Samsung galaxy", id: 839}, {name: "Nokia 7", id: 814}, {name: "Samsung S20s ultra", id: 514}, {name: "Apple iphone ultra", id: 159},