Programming Articles - Page 2211 of 3366

Minimum sum of two numbers formed from digits of an array in C++

Narendra Kumar
Updated on 20-Dec-2019 09:33:40

534 Views

DescriptionGiven an array of digits which contains values from 0 to 9. The task is to find the minimum possible sum of two numbers formed from digits of the array. Please note that we have to use all digits of given arrayExampleIf input array is {7, 5, 1, 3, 2, 4} then minimum sum is 382 as, we can create two number 135 and 247.AlgorithmSort the array in ascending orderCreate two number by picking a digit from sorted array alternatively i.e. from even and odd indexExample Live Demo#include using namespace std; int getMinSum(int *arr, int n) {    sort(arr, arr ... Read More

What are the rules to follow while using exceptions in java lambda expressions?

raja
Updated on 20-Dec-2019 08:36:16

3K+ Views

The lambda expressions can't be executed on their own. It is used to implement methods that have declared in a functional interface. We need to follow a few rules in order to use the exception handling mechanism in a lambda expression.Rules for Lambda ExpressionA lambda expression cannot throw any checked exception until its corresponding functional interface declares a throws clause.An exception thrown by any lambda expression can be of the same type or sub-type of the exception declared in the throws clause of its functional interface.Example-1interface ThrowException { void throwing(String message); } public class LambdaExceptionTest1 { ... Read More

Python Program to print all permutations of a given string

Pavitra
Updated on 20-Dec-2019 07:34:07

629 Views

In this article, we will learn about the solution to the problem statement given below.Problem statement − We are given a string we need to display all the possible permutations of the string.Now let’s observe the solution in the implementation below −Example Live Demo# conversion def toString(List):    return ''.join(List) # permutations def permute(a, l, r):    if l == r:       print (toString(a))    else:       for i in range(l, r + 1):          a[l], a[i] = a[i], a[l]          permute(a, l + 1, r)          a[l], ... Read More

Python program to find uncommon words from two Strings

Pavitra
Updated on 20-Dec-2019 07:31:43

688 Views

In this article, we will learn about the solution to the problem statement given below.Problem statement − We are given two strings, we need to get the uncommon words from the given strings.Now let’s observe the solution in the implementation below −Example Live Demo# uncommon words def find(A, B):    # count    count = {}    # insert in A    for word in A.split():       count[word] = count.get(word, 0) + 1    # insert in B    for word in B.split():       count[word] = count.get(word, 0) + 1    # return ans    return [word ... Read More

Python program to find number of local variables in a function

Pavitra
Updated on 20-Dec-2019 07:29:18

951 Views

In this article, we will learn about the solution to the problem statement given below.Problem statement − We are given a function, we need to display the number of local variables in the function.Now let’s observe the solution in the implementation below −Example Live Demo# checking locals def scope():    a = 25.5    b = 5    str_ = 'Tutorialspoint' # main print("Number of local varibales available:", scope.__code__.co_nlocals)OutputNumber of local varibales available: 3Example Live Demo# checking locals def empty():    pass def scope():    a, b, c = 9, 23.4, True    str = 'Tutiorialspoint' # main print("Number of local varibales ... Read More

Python Program to Count trailing zeroes in factorial of a number

Pavitra
Updated on 20-Dec-2019 07:24:23

840 Views

In this article, we will learn about the solution to the problem statement given below.Problem statement − We are given an integer n, we need to count the number of trailing zeros in the factorial.Now let’s observe the solution in the implementation below −Example Live Demo# trailing zero def find(n):    # Initialize count    count = 0    # update Count    i = 5    while (n / i>= 1):       count += int(n / i)       i *= 5    return int(count) # Driver program n = 79 print("Count of trailing 0s "+"in", n, ... Read More

Python Program to Count set bits in an integer

Pavitra
Updated on 20-Dec-2019 07:22:44

461 Views

In this article, we will learn about the solution to the problem statement given below.Problem statement − We are given an integer n, we need to count the number of 1’s in the binary representation of the numberNow let’s observe the solution in the implementation below −#naive approachExample Live Demo# count the bits def count(n):    count = 0    while (n):       count += n & 1       n >>= 1    return count # main n = 15 print("The number of bits :", count(n))OutputThe number of bits : 4#recursive approachExample Live Demo# recursive way def count( ... Read More

Python Program to Count number of binary strings without consecutive 1’

Pavitra
Updated on 20-Dec-2019 07:20:06

298 Views

In this article, we will learn about the solution to the problem statement given below.Problem statement − We are given a positive integer N, we need to count all possible distinct binary strings available with length N such that no consecutive 1’s exist in the string.Now let’s observe the solution in the implementation below −Example Live Demo# count the number of strings def countStrings(n):    a=[0 for i in range(n)]    b=[0 for i in range(n)]    a[0] = b[0] = 1    for i in range(1, n):       a[i] = a[i-1] + b[i-1]       b[i] = ... Read More

Find frequency of each word in a string in Python

Pradeep Elance
Updated on 20-Dec-2019 07:19:17

11K+ Views

As a part of text analytics, we frequently need to count words and assign weightage to them for processing in various algorithms, so in this article we will see how we can find the frequency of each word in a given sentence. We can do it with three approaches as shown below.Using CounterWe can use the Counter() from collections module to get the frequency of the words. Here we first apply the split() to generate the words from the line and then apply the most_common ().Example Live Demofrom collections import Counter line_text = "Learn and practice and learn to practice" freq ... Read More

Python Program to Count Inversions in an array

Pavitra
Updated on 20-Dec-2019 07:17:40

274 Views

In this article, we will learn about the solution to the problem statement given below.Problem statement − We are given a list, we need to count the inversion required and display it.Inversion count is obtained by counting how many steps are needed for the array to be sorted.Now let’s observe the solution in the implementation below −Example Live Demo# count def InvCount(arr, n):    inv_count = 0    for i in range(n):       for j in range(i + 1, n):          if (arr[i] > arr[j]):             inv_count += 1    return ... Read More

Advertisements