Interview Query

N-gram Dictionary

3
Have you seen this question before?

Write a function get_ngrams to take in a word (string) and return a dictionary of n-grams and their frequency in the given string.

Example 1:

Input:

string = 'banana' 
n=2

Output:

output = {'ba':1, 'an':2, 'na':2} 

Example 2:

Input:

string = 'banana' 
n=3 

Output:

output = {'ban':1, 'ana':2, 'nan':1}
Next question: Compute Variance
.....
Python 3.9.6
Loading editor
Use Shift + Enter to run code