Python Articles

Page 10 of 852

Python Program to Select the nth Smallest Element from a List in Expected Linear Time

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 516 Views

When it is required to select the nth smallest element from a list in linear time complexity, two methods are required. One method to find the smallest element, and another method that divides the list into two parts. This division depends on the ‘i’ value that is given by user. Based on this value, the list is split, and the smallest element is determined.Below is a demonstration of the same −Exampledef select_smallest(my_list, beg, end, i):    if end - beg k:       return select_smallest(my_list, pivot_val + 1, end, i - k)    return my_list[pivot_val] def ...

Read More

Python Program to Select the nth Largest Element from a List in Expected Linear Time

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 449 Views

When it is required to select the nth largest element from a list in linear time complexity, two methods are required. One method to find the largest element, and another method that divides the list into two parts. This division depends on the ‘i’ value that is given by user. Based on this value, the list is split, and the largest element is determined.Below is a demonstration of the same −Exampledef select_largest(my_list, beg, end, i):    if end - beg k:       return select_largest(my_list, beg, pivot_val, i - k)    return my_list[pivot_val] def start_partition(my_list, beg, ...

Read More

Python Program to Reverse a Stack using Recursion

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 705 Views

When it is required to reverse a stack data structure using recursion, a ‘stack_reverse’ method, in addition to methods to add value, delete value, and print the elements of the stack are defined.Below is a demonstration of the same −Exampleclass Stack_structure:    def __init__(self):       self.items = []    def check_empty(self):       return self.items == []    def push_val(self, data):       self.items.append(data)    def pop_val(self):       return self.items.pop()    def print_it(self):       for data in reversed(self.items):          print(data) def insert_bottom(instance, data):   ...

Read More

Python Program to Implement Stack Using Two Queues

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 612 Views

When it is required to implement a stack using two queues, a ‘Stack_structure’ class is required along with a Queue_structure class. Respective methods are defined in these classes to add and delete values from the stack and queue respectively.Below is a demonstration of the same −Exampleclass Stack_structure:    def __init__(self):       self.queue_1 = Queue_structure()       self.queue_2 = Queue_structure()    def check_empty(self):       return self.queue_2.check_empty()    def push_val(self, data):       self.queue_1.enqueue_operation(data)       while not self.queue_2.check_empty():          x = self.queue_2.dequeue_operation()          self.queue_1.enqueue_operation(x)     ...

Read More

Python Program to Implement Queues using Stacks

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 443 Views

When it is required to implement a queue using a stack, a queue class can be defined, where two stack instances can be defined. Different operations can be performed on the queue that are defined as methods in this class.Below is a demonstration of the same −Exampleclass Queue_structure:    def __init__(self):       self.in_val = Stack_structure()       self.out_val = Stack_structure()    def check_empty(self):       return (self.in_val.check_empty() and self.out_val.check_empty())    def enqueue_operation(self, data):       self.in_val.push_operation(data)    def dequeue_operation(self):       if self.out_val.check_empty():          while not self.in_val.check_empty():   ...

Read More

Python Program to Display Fibonacci Sequence Using Recursion

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 436 Views

When it is required to print the fibonacci sequence using the method of recursion, a method can be declared that calls the same method again and again until a base value is reached.Below is a demonstration of the same −Exampledef fibonacci_recursion(my_val):    if my_val

Read More

Find yesterday’s, today’s and tomorrow’s date in Python

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 524 Views

When it is required to find yesterday, today and tomorrow’s date with respect to current date, the current time is determined, and a method is used (in built) that helps find previous day and next day’s dates.Below is a demonstration of the same −Examplefrom datetime import datetime, timedelta present = datetime.now() yesterday = present - timedelta(1) tomorrow = present + timedelta(1) print("Yesterday was :") print(yesterday.strftime('%d-%m-%Y')) print("Today is :") print(present.strftime('%d-%m-%Y')) print("Tomorrow would be :") print(tomorrow.strftime('%d-%m-%Y'))OutputYesterday was : 05-04-2021 Today is : 06-04-2021 Tomorrow would be : 07-04-2021ExplanationThe required packages are imported into the environment.The current date is determined using ...

Read More

Python program to find difference between current time and given time

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 443 Views

When it is required to find the difference between the current time and a given time, a method can be defined, that takes the hours, minutes, and seconds as parameter. It then calculates the difference between two given times.Below is a demonstration of the same −Exampledef difference_time(h_1, m_1, h_2, m_2):    t_1 = h_1 * 60 + m_1    t_2 = h_2 * 60 + m_2    if (t_1 == t_2):       print("The times are the same")       return    else:       diff = t_2-t_1    hours = (int(diff / 60)) ...

Read More

Python Program to Count Number of Leaf Node in a Tree

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 703 Views

When it is required to count the number of leaf nodes in a Tree, a ‘Tree_structure’ class is created, methods to add root value, and other children values are defined. Various options are given that the user can select. Based on the user’s choice, the operation is performed on the Tree elements.Below is a demonstration of the same −Exampleclass Tree_structure:    def __init__(self, data=None):       self.key = data       self.children = []    def set_root_node(self, data):       self.key = data    def add_vals(self, node):       self.children.append(node)    def search_val(self, ...

Read More

Python Program to Count Number of Non Leaf Nodes of a given Tree

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 259 Views

When it is required to find the count of the non leaf nodes in a Tree, a ‘Tree_structure’ class is created, methods to set a root value, and to add other values are defined. Various options are given that the user can select. Based on the user’s choice, the operation is performed on the Tree elements.Below is a demonstration of the same −Exampleclass Tree_structure:    def __init__(self, data=None):       self.key = data       self.children = []    def set_root(self, data):       self.key = data    def add_vals(self, node):       self.children.append(node) ...

Read More
Showing 91–100 of 8,519 articles
« Prev 1 8 9 10 11 12 852 Next »
Advertisements