Skip to content
Advertisement

Tag: search

First occurrence of 1 in unsorted binary array

I am solving a problem to find the first 1 in an unsorted array of 1s and 0s. If any 1 is found program should return the index otherwise return -1. I would like to have divide and conquer solution for this problem, but I am struggling with the base case. So what should my base case be – to

How to search a string for a long list of patterns

I’m writing a tool to index a document. I have a long list of hundreds, perhaps thousands of fixed patterns. E.g. my index might look like {“cat training”:”p.27″, “cat handling”:”p.29″, “cat”:”p.15″, “dog training”:”p.62″, “dog”:”p.60″} and so on. Now I want to search my text (for the sake of argument, each paragraph is a single string) for all instances of any

Searching any tree in Python

I need to write a function in Python that takes a tree and an index and returns the subtree or leaf at that index. I tried with loops and nested loops until I realized that the tree that had to run the tests was always the same: which actually looks like this: Sample tree So all I needed to pass

Get the part of the string matched by regex

In the case of re.search(), is there a way I can get hold of just the part of input string that matches the regex? i.e. I just want the “heeehe” part and not the stuff that comes before it: Answer match.group(0) is the matched string. Demo: You can also omit the argument, 0 is the default.

Finding a key recursively in a dictionary

I’m trying to write a very simple function to recursively search through a possibly nested (in the most extreme cases ten levels deep) Python dictionary and return the first value it finds from the given key. I cannot understand why my code doesn’t work for nested dictionaries. It returns None. It does work, however, for _finditem({“B”:1,”A”:2},”A”), returning 2. I’m sure

Advertisement