Akhil Sharma

Akhil Sharma

507 Articles Published

Articles by Akhil Sharma

Page 20 of 51

Golang program to find the depth of a node in a binary search tree

Akhil Sharma
Akhil Sharma
Updated on 03-Apr-2023 684 Views

In this Golang article, we are going to find the depth of a node in a binary search tree using recursion and iterative method. The binary search tree is a useful data structure for efficient searching, insertion, and deletion of elements. A binary search tree (BST) is a type of binary tree where every node has at most two children, commonly referred to as the left child and the right child. Syntax func (n *Node) Depth(value int) int {…} The Depth() function is used to find the depth of a node in a binary search tree. It takes an ...

Read More

Golang program to find intersection of two sorted arrays using two pointer approach

Akhil Sharma
Akhil Sharma
Updated on 03-Apr-2023 581 Views

In this Go language article, we will write programs to find intersection of two arrays using two pointer approach. Intersection means similarity or most common and intersection of two arrays refers to the similar elements between two arrays. Syntax func make ([] type, size, capacity) The make function in go language is used to create an array/map it accepts the type of variable to be created, its size and capacity as arguments func append(slice, element_1, element_2…, element_N) []T The append function is used to add values to an array slice. It takes number of arguments. The first argument ...

Read More

Golang program to implement depth first search

Akhil Sharma
Akhil Sharma
Updated on 03-Apr-2023 5K+ Views

In this article, we are going to learn how to use internal Golang functions like make, append, and range to implement depth first search. Depth first search is a traversal algorithm used for graph and tree data structures. It explores all the nodes of the graph recursively. Syntax func make ([] type, size, capacity) The make function in go language is used to create an array/map it accepts the type of variable to be created, its size and capacity as arguments func append(slice, element_1, element_2…, element_N) []T The append function is used to add values to an array ...

Read More

Golang program to get the individual components of the current date-time

Akhil Sharma
Akhil Sharma
Updated on 03-Apr-2023 314 Views

In this article, we are going to learn how to use various internal time function in Golang that will get the individual components of the current date-time. Now function from the time package will be used to get the current date-time and the individual components of date-time are calculated using the individual components as methods. Syntax func Now() Time The Now() function is defined in time package. This function generates the current local time. To use this function, we have to first import the time package in our program. time.Month() This method belongs to the time package. It ...

Read More

Golang program to display time in different country’s format

Akhil Sharma
Akhil Sharma
Updated on 03-Apr-2023 494 Views

In Golang we can use the functions like time.Format() and Now() Time to display the time in different country’s format. Now function from the time package is used to get the current local time and the format function is used to format the time string using different format strings. Syntax time.Format() This function is present in the time package. It takes a layout input string that depicts how the Output string will be formatted. func Now() Time The Now() function is defined in time package. This function generates the current local time. To use this function, we have ...

Read More

Golang program to demonstrate the argument passing to block

Akhil Sharma
Akhil Sharma
Updated on 03-Apr-2023 227 Views

In this article, we are going to learn how to demonstrate the argument to block using the user-defined function, iterations, and square of numbers . A block is created using curly braces with a definite begin and end braces. The different operations are performed inside the braces. Algorithm Step 1 − Import the required packages in the program Step 2 − Create a main function and in there create a slice Step 3 − Use a function to pass the argument and print the values with the help of iteration Step 4 − Print statement is executed ...

Read More

Golang program to get the current day of the year out of 365

Akhil Sharma
Akhil Sharma
Updated on 03-Apr-2023 946 Views

In Golang we can use functions like YearDay and Sub as well as arithmetic operation method to get the current day of the year out of 365. The current time is obtained using the Now function from the time package and YearDay() function is used to obtain the current day of the year out of 365. Method 1: Using YearDay and Sub Functions In this method, the number of days between the current date and start of the year are calculated using currentTime.Sub(start_of_year).Hours() / 24 as well as simple YearDay method. Syntax func Now() Time The Now() function ...

Read More

Golang program to add two dates

Akhil Sharma
Akhil Sharma
Updated on 03-Apr-2023 471 Views

In this Golang article, we will write programs to add two dates. The dates are added using methods like Add(), AddDate(). There are different formats to represent a date. Here, the dates after addition will be represented using YY/MM/DD format. Syntax func (t Time) Sub(u Time) Duration The sub() function in Go is used to get the difference between two dates. In this function the first two parameters i.e., t and u are date values and this function returns the difference between two values in hours, minutes and seconds. time.Parse() This function belongs to the time package. It ...

Read More

Haskell Program to Convert String value to byte Value

Akhil Sharma
Akhil Sharma
Updated on 28-Mar-2023 440 Views

In Haskell, we will convert String Value to Byte value by using ord , B.pack and fromNum functions. In the first example, we are going to use (stringToBytes = map ord ) function and in the second example, we are going to call (stringToBytes = B.pack) function. And in the third example, we are going to use (byteList = map fromEnum str) function. Algorithm Step 1 − The Data.Char module is imported. Step 2 − The stringToBytes function is defined using ord function as, Step 3 − The program execution will be started from main function. The main() function ...

Read More

Haskell Program to Convert List to a Map

Akhil Sharma
Akhil Sharma
Updated on 28-Mar-2023 856 Views

In Haskell, we will convert List to a Map by using fromList and foldl functions. This can also be done using recursion and pattern matching. In the first example, we are going to use (listToMap xs = Map.fromList xs) function and in the second example, we are going to use (listToMap xs = List.foldl' (\acc (k, v) -> Map.insert k v acc) Map.empty xs) function. In the third example, we are going to use base and recursive case of the recursive function. Algorithm Step 1 − The qualified Data.Map module is imported. Step 2 − The listToMap function is ...

Read More
Showing 191–200 of 507 articles
« Prev 1 18 19 20 21 22 51 Next »
Advertisements