Programming Articles - Page 2199 of 3366

Python program to interchange first and last elements in a list

Pavitra
Updated on 11-Jul-2020 12:01:53

743 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 swap the last element with the first element.There are 4 approaches to solve the problem as discussed below−Approach 1 − The brute-force approachExample Live Demodef swapLast(List):    size = len(List)    # Swap operation    temp = List[0]    List[0] = List[size - 1]    List[size - 1] = temp    return List # Driver code List = ['t', 'u', 't', 'o', 'r', 'i', 'a', 'l'] print(swapLast(List))Output['t', 'u', 't', 'o', 'r', 'i', 'a', 'l']Approach 2 − The ... Read More

Python program to insert an element into sorted list

Pavitra
Updated on 24-Dec-2019 05:30:25

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 insert an element in a list without changing sorted orderThere are two approaches as discussed below−Approach 1: The brute-force methodExample Live Demodef insert(list_, n):    # search    for i in range(len(list_)):       if list_[i] > n:          index = i          break    # Insertion    list_ = list_[:i] + [n] + list_[i:]    return list_ # Driver function list_ = ['t', 'u', 't', 'o', 'r'] n = ... Read More

Python program to get all subsets of a given size of a set

Pavitra
Updated on 24-Dec-2019 05:26:57

926 Views

In this article, we will learn about the solution to the problem statement given below.Problem statement − We are given a set, we need to list all the subsets of size nWe have three approaches to solve the problem −Using itertools.combinations() methodExample Live Demo# itertools module import itertools def findsubsets(s, n):    return list(itertools.combinations(s, n)) #main s = {1, 2, 3, 4, 5} n = 4 print(findsubsets(s, n))Output[(1, 2, 3, 4), (1, 2, 3, 5), (1, 2, 4, 5), (1, 3, 4, 5), (2, 3, 4, 5)]Using map() and combination() methodExample# itertools module from itertools import combinations def findsubsets(s, n):    return ... Read More

Python Program to find whether a no is power of two

Pavitra
Updated on 24-Dec-2019 05:18:26

2K+ Views

In this article, we will learn about the solution to the problem statement given below.Problem statement − We are given a number, we need to check that the number is a power of two or not.We can solve this using two approaches as discussed below.Approach 1: Taking the log of the given number on base 2 to get the powerExample Live Demo# power of 2 def find(n):    if (n == 0):       return False    while (n != 1):       if (n % 2 != 0):          return False       n = ... Read More

Python program to find the sum of all items in a dictionary

Pavitra
Updated on 11-Jul-2020 11:56:02

1K+ Views

In this article, we will learn about the solution to the problem statement given below.Problem statement − We are given a dictionary, and we need to print the 3 highest value in a dictionary.Three approaches to the problem statement are given below:Approach 1 − Calculating sum from the dictionary iterableExample Live Demo# sum function def Sum(myDict):    sum_ = 0    for i in myDict:       sum_ = sum_ + myDict[i]    return sum_ # Driver Function dict = {'T': 1, 'U':2, 'T':3, 'O':4, 'R':5} print("Sum of dictionary values :", Sum(dict))OutputSum of dictionary values : 14Approach 2 − Calculating the ... Read More

What are the rules for a local variable in lambda expression in Java?

raja
Updated on 11-Jul-2020 11:57:48

4K+ Views

A lambda expression can capture variables like local and anonymous classes. In other words, they have the same access to local variables of the enclosing scope. We need to follow some rules in case of local variables in lambda expressions.A lambda expression can't define any new scope as an anonymous inner class does, so we can't declare a local variable with the same which is already declared in the enclosing scope of a lambda expression.Inside lambda expression, we can't assign any value to some local variable declared outside the lambda expression. Because the local variables declared outside the lambda expression can be final or effectively final.The rule of ... Read More

Priority queue of pairs in C++ (Ordered by first)

Sunidhi Bansal
Updated on 23-Dec-2019 11:40:06

3K+ 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

Problem with scanf() when there is fgets()/gets()/scanf() after it in C

Sunidhi Bansal
Updated on 23-Dec-2019 11:37:34

557 Views

The problem states that what will be the working or the output if the scanf is followed by fgets()/gets()/scanf().fgets()/gets() followed by scanf()Example Live Demo#include int main() {    int x;    char str[100];    scanf("%d", &x);    fgets(str, 100, stdin);    printf("x = %d, str = %s", x, str);    return 0; }OutputInput: 30 String Output: x = 30, str =Explanationfgets() and gets() are used to take string input from the user at the run time. I above code when we run enter the integer value then it won’t take the string value because when we gave newline after the integer ... Read More

Process Synchronization in C/C++

Sunidhi Bansal
Updated on 23-Dec-2019 11:33:09

7K+ Views

Process synchronization is the technique to overcome the problem of concurrent access to shared data which can result in data inconsistency. A cooperating process is the one which can affect or be affected by other process which will lead to inconsistency in processes data therefore Process synchronization is required for consistency of data.The Critical-Section ProblemEvery process has a reserved segment of code which is known as Critical Section. In this section, process can change common variables, update tables, write files, etc. The key point to note about critical section is that when one process is executing in its critical section, ... Read More

Program for power of a complex number in O(log n) in C++

Sunidhi Bansal
Updated on 23-Dec-2019 11:18:15

1K+ Views

Given a complex number in the form of x+yi and an integer n; the task is calculate and print the value of the complex number if we power the complex number by n.What is a complex number?A complex number is number which can be written in the form of a + bi, where a and b are the real numbers and i is the solution of the equation or we can say an imaginary number. So, simply putting it we can say that complex number is a combination of Real number and imaginary number.Raising power of a complex numberTo raise ... Read More

Advertisements