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
-
Economics & Finance
Articles on Trending Technologies
Technical articles with clear explanations and examples
Python Program to Implement Binomial Tree
A binomial tree is a data structure used in computer science and financial modeling. It consists of nodes where each tree of order k has 2^k nodes. In Python, we can implement this using object-oriented programming with a class that manages tree creation and combination operations. Binomial Tree Class Implementation Here's how to create a binomial tree class with methods to add children and combine trees ? class BinomialTree: def __init__(self, key): self.key = key self.children ...
Read MorePython Program to Split the array and add the first part to the end
When it is required to split the array and add the first part to the end, we can use list slicing or implement a rotation algorithm. This operation is commonly known as left rotation of an array. A list can be used to store heterogeneous values (i.e data of any data type like integer, floating point, strings, and so on). Method 1: Using List Slicing The simplest approach is to use Python's list slicing to split and rearrange the array ? def split_and_rotate(arr, k): """Split array at position k and move ...
Read MorePython Program to Create a Class and Get All Possible Subsets from a Set of Distinct Integers
When it is required to create a class to get all the possible subsets of integers from a list, object oriented method is used. Here, a class is defined, and attributes are defined. Functions are defined within the class that perform certain operations. An instance of the class is created, and the functions are used to perform operations. Below is a demonstration for the same − Example class get_subset: def sort_list(self, my_list): return self.subset_find([], sorted(my_list)) ...
Read MorePython Program to Create a Class wherein a Method accepts a String from the User and Another Prints it
When it is required to create a class that has a method to accept a string from the user and another method to print the string, object-oriented programming is used. Here, a class is defined with attributes and methods that perform specific operations on user input. Below is a demonstration for the same − Example class StringHandler: def __init__(self): self.string = "" def get_data(self): self.string = input("Enter ...
Read MorePython Program to Create a class performing Calculator Operations
When it is required to create a class that performs calculator operations, object oriented method is used. Here, a class is defined, and attributes are defined. Functions are defined within the class that perform certain operations. An instance of the class is created, and the functions are used to perform calculator operations. Below is a demonstration for the same − Example class calculator_implementation(): def __init__(self, in_1, in_2): self.a = in_1 self.b = in_2 ...
Read MoreRemove duplicate tuples from list of tuples in Python
When working with lists of tuples, you often need to remove duplicates to clean your data. Python provides several methods to accomplish this task efficiently. The any() method checks if any value in an iterable is True. If at least one value is True, it returns True; otherwise, it returns False. The enumerate() method adds a counter to an iterable and returns it as an enumerate object, which is useful for getting both index and value during iteration. Method 1: Using set() (Most Efficient) The simplest approach is to convert the list to a set and ...
Read MoreInitialize tuples with parameters in Python
When it is required to initialize tuples with certain parameters, the tuple() method and the * operator can be used. The tuple() method converts the iterable passed to it as a parameter into a tuple. The * operator can be used to repeat a single value multiple times, making it useful for creating tuples with default values. Basic Tuple Initialization Here's how to create a tuple with repeated default values and modify specific positions ? N = 6 print("The value of N has been initialized to " + str(N)) default_val = 2 print("The default ...
Read MoreConvert Tuple to integer in Python
When it is required to convert a tuple into an integer, the lambda function and the reduce function can be used. This approach treats each tuple element as a digit and combines them into a single integer. Anonymous function is a function which is defined without a name. The reduce() function takes two parameters − a function and a sequence, where it applies the function to all the elements of the tuple/sequence. It is present in the functools module. In general, functions in Python are defined using def keyword, but anonymous function is defined with the help of ...
Read MorePython Program to Implement Shell Sort
Shell Sort is an efficient sorting algorithm that improves upon insertion sort by comparing elements separated by a gap (interval). It starts with a large gap and gradually reduces it until the gap becomes 1, at which point it performs a final insertion sort on the nearly sorted array. The algorithm works by sorting sub-arrays of elements that are a certain distance apart, making the overall array more organized with each pass ? How Shell Sort Works Shell Sort follows these steps ? Start with a gap (interval) equal to half the array length Compare ...
Read MoreSort lists in tuple in Python
When it is required to sort the lists in a tuple, the tuple() method, the sorted() method and a generator expression can be used. The sorted() method is used to sort the elements of a list. It is a built-in function that returns the sorted list without modifying the original. Generator expression is a simple way of creating iterators. It automatically implements a class with __iter__() and __next__() methods and keeps track of the internal states, as well as raises StopIteration exception when no values are present that could be returned. The tuple() method takes an iterable ...
Read More