Programming Articles - Page 2202 of 3366

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

665 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

716 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

476 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

213 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

Python program to Count Even and Odd numbers in a List

Pavitra
Updated on 11-Jul-2020 11:25:39

6K+ 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 even and odd numbers in a list.There are three methods as discussed below−Approach 1 − Using brute-force approachExample Live Demolist1 = [21, 3, 4, 6, 33, 2, 3, 1, 3, 76] even_count, odd_count = 0, 0 # enhanced for loop for num in list1:    #even numbers    if num % 2 == 0:       even_count += 1    #odd numbers    else:       odd_count += 1 print("Even numbers available in the ... Read More

Python Program to convert Kilometers to Miles

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

367 Views

In this article, we will learn about the solution to the problem statement given below.Problem statement − We are given distance in kilometers and we need to convert it into milesAs we know that 1 kilometer equals 0.62137 miles.Formula UsedMiles = kilometer * 0.62137Now let’s observe the concept in the implementation below−Example Live Demokilometers = 5.5 # conversion factor as 1 km = 0.621371 miles conv = 0.621371 # calculation miles = kilometers * conv print(kilometers, "kilometers is equal to ", miles, "miles")Output5.5 kilometers is equal to 3.4175405 milesAll the variables are declared in the local scope and their references are seen ... Read More

Advertisements