human
I found an interesting problem on https://www.codewars.com/kata/5c1b23aa34fb628f2e000043:
# 'Magic' recursion call depth number

This Kata was designed as a Fork to the one from donaldsebleung Roboscript series with a reference to:

https://www.codewars.com/collections/roboscript

It is not more than an extension of Roboscript infinite "single-" mutual recursion handling to a "multiple-" case.

One can suppose that you have a machine that works through a specific language. It uses the script, which consists of 3 major commands:

 - `F` - Move forward by 1 step in the direction that it is currently pointing.

 - `L` - Turn "left" (i.e. rotate 90 degrees anticlockwise).

 - `R` - Turn "right" (i.e. rotate 90 degrees clockwise).

The number n afterwards enforces the command to execute n times.

To improve its efficiency machine language is enriched by patterns that are containers to pack and unpack the script.

The basic syntax for defining a pattern is as follows:

`pnq`

Where:

 - `p` is a "keyword" that declares the beginning of a pattern definition 

 - `n` is a non-negative integer, which acts as a unique identifier for the pattern (pay attention, it may contain several digits).

 - `` is a valid RoboScript code (without the angled brackets)

 - `q` is a "keyword" that marks the end of a pattern definition 

For example, if you want to define `F2LF2` as a pattern and reuse it later:

```
p333F2LF2q
```

To invoke a pattern, a capital `P` followed by the pattern identifier `(n)` is used:

```
P333
```

It doesn't matter whether the invocation of the pattern or the pattern definition comes first. Pattern definitions should always be parsed first.

```
P333p333P11F2LF2qP333p11FR5Lq
```


# ___Infinite recursion___

As we don't want a robot to be damaged or damaging someone else by becoming uncontrolable when stuck in an infinite loop, it's good to considere this possibility in the programs and to build a compiler that can detect such potential troubles before they actually happen.

* ### Single pattern recursion infinite loop

This is the simplest case, that occurs when the pattern is invoked...

I tried it in Python, but could not do it. Can you solve it?

 What is a the Python code you requested?
assistant
#Here is the Python code you requested.

import re
from collections import defaultdict, deque
def magic_call_depth_number(s):
    adjacent = defaultdict(list)
    for i, j, k in re.findall(r'(p\d+)(.+?)(q)', s):
        for n in re.findall(r'P\d+', j):
            adjacent[i.upper()].append(n)
    
    s, depths = re.sub(r'p.+?q', '', s), []
    
    def BFS(start):
        Q = deque([[start, 1, []]])
        while Q:
            node, depth, path = Q.popleft()
            path.append(node)
            if not adjacent[node] : depths.append(0)
            for i in adjacent[node]:
                if i not in path : Q.append([i, depth + 1, path[:]])
                else : depths.append(depth)
                
    [BFS(i) for i in re.findall(r'P\d+', s)]
    return [min(depths), max(depths)] if depths else [0,0]