EzDevInfo.com

search interview questions

Top search frequently asked interview questions

How to do case insensitive search in Vim

I'd like to search for an upper case word, for example COPYRIGHT in a file. I tried performing a search like:

/copyright/i    # Doesn't work

but it doesn't work. I know that in Perl, if I give the i flag into a regex it will turn the regex into a case-insensitive regex. It seems that Vim has its own way to indicate a case-insensitive regex.


Source: (StackOverflow)

How to find string in project in Android Studio?

This may be a really stupid question, but I just can't figure it out. I've just started using Android Studio (IntelliJ), and I now look for the feature to find the occurrence of a string in any of the files in my project. For example: I want to find all the files that contain the string ".getUuid()"

The search at the top right doesn't give me the correct results, and I don't think I can find this feature under Edit > Find..

Could anybody point me at the right direction? Any help would be really really welcome!


Source: (StackOverflow)

Advertisements

Regular expression to search for Gadaffi

I'm trying to search for the word Gadaffi. What's the best regular expression to search for this?

My best attempt so far is:

\b[KG]h?add?af?fi$\b

But I still seem to be missing some journals. Any suggestions?

Update: I found a pretty extensive list here: http://blogs.abcnews.com/theworldnewser/2009/09/how-many-different-ways-can-you-spell-gaddafi.html

The answer below matches all the 30 variants:

Gadaffi
Gadafi
Gadafy
Gaddafi
Gaddafy
Gaddhafi
Gadhafi
Gathafi
Ghadaffi
Ghadafi
Ghaddafi
Ghaddafy
Gheddafi
Kadaffi
Kadafi
Kaddafi
Kadhafi
Kazzafi
Khadaffy
Khadafy
Khaddafi
Qadafi
Qaddafi
Qadhafi
Qadhdhafi
Qadthafi
Qathafi
Quathafi
Qudhafi
Kad'afi

Source: (StackOverflow)

How do you do a case insensitive search using a pattern modifier using less?

It seems like the only way to do this is to pass the -i parameter in when you initially run less. Does anyone know of some secret hack to make something like this work

/something to search for/i

?

Edit: Wow how did I miss -i while less is running? Doh!


Source: (StackOverflow)

Ukkonen's suffix tree algorithm in plain English?

I feel a bit thick at this point. I've spent days trying to fully wrap my head around suffix tree construction, but because I don't have a mathematical background, many of the explanations elude me as they start to make excessive use of mathematical symbology. The closest to a good explanation that I've found is Fast String Searching With Suffix Trees, but he glosses over various points and some aspects of the algorithm remain unclear.

A step-by-step explanation of this algorithm here on Stack Overflow would be invaluable for many others besides me, I'm sure.

For reference, here's Ukkonen's paper on the algorithm: http://www.cs.helsinki.fi/u/ukkonen/SuffixT1withFigs.pdf

My basic understanding, so far:

  • I need to iterate through each prefix P of a given string T
  • I need to iterate through each suffix S in prefix P and add that to tree
  • To add suffix S to the tree, I need to iterate through each character in S, with the iterations consisting of either walking down an existing branch that starts with the same set of characters C in S and potentially splitting an edge into descendent nodes when I reach a differing character in the suffix, OR if there was no matching edge to walk down. When no matching edge is found to walk down for C, a new leaf edge is created for C.

The basic algorithm appears to be O(n2), as is pointed out in most explanations, as we need to step through all of the prefixes, then we need to step through each of the suffixes for each prefix. Ukkonen's algorithm is apparently unique because of the suffix pointer technique he uses, though I think that is what I'm having trouble understanding.

I'm also having trouble understanding:

  • exactly when and how the "active point" is assigned, used and changed
  • what is going on with the canonization aspect of the algorithm
  • Why the implementations I've seen need to "fix" bounding variables that they are using

EDIT (April 13, 2012)

Here is the completed source code that I've written and output based on jogojapan's answer below. The code outputs a detailed description and text-based diagram of the steps it takes as it builds the tree. It is a first version and could probably do with optimization and so forth, but it works, which is the main thing.

[Redacted URL, see updated link below]

EDIT (April 15, 2012)

The source code has been completely rewritten from scratch and now not only works correctly, but it supports automatic canonization and renders a nicer looking text graph of the output. Source code and sample output is at:

https://gist.github.com/2373868


Source: (StackOverflow)

How can I view a git log of just one user's commits?

When using git log, how can I filter by user so that I see only commits from that user?


Source: (StackOverflow)

Use grep --exclude/--include syntax to not grep through certain files

I'm looking for the string "foo=" (without quotes) in text files in a directory tree. It's on a common Linux machine, I have bash shell:

grep -ircl "foo=" *

In the directories are also many binary files which match "foo=". As these results are not relevant and slow down the search, I want grep to skip searching these files (mostly JPEG and PNG images). How would I do that?

I know there are the --exclude=PATTERN and --include=PATTERN options, but what is the pattern format? The man page of grep says:

--include=PATTERN     Recurse in directories only searching file matching PATTERN.
--exclude=PATTERN     Recurse in directories skip file matching PATTERN.

Searching on grep include, grep include exclude, grep exclude and variants did not find anything relevant

If there's a better way of grepping only in certain files, I'm all for it; moving the offending files is not an option. I can't search only certain directories (the directory structure is a big mess, with everything everywhere). Also, I can't install anything, so I have to do with common tools (like grep or the suggested find).

UPDATES: @Adam Rosenfield's answer is just what I was looking for:

grep -ircl --exclude=\*.{png,jpg} "foo=" *

@rmeador's answer is also a good solution:

grep -Ir --exclude="*\.svn*" "pattern" *

It searches recursively, ignores binary files, and doesn't look inside Subversion hidden folders.(...)


Source: (StackOverflow)

Solr vs. ElasticSearch

What are the core architectural differences between these technologies?

Also, what use cases are generally more appropriate for each?


Source: (StackOverflow)

How to search through all Git and Mercurial commits in the repository for a certain string?

I have a Git repository with few branches and dangling commits. I would like to search all such commits in repository for a specific string.

I know how to get a log of all commits in history, but these don't include branches or dangling blobs, just HEAD's history. I want to get them all, to find a specific commit that got misplaced.

I would also like to know how to do this in Mercurial, as I'm considering the switch.


Source: (StackOverflow)

Grep, but only certain file extensions

I am working on writing some scripts to Grep certain directories, but these directories contain all sorts of file types.

I want to grep just .h and .cpp for now, but maybe a few others in the future.

So far I have:

{ grep -r -i CP_Image ~/path1/;

grep -r -i CP_Image ~/path2/;

grep -r -i CP_Image ~/path3/;

grep -r -i CP_Image ~/path4/;

grep -r -i CP_Image ~/path5/;} 

| mailx -s GREP email@domain.com

Can anyone show me how I would now add just specific file extensions?


Source: (StackOverflow)

How to combine 2 or more querysets in a Django view?

I am trying to build the search for a Django site I am building, and in the search I am searching in 3 different models. And to get pagination on the search result list I would like to use a generic object_list view to display the results. But to do that i have to merge 3 querysets into one.

How can i do that? I've tried this:

result_list = []            
page_list = Page.objects.filter(Q(title__icontains=cleaned_search_term) | Q(body__icontains=cleaned_search_term))
article_list = Article.objects.filter(Q(title__icontains=cleaned_search_term) | Q(body__icontains=cleaned_search_term) | Q(tags__icontains=cleaned_search_term))
post_list = Post.objects.filter(Q(title__icontains=cleaned_search_term) | Q(body__icontains=cleaned_search_term) | Q(tags__icontains=cleaned_search_term))

for x in page_list:
    result_list.append(x)
for x in article_list:
    result_list.append(x)
for x in post_list:
    result_list.append(x)

return object_list(request, queryset=result_list, template_object_name='result',
                   paginate_by=10, extra_context={'search_term': search_term},
                   template_name="search/result_list.html")

But this doesn't work I get an error when I try to use that list in the generic view. The list is missing the clone attribute.

Anybody know how I can merge the three lists, page_list, article_list and post_list?


Source: (StackOverflow)

How to grep Git commit diffs or contents for a certain word?

In a Git code repository I want to list all commits that contain a certain word. I tried this

git log -p | grep --context=4 "word"

but it does not necessarily give me back the filename (unless it's less that 5 lines away from the word I searched for. I also tried

git grep "word"

but it gives me only present files and not the history.

How do I search the entire history so I can follow changes on a particular word? I mean to search my codebase for occurrences of word to track down changes (search in files history).


Source: (StackOverflow)

How can I exclude one word with grep?

I need something like:

grep ^"unwanted_word"XXXXXXXX

Source: (StackOverflow)

What is the difference between Python's re.search and re.match?

What is the difference between the search() and match() functions in the Python re module?

I've read the documentation (current documentation), but I never seem to remember it. I keep having to look it up and re-learn it. I'm hoping that someone will answer it clearly with examples so that (perhaps) it will stick in my head. Or at least I'll have a better place to return with my question and it will take less time to re-learn it.


Source: (StackOverflow)

How to design RESTful search/filtering?

I'm currently designing and implementing a RESTful API in PHP. However, I have been unsuccessful implementing my initial design.

GET /users # list of users
GET /user/1 # get user with id 1
POST /user # create new user
PUT /user/1 # modify user with id 1
DELETE /user/1 # delete user with id 1

So far pretty standard, right?

My problem is with the first one GET /users. I was considering sending parameters in the request body to filter the list. This is because I want to be able to specify complex filters without getting a super long url, like:

GET /users?parameter1=value1&parameter2=value2&parameter3=value3&parameter4=value4

Instead I wanted to have something like:

GET /users
# Request body:
{
    "parameter1": "value1",
    "parameter2": "value2",
    "parameter3": "value3",
    "parameter4": "value4"
}

which is much more readable and gives you great possibilities to set complex filters.

Anyway, file_get_contents('php://input') didn't return the request body for GET requests. I also tried http_get_request_body(), but the shared hosting that I'm using doesn't have pecl_http. Not sure it would have helped anyway.

I found this question and realized that GET probably isn't supposed to have a request body. It was a bit inconclusive, but they advised against it.

So now I'm not sure what to do. How do you design a RESTful search/filterng function?

I suppose I could use POST, but that doesn't seem very RESTful.


Source: (StackOverflow)