Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python Articles
Page 111 of 855
Program to check if binary string has at most one segment of ones or not using Python
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 MoreProgram to find nearest point that has the same x or y coordinate using Python
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 MoreProgram to count items matching a rule using Python
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 MoreProgram to merge strings alternately using Python
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 MoreProgram to find longest nice substring using Python
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 MoreProgram to find largest perimeter triangle using Python
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 MoreCreating scrollable Listbox within a grid using Tkinter
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 MoreCall a Function with a Button or a Key in Tkinter
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 MoreHow to clear Text widget content while clicking on the Entry widget itself in Tkinter?
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 MoreHow to change the Entry Widget Value with a Scale in Tkinter?
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