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
66 changes: 66 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,71 @@ _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](https://github.com/avelino/awesome-go/tree/main/impl/algorithms)
- [Data Structures](https://github.com/avelino/awesome-go/tree/main/impl/algorithms/data_structures)
- [Graphs](https://github.com/avelino/awesome-go/blob/main/impl/algorithms/data_structures/graphs.go)
- [Linked List](https://github.com/avelino/awesome-go/blob/main/impl/algorithms/data_structures/linkedlist.go)
- [Queue](https://github.com/avelino/awesome-go/blob/main/impl/algorithms/data_structures/queue.go)
- [Stack](https://github.com/avelino/awesome-go/blob/main/impl/algorithms/data_structures/stack.go)
- [Tree](https://github.com/avelino/awesome-go/blob/main/impl/algorithms/data_structures/tree.go)
- [Dynamic Programming](https://github.com/avelino/awesome-go/tree/main/impl/algorithms/dynamic_programming)
- [Fibonacci Sequence](https://github.com/avelino/awesome-go/blob/main/impl/algorithms/dynamic_programming/FibonacciSequence.go)
- [Knapsack Problem](https://github.com/avelino/awesome-go/blob/main/impl/algorithms/dynamic_programming/KnapsackProblem.go)
- [Longest Common](https://github.com/avelino/awesome-go/blob/main/impl/algorithms/dynamic_programming/LongestCommonSubsequence.go)
- [Greedy Algorithms](https://github.com/avelino/awesome-go/tree/main/impl/algorithms/greedy_algorithms)
-[Dijkstra](https://github.com/avelino/awesome-go/tree/main/impl/algorithms/greedy_algorithms/dijkstra.go)
-[Kruskal](https://github.com/avelino/awesome-go/tree/main/impl/algorithms/greedy_algorithms/kruskal.go)
-[Prim](https://github.com/avelino/awesome-go/tree/main/impl/algorithms/greedy_algorithms/prim.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)
- [archive files](https://github.com/avelino/awesome-go/blob/main/impl/files_manipulation/ArchiveFiles.go)
- [change file permissions](https://github.com/avelino/awesome-go/blob/main/impl/files_manipulation/ChangeFilePermissions.go)
- [check file exist](https://github.com/avelino/awesome-go/blob/main/impl/files_manipulation/CheckFileExist.go)
- [check file permission](https://github.com/avelino/awesome-go/blob/main/impl/files_manipulation/CheckFilePermissions.go)
- [compress files](https://github.com/avelino/awesome-go/blob/main/impl/files_manipulation/CompressFile.go)
- [copy a file](https://github.com/avelino/awesome-go/blob/main/impl/files_manipulation/CopyFile.go)
- [create empty file](https://github.com/avelino/awesome-go/blob/main/impl/files_manipulation/CreateEmptyFile.go)
- [decompress files](https://github.com/avelino/awesome-go/blob/main/impl/files_manipulation/DecompressFile.go)
- [download file over http](https://github.com/avelino/awesome-go/blob/main/impl/files_manipulation/DownloadFileOverHTTP.go)
- [extract files](https://github.com/avelino/awesome-go/blob/main/impl/files_manipulation/ExtractFiles.go)
- [get file info](https://github.com/avelino/awesome-go/blob/main/impl/files_manipulation/GetFileInfo.go)
- [hardlink symlink](https://github.com/avelino/awesome-go/blob/main/impl/files_manipulation/HardlinkSymlink.go)
- [hash and checksum](https://github.com/avelino/awesome-go/blob/main/impl/files_manipulation/HashAndChecksum.go)
- [hash and checksum2](https://github.com/avelino/awesome-go/blob/main/impl/files_manipulation/HasAndChecksum2.go)
- [open file to read](https://github.com/avelino/awesome-go/blob/main/impl/files_manipulation/OpenFileToRead.go)
- [open file to write](https://github.com/avelino/awesome-go/blob/main/impl/files_manipulation/OpenFileToWrite.go)
- [open or close a file](https://github.com/avelino/awesome-go/blob/main/impl/files_manipulation/OpenCloseFile.go)
- [quick read whole file](https://github.com/avelino/awesome-go/blob/main/impl/files_manipulation/QuickReadWholeFile.go)
- [quick write to a file](https://github.com/avelino/awesome-go/blob/main/impl/files_manipulation/QuickWriteToFile.go)
- [read all bytes from a file](https://github.com/avelino/awesome-go/blob/main/impl/files_manipulation/ReadAllBytesFromFile.go)
- [read from a file](https://github.com/avelino/awesome-go/blob/main/impl/files_manipulation/ReadFromFile.go)
- [read n bytes from a file](https://github.com/avelino/awesome-go/blob/main/impl/files_manipulation/ReadNBytesFromFile.go)
- [read with scanner](https://github.com/avelino/awesome-go/blob/main/impl/files_manipulation/ReadWithScanner.go)
- [remove a file](https://github.com/avelino/awesome-go/blob/main/impl/files_manipulation/RemoveFile.go)
- [rename a file](https://github.com/avelino/awesome-go/blob/main/impl/files_manipulation/RenameFile.go)
- [seek position in a file](https://github.com/avelino/awesome-go/blob/main/impl/files_manipulation/SeekPositionInFile.go)
- [temp files and dirs](https://github.com/avelino/awesome-go/blob/main/impl/files_manipulation/TempFilesAndDirs.go)
- [truncate file](https://github.com/avelino/awesome-go/blob/main/impl/files_manipulation/TruncateFile.go)
- [use buffered reader](https://github.com/avelino/awesome-go/blob/main/impl/files_manipulation/UseBufferedReader.go)
- [write bytes to a file](https://github.com/avelino/awesome-go/blob/main/impl/files_manipulation/WriteBytesToFile.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()
}
169 changes: 169 additions & 0 deletions impl/algorithms/data_structures/linkedlist.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
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 0
}

size := 0
for head != nil {
size++
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
return newNode
}

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) *Node {
newNode := newNode(data)

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

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

if current == nil {
return *headRef
}

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

return *headRef
}

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

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

if head.next == 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
}

if position == 0 {
return head.next
}

current := head
var prev *Node

for current != nil && position > 0 {
prev = current
current = current.next
position--
}

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

prev.next = current.next
return head
}

func main() {
// create a linked list and perform operations
var head *Node
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