How to Solve the Josephus Problem Using a Linked List in Python

In this tutorial, we will learn how to program "How to Solve the Josephus Problem Using a Linked List in Python". The objective is to solve the Josephus problem using a linked list. This tutorial will guide you step by step through methods for solving the Josephus problem. By the end of this tutorial, you will have a solid understanding of how to implement this task effectively in Python, helping you strengthen your problem-solving abilities and improve your coding skills.

This topic is straightforward and easy to understand. Simply follow the instructions provided, and you will complete it with ease. The program will guide you step by step through the process of solving the Josephus problem. So, let’s dive into the coding process!

Getting Started:

First you will have to download & install the Python IDLE's, here's the link for the Integrated Development And Learning Environment for Python https://www.python.org/downloads/.

Creating Main Function

This is the main function of the application. The following code will display a simple GUI in terminal console that will display program. To do this, simply copy and paste these blocks of code into the IDLE text editor.
  1. class Node:
  2.     def __init__(self, data):
  3.         self.data = data
  4.         self.next = None
  5.  
  6.  
  7. class CircularLinkedList:
  8.     def __init__(self):
  9.         self.head = None
  10.  
  11.     def append(self, data):
  12.         node = Node(data)
  13.         self.insert_at_end(node)
  14.  
  15.     def get_node(self, index, start):
  16.         if self.head is None:
  17.             return None
  18.         current = start
  19.         for _ in range(index):
  20.             current = current.next
  21.         return current
  22.  
  23.     def get_prev_node(self, ref_node):
  24.         if self.head is None:
  25.             return None
  26.         current = self.head
  27.         while current.next != ref_node:
  28.             current = current.next
  29.         return current
  30.  
  31.     def insert_after(self, ref_node, new_node):
  32.         new_node.next = ref_node.next
  33.         ref_node.next = new_node
  34.  
  35.     def insert_before(self, ref_node, new_node):
  36.         prev_node = self.get_prev_node(ref_node)
  37.         self.insert_after(prev_node, new_node)
  38.  
  39.     def insert_at_end(self, new_node):
  40.         if self.head is None:
  41.             self.head = new_node
  42.             new_node.next = new_node
  43.         else:
  44.             self.insert_before(self.head, new_node)
  45.  
  46.     def remove(self, node):
  47.         if self.head.next == self.head:
  48.             self.head = None
  49.         else:
  50.             prev_node = self.get_prev_node(node)
  51.             prev_node.next = node.next
  52.             if self.head == node:
  53.                 self.head = node.next
  54.  
  55.  
  56. def has_one_node(cllist):
  57.     return cllist.head.next == cllist.head
  58.  
  59.  
  60. def get_josephus_solution(cllist, k):
  61.     if cllist.head is None:
  62.         return None
  63.     start = cllist.head
  64.     while not has_one_node(cllist):
  65.         to_remove = cllist.get_node(k - 1, start)
  66.         start = to_remove.next
  67.         cllist.remove(to_remove)
  68.     return cllist.head.data
  69.  
  70.  
  71. # ------------------- MAIN PROGRAM LOOP -------------------
  72. while True:
  73.     print("\n================ Solve the Josephus Problem Using a Linked List ================\n")
  74.  
  75.     a_cllist = CircularLinkedList()
  76.     n = int(input('Input number of people: '))
  77.     k = int(input('The kth person will be executed. Input k: '))
  78.  
  79.     for i in range(1, n + 1):
  80.         a_cllist.append(i)
  81.  
  82.     ans = get_josephus_solution(a_cllist, k)
  83.     print(f'The person at position {ans} won\'t be killed.')
  84.  
  85.     opt = input("\nDo you want to try again? (yes/no): ").strip().lower()
  86.     if opt == 'no':
  87.         print("Exiting program...")
  88.         break
  89.     elif opt != 'yes':
  90.         print("Invalid choice. Exiting program...")
  91.         break

Output:

There you have it we successfully created How to Solve the Josephus Problem Using a Linked List in Python. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

More Tutorials for Python Language

Python Tutorials