-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #91 from BrianLusina/feat/circular-linked-list
Circular linked list
- Loading branch information
Showing
10 changed files
with
463 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# Josephus Circle | ||
|
||
Children often play a counting-out game to randomly select one person from the group by singing a rhyme. The purpose is | ||
to select one person, either as a straightforward winner, or as someone who is eliminated. | ||
|
||
Josephus problem is related to this concept. In this problem, people are standing in one circle waiting to be executed. | ||
Following points list the specifications of Josephus problem: | ||
|
||
The counting out begins at a specified point in a circle and continues around the circle in a fixed direction. | ||
|
||
In each step, a certain number of people are skipped and the next person is executed. | ||
|
||
For example, if we have 𝑛 people, and 𝑘-1 people are skipped every time, it means that the | ||
𝑘th person is executed. Here, 𝑘 is the step-size. | ||
|
||
## Reference | ||
|
||
- https://en.wikipedia.org/wiki/Josephus_problem | ||
- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
from datastructures.linked_lists.circular import CircularLinkedList | ||
from datastructures.linked_lists.circular.node import CircularNode | ||
|
||
|
||
def josephus_circle(circular_list: CircularLinkedList, step: int) -> CircularNode: | ||
current = circular_list.head | ||
|
||
length = len(circular_list) | ||
while length > 1: | ||
count = 1 | ||
while count != step: | ||
current = current.next | ||
count += 1 | ||
circular_list.delete_node(current) | ||
current = current.next | ||
length -= 1 | ||
|
||
return current |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import unittest | ||
from datastructures.linked_lists.circular import CircularLinkedList | ||
from datastructures.linked_lists.circular.node import CircularNode | ||
from . import josephus_circle | ||
|
||
|
||
class JosephusCircularTestCase(unittest.TestCase): | ||
def test_1(self): | ||
"""should run through the circle of 1-2-3-4 with a step of 2 remaining with 1""" | ||
data = [1, 2, 3, 4] | ||
circular_linked_list = CircularLinkedList() | ||
for d in data: | ||
circular_linked_list.append(d) | ||
|
||
step = 2 | ||
expected = CircularNode(1) | ||
actual = josephus_circle(circular_linked_list, step) | ||
self.assertEqual(expected, actual) | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.