Python Articles

Page 111 of 855

Program to check if binary string has at most one segment of ones or not using Python

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

Binary strings often need validation to check specific patterns. A common requirement is to verify if a binary string has at most one contiguous segment of ones. This means all the '1's should be grouped together without any '0's in between. For example, "111000" has one segment of ones, while "11010" has two separate segments. Problem Understanding Given a binary string without leading zeros, we need to determine if it contains at most one contiguous segment of ones. The string should have all '1's grouped together, followed by all '0's (if any). Examples "111000" ...

Read More

Program to find nearest point that has the same x or y coordinate using Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 2K+ Views

Finding the nearest point that shares the same x or y coordinate is a common problem in computational geometry. We need to find a valid point (one that shares either x or y coordinate) with the smallest Manhattan distance from our current location. The Manhattan distance between two points (a, b) and (p, q) is calculated as |a - p| + |b - q|. When multiple points have the same minimum distance, we return the one with the smallest index. Problem Statement Given a list of points and a current location, find the index of the valid ...

Read More

Program to count items matching a rule using Python

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

Suppose we have an array nums, where each nums[i] contains three elements [type_i, color_i, name_i]. These are describing the type, color, and name of the ith item. We also have a rule represented by two other strings, ruleKey and ruleValue. Now we can say the ith item matches the rule if one of the following is true − ruleKey = "type" and ruleValue = type_i. ruleKey = "color" and ruleValue = color_i. ruleKey = "name" and ruleValue = name_i. We have to find the ...

Read More

Program to merge strings alternately using Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 6K+ Views

Suppose we have two strings s and t. We have to merge them by adding letters in alternating fashion, starting from s. If s and t are not of same length, add the extra letters onto the end of the merged string. So, if the input is like s = "major" and t = "general", then the output will be "mgaejnoerral". Since t is larger than s, we have added extra part "ral" at the end. Algorithm To solve this, we will follow these steps − Initialize i := j := 0 ...

Read More

Program to find longest nice substring using Python

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

A nice substring is one where every letter appears in both uppercase and lowercase. For example, "bBb" is nice because 'b' appears as both 'b' and 'B'. This tutorial shows how to find the longest nice substring in Python. Problem Understanding Given a string, we need to find the longest substring where every letter appears in both uppercase and lowercase forms. If multiple substrings have the same maximum length, return the one that appears first. Algorithm Approach We'll use a nested loop approach to check all possible substrings: For each starting position, build a ...

Read More

Program to find largest perimeter triangle using Python

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

Suppose we have an array nums of positive lengths, we have to find the largest perimeter of a triangle by taking three values from that array. When it is impossible to form any triangle of non-zero area, then return 0. So, if the input is like [8, 3, 6, 4, 2, 5], then the output will be 19. Triangle Inequality Rule For three sides to form a triangle, they must satisfy the triangle inequality: the sum of any two sides must be greater than the third side. For sides a, b, c where a ≥ b ≥ ...

Read More

Creating scrollable Listbox within a grid using Tkinter

Dev Prakash Sharma
Dev Prakash Sharma
Updated on 25-Mar-2026 4K+ Views

A Listbox widget displays a list of items such as numbers, employee names, or any collection of data. When dealing with long lists, we need scrollbars to navigate through all items effectively. By attaching a Scrollbar to a Listbox and configuring them properly, we can create a scrollable interface within a grid layout. Basic Scrollable Listbox Let's start with a simple scrollable Listbox containing numbers from 1 to 100 − # Import the required libraries from tkinter import * from tkinter import ttk # Create an instance of Tkinter Frame win = Tk() win.title("Scrollable Listbox ...

Read More

Call a Function with a Button or a Key in Tkinter

Dev Prakash Sharma
Dev Prakash Sharma
Updated on 25-Mar-2026 12K+ Views

In Tkinter applications, you can call functions when buttons are clicked or keys are pressed. This is achieved using the command parameter for buttons and the bind() method for key events. Calling a Function with Button Click The simplest way to call a function is by using the command parameter when creating a button ? # Import the required libraries from tkinter import * from tkinter import ttk from tkinter import messagebox # Create an instance of Tkinter Frame win = Tk() # Set the geometry of Tkinter Frame win.geometry("700x350") # Define a ...

Read More

How to clear Text widget content while clicking on the Entry widget itself in Tkinter?

Dev Prakash Sharma
Dev Prakash Sharma
Updated on 25-Mar-2026 5K+ Views

The Tkinter Text widget is an input widget that supports multiline user input, functioning as a text editor. You can clear its content using the delete(0, END) method. Similarly, you can clear an Entry widget's content by clicking on it, achieved by binding a function to a click event. Example Here's how to create an Entry widget that clears its placeholder text when clicked ? # Import the required libraries from tkinter import * # Create an instance of Tkinter Frame win = Tk() # Set the geometry of Tkinter Frame win.geometry("700x250") # ...

Read More

How to change the Entry Widget Value with a Scale in Tkinter?

Dev Prakash Sharma
Dev Prakash Sharma
Updated on 25-Mar-2026 2K+ Views

The Tkinter Entry widget accepts single-line user input and can be synchronized with a Scale widget to create an interactive value selector. By sharing a common variable between both widgets, changes to the Scale automatically update the Entry value. Key Components To link an Entry widget with a Scale widget, you need: IntVar() or DoubleVar() − A Tkinter variable shared between widgets textvariable parameter in Entry widget variable parameter in Scale widget Basic Example Here's how to create a synchronized Entry and Scale widget ? import tkinter as tk from tkinter ...

Read More
Showing 1101–1110 of 8,549 articles
« Prev 1 109 110 111 112 113 855 Next »
Advertisements