feat(searches): Add Floyd's Cycle-Finding Algorithm - #13609
Conversation
Signed-off-by: Aditya Kumar <aditya.kumar60@infosys.com>
There was a problem hiding this comment.
Click here to look at the relevant links ⬇️
🔗 Relevant Links
Repository:
Python:
Automated review generated by algorithms-keeper. If there's any problem regarding this review, please open an issue about it.
algorithms-keeper commands and options
algorithms-keeper actions can be triggered by commenting on this PR:
@algorithms-keeper reviewto trigger the checks for only added pull request files@algorithms-keeper review-allto trigger the checks for all the pull request files, including the modified files. As we cannot post review comments on lines not part of the diff, this command will post all the messages in one comment.NOTE: Commands are in beta and so this feature is restricted only to a member or owner of the organization.
|
|
||
|
|
||
| def floyds_cycle_finding( | ||
| f: Callable[[Any], Any], x0: Any |
There was a problem hiding this comment.
Please provide descriptive name for the parameter: f
for more information, see https://pre-commit.ci
dbc879a to
77a3dd9
Compare
…rithm Signed-off-by: Aditya Kumar <aditya.kumar60@infosys.com>
44efada to
0dd6821
Compare
for more information, see https://pre-commit.ci
1ce9e37 to
1354285
Compare
Signed-off-by: Aditya Kumar <aditya.kumar60@infosys.com>
1354285 to
19aca57
Compare
|
@priya-sundaram-dev Are these tests running in an infinite loop in CI? |
|
Yes — confirmed locally. Two of the three doctests spin forever, which is why the
Root cause is the asymmetric setup in Phase 1: The classic tortoise-and-hare form fixes it — start both at the same point and check equality after advancing: tortoise = successor_function(start_value)
hare = successor_function(successor_function(start_value))
while tortoise != hare:
if tortoise is None or hare is None:
return None
tortoise = successor_function(tortoise)
hare = successor_function(successor_function(hare))With that Phase 1, both examples terminate. One heads-up though: the list case then returns Happy to open a small PR with the corrected Phase 1 + fixed doctests if that's useful. |
Describe your change:
This pull request introduces an implementation of Floyd's Cycle-Finding Algorithm, also known as the "Tortoise and Hare" algorithm. This well-known algorithm is used to efficiently detect a cycle in a sequence of iterated values and also finds the cycle's starting point and length.
The file
floyds_cycle_finding.pyhas been added to thesearches/directory.Checklist: