Python Articles

Page 502 of 855

Python Program to Reverse a Stack using Recursion

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 724 Views

A stack is a Last-In-First-Out (LIFO) data structure. To reverse a stack using recursion, we need two key functions: one to reverse the stack and another to insert elements at the bottom of the stack. Stack Implementation First, let's create a stack class with basic operations ? class Stack: def __init__(self): self.items = [] def is_empty(self): return len(self.items) == 0 ...

Read More

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

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 466 Views

When it is required to select the nth largest element from a list in linear time complexity, we can use the QuickSelect algorithm. This algorithm uses a partitioning approach similar to QuickSort but only recurses on one side, achieving expected O(n) time complexity. The algorithm works by partitioning the list around a pivot element and then deciding which partition contains the nth largest element ? How the Algorithm Works The QuickSelect algorithm consists of two main functions: select_largest − The main function that finds the ith largest element start_partition − A helper function that partitions ...

Read More

How to get the details of a user-defined function in a database from AWS Glue Data catalog using Boto3

Ashish Anand
Ashish Anand
Updated on 25-Mar-2026 393 Views

Let's see how a user can get the details of a specified function definition from AWS Glue Data Catalog using the Boto3 library. Problem Statement Use boto3 library in Python to get the details of a user-defined function named insert_employee_record in database employee from AWS Glue Data Catalog. Approach Step 1: Import boto3 and botocore exceptions to handle exceptions. Step 2: database_name and function_name are the required parameters. It fetches the definition of a given function in a specified database. Step 3: Create an AWS session using boto3. Make sure region_name is mentioned in the ...

Read More

How to get the details of a trigger from AWS Glue Data catalog using Boto3

Ashish Anand
Ashish Anand
Updated on 25-Mar-2026 389 Views

AWS Glue Data Catalog allows you to manage triggers for data processing workflows. Using Boto3, you can programmatically retrieve detailed information about any trigger in your account. Prerequisites Before getting trigger details, ensure you have: AWS credentials configured (via AWS CLI, IAM roles, or environment variables) Proper IAM permissions for Glue operations Boto3 library installed: pip install boto3 Approach To get trigger details from AWS Glue Data Catalog: Step 1: Import boto3 and botocore exceptions to handle errors Step 2: Create an AWS session and Glue client Step 3: Call get_trigger() ...

Read More

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

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 540 Views

Finding the nth smallest element from a list in expected linear time requires implementing the Quickselect algorithm. This algorithm uses partitioning similar to quicksort but only searches one partition, achieving O(n) average time complexity. The algorithm works by selecting a pivot element, partitioning the list around it, and then recursively searching only the partition that contains the nth smallest element. Algorithm Implementation Here's the implementation using two functions − one for selection and another for partitioning ? def select_smallest(numbers, beg, end, i): if end - beg k: ...

Read More

Python Program to Check String is Palindrome using Stack

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 989 Views

When it is required to check if a string is a palindrome using stack data structure, a stack class is created, and push and pop methods are defined to add and delete values from stack. Another method checks to see if the stack is empty or not. A palindrome is a word that reads the same forwards and backwards, like "madam" or "racecar". Using a stack's LIFO (Last In, First Out) property, we can reverse a string and compare it with the original. Stack Implementation First, let's create a stack class with basic operations ? ...

Read More

Python Program to Reverse only First N Elements of a Linked List

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 292 Views

When it is required to reverse a specific set of elements in a linked list, a method named 'reverse_list' is defined. This iterates through the list and reverses the first N elements while keeping the remaining elements unchanged. Below is a demonstration for the same − Example class Node: def __init__(self, data): self.data = data self.next = None class LinkedList_structure: def __init__(self): self.head ...

Read More

Python Program to Find Number of Occurrences of All Elements in a Linked List

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 237 Views

When working with linked lists, it's often useful to find the number of occurrences of elements. This implementation demonstrates how to create a linked list and count occurrences of specific elements using a custom count_elem method. Below is a demonstration for the same − Linked List Implementation First, we create the basic structure with Node and LinkedList classes: class Node: def __init__(self, data): self.data = data self.next = None class LinkedList_structure: ...

Read More

Python Program to Find the first Common Element between the 2 given Linked Lists

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 347 Views

When it is required to find the common element that occurs for the first time between two linked lists, a method to add elements to the linked list, and a method to get the common element that occurs for the first time in these linked lists is defined. Below is a demonstration for the same − Example class Node: def __init__(self, data): self.data = data self.next = None class LinkedList_structure: def ...

Read More

Python Program to Add Corresponding Positioned Elements of 2 Linked Lists

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 232 Views

When working with linked lists, you may need to add corresponding elements from two lists at the same positions. This involves traversing both lists simultaneously and creating a new linked list with the sum of elements at each position. Below is a demonstration for the same − Node and LinkedList Classes First, we define the basic structure for nodes and linked lists ? class Node: def __init__(self, data): self.data = data self.next = None ...

Read More
Showing 5011–5020 of 8,546 articles
« Prev 1 500 501 502 503 504 855 Next »
Advertisements