Server Side Programming Articles - Page 1991 of 2650

Python - Check if all elements in a List are same

Niharikaa Aitam
Updated on 20-Jun-2025 18:37:26

28K+ Views

In Python, a list is a collection of ordered and mutable elements enclosed in square braces[]. Each element in a list has a unique position index, starting from 0. It can accept duplicate elements. In some cases, we may need to check if all the elements in a list are identical or not to detect a special condition in algorithms. Let's go through the different methods to check if all elements in a list are the same or not. Using set() function The Python set() function (built-in) is an unordered collection of unique elements. To check if all elements in a list ... Read More

Find the count of Strictly decreasing Subarrays in C++

Arnab Chakraborty
Updated on 19-Dec-2019 12:34:05

257 Views

Suppose we have an array A. And we have to find the total number of strictly decreasing subarrays of length > 1. So if A = [100, 3, 1, 15]. So decreasing sequences are [100, 3], [100, 3, 1], [15] So output will be 3. as three subarrays are found.The idea is find subarray of len l and adds l(l – 1)/2 to result.Example Live Demo#include using namespace std; int countSubarrays(int array[], int n) {    int count = 0;    int l = 1;    for (int i = 0; i < n - 1; ++i) {       ... Read More

Generate two output strings depending upon occurrence of character in input string in Python

Niharikaa Aitam
Updated on 20-Jun-2025 19:19:22

160 Views

In Python, a string is one of the data structures that is a sequence of characters enclosed within single quotes '' or double quotes "". It is immutable, i.e., once a string is created, it cannot be changed. When we want to generate two output strings based on character occurrence in Python, we have to follow the steps below - First, we need to initialize a dictionary or use the collections.Counter class to count the frequency of each character in the input string. Next, we have to go through the input ... Read More

Find the count of numbers that can be formed using digits 3 and 4 only and having length at max N in C++

Arnab Chakraborty
Updated on 19-Dec-2019 12:31:23

117 Views

Given a number N. We have to find the count of such numbers that can be formed using digit 3 and 4. So if N = 6, then the numbers will be 3, 4, 33, 34, 43, 44.We can solve this problem if we look closely, for single digit number it has 2 numbers 3 and 4, for digit 2, it has 4 numbers 33, 34, 43, 44. So for m digit numbers, it will have 2m values.Example Live Demo#include #include using namespace std; long long countNumbers(int n) {    return (long long)(pow(2, n + 1)) - 2; } int main() {    int n = 3;    cout

Find the first repeated word in a string in Python using Dictionary

Pradeep Elance
Updated on 19-Dec-2019 12:30:11

2K+ Views

In a given sentence there may be a word which get repeated before the sentence ends. In this python program, we are going to catch such word which is repeated in sentence. Below is the logical steps we are going to follow to get this result.Splits the given string into words separated by space.Then we convert these words into Dictionary using collectionsTraverse this list of words and check which first word has the frequency > 1Program - Find the Repeated WordIn the below program we use the counter method from the collections package to keep a count of the words.Example Live ... Read More

Exploring Correlation in Python

Pradeep Elance
Updated on 19-Dec-2019 12:26:02

576 Views

Correlation is a statistical term to measure the relationship between two variables. If the relationship is string, means the change in one variable reflects a change in another variable in a predictable pattern then we say that the variables are correlated. Further the variation in first variable may cause a positive or negative variation in the second variable. Accordingly, they are said to be positively or negatively correlated. Ideally the value of the correlation coefficient varies between -1 to +1.If the value is +1 or close to it then we say the variables are positively correlated. And they vary in ... Read More

Find the closest and smaller tidy number in C++

Arnab Chakraborty
Updated on 19-Dec-2019 12:23:29

151 Views

Suppose we have a number n, we have to find the closest and smaller tidy number of n. So a number is called tidy number, if all of its digits are sorted in non-decreasing order. So if the number is 45000, then the nearest and smaller tidy number will be 44999.To solve this problem, we will traverse the number from end, when the tidy property is violated, then we reduce digit by 1, and make all subsequent digit as 9.Example Live Demo#include using namespace std; string tidyNum(string number) {    for (int i = number.length()-2; i >= 0; i--) {   ... Read More

Find the center of the circle using endpoints of diameter in C++

Arnab Chakraborty
Updated on 19-Dec-2019 12:20:38

209 Views

Suppose we have two endpoints of diameter of a circle. These are (x1, y1) and (x2, y2), we have to find the center of the circle. So if two points are (-9, 3) and (5, -7), then the center is at location (-2, -2).We know that the mid points of two points are −$$(x_{m},y_{m})=\left(\frac{(x_{1}+x_{2})}{2},\frac{(y_{1}+y_{2})}{2}\right)$$Example Live Demo#include using namespace std; class point{    public:       float x, y;       point(float x, float y){          this->x = x;          this->y = y;       }       void display(){          cout

Different messages in Tkinter - Python

Pradeep Elance
Updated on 19-Dec-2019 12:22:14

472 Views

Tkinter is the GUI module of python. It uses various message display options which are in response to the user actions or change in state of a running program. The message box class is used to display variety of messages like confirmation message, error message, warning message etc.Example-1The below example shows display of a message with the background colour, font size and colour etc customizable.import tkinter as tk main = tk.Tk() key = "the key to success is to focus on goals and not on obstacles" message = tk.Message(main, text = key) message.config(bg='white', font=('times', 32, 'italic')) message.pack() ... Read More

Find sum of a number and its maximum prime factor in C++

Arnab Chakraborty
Updated on 19-Dec-2019 12:17:22

195 Views

Suppose we have a positive number n, and we have to find the sum of N and its maximum prime factor. So when the number is 26, then maximum prime factor is 13, so sum will be 26 + 13 = 39.Approach is straight forward. Simply find the max prime factor, and calculate the sum and return.Example Live Demo#include #include using namespace std; int maxPrimeFact(int n){    int num = n;    int maxPrime = -1;    while (n % 2 == 0) {       maxPrime = 2;       n /= 2;    }    for (int i = 3; i 2)    maxPrime = n;    return maxPrime; } int getRes(int n) {    int sum = maxPrimeFact(n) + n;    return sum; } int main() {    int n = 26;    cout

Advertisements