Server Side Programming Articles - Page 1859 of 2646

Course Schedule II in Python

Arnab Chakraborty
Updated on 29-Apr-2020 12:26:29

382 Views

Suppose there are a total of n courses, these are labeled from 0 to n-1. Some courses may have prerequisites, given the total number of courses and a list of prerequisite pairs, we have to find the ordering of courses that we should take to finish all courses. There may be multiple correct orders, we just need to find one of them. If it is impossible to finish all courses, then return an empty array.So if the input is like 2, [[1, 0]], then the result will be [0, 1]. There are a total of 2 courses to take. To ... Read More

Course Schedule in Python

Arnab Chakraborty
Updated on 29-Apr-2020 12:25:39

1K+ Views

Suppose there are a total of numCourses courses we have to take, labeled from 0 to numCourses-1. Some courses may have prerequisites, for example to take course 0 we have to first take course 1, which is expressed using a pair: [0, 1]. Suppose there are total number of courses that is provided and a list of prerequisite pairs, we have to check whether is it possible for you to finish all courses?So if the input is like − numCourses = 2 and prerequisites = [[1, 0]], then the result will be true, because there are a total of 2 ... Read More

Word Ladder in C++

Arnab Chakraborty
Updated on 29-Apr-2020 12:23:44

902 Views

Suppose we have two words (beginWord and endWord), and we have dictionary's word list, find the length of shortest transformation sequence from beginWord to endWord, such that −Only one letter can be converted at a time.In each transformed word must exist in the word list. The beginWord is not a transformed word.We have to keep in mind that −Return 0 when there is no such change sequence.All words have the same length.All words contain only lowercase characters.We can assume no duplicates in the word list.So if the input is like: beginWord = "hit", endWord = "cog", and wordlist = ["hot", ... Read More

Coroutines in C/C++

Ayush Gupta
Updated on 16-Mar-2020 10:14:42

287 Views

In this tutorial, we will be discussing a program to understand coroutines in C/C++.Coroutines are control instructions which switch the execution control between two routines which returning any of them.Example Live Demo#include int range(int a, int b){    static long long int i;    static int state = 0;    switch (state){    case 0:       state = 1;       for (i = a; i < b; i++){          return i;       //returning control       case 1:; //resuming control       }    }    state = 0;    return 0; } int main(){    int i;    for (; i=range(1, 5);)       printf("control at main :%d", i);    return 0; }Outputcontrol at main :1 control at main :2 control at main :3 control at main :4

Creating a C/C++ Code Formatting tool with help of Clang tools

Ayush Gupta
Updated on 16-Mar-2020 10:11:22

235 Views

In this tutorial, we will be discussing a program to create a C/C++ code formatting tool with the help of clang tools.SETUPsudo apt install python sudo apt install clang-format-3.5Then we will create a python file in location where the current user has read and write permissions.Exampleimport os cpp_extensions = (".cxx", ".cpp", ".c", ".hxx", ".hh", ".cc", ".hpp") for root, dirs, files in os.walk(os.getcwd()):    for file in files:       if file.endswith(cpp_extensions):          os.system("clang-format-3.5 -i -style=file " + root + "/" + file)Create a file formatting file in the top directory of the current user.Outputclang-format-3.5 -style=google -dump-config ... Read More

Create Directory or Folder with C/C++ Program

Ayush Gupta
Updated on 14-Feb-2025 18:05:50

4K+ Views

Creating a folder or directory on your computer is an important task in programming. A directory is like a container that helps store and organize files. In C and C++, you often need to create directories to store data, logs, or configuration files. Creating directories makes file management easier. For example, if your program needs to store logs, it should check if a "logs" folder exists. If not, it can create the folder automatically, so you don't have to do it manually. In this article, we'll show you how to create a directory in C or C++. Creating Directories ... Read More

Counts of distinct consecutive sub-string of length two using C++ STL

Ayush Gupta
Updated on 16-Mar-2020 10:04:44

154 Views

In this tutorial, we will be discussing a program to count distinct consecutive sub-string of length two using C++ STL.For this we will provided with a string. Our task is to count and print all the unique substrings of length two from the given string.Example Live Demo#include using namespace std; void calc_distinct(string str){    map dPairs;    for (int i=0; i

Counting Inversions using Set in C++ STL

Ayush Gupta
Updated on 16-Mar-2020 10:02:31

269 Views

In this tutorial, we will be discussing a program to count inversions using set in C++ STL.Inversion count is a measure of how near the array is to be completely sorted. If the array is already sorted, the inversion count will be 0.Example Live Demo#include using namespace std; //returning inversion count int get_Icount(int arr[],int n){    multiset set1;    set1.insert(arr[0]);    int invcount = 0; //initializing result    multiset::iterator itset1;    for (int i=1; i

Count the number of 1’s and 0’s in a binary array using STL in C++

Ayush Gupta
Updated on 16-Mar-2020 10:00:27

420 Views

In this tutorial, we will be discussing a program to count the number of 1’s and 0’s in a binary array using STL in C++.For this we will be provided with an array. Our task is to count the number of 0’s and 1’s present in the array.Example Live Demo#include using namespace std; // checking if element is 1 or not bool isOne(int i){    if (i == 1)       return true;    else       return false; } int main(){    int a[] = { 1, 0, 0, 1, 0, 0, 1 };    int n = sizeof(a) / sizeof(a[0]);    int count_of_one = count_if(a, a + n, isOne);    cout

Count smaller elements on right side using Set in C++ STL

Ayush Gupta
Updated on 16-Mar-2020 09:58:45

314 Views

In this tutorial, we will be discussing a program to count smaller elements on right side using set in C++ STL.For this we will be provided with an array. Our task is to construct a new array and add the number of smaller elements on the right side of the current element at its position.Example Live Demo#include using namespace std; void count_Rsmall(int A[], int len){    set s;    int countSmaller[len];    for (int i = len - 1; i >= 0; i--) {       s.insert(A[i]);       auto it = s.lower_bound(A[i]);       countSmaller[i] = ... Read More

Advertisements