Programming Articles - Page 2197 of 3363

C Program for Minimum number of jumps to reach the end

sudhir sharma
Updated on 24-Dec-2019 06:12:27

673 Views

We are given, an array of non-negative integers denoting the maximum number of steps that can be made forward from that element. The pointer is initially positioned at the first index [0 index] of the array. Your goal is to reach the last index of the array in the minimum number of steps. If it is not possible to reach the end of the array then print the maximum integer.naive approach is to begin from initial{the primary} component and recursively call for all the components accessible from the first element. The minimum range of jumps to reach the end from ... Read More

C Program for Minimum Cost Path

sudhir sharma
Updated on 24-Dec-2019 06:06:27

936 Views

Here, we will solve the minimum cost path problem in C. The implication is done on a 2D-matrix where each cell has a cost to travel. We have to find a path from the left top corner to the bottom right corner with minimum travel cost. You can only traverse down and right lower cells from a given cell.To solve this particular problem dynamic programming is much better to approach than recursion.Given cost matrix cost[ ][ ] and a position (m, n), we have to write a function that returns cost of minimum path to reach (m, n) from (0, ... Read More

Python-program-to-convert-pos-to-sop

Pavitra
Updated on 24-Dec-2019 05:55:35

260 Views

In this article, we will learn about the solution to the problem statement given below.Problem statement − We are given pos form we need to convert it into its equivalent sop formThe conversion can be done by first counting the number of alphabets in the pos form and then calculating all the max and the minterms.Now let’s observe the concept in the implementation below−Example# Python code to convert standard POS form # to standard SOP form # to calculate number of variables def count_no_alphabets(POS):    i = 0    no_var = 0    # total no. of alphabets before will be ... Read More

Python program to print all Prime numbers in an Interval

Pavitra
Updated on 24-Dec-2019 05:46:13

575 Views

In this article, we will learn about the solution to the problem statement given below.Problem statement − We are given an interval we need to compute all the prime numbers in a given rangeHere we will be discussing a brute-force approach to get the solution i.e. the basic definition of a prime number. Prime numbers are the number which has 1 and itself as a factor and rests all the numbers are not its factors.Each time the condition of a prime number is evaluated to be true computation is performed.Now let’s observe the concept in the implementation below−Example Live Demostart = 1 ... Read More

Python program to interchange first and last elements in a list

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

776 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

950 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

Advertisements