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

Update Single Field in MySQL Query for Null Values

AmitDiwan
Updated on 24-Dec-2019 06:06:12

201 Views

For this, you can use COALESCE(). Let us first create a table −mysql> create table DemoTable1805      (      Name1 varchar(20),      Name2 varchar(20)      ); Query OK, 0 rows affected (0.00 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1805 values('Chris', NULL); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1805 values('David', 'Mike'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1805 values(NULL, 'Mike'); Query OK, 1 row affected (0.00 sec)Display all records from the table using select statement −mysql> select * from DemoTable1805;This will produce the ... Read More

MySQL Rows Concatenation to Fetch Maximum Value from Duplicate IDs

AmitDiwan
Updated on 24-Dec-2019 06:03:39

146 Views

For this, you can use GROUP BY clause. To find maximum value, use MAX() function. Let us first create a table −mysql> create table DemoTable1804      (      Id int,      Marks1 int,      Marks2 int,      Marks3 int      ); Query OK, 0 rows affected (0.00 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1804 values(1, 56, 89, 34); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1804 values(1, 98, null, 94); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1804 values(2, 34, 45, 78); ... Read More

Use CONTAINS with CURDATE in MySQL

AmitDiwan
Updated on 24-Dec-2019 06:03:28

229 Views

For this, you can use CONCAT() with CURDATE().There is no function with the name CONTAINS() in MySQL.Let us first get the current date. The current date is as follows −mysql> select curdate();This will produce the following output −+------------+ | curdate()  | +------------+ | 2019-11-28 | +------------+ 1 row in set (0.00 sec)We will now create a table −mysql> create table DemoTable1803      (      Name varchar(20),      JoiningYear varchar(20)      ); Query OK, 0 rows affected (0.00 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1803 values('Chris', '2020/2017'); Query OK, 1 row ... Read More

MySQL Group By for Separate ID Without Using Group By

AmitDiwan
Updated on 24-Dec-2019 05:59:27

164 Views

For this, you can use DISTINCT keyword. Let us first create a table −mysql> create table DemoTable1801      (      Name varchar(20),      Score int      ); Query OK, 0 rows affected (0.00 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1801 values('John', 98); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1801 values('John', 98); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1801 values('John', 99); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1801 values('Carol', 99); Query OK, 1 row affected (0.00 sec)Display all records ... Read More

Convert POS to SOP in Python

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

Print All Prime Numbers in an Interval using Python

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

578 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

Insert Element into Sorted List in Python

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

Get All Subsets of a Given Size of a Set in Python

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 Number 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

Advertisements