Programming Leftovers
C/C++
-
Merge N Sorted Linked Lists Using Min Heap
Here, the goal is to benefit from the fact that a min-root heap always returns the smallest member. The initial entry of each linked list is the smallest element in its corresponding list since, in this technique, all the linked lists are already sorted. We take advantage of this circumstance by creating a min-heap out of the initial member of each linked list. The smallest element is then obtained by extracting the top element (root) of the min-heap. We get the smallest element across all linked lists by doing this. The next element is then added to the min-heap after we increase the pointer to the list that the freshly extracted element belongs to. A new element is taken from the min-heap, the linked list pointer that contains it is incremented, and the newly pointed element is then added to the min-heap. Until the min-heap is completely empty, this operation is repeated. Keep in mind that we continuously add the elements that we extract from the min-heap to a separate result list that we are keeping.
The method’s flexibility is a key benefit. With a few minor adjustments, this approach may also be used to combine the N sorted arrays. Some of the other ways fail to do this, but this approach succeeds even when all the linked lists are not the same size.
-
Char Array to String in C++
In the C++ programming language, many different types of data types are used to store the data. These data types are built-in datatypes. In the C++ language, one of these basic data types is “char” which is known as the character that has a buffer size of 1 byte. Whereas, “string” is not a built-in data type; it functions in a way that is similar to one when used simply. In simple words, it can store the characters similar to character arrays and allows the users to perform the operations on strings that are not possible with character arrays. Because the user sometimes wants to supply the string as an argument, we first transform the character array into a string. It is simpler to manipulate the strings when they are converted to a string object since we can then use a variety of methods and have overloaded operators such as the += operator method, pushback method, append method, etc.
-
Argc and Argv in C++
We learned a lot about how to pass parameters in functions previously. We further discovered why parameters are passed and what we get in return from functions. However, we often did not supply any arguments or parameters to the main() function. In C++ programs, we can also pass the parameters in the main() function. These parameters are called command line arguments or command line parameters. We pass the parameters while running the program on the command prompt window just after the program’s name. These two parameters are “argc” and “argv”. The “argc” stands for argument count and it is used to store the non-negative integer type argument including the program name. The argv stands for argument vector because it is a character pointer of an array that stores the actual parameters.
-
Execv() Function in C Language
There are several ways and functions to start the processes from a program. Each of them offers different solutions to the programmer depending on the needs of the particular case.
The execXX() family is a set of system functions that are defined in the “unistd.h” header to run a process from within a program. This family of functions allows us to send the input arguments to the executable that calls it which is why it is a very useful resource for interaction between processes.
In this Linux Hint article, you’ll learn all about the execv() function in the C language. We will look at its syntax, the theoretical description of how it works, the input and output arguments, and the type of data that each of them processes. You’ll also learn how to create and use the arrays of strings and pointers that are part of the input arguments of this family of functions and are passed to the calling process.
Then, we apply what we learned in a practical example that shows the correct implementation and calling of the execv() function.
-
EOF in C++
In file handling, we don’t know how much data is stored in the file. Since the extent of data in filing is mostly unknown, how can we read the file in that case? When we don’t know how much data the file contains, we use the eof() method to read a file. The eof() method is “End of File” which reads the file until the control reaches the end of the file. This methodology is very suitable to present the data of files. The things we know about the files are the type of file data and the formatting style of the stored data. The eof() function allows us to determine when the file has reached its end.
EOF() is a bool-type constant method that returns true when the end of the file is reached and it returns false when there is still data to be read. It is a method of input-output stream class (ios) that reads the data of a file until the end is encountered.
Git
-
Gogs Is the Easiest Way to Run a Local Git Server (Here's How to Set It Up)
Don’t want to push your code to a hosted Git repository? Then run your own Git server in-house. Gogs is the easiest way to do that. Here’s how to set it up.
-
How to Rename a Branch in Git
Branching is a trivial exercise in Git. Unfortunately, trivial tasks rarely get due attention, and mistakes happen. If you’ve misnamed a branch, it’ll have to be renamed. Here’s how to do that.
Shell/Bash
-
Remove a Specific Element from an Array in Bash
Bash handles only the associative arrays and the one-dimensional numerically indexed arrays. In Bash scripting, arrays do not necessarily consist of a group of associated elements. The array elements can be strings and numbers because Bash does not differentiate between them. In this article, we will perform the deletion operations on the Bash arrays with different approaches. We need to know the index position of the element in the array or key to remove it from the array. The “unset” command is commonly used in Bash to remove an element.
-
How to Use the Bash “Set –X” Option
The set command of Linux is used to set and unset specific options. These options manage the behavior of a given script and enable the task execution without error. In this article, we use the “x” option with the set command which informs the Bash to display the statements as they are processed. It is immensely useful for debugging when we need to determine which lines were executed and in what sequence. The set -x operation can be employed directly in an open Bash shell or within a Bash script. In the following examples, we use the set command with the “x” option to enable and disable the various functionalities.
-
What is $0 in a Bash Script?
Bash is a command interpreter and script. Like other forms of coding, it supports packet forwarding, variables, and functions. A script is a file with commands that the software may view and perform. In Linux, bash gives us positional parameters like “$0,” “$1,” “$2,” “$3,” etc. In this article, one of its positional parameters, “$0,” will be used.
Python
-
Malware Delivered to PyTorch Users in Supply Chain Attack
Now part of the Linux Foundation umbrella, PyTorch is based on the Torch library and is used for applications in computer vision and natural language processing fields.
-
PyTorch admins warn of malicious dependency
Package maintainers for open source framework PyTorch have said a malicious dependency imitating one of its own was available on a prominent code repository during the holiday season.