Python Articles - Page 612 of 829

Serialize and Deserialize BST in Python

Arnab Chakraborty
Updated on 17-Nov-2020 11:19:53

615 Views

Suppose we want to design an algorithm to serialize and deserialize a binary search tree. Serialization is the process of converting something (data structure or object) into a sequence of bits so that it can be stored in a file or memory buffer, or transmitted across a network connection link. This can be reconstructed later that process is deserialization.So, if the input is like [5, 2, 9, 1, 3, 7], then the output will be Serialized output 5.2.9.1.3.7.N.N.N.N.N.N.N Deserialized output: 1, 2, 3, 5, 7, 9, (inorder traversal)To solve this, we will follow these steps −Define a function serialize() . ... Read More

Max Increase to Keep City Skyline in Python

Arnab Chakraborty
Updated on 17-Nov-2020 11:09:19

318 Views

Suppose we have a 2-dimensional array called grid, where each value of grid[i][j] represents the height of a building located there. We can increase the height of any number of buildings, by any amount. Height 0 is considered to be a building as well. At the end, the "skyline" when viewed from all four directions of the grid, must be the same as the skyline of the original grid. Because a city's skyline is the outer contour of the rectangles formed by all the buildings when viewed from a distance. So we have to find the maximum total sum that ... Read More

Design Log Storage System in Python

Arnab Chakraborty
Updated on 16-Nov-2020 14:32:07

434 Views

Suppose we have some logs, that each log contains a unique id and timestamp. The Timestamp is a string that has the format: Year:Month:Day:Hour:Minute: Second, for example, 2019:01:01:23:59:59. All domains are zero-padded decimal numbers.We have to design a log storage system to implement the following functions −void Put(int id, string timestamp): This will take the log's unique id and timestamp, and it stores the log in the storage system.int[] Retrieve(String start, String end, String granularity): This will return the id of logs whose timestamps are within the range from start to end parameters. The granularity parameter indicates the time level ... Read More

Python - Inserting item in sorted list maintaining order

Hafeezul Kareem
Updated on 13-Nov-2020 19:17:11

8K+ Views

In this article, we are going to learn how to insert an item in a sorted list maintaining the order. Python has a built-in module called bisect that helps us to insert any element in an appropriate position in the list.Follow the below steps to write the code.Import the module bisect.Initialize list and element that need to insertThe module bisect has a method called insort that inserts an element into a list in an appropriate position. Use the method and insert the element.Print the list.Example Live Demo# importing the module import bisect # initializing the list, element numbers = [10, ... Read More

Python - Intersect two dictionaries through keys

Hafeezul Kareem
Updated on 13-Nov-2020 19:13:25

2K+ Views

In this article, we are going to learn how to intersect two dictionaries using keys. We have to create a new dictionary with common keys. Let's see an example.Input: dict_1 = {'A': 1, 'B': 2, 'C': 3} dict_2 = {'A': 1, 'C': 4, 'D': 5} Output: {'A': 1, 'C': 3}We are going to use the dictionary comprehension to solve the problem. Follow the below steps to write the code.Initialize dictionaries.Iterate over the dictionary one and add elements that are not in dictionary two.Print the result.Example Live Demo# initializing the dictionaries dict_1 = {'A': 1, 'B': 2, 'C': 3} dict_2 = ... Read More

Python - Intersection of multiple lists

Hafeezul Kareem
Updated on 13-Nov-2020 19:08:20

1K+ Views

In this article, we are going to see how to intersect two lists that contain multiple lists in different ways. Let's start in the traditional way.Follow the below the steps to solve the problemInitialize two lists with multiple listsIterate over the first list and add the current item in the new list if it presents in the second list as well.Print the result.Example Live Demo# initializing the lists list_1 = [[1, 2], [3, 4], [5, 6]] list_2 = [[3, 4]] # finding the common items from both lists result = [sub_list for sub_list in list_1 if sub_list in list_2] ... Read More

Python - Intersection of two String

Hafeezul Kareem
Updated on 13-Nov-2020 19:02:25

4K+ Views

In this article, we are going to learn how to intersect two strings in different ways.Follow the below the steps to solve the problem.Initialize two strings and an empty string.Iterate over the first string and add the current character to the new string if it presents in the second string as well and not present in new string already.Print the result.Example Live Demo# 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:     ... Read More

Python - Join tuple elements in a list

Hafeezul Kareem
Updated on 13-Nov-2020 18:56:17

11K+ Views

In this article, we are going to learn how to join tuple elements in a list. It's a straightforward thing using join and map methods. Follow the below steps to complete the task.Initialize list with tuples that contain strings.Write a function called join_tuple_string that takes a tuple as arguments and return a string.Join the tuples in the list using map(join_tuple_string, list) method.Convert the result to list.Print the result.Example# 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) -> str:    return ... Read More

Python - Joining only adjacent words in list

Hafeezul Kareem
Updated on 13-Nov-2020 18:52:49

359 Views

In this article, we are going to learn how to join adjacent words in a list, not digits. Follow the below steps to solve the problem.Initialize the list.Find the words that are not digits using isalpha method.4Join the words using join method.Add all the digits at the end by finding them using the isdigit method.Print the result.Example Live Demo# initialzing the list strings = ['Tutorials', '56', '45', 'point', '1', '4'] # result result = [] words = [element for element in strings if element.isalpha()] digits = [element for element in strings if element.isdigit()] # adding the elements to ... Read More

Python - Joining unicode list elements

Hafeezul Kareem
Updated on 13-Nov-2020 18:47:39

465 Views

In this article, we are going to learn how to join Unicode list elements. Follow the below steps to write the code.Initialize the list.Convert all the elements into Unicode using map and string.encode methods.Convert each encoded string using the decode method.Join the strings using join method.Print the result.Example Live Demo# initializing the list strings = ['Tutorialspoint', 'is a popular', 'site', 'for tech leranings'] def get_unicode(string): return string.encode() # converting to unicode strings_unicode = map(get_unicode, strings) # joining the unicodes result = ' '.join(unicode.decode() for unicode in strings_unicode) # printing the result print(result)If you run the above code, ... Read More

Advertisements