Hafeezul Kareem

Hafeezul Kareem

258 Articles Published

Articles by Hafeezul Kareem

Page 8 of 26

Custom len() Function In Python

Hafeezul Kareem
Hafeezul Kareem
Updated on 25-Mar-2026 871 Views

The len() function in Python returns the number of items in an object. You can create a custom version by iterating through any iterable and counting its elements. How len() Works Internally Python's built-in len() function calls the __len__() method of an object. For custom implementation, we iterate through elements and count them manually. Creating a Custom Length Function Basic Implementation Here's how to implement a custom length function using iteration − def custom_length(iterable): """Calculate length of any iterable by counting elements""" count = 0 ...

Read More

Why importing star is a bad idea in python

Hafeezul Kareem
Hafeezul Kareem
Updated on 25-Mar-2026 597 Views

Importing all methods from a module using from module import * (star import) is considered a bad practice in Python for several important reasons. Problems with Star Imports Star imports create the following issues ? Namespace pollution − All module functions are imported into the current namespace Name conflicts − Imported functions can override your local functions Unclear code origin − Difficult to identify which module a function belongs to IDE limitations − Code editors cannot provide proper autocompletion and error detection Creating the Sample Module First, let's create a simple module file ...

Read More

Ways to increment a character in python

Hafeezul Kareem
Hafeezul Kareem
Updated on 25-Mar-2026 13K+ Views

In this tutorial, we are going to see different methods to increment a character in Python. Characters cannot be directly incremented like integers, so we need to convert them to their ASCII values first. Why Direct Increment Fails Let's first see what happens if we try to add an integer to a character directly without proper conversion ? # str initialization char = "t" # try to add 1 to char char += 1 # gets an error If you execute the above program, it produces the following result − ...

Read More

Swap two variables in one line in C/C++, Python, PHP and Java

Hafeezul Kareem
Hafeezul Kareem
Updated on 15-Mar-2026 697 Views

In this tutorial, we are going to learn how to swap two variables in different languages. Swapping means interchanging the values of two variables. Let's see an example. Input a = 3 b = 5 Output a = 5 b = 3 Let's see how to achieve this in different programming languages. Python We can swap variables with one line of code in Python using tuple unpacking − Example # initializing the variables a, b = 3, 5 # printing before swapping print("Before swapping:-", a, b) ...

Read More

k-Rough Number or k-Jagged Number in C++

Hafeezul Kareem
Hafeezul Kareem
Updated on 11-Mar-2026 314 Views

In this tutorial, we are going to write a program that checks whether the given number is k-rough or k-jagged number or not.The number whose smallest prime factor is greater than or equal to the given k, it is called k-rough or k-jagged number.Let's see the steps to solve the problem.Initialise the numbers n and k.Find all the prime numbers that are factors of n and store them in a vector.Get the first element from the vector and compare it with k to check whether n is k-rough or k-jagged number or not.ExampleLet's see the code.#include using namespace std; ...

Read More

K-th digit in 'a' raised to power 'b' in C++

Hafeezul Kareem
Hafeezul Kareem
Updated on 11-Mar-2026 417 Views

In this tutorial, we are going to write a program that finds the k-th digit from the right side in the number abIt's a straightforward problem. Let's see the steps to solve it.Initialise the numbers a, b, and k.Find the value of abusing pow method.Write a loop that iterates until power value is less than zero or count is less than k.Get the last digit from the power value.Increment the counter.Check whether k and counter are equal or not.Return the digit if they are equalReturn -1.ExampleLet's see the code.#include using namespace std; int getTheDigit(int a, int b, int k) ...

Read More

K-th Element of Two Sorted Arrays in C++

Hafeezul Kareem
Hafeezul Kareem
Updated on 11-Mar-2026 279 Views

In this tutorial, we are going to write a program that finds the k-th element from thee merged array of two sorted arrays.Let's see the steps to solve the problem.Initialise the two sorted arrays.Initialise an empty array of size m + n.Merge the two arrays into the new array.Return the k - 1 element from the merged array.ExampleLet's see the code.#include using namespace std; int findKthElement(int arr_one[], int arr_two[], int m, int n, int k) {    int sorted_arr[m + n];    int i = 0, j = 0, index = 0;    while (i < m && j ...

Read More

K-th Greatest Element in a Max-Heap in C++

Hafeezul Kareem
Hafeezul Kareem
Updated on 11-Mar-2026 1K+ Views

In this tutorial, we are going to write a program that finds the k-th largest element from the max-heap.We will use priority queue to solve the problem. Let's see the steps to complete the program.Initialise the max-heap with correct values.Create a priority queue and insert the root node of the max-heap.Write a loop that iterates k - 1 times.Pop the greatest element from the queue.Add the left and right nodes of the above node into the priority queue.The greatest element in priority queue is the k-th greatest element now.Return it.ExampleLet's see the code.#include using namespace std; struct Heap { ...

Read More

k-th missing element in an unsorted array in C++

Hafeezul Kareem
Hafeezul Kareem
Updated on 11-Mar-2026 258 Views

In this tutorial, we are going to write a program that finds out the k-th missing element in the given unsorted array.Find the k-th number that is missing from min to max in the given unsorted array. Let's see the steps to solve the problem.Initialise the unsorted array.Insert all the elements into a set.Find the max and min elements from the array.Write a loop that iterates from min to max and maintain a variable for the count.If the current element is present in the set, then increment the count.If the count is equal to k, then return i.ExampleLet's see the ...

Read More

k-th missing element in sorted array in C++

Hafeezul Kareem
Hafeezul Kareem
Updated on 11-Mar-2026 361 Views

In this tutorial, we are going to write a program that finds out the k-th missing element in the given sorted array.Find the k-th number that is missing from min to max in the given unsorted array. Let's see the steps to solve the problem.Initialise the sorted array.Initialise two variables difference and count with k.Iterate over the array.If the current element is not equal to the next element.Find the difference between the two numbers.If the difference is greater than or equal to k, then return current element plus count.Else subtract difference from the count.Return -1.ExampleLet's see the code.#include using ...

Read More
Showing 71–80 of 258 articles
« Prev 1 6 7 8 9 10 26 Next »
Advertisements