Euclidean AlgorithmThe Euclidean Algorithm is used to find the Greatest Common Divisor (GCD) of two numbers. The GCD of two integers is the largest number that divides both of them without leaving a remainder. This algorithm is based on the principle that the GCD of two numbers also divides their difference. The method repeatedly replaces the larger number by the remainder of dividing the larger number by the smaller one, until one of the numbers becomes zero. Problem Statement You are given two positive integers a and b. You need to find the greatest common divisor (GCD) of these two ... Read More
The activity selection problem selects the maximum number of non-overlapping activities from a given set. Each activity has a start and finish time, and a single person can perform only one activity at a time. Problem Statement You are given n activities, each defined by a start time and a finish time. The goal is to choose the maximum number of activities that a single person can perform without any overlap between the selected activities. Variable Notations Below are the variables used in the problem definition: N: Total number of activities S: ... Read More
In the English alphabet, letters are categorized into vowels and consonants. The vowels are (a, e, i, o, u), and the remaining alphabetic characters are considered as consonants. In this article, we are going to detect the vowels vs the consonants in Python. Python provides a simple way to solve this problem by using the built-in string methods and logical conditions. Using Python in Operator The in operator is used to check whether the provided assignment character exists in the string. It returns true if it exists; otherwise, it returns false. To detect whether a letter is a ... Read More
Using %r instead of %s in PythonIn Python, string formatting is done by using the %(formatting operator). The %s uses the str() to convert the value, while the %r uses the repr() to convert values. Both operators look similar, but the difference is that they serve different purposes: %s: It converts the value to a human-readable string. %r: It converts the value that contains the quotes, escape characters, etc. Let's dive into the article to learn when to use %r instead of %s, which helps in debugging and logging. ... Read More
The self-destructing code is a type of program that can delete itself. It automatically executes the program and then removes the executable file once the execution is done. In this article, we will learn how to write self-destructing code for a C program. Write a Self Destructing Code in C++ You can write a self-destructing program that deletes its own executable file after it finishes running by using the remove() function. The remove() function is a built-in function from the C standard library () that deletes the specified file from the file system. To delete the program's own executable file, ... Read More
In C, the strings are basically array of characters. In C++ the std::string is an advancement of that array. There are some additional features with the traditional character array. The null terminated strings are basically a sequence of characters, and the last element is one null character (denoted by '\0'). When we write some string using double quotes ("..."), then it is converted into null terminated strings by the compiler. The size of the string may smaller than the array size, but if there are some null character inside that array, that will be treated as the end of that ... Read More
In C++, function overloading and const keyword are used for different purposes. Function overloading provides different ways to call a function with different parameter types that make the program more readable. While the const keyword provides the ways of declaration such as variable, member variable, function parameters, member function, and return type. What is Function Overloading? Function overloading is the process of defining multiple functions having the same name but different parameter lists. It is also known as compile-time polymorphism. Here, we have list of three points to describe function overloading in C++: The parameter ... Read More
In C++, you can read data from a text file using file handling features provided by the header. This is useful when you want your program to read input stored in a file instead of typing it every time. To do this, you use a special object called ifstream (input file stream), which helps your program open the file and read its contents line by line or word by word. Reading a text file is helpful when: You want to process saved data (like scores, settings, or logs). You want to ... Read More
Listing files in a directory is used to write a program that opens a specified folder (e.g: "/myfiles"), reads its contents, and displays the names of each file and subfolder one by one. In C/C++, to see all the files in a directory, you can use special system functions that let you read the directory's contents. In real life, we open folder to see the contents inside the files. Similarly, in C or C++, we can write a program to display all the files and folders in a directory. Algorithm Following is the algorithm to get the list of files ... Read More
The auto and decltype serve different purposes so they don't map one-to-one. The auto is a keyword in C++11 and later that is used for automatic type deduction. The decltype type specifier yields the type of a specified expression. Unlike auto that deduces types based on values being assigned to the variable, decltype deduces the type from an expression passed to it. The value returned by decltype can directly be used to define another variable. The auto follows the rules of template parameter deduction. You can read more about these rule at Template Argument Deduction While decltype has rules it ... Read More