Found 26504 Articles for Server Side Programming

Python program to find the second largest number in a list

Pavitra
Updated on 11-Jul-2020 11:30:42

4K+ 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 display the second largest number in a list.There are three approaches to solve the problem−Approach 1 − We use the set() function & remove() functionExample Live Demolist1 = [11, 22, 1, 2, 5, 67, 21, 32] # to get unique elements new_list = set(list1) # removing the largest element from list1 new_list.remove(max(new_list)) # now computing the max element by built-in method? print(max(new_list))Output32Approach 2 − We use sort() method and negative indexesExample Live Demolist1 = [11, 22, 1, 2, ... Read More

Python program to find occurrence to each character in given string

Pavitra
Updated on 11-Jul-2020 11:31:49

2K+ 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 find the occurrence of each character in a given string.Here we will be discussing 3 approaches as discussed below:LApproach 1 − The brute-force approachExample Live Demotest_str = "Tutorialspoint" #count dictionary count_dict = {} for i in test_str:    #for existing characters in the dictionary    if i in count_dict:       count_dict[i] += 1    #for new characters to be added    else:       count_dict[i] = 1 print ("Count of all characters in Tutorialspoint ... Read More

Python program to find the largest number in a list

Pavitra
Updated on 23-Dec-2019 08:09:45

1K+ 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 calculate the largest element of the list.Here we will take the help of built-in functions to reach the solution of the problem statementUsing sort() functionExample# list list1 = [23, 1, 32, 67, 2, 34, 12] # sorting list1.sort() # printing the last element print("Largest element is:", list1[-1])OutputLargest in given array is 67Using max() functionExample Live Demo# list list1 = [23, 1, 32, 67, 2, 34, 12] # printing the maximum element print("Largest element is:", max(list1))OutputLargest in given ... Read More

Python Program to find the largest element in an array

Pavitra
Updated on 23-Dec-2019 08:07:18

1K+ Views

In this article, we will learn about the solution to the problem statement given below.Problem statement − We are given an array, we need to calculate the largest element of the array.Here we use the bruteforce approach in which we compute the largest element by traversing the whole loop and get the element.We can observe the implementation below.Example Live Demo# largest function def largest(arr, n):    #maximum element    max = arr[0]    # traverse the whole loop    for i in range(1, n):       if arr[i] > max:          max = arr[i]    return max # ... Read More

Python program to create a dictionary from a string

Pavitra
Updated on 11-Jul-2020 11:32:35

660 Views

In this article, we will learn about the solution to the problem statement given below.Problem statement − We are given a string input, we need to convert it into dictionary typeHere we will discuss two methods to solve the problem without using a built-in dict() function.Method 1 − Using eval() methodEval method is used only when the syntax or the formation of string resembles that of a dictionary. Direct conversion of string to the dictionary can happen in that case as discussed below.Example Live Demo# String string = "{'T':1, 'U':2, 'T':3, 'O':4, 'R':5}" # eval() function dict_string = eval(string) print(dict_string) print(dict_string['T']) print(dict_string['T'])Output{'T': ... Read More

Count words in a sentence in Python program

Pavitra
Updated on 25-Aug-2023 02:03:41

36K+ 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 count the number of words in the stringApproach 1 − Using split() functionThe split() function breaks the string into a list iterable with space as a delimiter. if the split() function is used without specifying the delimiter space is allocated as a default delimiter.Example Live Demotest_string = "Tutorials point is a learning platform" #original string print ("The original string is : " + test_string) # using split() function res = len(test_string.split()) # total no of words print ... Read More

Count upper and lower case characters without using inbuilt functions in Python program

Pavitra
Updated on 23-Dec-2019 07:54:17

708 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 count the number of uppercase and lowercase characters present in the string without using the inbuilt functionThis can be easily solved by using islower() and isupper() function available in python. But here there is a constraint to use the inbuilt function. So here we take the help of the ASCII value of the characters.Using the ord() function we compute the ASCII value of each character present in the string and then compare to check for uppercase ... Read More

Priority Queue in C++ Standard Template Library (STL)

Sunidhi Bansal
Updated on 23-Dec-2019 09:29:44

467 Views

Priority queue is an abstract data type for storing a collection of prioritized elements that supports insertion and deletion of an element based upon their priorities, that is, the element with first priority can be removed at any time. The priority queue doesn’t stores elements in linear fashion with respect to their locations like in Stacks, Queues, List, etc. The priority queue ADT(abstract data type) stores elements based upon their priorities.Priority Queue supports the following functions −Size() − it is used to calculate the size of the priority queue as it returns the number of elements in it.Empty() − it return ... Read More

Count positive and negative numbers in a list in Python program

Pavitra
Updated on 11-Jul-2020 11:24:45

4K+ Views

In this article, we will learn about the solution to the problem statement given below.Problem statement − We are given a list iterable, we need to count positive and negative numbers in it and display them.Approach 1 − Brute-force approach using iteration construct(for)Here we need to iterate each element in the list using a for loop and check whether num>=0, to filter the positive numbers. If the condition evaluates to be true, then increase pos_count otherwise, increase neg_count.Example Live Demolist1 = [1, -2, -4, 6, 7, -23, 45, -0] pos_count, neg_count = 0, 0 # enhanced for loop   for num in ... Read More

Product of all Subsequences of size K except the minimum and maximum Elements in C++

Sunidhi Bansal
Updated on 23-Dec-2019 07:45:59

206 Views

Given an array arr[n], containing n number of integers and an integer k for defining the size; the task is to print the product of all the subsequences of size k except the minimum and maximum elements.Let us assume we have a set of 4 elements {1, 2, 3, 4} and k as 2 so its subsets will be − {1, 2}, {2, 3}, {3, 4}, {1, 4}, {1, 3}, {2, 4}So excluding the maximum element 4, and minimum element 1, the remaining elements will be −2, 3, 3, 3, 2, product of which will be −2 * 3 * ... Read More

Advertisements