Server Side Programming Articles - Page 2138 of 2650

Print anagrams together in Python using List and Dictionary

Pradeep Elance
Updated on 23-Oct-2019 07:52:49

707 Views

In this tutorial, we are going to write a program which finds and prints anagrams using list and dictionary. We have different approaches to every problem. Try to write code without following the tutorial. If you are not able to generate any ideas to write logic, follow the below steps.Algorithm1. Initialize a list of strings. 2. Initialize an empty dictionary. 3. Iterate through the list of strings.    3.1. Sort the string and check if it is present in the dictionary as key or not.       3.1.1. If the sorted string is already present dictionary as a key ... Read More

Print first m multiples of n without using any loop in Python

Pradeep Elance
Updated on 23-Oct-2019 07:48:16

740 Views

In this tutorial, we are going to write a program to find out m multiples of a number n without using loops. For example, we have a number n = 4 and m = 3, the output should be 4, 8, 12. Three multiples of four. Here, the main constraint is not to use loops.We can use the range() function to get the desired output without loops. What is the work of the range() function? range() function returns a range object which we can convert into an iterator.Let's see the syntax of range().Syntaxrange(start, end, step)Algorithmstart - starting number to the ... Read More

Python program to count occurrences of a word in a string

Pradeep Elance
Updated on 23-Oct-2019 07:36:17

4K+ Views

In this tutorial, we are going to write a program that counts the number of times a word occurs in the string. You are given the word and a string, we have to calculate the frequency of the word in the string.Suppose we have a string I am a programmer. I am a student. And the word is. The program that we are going to write will return a number 2 as the word occurs two times in the string.Let's follow the below steps to achieve our goal.Algorithm1. Initialize the string and the word as two variables. 2. Split the string ... Read More

Python program to find the IP Address of the client

Pradeep Elance
Updated on 25-Aug-2023 00:22:01

61K+ Views

In this tutorial, we are going to find the IP address of the client using the socket module in Python. Every laptop, mobile, tablet, etc.., have their unique IP address. We will find it by using the socket module. Let's see the steps to find out the IP address of a device.Algorithm Import the socket module. Get the hostname using the socket.gethostname() method and store it in a variable. Find the IP address by passing the hostname as an argument to the socket.gethostbyname() method and store it in a variable. Print the IP address. Let's write code for the above ... Read More

Sort a list according to the Length of the Elements in Python program

Pradeep Elance
Updated on 23-Oct-2019 06:54:17

6K+ Views

We have a list of strings and our goal is to sort the list based on the length of strings in the list. We have to arrange the strings in ascending order according to their lengths. We can do this using our algorithms or Python built-in method sort() or function sorted() along with a key.Let's take an example to see the output.Input: strings = ["hafeez", "aslan", "honey", "appi"] Output: ["appi", "aslan", "honey", "hafeez"]Let's write our program using sort(key) and sorted(key). Follow the below steps to achieve the desired output using a sorted(key) function.Algorithm1. Initialize the list of strings. 2. Sort ... Read More

Sort Tuples in Increasing Order by any key in Python program

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

216 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

140 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

149 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

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

Advertisements