Your question is Word Co-Occurrence in a Line. Start with the requirements on the right.
Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.
Tessian Email Security analyses message text for suspicious language patterns. Given one line of text and a window size, count how often each pair of distinct words co-occurs within that many token positions.
Implement find_cooccurrences(line, window_size). Tokenize line by extracting contiguous alphanumeric sequences, convert tokens to lowercase, and ignore punctuation. For every pair of distinct words whose positions differ by at most window_size - 1, increment that pair's count once. Each pair must be represented in lexicographic order as [word1, word2, count]. Return all pairs sorted lexicographically by word1, then word2. Return an empty list when no pair co-occurs.
Repeated occurrences count separately. For example, the two occurrences of alert in alert alert review do not form a pair with each other, but both can co-occur with review when the window permits it.
def find_cooccurrences(line, window_size):