Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Python Articles
Page 10 of 852
Python Program to Select the nth Smallest Element from a List in Expected Linear Time
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 MorePython Program to Select the nth Largest Element from a List in Expected Linear Time
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 MorePython Program to Reverse a Stack using Recursion
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 MorePython Program to Implement Stack Using Two Queues
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 MorePython Program to Implement Queues using Stacks
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 MorePython Program to Display Fibonacci Sequence Using Recursion
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 MoreFind yesterday’s, today’s and tomorrow’s date in Python
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 MorePython program to find difference between current time and given time
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 MorePython Program to Count Number of Leaf Node in a Tree
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 MorePython Program to Count Number of Non Leaf Nodes of a given Tree
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