Server Side Programming Articles - Page 1697 of 2650

Bisect Algorithm Functions in Python

Pradeep Elance
Updated on 22-Jul-2020 08:12:31

254 Views

This module provides support for maintaining a list in sorted order without having to sort the list after each insertion of new element. We will focus on two functions namely insort_left and insort_right.insort_leftThis function returns the sorted list after inserting number in the required position, if the element is already present in the list, the element is inserted at the leftmost possible position. This function takes 4 arguments, list which has to be worked with, number to insert, starting position in list to consider, ending position which has to be considered. The default value of the beginning and end position ... Read More

Avoiding class data shared among the instances in Python

Pradeep Elance
Updated on 22-Jul-2020 08:06:41

156 Views

When we instantiate a class in Python, all its variables and functions also get inherited to the new instantiated class. But there may be e occasions when we do not want some of the variables of the parent class to be inherited by the child class. In this article, we will explore two ways to do that.Instantiation ExampleIn the below example we show how the variables are instance heated from a given class and how the variables are shared across all the instantiated classes. Live Democlass MyClass:    listA= [] # Instantiate Both the classes x = MyClass() y = ... Read More

Average of each n-length consecutive segment in a Python list

Pradeep Elance
Updated on 22-Jul-2020 07:53:06

361 Views

We have a list containing only numbers. We plan to get the average of a set of sequential numbers from the list which keeps rolling from the first number to next number and then to next number and so on.ExampleThe below example simplifies the requirement of finding the average of each 4-length consecutive elements of the list.Given list: [10, 12, 14, 16, 18, 20, 22, 24, 26] Average of every segment of 4 consecutive numbers: [13.0, 15.0, 17.0, 19.0, 21.0, 23.0]With sum and rangeWe use the list comprehension approach to take the sum of the consecutive numbers by ... Read More

asksaveasfile() function in Python Tkinter

Pradeep Elance
Updated on 22-Jul-2020 07:49:40

1K+ Views

TKinter is a Python module which is used for GUI programming in Python. We create a Canvas and place our UI components with many properties and behaviors in it. In this article, we will see e how to use the ask essay file function to save files created through Python programs into the local drives.We first create a canvas on which we again place a button using the TTK dot button function. Then declare another function that will use the ask fine to define the file type and save the file into location in the local drive.Examplefrom tkinter import * ... Read More

Arithmetic operations in excel file using openpyxl in Python

Pradeep Elance
Updated on 22-Jul-2020 07:45:19

415 Views

Python can help us use excel files directly from the python environment. We can refer to the each cell or a range of cells in excel and apply arithmetic operators on those cells. The results of those operations can also be stored at some cells whose location can be specified by the python program.In the below examples we are performing various arithmetic operations using inbuilt functions of excel. Like sum or average of numbers inside cells. The results are also stored at specific locations. We use the openpyxl module which opens a workbook and marks it active. Then we store ... Read More

Build Array Where You Can Find The Maximum Exactly K Comparisons in C++

Arnab Chakraborty
Updated on 21-Jul-2020 09:24:12

188 Views

Suppose we have three integers n, m, and k. If we have following algorithm to find the maximum element of an array of positive integers −max_val := -1 max_ind := -1 search_cost := 0 n := size of arr for initialize i := 0, when i < n, update (increase i by 1), do:    if max_val < arr[i], then:       max_val := arr[i]       max_ind := i       (increase search_cost by 1) return max_indWe have to make the array arr which has the following criteria: arr has exactly n integers. all elements arr[i] are in range 1 to m(including) (0

Number of Ways to Paint N × 3 Grid in C++ program

Arnab Chakraborty
Updated on 21-Jul-2020 09:15:13

177 Views

Suppose we have a grid whose size is n x 3 and we want to paint every cell of the grid with exactly one of the three colors. Here the colors that will be used are Red, Yellow and Green.Now there is a constraint, that is no two adjacent cells have the same color. We have n number of rows of the grid. Finally, we have to find the number of ways we can paint this grid. The answer may be very large so return it modulo 10^9 + 7.So, if the input is like 1, then the output will ... Read More

The Maze III in C++

Arnab Chakraborty
Updated on 21-Jul-2020 09:13:05

515 Views

Suppose there is a maze with empty spaces and walls and there is also a ball in that maze. The ball can go through empty spaces by rolling up (u), down (d), left (l) or right (r) directions, but it keeps rolling until hitting a wall. When the ball stops, it could choose the next direction. One hole is also in that maze. The ball will drop into the hole if it rolls on to the hole.So if we have the ball position, the hole position and the maze, we have to find out how the ball could drop into ... Read More

Substring with Concatenation of All Words in C++ Program

Arnab Chakraborty
Updated on 21-Jul-2020 09:09:36

223 Views

Suppose we have a string, s, and we also have a list of words, words present in the array are all of the same length. We have to find all starting indices of substring(s) in s that is a concatenation of each word in words exactly once and without any intervening characters.So if the input is like “barfoothefoobarman” and words are [“foo”, “bar”], then the output will be [0, 9]. This is because the substring starting at index 0 and 9 are “barfoo” and “foobar”.To solve this, we will follow these steps −Define a method called ok(), this will take ... Read More

Encode String with Shortest Length in C++

Arnab Chakraborty
Updated on 21-Jul-2020 09:06:42

679 Views

Suppose we have a non-empty string; we have to encode this string such that its encoded length will be minimum.The encoding rule is like − k[encoded_string], where the encoded_string inside [ ] is being repeated exactly k times. We have to keep in mind that k will be a positive integer and encoded string will not be empty or have extra space. We can assume that the input string contains only lowercase letters. If the encoding process does not make the string shorter, then do not encode that string.So, if the input is like "aaaaa", then the output will be ... Read More

Advertisements