Programming Articles

Page 502 of 2547

Max Increase to Keep City Skyline in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 353 Views

The Max Increase to Keep City Skyline problem asks us to find the maximum total sum that building heights can be increased while maintaining the original skyline when viewed from all four directions. The skyline from each direction is determined by the maximum height in each row (left/right view) and each column (top/bottom view). Problem Understanding Given a 2D grid where each value represents a building height, we need to ? Calculate the skyline from left/right (maximum in each row) Calculate the skyline from top/bottom (maximum in each column) For each position, the maximum possible height ...

Read More

Design Log Storage System in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 454 Views

A log storage system manages logs with unique IDs and timestamps. Each timestamp follows the format Year:Month:Day:Hour:Minute:Second (e.g., "2019:01:01:23:59:59") with zero-padded decimal numbers. We need to implement two main functions: put(id, timestamp) − Stores a log with its unique ID and timestamp retrieve(start, end, granularity) − Returns log IDs within a time range based on specified granularity (Year, Month, Day, Hour, Minute, Second) How Granularity Works The granularity parameter determines the precision level for comparison. For example, with granularity "Day", timestamps are compared only up to the day level, ignoring hours, minutes, and seconds. ...

Read More

Python - Inserting item in sorted list maintaining order

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

In this article, we are going to learn how to insert an item in a sorted list while maintaining the order. Python has a built-in module called bisect that helps us insert any element in the appropriate position in the list efficiently. Using bisect.insort() The bisect.insort() method uses binary search to find the correct insertion point and inserts the element while maintaining the sorted order ? # importing the module import bisect # initializing the list, element numbers = [10, 23, 27, 32] element = 25 # inserting element using bisect.insort(list, element) bisect.insort(numbers, element) ...

Read More

Python - Intersect two dictionaries through keys

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

In Python, intersecting two dictionaries through keys means creating a new dictionary containing only the keys that exist in both dictionaries. This is useful for finding common data between two datasets. Input: dict_1 = {'A': 1, 'B': 2, 'C': 3} dict_2 = {'A': 1, 'C': 4, 'D': 5} Output: {'A': 1, 'C': 3} Using Dictionary Comprehension Dictionary comprehension provides the most readable approach to intersect dictionaries by keys ? # initializing the dictionaries dict_1 = {'A': 1, 'B': 2, 'C': 3} dict_2 = {'A': 1, 'C': 4, 'D': 5} # finding ...

Read More

Python - Intersection of multiple lists

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

In this article, we will explore different approaches to find the intersection of multiple lists containing sub-lists. The intersection returns only the common sub-lists present in all input lists. Using List Comprehension The simplest approach uses list comprehension to check if each sub-list exists in all other lists − # initializing the lists list_1 = [[1, 2], [3, 4], [5, 6]] list_2 = [[3, 4], [7, 8]] # finding the common items from both lists result = [sub_list for sub_list in list_1 if sub_list in list_2] # printing the result print("Intersection using list comprehension:", ...

Read More

Python - Intersection of two String

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

String intersection finds the common characters between two strings. Python offers multiple approaches: iterating through characters or using set operations for efficient intersection. Using Character Iteration This method iterates through the first string and adds characters that exist in both strings while maintaining order ? # initializing the string string_1 = 'tutorialspoint' string_2 = 'tut' result = '' # finding the common chars from both strings for char in string_1: if char in string_2 and not char in result: result += char ...

Read More

Python - Join tuple elements in a list

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

In this article, we are going to learn how to join tuple elements in a list. Python provides several approaches to convert tuples containing strings into joined strings using methods like join() and map(). Using map() with a Custom Function Create a function to join tuple elements and apply it to all tuples using map() ? # initializing the list with tuples string_tuples = [('A', 'B', 'C'), ('Tutorialspoint', 'is a', 'popular', 'site', 'for tech learnings')] # function that converts tuple to string def join_tuple_string(strings_tuple): return ' '.join(strings_tuple) # joining all ...

Read More

Python - Joining only adjacent words in list

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

In this article, we are going to learn how to join adjacent words in a list while keeping digits separate. This technique is useful when processing mixed data that contains both text and numeric values. Approach To solve this problem, we need to ? Initialize the list with mixed string and numeric elements Separate words from digits using isalpha() and isdigit() methods Join all the words together using the join() method Add all digits to the result list while maintaining their order Using List Comprehension The most straightforward approach uses list comprehension to ...

Read More

Python - Joining unicode list elements

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

In Python, when working with Unicode strings, you might need to join list elements that contain Unicode characters. This is straightforward in Python 3 since all strings are Unicode by default. Basic Unicode String Joining The simplest way to join Unicode strings is using the join() method ? # Unicode strings with special characters unicode_strings = ['Hello', 'Wörld', 'Pythön', '🐍'] # Join with space separator result = ' '.join(unicode_strings) print(result) Hello Wörld Pythön 🐍 Joining Mixed Content Lists When your list contains mixed data types, convert them to strings ...

Read More

Python - Largest number possible from list of given numbers

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

In this article, we will learn how to find the largest possible number from a given list of numbers by arranging them optimally. We'll explore two different approaches to solve this problem effectively. Method 1: Using itertools.permutations The first approach generates all possible permutations of the numbers and finds the maximum value ? import itertools # initializing the list numbers = [45, 35, 138, 43, 67] # result list to store all permutations result = [] # generate all permutations and join them as strings for permutation in itertools.permutations(str(number) for number in numbers): ...

Read More
Showing 5011–5020 of 25,466 articles
« Prev 1 500 501 502 503 504 2547 Next »
Advertisements