In this article, we will learn how to initialize a list with alternate 0s and 1s. Given a list length, we need to create a list where elements alternate between 1 and 0 starting with 1. Method 1: Using a For Loop The simplest approach is to iterate through the range and append values based on whether the index is even or odd ? # initializing an empty list result = [] length = 7 # iterating through the range for i in range(length): # checking if index is even or ... Read More
In this article, we are going to learn how to make pairs from two lists such that no similar elements make a pair. This is useful when you need to create combinations where each pair contains different values. Using List Comprehension The most straightforward approach is to use nested list comprehension with a condition to filter out pairs with identical elements − # initializing the lists numbers_1 = [1, 2, 3, 4, 5] numbers_2 = [5, 8, 7, 1, 3, 6] # making pairs result = [(i, j) for i in numbers_1 for j in ... Read More
In this article, we will learn about the pytrie module for prefix matching strings from a list of strings. The pytrie module provides efficient trie data structures for fast prefix-based operations. What is Prefix Matching? Prefix matching finds all strings in a collection that start with a given prefix. For example ? Input: List: ['tutorialspoint', 'tutorials', 'tutorialspython', 'python'] Prefix: 'tutorials' Output: ['tutorialspoint', 'tutorials', 'tutorialspython'] Installing pytrie First, install the pytrie module using pip ? pip install pytrie Using pytrie.StringTrie The pytrie.StringTrie data structure allows us to create, ... Read More
In this article, we are going to scrape the text from Wikipedia's Infobox using BeautifulSoup and requests in Python. We can do it in 10 minutes. It's straightforward and useful for extracting structured information from Wikipedia pages. Prerequisites We need to install bs4 and requests. Execute the below commands to install ? pip install beautifulsoup4 pip install requests Steps to Extract Infobox Data Follow the below steps to write the code to fetch the text that we want from the infobox ? Import the bs4 and requests modules. Send an HTTP ... Read More
A DataFrame is a two-dimensional tabular data structure with rows and columns, similar to a spreadsheet or database table. Selecting specific columns efficiently is crucial for data analysis. This article demonstrates how to select DataFrame columns using regular expressions and data types. Creating a Sample DataFrame Let's start by creating a DataFrame from a movies dataset ? import pandas as pd # Create DataFrame from CSV movies_dataset = pd.read_csv("https://raw.githubusercontent.com/sasankac/TestDataSet/master/movies_data.csv") # Display basic info print(type(movies_dataset)) print(movies_dataset.head()) budget id original_language original_title popularity release_date ... Read More
Finding airports in correct order from a shuffled list of flights is a classic graph traversal problem. We need to reconstruct the travel itinerary by finding an Eulerian path through the flight connections, ensuring lexicographical ordering when multiple valid paths exist. Algorithm Overview The solution uses Hierholzer's algorithm to find an Eulerian path in a directed graph ? Build adjacency list from flight pairs Track in-degree and out-degree for each airport Find starting airport (out-degree - in-degree = 1, or lexicographically smallest) Use DFS to traverse and build the path Return reversed path for correct order ... Read More
Suppose we have a string s and another number k, we have to check whether we can create k palindromes using all characters in s or not. So, if the input is like s = "amledavmel" k = 2, then the output will be True, as we can make "level" and "madam". Algorithm To solve this, we will follow these steps − d := a map where store each unique characters and their frequency cnt := 0 for each key in d, do if d[key] is odd, then cnt := cnt + 1 ... Read More
Suppose we have a number n; we have to find the minimum number of Fibonacci numbers required to add up to n. This problem uses a greedy approach where we always pick the largest possible Fibonacci number. So, if the input is like n = 20, then the output will be 3, as we can use the Fibonacci numbers [2, 5, 13] to sum to 20. Algorithm To solve this, we will follow these steps − Initialize result counter to 0 Generate Fibonacci numbers up to n ... Read More
In this tutorial, we'll create Excel charts using Python's openpyxl module. We'll build a spreadsheet with tennis players' Grand Slam titles and create a bar chart to visualize the data. What is openpyxl? The openpyxl module is a powerful Python library for working with Excel files (.xlsx). Unlike the xlrd module which is read-only, openpyxl supports both reading and writing operations, making it ideal for creating charts and manipulating Excel data. Installation First, install the openpyxl module ? pip install openpyxl Creating a Spreadsheet with Data Let's start by creating a ... Read More
Excel spreadsheets contain cells with values, formulas, or cell references. In Python, we can simulate this by processing a 2D matrix where each cell can contain a number, a cell reference (like "B1"), or a formula (like "=A1+A2"). Problem Understanding Given a 2D matrix representing an Excel spreadsheet, we need to evaluate all formulas and cell references to get the final computed values. Columns are labeled A, B, C... and rows are numbered 1, 2, 3... Input Matrix Output Matrix B170 35=A1+A2 770 3510 ... Read More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Economics & Finance