Found 10476 Articles for Python

Program to count items matching a rule using Python

Arnab Chakraborty
Updated on 29-May-2021 14:32:09

475 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 is matched 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 number of matching we can find.So, if the input is likeBikeblueElecBCarsilverSumoBikeblueTVSAnd ruleKey = "color", ruleValue = "blue", then the output ... Read More

Program to merge strings alternately using Python

Arnab Chakraborty
Updated on 29-May-2021 14:32:28

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" t = "general", then the output will be "mgaejnoerral", as t is larger than s, so we have added extra part "ral" at the end.To solve this, we will follow these steps −i := j := 0result := blank stringwhile i < size of s and j < size of ... Read More

Program to find longest nice substring using Python

Arnab Chakraborty
Updated on 29-May-2021 14:32:57

590 Views

Suppose we have a string s. We have to find longest nice substring of s. For a string s, it will be said to nice when, for every letter of the alphabet in s, it appears in uppercase and lowercase both. If there are multiple such substring, then return the substring of the earliest occurrence.So, if the input is like s = "ZbybBbz", then the output will be "bBb" as this contains lowercase and uppercase B's.To solve this, we will follow these steps −cur_max:= -1res:= blank stringfor i in range 0 to size of s, doc := s[i]upper := a ... Read More

Program to find longest distance of 1s in binary form of a number using Python

Arnab Chakraborty
Updated on 29-May-2021 14:33:17

313 Views

Suppose we have a number N, we have to find the longest distance between two consecutive 1's in its binary representation. If there are no two-consecutive 1's, then return 0.So, if the input is like 71, then the output will be 4, because 71 in binary is 1000111. Now there are four ones, and first 1 and the second 1 are at distance 4. All others are one distance away. So longest distance is 4 here.To solve this, we will follow these steps −K := make a list of bits of binary representation of NMax := 0, C := 0, ... Read More

Program to find largest perimeter triangle using Python

Arnab Chakraborty
Updated on 29-May-2021 14:14:25

559 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.To solve this, we will follow these steps −sort the list numsa := delete last element from numsb := delete last element from numsc := delete last element from numswhile b+c

Creating scrollable Listbox within a grid using Tkinter

Dev Prakash Sharma
Updated on 26-May-2021 12:13:53

3K+ Views

A Listbox widget displays a list of items such as numbers list, items list, list of employees in a company, etc. There might be a case when a long list of items in a Listbox needs a way to be viewed inside the window. For this purpose, we can attach scrollbars to the Listbox widget by initializing the Scrollbar() object. If we configure and attach the Listbox with the scrollbar, it will make the Listbox scrollable.ExampleIn this example, we will create a Listbox with a list of numbers ranging from 1 to 100. The Listbox widget has an associated Scrollbar ... Read More

Vertical and Horizontal Scrollbars on Tkinter Widget

Dev Prakash Sharma
Updated on 26-May-2021 12:13:02

4K+ Views

Scrollbars are useful to provide dynamic behavior in an application. In a Tkinter application, we can create Vertical as well as Horizontal Scrollbars. Scrollbars are created by initializing the object of Scrollbar() widget.To create a horizontal scrollbar, we have to provide the orientation, i.e., "horizontal" or "vertical". Scrollbars can be accessible once we configure the particular widget with the scrollbars.Example#Import the required libraries from tkinter import * #Create an instance of Tkinter Frame win = Tk() #Set the geometry of Tkinter Frame win.geometry("700x350") #Create some dummy Text text_v = "Python is dynamically-typed and garbage-collected. It supports multiple ... Read More

Call a Function with a Button or a Key in Tkinter

Dev Prakash Sharma
Updated on 26-May-2021 12:10:47

11K+ Views

Let assume that we want to call a function whenever a button or a key is pressed for a particular application. We can bind the function that contains the operation with a button or key using the bind(', ' callback_function) method. Here, you can bind any key to the event or function that needs to be called.ExampleIn this example, we have created a function that will open a dialog box whenever we click 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() ... Read More

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

Dev Prakash Sharma
Updated on 26-May-2021 12:09:09

4K+ Views

Tkinter Text widget is an Input widget that supports multiline user input. It is also known as Text Editor which allows users to write the content and data in it. The content of the text widget can be cleared by defining the delete(0, END) command. Similarly, we can clear the content by clicking the Entry widget itself. This can be achieved by binding the function with a click event.Example#Import the required libraries from tkinter import * #Create an instance of Tkinter Frame win = Tk() #Set the geometry of Tkinter Frame win.geometry("700x250") #Define a function to clear ... Read More

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

Dev Prakash Sharma
Updated on 26-May-2021 12:07:50

2K+ Views

Tkinter Entry widget is an input widget that supports only single-line user input. It accepts all the characters in the text field unless or until there are no restrictions set for the input. We can change the value of the Entry widget with the help of the Scale widget. The Scale widget contains a lower value and a threshold that limits the user to adjust the value in a particular range.To update the value in the Entry widget while updating the value of Scale widget, we have to create a variable that has to be given to both the scale ... Read More

Advertisements