binary-search

Why does numpy silently convert my int array to strings when calling searchsorted?

 ̄綄美尐妖づ 提交于 2019-12-13 13:35:56
问题 I found a nasty bug in my code where I forgot to convert an integer from str to int before looking it up in a sorted array of integers. Having fixed it, I am still surprised that this didn't cause an explicit exception. Here's a demo: In [1]: import numpy as np In [2]: a = np.arange(1000, dtype=int) In [3]: a.searchsorted('15') Out[3]: 150 In [4]: a.searchsorted('150') Out[4]: 150 In [5]: a.searchsorted('1500') Out[5]: 151 In [6]: a.searchsorted('foo') Out[6]: 1000 With a float array this

How to implement a binary search?

心不动则不痛 提交于 2019-12-13 11:26:09
问题 I'm trying to create a binary search function, I've attempted it with the code below but am a beginner at python ; I'm getting the error "list indices must be integers, not str" on the line i == alist[i] . Could you please help me fix the problem? Here's the whole code: def bsort(alist, i): left = 0 right = len(alist)-1 i == alist[i] [mid] = (left + right)//2 if alist[mid] < i : left = [mid] + 1 elif alis[mid] > i : right = [mid] + 1 elif alist[mid] == i : print ("word found at", i ) elif

Complexity of binary search on a string

♀尐吖头ヾ 提交于 2019-12-13 09:34:14
问题 I have an sorted array of strings: eg: ["bar", "foo", "top", "zebra"] and I want to search if an input word is present in an array or not. eg: search (String[] str, String word) { // binary search implemented + string comaparison. } Now binary search will account for complexity which is O(logn), where n is the length of an array. So for so good. But, at some point we need to do a string compare, which can be done in linear time. Now the input array can contain of words of different sizes. So

Binary Search - How to load +5M records from a file into Range<int>[] array?

痴心易碎 提交于 2019-12-13 09:07:48
问题 This question is a follow up to my previous question regarding binary search (Fast, in-memory range lookup against +5M record table). I have sequential text file, with over 5M records/lines, in the format below. I need to load it into Range<int>[] array. How would one do that in a timely fashion? File format: start int64,end int64,result int start int64,end int64,result int start int64,end int64,result int start int64,end int64,result int ... 回答1: I'm going to assume you have a good disk.

Given a binary search tree and a number, find a path whose node's data added to be the given number.

余生颓废 提交于 2019-12-13 07:15:23
问题 Given a binary search tree and a number, find if there is a path from root to a leaf such that all numbers on the path added up to be the given number. I know how to do it by recursively. But, I prefer an iterative solution. If we iterate from root to a leaf each time, there will be overlap because some paths may have overlap. What if the tree is not binary search ? Thanks 回答1: Basically this problem can be solved using Dynamic Programming on tree to avoid those overlapping paths. The basic

c# - Binary Search String List

有些话、适合烂在心里 提交于 2019-12-13 07:13:46
问题 I'm currently building a console application that can search and sort different types of data. As the question suggests; I need help making a binary search algorithm for a string list. I must point out that I can't make use of the built-in c# functions like list.binarysearch , the algorithm has to be coded from scratch. Also the list contains multiple items of the same string value so I need to find all of these too. I've already sorted the list in alphabetical order so I will make use of

Ruby on Rails, ActiveRecord, Binary search

 ̄綄美尐妖づ 提交于 2019-12-13 04:17:12
问题 If I had the following table. create_table :my_table, :id => false do |t| t.string :key_column t.string :value_column end How would I ensure that the rows are optimaly stored for binary search by the field of :key? And how would I make sure binary search is used? 回答1: For any interesting number of rows, the optimal way (for most definitions of "optimal") to access a single random record by key is to create an index. CREATE INDEX my_index ON my_table ( key_column ); or in an ActiveRecord

If statement not recognizing true conditions?

痴心易碎 提交于 2019-12-13 02:32:21
问题 I'm having trouble with this binary search algorithm. Here are explanations of the variables. value: the number being searched within the array values[]: the array that is being searched n: number of elements in the array high: highest element (by zero indexed position) of portion of the array being searched low: lowest element (by zero indexed position) the portion of the array being searched My problem isn't the recursion. The portion of the array being searched centers around "value" and

Binary Search Code

丶灬走出姿态 提交于 2019-12-13 02:28:04
问题 I am coding my own function for a binary search algorithm and I can't seem to find the discrepancies in logic. When ever I search for 4 it does not return the ideal response. Code Below: var list = [1,2,3,4,6,7,13,18,19]; function binarySearch(list,number) { var newList = list; while (newList.length >= 1) { var halfNum = Math.round(newList.length/2); if (newList[halfNum] === number) { return "Number Found"; } else if (newList[halfNum] < number) { newList = newList.slice(halfNum + 1,newList

Time complexity issues with multimap

扶醉桌前 提交于 2019-12-12 13:23:23
问题 I created a program that finds the median of a list of numbers. The list of numbers is dynamic in that numbers can be removed and inserted (duplicate numbers can be entered) and during this time, the new median is re-evaluated and printed out. I created this program using a multimap because 1) the benefit of it being already being sorted, 2) easy insertion, deletion, searching (since multimap implements binary search) 3) duplicate entries are allowed. The constraints for the number of entries