Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEATURE] : added /impl directory for algorithms implementation and other utility Go programs. #5172

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
7 changes: 7 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,13 @@ that the resulting list has at least 3 projects in every category, and that the

Fill out the template in your PR with the links asked for. If you accidentally remove the PR template from the submission, you can find it [here](https://github.com/avelino/awesome-go/blob/main/.github/PULL_REQUEST_TEMPLATE.md).

## Contributions to the implementation of algorithms and utility programs - `/impl` directory

Open a pull request against the `/impl` directory that fixes or adds a feature.

- The pull request must add one and only one feature or any fixes;
- You should make sure to test the changes locally;
- It will be reviewed by one of the awesome-go maintainers, [@pacifiquem](https://github.com/pacifiquem) is the main reviewer for this topic.

## Congrats, your project got accepted - what now?
You are an outstanding project now! Feel encouraged to tell others about it by adding one of these badges:
Expand Down
63 changes: 63 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ Please take a quick gander at the [contribution guidelines](https://github.com/a
- [Hardware](#hardware)
- [Images](#images)
- [IoT (Internet of Things)](#iot-internet-of-things)
- [Implementations](#implementations)
- [Job Scheduler](#job-scheduler)
- [JSON](#json)
- [Logging](#logging)
Expand Down Expand Up @@ -1409,6 +1410,68 @@ _Libraries for programming devices of the IoT._

**[⬆ back to top](#contents)**

## Implementations

_Implementations Of Algorithms & utility programs in Go._

- [Implementations](https://github.com/avelino/awesome-go/tree/main/impl/algorithms)
- [Algorithms](https://github.com/avelino/awesome-go/tree/main/impl/algorithms)
- [Data Structures](https://github.com/avelino/awesome-go/tree/main/impl/algorithms/Data%20Structures)
- [Graphs](https://github.com/avelino/awesome-go/blob/main/impl/algorithms/Data%20Structures/graphs.go)
- [Linked List](https://github.com/avelino/awesome-go/blob/main/impl/algorithms/Data%20Structures/linkedlist.go)
- [Queue](https://github.com/avelino/awesome-go/blob/main/impl/algorithms/Data%20Structures/queue.go)
- [Stack](https://github.com/avelino/awesome-go/blob/main/impl/algorithms/Data%20Structures/stack.go)
- [Tree](https://github.com/avelino/awesome-go/blob/main/impl/algorithms/Data%20Structures/tree.go)
- [Dynamic Programming](https://github.com/avelino/awesome-go/tree/main/impl/algorithms/Dynamic%20Programming)
- [Greedy Algorithms](https://github.com/avelino/awesome-go/tree/main/impl/algorithms/Greedy%20Algorithms)
- [Fibonacci Sequence](https://github.com/avelino/awesome-go/blob/main/impl/algorithms/Dynamic%20Programming/Fibonacci%20Sequence.go)
- [Longest Common](https://github.com/avelino/awesome-go/blob/main/impl/algorithms/Dynamic%20Programming/Longest%20Common%20Subsequence.go)
- [Knapsack Problem](https://github.com/avelino/awesome-go/blob/main/impl/algorithms/Dynamic%20Programming/Knapsack%20Problem.go)
- [Searching](https://github.com/avelino/awesome-go/tree/main/impl/algorithms/searching)
- [Binary Search](https://github.com/avelino/awesome-go/blob/main/impl/algorithms/searching/binary.go)
- [Jump Search](https://github.com/avelino/awesome-go/blob/main/impl/algorithms/searching/jump.go)
- [Linear Search](https://github.com/avelino/awesome-go/blob/main/impl/algorithms/searching/linear.go)
- [Sorting](https://github.com/avelino/awesome-go/tree/main/impl/algorithms/sorting)
- [Bubble Sort](https://github.com/avelino/awesome-go/blob/main/impl/algorithms/sorting/bubble.go)
- [Heap Sort](https://github.com/avelino/awesome-go/blob/main/impl/algorithms/sorting/heap.go)
- [Insertion Sort](https://github.com/avelino/awesome-go/blob/main/impl/algorithms/sorting/insertion.go)
- [Merge Sort](https://github.com/avelino/awesome-go/blob/main/impl/algorithms/sorting/merge.go)
- [Quick Sort](https://github.com/avelino/awesome-go/blob/main/impl/algorithms/sorting/quick.go)
- [Selection Sort](https://github.com/avelino/awesome-go/blob/main/impl/algorithms/sorting/selection.go)
- [File samples](https://github.com/avelino/awesome-go/tree/main/impl/files-manipulation)
- [Creating Empty File](https://github.com/avelino/awesome-go/blob/main/impl/files-manipulation/01-CreateEmptyFile.go)
- [Truncate File](https://github.com/avelino/awesome-go/blob/main/impl/files-manipulation/02-TruncateFile.go)
- [Get File Info](https://github.com/avelino/awesome-go/blob/main/impl/files-manipulation/03-GetFileInfo.go)
- [Rename a File](https://github.com/avelino/awesome-go/blob/main/impl/files-manipulation/04-RenameFile.go)
- [Remove a File](https://github.com/avelino/awesome-go/blob/main/impl/files-manipulation/05-RemoveFile.go)
- [Open ~ Close a File](https://github.com/avelino/awesome-go/blob/main/impl/files-manipulation/06-OpenCloseFile.go)
- [Check File Exist](https://github.com/avelino/awesome-go/blob/main/impl/files-manipulation/07-CheckFileExist.go)
- [Check File Permission](https://github.com/avelino/awesome-go/blob/main/impl/files-manipulation/08-CheckFilePermissions.go)
- [Change File Permissions](https://github.com/avelino/awesome-go/blob/main/impl/files-manipulation/09-ChangeFilePermissions.go)
- [Hardlink Symlink](https://github.com/avelino/awesome-go/blob/main/impl/files-manipulation/10-HardlinkSymlink.go)
- [Copy a File](https://github.com/avelino/awesome-go/blob/main/impl/files-manipulation/11-CopyFile.go)
- [Seek Position in a File](https://github.com/avelino/awesome-go/blob/main/impl/files-manipulation/12-SeekPositionInFile.go)
- [Write Bytes to a file](https://github.com/avelino/awesome-go/blob/main/impl/files-manipulation/13-WriteBytesToFile.go)
- [Quick write to a file](https://github.com/avelino/awesome-go/blob/main/impl/files-manipulation/14-QuickWriteToFile.go)
- [Open file to write](https://github.com/avelino/awesome-go/blob/main/impl/files-manipulation/15-OpenFileToWrite.go)
- [Read from a file](https://github.com/avelino/awesome-go/blob/main/impl/files-manipulation/16-ReadFromFile.go)
- [Open file to read](https://github.com/avelino/awesome-go/blob/main/impl/files-manipulation/17-OpenFileToRead.go)
- [Read N bytes from a file](https://github.com/avelino/awesome-go/blob/main/impl/files-manipulation/18-ReadNBytesFromFile.go)
- [Read all bytes from a file](https://github.com/avelino/awesome-go/blob/main/impl/files-manipulation/19-ReadAllBytesFromFile.go)
- [Quick readle file](https://github.com/avelino/awesome-go/blob/main/impl/files-manipulation/20-QuickReadWholeFile.go)
- [Use buffered reader](https://github.com/avelino/awesome-go/blob/main/impl/files-manipulation/21-UseBufferedReader.go)
- [Read with scanner](https://github.com/avelino/awesome-go/blob/main/impl/files-manipulation/22-ReadWithScanner.go)
- [Archieve files](https://github.com/avelino/awesome-go/blob/main/impl/files-manipulation/23-ArchiveFiles.go)
- [Extract Files](https://github.com/avelino/awesome-go/blob/main/impl/files-manipulation/24-ExtractFiles.go)
- [Compress files](https://github.com/avelino/awesome-go/blob/main/impl/files-manipulation/25-CompressFile.go)
- [Decompress files](https://github.com/avelino/awesome-go/blob/main/impl/files-manipulation/26-DecompressFile.go)
- [Temp files and dirs](https://github.com/avelino/awesome-go/blob/main/impl/files-manipulation/27-TempFilesAndDirs.go)
- [Download file over http](https://github.com/avelino/awesome-go/blob/main/impl/files-manipulation/28-DownloadFileOverHTTP.go)
- [Hash and checksum](https://github.com/avelino/awesome-go/blob/main/impl/files-manipulation/29-HashAndChecksum.go)
- [Has and checksum2](https://github.com/avelino/awesome-go/blob/main/impl/files-manipulation/30-HasAndChecksum2.go)

**[⬆ back to top](#contents)**

## Job Scheduler

_Libraries for scheduling jobs._
Expand Down
3 changes: 3 additions & 0 deletions impl/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Algorithms and utility programs - `/impl`

This subdirectory houses a collection of Go programs encompassing algorithm implementations and utility programs. These resources are crafted to provide efficient and versatile solutions for a variety of tasks, leveraging the power and simplicity of the Go programming language.
86 changes: 86 additions & 0 deletions impl/algorithms/Data Structures/graphs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package main

import "fmt"

type Graph struct {
graph map[int][]int
}

func NewGraph() *Graph {
return &Graph{
graph: make(map[int][]int),
}
}

func (g *Graph) AddEdge(node1, node2 int) {
g.graph[node1] = append(g.graph[node1], node2)
}

func (g *Graph) AddNode(node int) {
if _, ok := g.graph[node]; !ok {
g.graph[node] = []int{}
}
}

func (g *Graph) DFS(startNode int) {
visited := make(map[int]bool)
g.dfsHelper(startNode, visited)
}

func (g *Graph) dfsHelper(node int, visited map[int]bool) {
visited[node] = true
fmt.Printf("%d ", node)

for _, neighbor := range g.graph[node] {
if !visited[neighbor] {
g.dfsHelper(neighbor, visited)
}
}
}

func (g *Graph) BFS(startNode int) {
visited := make(map[int]bool)
queue := []int{startNode}
visited[startNode] = true

for len(queue) > 0 {
node := queue[0]
queue = queue[1:]
fmt.Printf("%d ", node)

for _, neighbor := range g.graph[node] {
if !visited[neighbor] {
queue = append(queue, neighbor)
visited[neighbor] = true
}
}
}
}

func main() {
// Create a graph
graph := NewGraph()

// Add nodes
graph.AddNode(1)
graph.AddNode(2)
graph.AddNode(3)
graph.AddNode(4)
graph.AddNode(5)

// Add edges
graph.AddEdge(1, 2)
graph.AddEdge(1, 3)
graph.AddEdge(2, 4)
graph.AddEdge(2, 5)

// Perform DFS traversal
fmt.Println("DFS traversal:")
graph.DFS(1)
fmt.Println()

// Perform BFS traversal
fmt.Println("BFS traversal:")
graph.BFS(1)
fmt.Println()
}
171 changes: 171 additions & 0 deletions impl/algorithms/Data Structures/linkedlist.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
package main

import (
"fmt"
)

type Node struct {
data int
next *Node
}

func newNode(data int) *Node {
return &Node{data: data, next: nil}
}

func (n *Node) size(head *Node) int {
if head == nil {
fmt.Println("Linkedlist is empty")
return -1
}

size := 0
for head != nil {
size += 1
head = head.next
}

return size
}

func (n *Node) display(head *Node) {
if head == nil {
fmt.Println("Linkedlist is empty")
return
}

for head != nil {
fmt.Print(head.data, " ")
head = head.next
}
fmt.Println()
}

func (n *Node) insertAtHead(data int, head *Node) *Node {
newNode := newNode(data)
newNode.next = head
head = newNode

return head
}

func (n *Node) insertAtTail(data int, head *Node) *Node {
newNode := newNode(data)
if head == nil {
return newNode
}

current := head
for current.next != nil {
current = current.next
}

current.next = newNode
return head
}

func (n *Node) insertAtPosition(headRef **Node, position, data int) {
newNode := newNode(data)

if *headRef == nil || position == 0 {
newNode.next = *headRef
*headRef = newNode
return
}

current := *headRef
for i := 0; current != nil && i < position-1; i++ {
current = current.next
}

if current == nil {
return
}

newNode.next = current.next
current.next = newNode
}

func (n *Node) deleteAtHead(head *Node) *Node {
if head == nil {
return nil
}
newHead := head.next
head = nil
return newHead
}

func (n *Node) deleteAtTail(head *Node) *Node {
if head == nil {
fmt.Println("Linked list is empty.")
return nil
}

if head.next == nil {
head = nil
return nil
}

cur := head
for cur.next.next != nil {
cur = cur.next
}

cur.next = nil
return head
}

func (n *Node) deleteAtPosition(position int, head *Node) *Node {
if head == nil {
fmt.Println("Linked list is empty.")
return nil
}

current := head
var prev *Node
i := 0

for current != nil && i < position-1 {
prev = current
current = current.next
i++
}

if current == nil || current.next == nil {
fmt.Println("Invalid position")
return head
}

temp := current.next
current.next = temp.next
temp = nil

if position == 1 {
head = current.next
}

return head
}

func main() {
// create a linked list and perform operations
head := newNode(1)
head = head.insertAtTail(2, head)
head = head.insertAtTail(3, head)
head = head.insertAtHead(0, head)
head.display(head) // expected output: 0 1 2 3
fmt.Println(head.size(head)) // expected output: 4
head = head.deleteAtHead(head)
head.display(head) // expected output: 1 2 3
head = head.deleteAtTail(head)
head.display(head) // expected output: 1 2
head = head.deleteAtPosition(1, head)
head.display(head) // expected output: 1
head = head.deleteAtPosition(0, head)
head.display(head) // expected output:
head = head.insertAtTail(2, head)
head = head.insertAtTail(3, head)
head = head.insertAtHead(1, head)
head = head.insertAtPosition(&head, 1, 4)
head.display(head) // expected output: 1 4 2 3
}
Loading
Loading