Programming Articles - Page 2368 of 3366

Sort Tuples in Increasing Order by any key in Python program

Pradeep Elance
Updated on 23-Oct-2019 06:53:14

220 Views

In this tutorial, we are going to sort a list of tuples in increasing order by nth index key. For example, we have a list of tuples [(2, 2), (1, 2), (3, 1)] then, we have to sort it using 0th index element. The output for that list would be [(1, 2), (2, 2), (3, 1)].We can achieve this by using the sorted method. We have to pass a key while giving the list to the sorted function. Here, the key is the index on which the sorting is based.sorted takes a list and returns that list in ascending order ... Read More

Reverse words in a given String in Python

Pradeep Elance
Updated on 23-Oct-2019 06:48:45

19K+ Views

We are given a string, and our goal is to reverse all the words which are present in the string. We can use the split method and reversed function to achieve the output. Let's see some sample test cases.Input: string = "I am a python programmer" Output: programmer python a am IInput: string = "tutorialspoint is a educational website" Output: website educational a is tutorialspointLet's follow the below steps to achieve our goal.Algorithm1. Initialize the string. 2. Split the string on space and store the resultant list in a variable called words. 3. Reverse the list words using reversed function. ... Read More

Minimize the total number of teddies to be distributed in C++

Narendra Kumar
Updated on 22-Oct-2019 12:23:51

145 Views

Problem statementGiven N number of students and an array which represent the mark obtained by students. School has dicided to give them teddy as a price. Hoever, school wants to save money, so they to minimize the total number of teddies to be distrubuted by imposing following constrain −All students must get atleast one teddyIf two students are sitting next to each other then student with the higher marks must get moreIf two students have same marks then they are allowed to get different number of teddiesExampleLet us suppose there are 3 students and marks obtained are represented in array ... Read More

Minimize the sum of squares of sum of N/2 paired formed by N numbers in C++

Narendra Kumar
Updated on 22-Oct-2019 12:16:10

154 Views

Problem statementGiven an array of n elements. The task is to create n/2 pairs in such a way that sum of squares of n/2 pairs is minimal.ExampleIf given array is −arr[] = {5, 10, 7, 4} then minimum sum of squares is 340 if we create pairs as (4, 10) and ( 5, 7)Algorithm1. Sort the array 2. Take two variables which point to start and end index of an array 3. Calulate sum as follows:    sum = arr[start] + arr[end];    sum = sum * sum; 4. Repeate this procedure till start < end and increment minSum as ... Read More

How can we implement a JSON array using Streaming API in Java?

raja
Updated on 18-Feb-2020 10:47:33

1K+ Views

The JsonGenerator interface can be used to write the JSON data to an output source in a streaming way. We can create or implement a JSON array using the writeStartArray() method of JsonGenerator, this method writes the JSON name/start array character pair within the current object context. The writeStartObject() method writes the JSON start object character, and valid only in an array context and the writeEnd() method writes the end of the current context.SyntaxJsonGenerator writeStartArray(String name)Exampleimport java.io.*; import javax.json.*; import javax.json.stream.*; public class JsonGeneratorTest {    public static void main(String[] args) throws Exception {       StringWriter writer = new ... Read More

Minimax Algorithm in Game Theory (Alpha-Beta Pruning) in C++

Narendra Kumar
Updated on 22-Oct-2019 12:09:07

3K+ Views

DescriptionAplha-Beta pruning is a optimization technique used in minimax algorithm. The idea benind this algorithm is cut off the branches of game tree which need not to be evaluated as better move exists already.This algorithm introduces two new fields −Alpha − This is best value(maximum) that maximizer player can guaratee at current level or its above levelBeta − This is the best value(minimum) that minimizer player can guaratee at the current level or its above level.ExampleIf game tree is −arr [] = {13, 8, 24, -5, 23, 15, -14, -20}then optimal value will be 13 if maximizer plays firstAlgorithm1. Start ... Read More

Check if a pair with given product exists in Linked list in C++

Arnab Chakraborty
Updated on 22-Oct-2019 11:55:49

170 Views

We have a set of elements. And a product K. The task is to check whether there exist two numbers in the linked list, whose product is same as K. If there are two numbers, then print them, if there are more than two numbers, then print any of them. Suppose the linked list is like this {2, 4, 8, 12, 15}, and k value is 16. then it will return (2, 8)We will solve this using the hashing technique. Take one hash table, and mark all elements as 0. Now iteratively mark all elements as 1 in hash table, ... Read More

Check if a pair with given product exists in a Matrix in C++

Arnab Chakraborty
Updated on 22-Oct-2019 11:51:52

141 Views

We have one matrix of order N x M. And a product K. The task is to check whether a pair with the given product is present in the matrix or not.Suppose a matrix is like below −12345678910111213141516Now if the K is 42, then there is a pair like (6, 7)To solve this problem, we will use hashing. We will create hash table by taking all elements of the matrix. We will start traversing the matrix, while traversing, check whether the current element of the matrix is divisible by the given product, and when the product K is divided by ... Read More

Check if a number is divisible by all prime divisors of another number in C++

Arnab Chakraborty
Updated on 22-Oct-2019 11:44:31

520 Views

Suppose there are two numbers. We have to check whether a number is divisible by all of the prime factors or the second number or not. Suppose a number is 120. The prime factors are {2, 3, 5}, another number is 75, here the prime factors are {3, 5}. As 120 is divisible by 3 and 5 also, then the decision is yes.If one number is 1, then it has no prime divisors, so answer is True. Otherwise we have to find the GCD of these two numbers. If GCD is 1, then they are co-prime. So answer is false. ... Read More

Check if a number is Bleak in C++

Arnab Chakraborty
Updated on 22-Oct-2019 11:43:02

408 Views

Here we will see whether a number is Bleak or not. A number is said to be bleak if it cannot be represented as sum of a positive number x and set bit count in x. So x + set_bit_count(x) is not equal to n , for any non-negative number x.The concept is very simple, if the set bit count + the number is not same as the number, then that is Bleak, otherwise it is not.Example Live Demo#include using namespace std; int set_bit_count(int x) {    unsigned int bit_count = 0;    while (x != 0) {     ... Read More

Advertisements