From da846c640766cae5881b7ae5f56418279436864e Mon Sep 17 00:00:00 2001 From: MURANGWA Pacifique Date: Sat, 20 Jan 2024 10:55:19 +0200 Subject: [PATCH] fix: removal of deprecated packages --- impl/algorithms/Data Structures/linkedlist.go | 104 ++++---- impl/algorithms/Data Structures/queue.go | 123 +++++----- impl/algorithms/Greedy Algorithms/dijkstra.go | 223 ++++-------------- impl/algorithms/Greedy Algorithms/prim.go | 2 +- .../files-manipulation/14-QuickWriteToFile.go | 4 +- .../19-ReadAllBytesFromFile.go | 4 +- .../20-QuickReadWholeFile.go | 4 +- ...OverHTTP.go => 27-DownloadFileOverHTTP.go} | 0 .../files-manipulation/27-TempFilesAndDirs.go | 47 ---- ...shAndChecksum.go => 28-HashAndChecksum.go} | 4 +- 10 files changed, 168 insertions(+), 347 deletions(-) rename impl/files-manipulation/{28-DownloadFileOverHTTP.go => 27-DownloadFileOverHTTP.go} (100%) delete mode 100644 impl/files-manipulation/27-TempFilesAndDirs.go rename impl/files-manipulation/{29-HashAndChecksum.go => 28-HashAndChecksum.go} (88%) diff --git a/impl/algorithms/Data Structures/linkedlist.go b/impl/algorithms/Data Structures/linkedlist.go index 678bea0b..64ee18aa 100644 --- a/impl/algorithms/Data Structures/linkedlist.go +++ b/impl/algorithms/Data Structures/linkedlist.go @@ -1,8 +1,6 @@ package main -import ( - "fmt" -) +import "fmt" type Node struct { data int @@ -16,12 +14,12 @@ func newNode(data int) *Node { func (n *Node) size(head *Node) int { if head == nil { fmt.Println("Linkedlist is empty") - return -1 + return 0 } size := 0 for head != nil { - size += 1 + size++ head = head.next } @@ -44,9 +42,7 @@ func (n *Node) display(head *Node) { func (n *Node) insertAtHead(data int, head *Node) *Node { newNode := newNode(data) newNode.next = head - head = newNode - - return head + return newNode } func (n *Node) insertAtTail(data int, head *Node) *Node { @@ -64,13 +60,13 @@ func (n *Node) insertAtTail(data int, head *Node) *Node { return head } -func (n *Node) insertAtPosition(headRef **Node, position, data int) { +func (n *Node) insertAtPosition(headRef **Node, position, data int) *Node { newNode := newNode(data) if *headRef == nil || position == 0 { newNode.next = *headRef *headRef = newNode - return + return *headRef } current := *headRef @@ -79,20 +75,20 @@ func (n *Node) insertAtPosition(headRef **Node, position, data int) { } if current == nil { - return + return *headRef } newNode.next = current.next current.next = newNode + + return *headRef } func (n *Node) deleteAtHead(head *Node) *Node { if head == nil { return nil } - newHead := head.next - head = nil - return newHead + return head.next } func (n *Node) deleteAtTail(head *Node) *Node { @@ -102,7 +98,6 @@ func (n *Node) deleteAtTail(head *Node) *Node { } if head.next == nil { - head = nil return nil } @@ -121,51 +116,54 @@ func (n *Node) deleteAtPosition(position int, head *Node) *Node { return nil } - current := head - var prev *Node - i := 0 - - for current != nil && i < position-1 { - prev = current - current = current.next - i++ + if position == 0 { + return head.next } - if current == nil || current.next == nil { + 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 } - temp := current.next - current.next = temp.next - temp = nil - - if position == 1 { - head = current.next - } - + prev.next = 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 -} \ No newline at end of file + // 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 +} diff --git a/impl/algorithms/Data Structures/queue.go b/impl/algorithms/Data Structures/queue.go index 00fa0cf1..c6ac549d 100644 --- a/impl/algorithms/Data Structures/queue.go +++ b/impl/algorithms/Data Structures/queue.go @@ -9,94 +9,95 @@ var rear int = -1 var front int = -1 func isEmpty() bool { -return front == -1 + return front == -1 } func peek() int { -if isEmpty() { -fmt.Println("Queue is empty") -return -1 -} -return queue[front] + if isEmpty() { + fmt.Println("Queue is empty") + return -1 + } + return queue[front] } func isFull() bool { return rear >= QUEUE_SIZE-1 - } - - func enqueue(data int) { +} + +func enqueue(data int) { if isFull() { - fmt.Println("Queue Overflow") - return - }rear += 1 + fmt.Println("Queue Overflow") + return + } + rear += 1 queue[rear] = data - + if front == -1 { front = 0 } - } - - func dequeue() { +} + +func dequeue() { if isEmpty() { - fmt.Println("Queue Underflow") - return + fmt.Println("Queue Underflow") + return } - + front += 1 if front > rear { front = -1 rear = -1 } - } - - func size() int { +} + +func size() int { if isEmpty() { - return 0 + return 0 } - + return rear - front + 1 } func display() { -if isEmpty() { -fmt.Println("Queue is empty") -return -} + if isEmpty() { + fmt.Println("Queue is empty") + return + } -for i := front; i <= rear; i++ { - fmt.Printf("%d ", queue[i]) -} -fmt.Println() + for i := front; i <= rear; i++ { + fmt.Printf("%d ", queue[i]) + } + fmt.Println() } func main() { -// testing the queue implementation -enqueue(1) -enqueue(2) -enqueue(3) -enqueue(4) -display() // output: 1 2 3 4 -dequeue() -dequeue() -display() // output: 3 4 -enqueue(5) -enqueue(6) -enqueue(7) -enqueue(8) -enqueue(9) -enqueue(10) -display() // output: 3 4 5 6 7 8 9 10 -enqueue(11) // output: Queue Overflow -dequeue() -dequeue() -dequeue() -dequeue() -dequeue() -dequeue() -dequeue() -dequeue() -dequeue() -display() // output: Queue is empty -dequeue() // output: Queue Underflow -} \ No newline at end of file + // testing the queue implementation + enqueue(1) + enqueue(2) + enqueue(3) + enqueue(4) + display() // output: 1 2 3 4 + dequeue() + dequeue() + display() // output: 3 4 + enqueue(5) + enqueue(6) + enqueue(7) + enqueue(8) + enqueue(9) + enqueue(10) + display() // output: 3 4 5 6 7 8 9 10 + enqueue(11) // output: Queue Overflow + dequeue() + dequeue() + dequeue() + dequeue() + dequeue() + dequeue() + dequeue() + dequeue() + dequeue() + display() // output: Queue is empty + dequeue() // output: Queue Underflow +} diff --git a/impl/algorithms/Greedy Algorithms/dijkstra.go b/impl/algorithms/Greedy Algorithms/dijkstra.go index 81ecc289..915827a2 100644 --- a/impl/algorithms/Greedy Algorithms/dijkstra.go +++ b/impl/algorithms/Greedy Algorithms/dijkstra.go @@ -1,203 +1,72 @@ -// C++ program for Dijkstra's single source shortest path -// algorithm. The program is for adjacency matrix -// representation of the graph -#include -using namespace std; -#include - -// Number of verticepackage main +package main import ( - "fmt" - "math" + "fmt" + "math" ) -// Number of vertices in the graph const V = 9 -// A utility function to find the vertex with minimum -// distance value, from the set of vertices not yet included -// in shortest path tree func minDistance(dist []int, sptSet []bool) int { - // Initialize min value - min := math.MaxInt32 - var minIndex int + min := math.MaxInt32 + var minIndex int - for v := 0; v < V; v++ { - if sptSet[v] == false && dist[v] <= min { - min = dist[v] - minIndex = v - } - } + for v := 0; v < V; v++ { + if sptSet[v] == false && dist[v] <= min { + min = dist[v] + minIndex = v + } + } - return minIndex + return minIndex } -// A utility function to print the constructed distance -// array func printSolution(dist []int) { - fmt.Println("Vertex \t Distance from Source") - for i := 0; i < V; i++ { - fmt.Printf("%d \t\t\t\t %d\n", i, dist[i]) - } + fmt.Println("Vertex \t Distance from Source") + for i := 0; i < V; i++ { + fmt.Printf("%d \t\t\t\t %d\n", i, dist[i]) + } } -// Function that implements Dijkstra's single source -// shortest path algorithm for a graph represented using -// adjacency matrix representation func dijkstra(graph [V][V]int, src int) { - dist := make([]int, V) - sptSet := make([]bool, V) + dist := make([]int, V) + sptSet := make([]bool, V) - // Initialize all distances as INFINITE and stpSet[] as false - for i := 0; i < V; i++ { - dist[i] = math.MaxInt32 - sptSet[i] = false - } + for i := 0; i < V; i++ { + dist[i] = math.MaxInt32 + sptSet[i] = false + } - // Distance of source vertex from itself is always 0 - dist[src] = 0 + dist[src] = 0 - // Find shortest path for all vertices - for count := 0; count < V-1; count++ { - // Pick the minimum distance vertex from the set of - // vertices not yet processed. u is always equal to - // src in the first iteration. - u := minDistance(dist, sptSet) + for count := 0; count < V-1; count++ { + u := minDistance(dist, sptSet) - // Mark the picked vertex as processed - sptSet[u] = true + sptSet[u] = true - // Update dist value of the adjacent vertices of the - // picked vertex. - for v := 0; v < V; v++ { - // Update dist[v] only if is not in sptSet, - // there is an edge from u to v, and total - // weight of path from src to v through u is - // smaller than current value of dist[v] - if !sptSet[v] && graph[u][v] != 0 && dist[u] != math.MaxInt32 && dist[u]+graph[u][v] < dist[v] { - dist[v] = dist[u] + graph[u][v] - } - } - } + for v := 0; v < V; v++ { + if !sptSet[v] && graph[u][v] != 0 && dist[u] != math.MaxInt32 && dist[u]+graph[u][v] < dist[v] { + dist[v] = dist[u] + graph[u][v] + } + } + } - // print the constructed distance array - printSolution(dist) + printSolution(dist) } - func main() { - // Create a sample adjacency matrix graph - graph := [V][V]int{ - {0, 4, 0, 0, 0, 0, 0, 8, 0}, - {4, 0, 8, 0, 0, 0, 0, 11, 0}, - {0, 8, 0, 7, 0, 4, 0, 0, 2}, - {0, 0, 7, 0, 9, 14, 0, 0, 0}, - {0, 0, 0, 9, 0, 10, 0, 0, 0}, - {0, 0, 4, 14, 10, 0, 2, 0, 0}, - {0, 0, 0, 0, 0, 2, 0, 1, 6}, - {8, 11, 0, 0, 0, 0, 1, 0, 7}, - {0, 0, 2, 0, 0, 0, 6, 7, 0}, - } + graph := [V][V]int{ + {0, 4, 0, 0, 0, 0, 0, 8, 0}, + {4, 0, 8, 0, 0, 0, 0, 11, 0}, + {0, 8, 0, 7, 0, 4, 0, 0, 2}, + {0, 0, 7, 0, 9, 14, 0, 0, 0}, + {0, 0, 0, 9, 0, 10, 0, 0, 0}, + {0, 0, 4, 14, 10, 0, 2, 0, 0}, + {0, 0, 0, 0, 0, 2, 0, 1, 6}, + {8, 11, 0, 0, 0, 0, 1, 0, 7}, + {0, 0, 2, 0, 0, 0, 6, 7, 0}, + } - // Set the source vertex for the algorithm - src := 0 - - // Call the Dijkstra's algorithm function with the graph and source vertex - dijkstra(graph, src) -}s in the graph -#define V 9 - -// A utility function to find the vertex with minimum -// distance value, from the set of vertices not yet included -// in shortest path tree -int minDistance(int dist[], bool sptSet[]) { - - // Initialize min value - int min = INT_MAX, min_index; - - for (int v = 0; v < V; v++) - if (sptSet[v] == false && dist[v] <= min) - min = dist[v], min_index = v; - - return min_index; + src := 0 + dijkstra(graph, src) } - -// A utility function to print the constructed distance -// array -void printSolution(int dist[]) { - cout << "Vertex \t Distance from Source" << endl; - for (int i = 0; i < V; i++) - cout << i << " \t\t\t\t" << dist[i] << endl; -} - -// Function that implements Dijkstra's single source -// shortest path algorithm for a graph represented using -// adjacency matrix representation -void dijkstra(int graph[V][V], int src) { - int dist[V]; // The output array. dist[i] will hold the - // shortest - // distance from src to i - - bool sptSet[V]; // sptSet[i] will be true if vertex i is - // included in shortest - // path tree or shortest distance from src to i is - // finalized - - // Initialize all distances as INFINITE and stpSet[] as - // false - for (int i = 0; i < V; i++) - dist[i] = INT_MAX, sptSet[i] = false; - - // Distance of source vertex from itself is always 0 - dist[src] = 0; - - // Find shortest path for all vertices - for (int count = 0; count < V - 1; count++) { - // Pick the minimum distance vertex from the set of - // vertices not yet processed. u is always equal to - // src in the first iteration. - int u = minDistance(dist, sptSet); - - // Mark the picked vertex as processed - sptSet[u] = true; - - // Update dist value of the adjacent vertices of the - // picked vertex. - for (int v = 0; v < V; v++) - - // Update dist[v] only if is not in sptSet, - // there is an edge from u to v, and total - // weight of path from src to v through u is - // smaller than current value of dist[v] - if (!sptSet[v] && graph[u][v] - && dist[u] != INT_MAX - && dist[u] + graph[u][v] < dist[v]) - dist[v] = dist[u] + graph[u][v]; - } - - // print the constructed distance array - printSolution(dist); -} - -// driver's code -int main() { - - /* Let us create the example graph discussed above */ - int graph[V][V] = { { 0, 4, 0, 0, 0, 0, 0, 8, 0 }, - { 4, 0, 8, 0, 0, 0, 0, 11, 0 }, - { 0, 8, 0, 7, 0, 4, 0, 0, 2 }, - { 0, 0, 7, 0, 9, 14, 0, 0, 0 }, - { 0, 0, 0, 9, 0, 10, 0, 0, 0 }, - { 0, 0, 4, 14, 10, 0, 2, 0, 0 }, - { 0, 0, 0, 0, 0, 2, 0, 1, 6 }, - { 8, 11, 0, 0, 0, 0, 1, 0, 7 }, - { 0, 0, 2, 0, 0, 0, 6, 7, 0 } - }; - - // Function call - dijkstra(graph, 0); - - return 0; - -} \ No newline at end of file diff --git a/impl/algorithms/Greedy Algorithms/prim.go b/impl/algorithms/Greedy Algorithms/prim.go index 1baa3cc5..8d865d43 100644 --- a/impl/algorithms/Greedy Algorithms/prim.go +++ b/impl/algorithms/Greedy Algorithms/prim.go @@ -69,4 +69,4 @@ func main() { } PrimMST(graph) -} \ No newline at end of file +} diff --git a/impl/files-manipulation/14-QuickWriteToFile.go b/impl/files-manipulation/14-QuickWriteToFile.go index 764b12fb..16a5377c 100644 --- a/impl/files-manipulation/14-QuickWriteToFile.go +++ b/impl/files-manipulation/14-QuickWriteToFile.go @@ -1,7 +1,7 @@ package main import ( - "io/ioutil" + "os" "log" ) @@ -11,7 +11,7 @@ func main() { Hızlıca Dosya Yazma (Quick Write to File) */ - err := ioutil.WriteFile("demo.txt", []byte("Hi!\n"), 0666) + err := os.WriteFile("demo.txt", []byte("Hi!\n"), 0666) if err != nil { log.Fatal(err) } diff --git a/impl/files-manipulation/19-ReadAllBytesFromFile.go b/impl/files-manipulation/19-ReadAllBytesFromFile.go index 5b00b012..0f4ee957 100644 --- a/impl/files-manipulation/19-ReadAllBytesFromFile.go +++ b/impl/files-manipulation/19-ReadAllBytesFromFile.go @@ -2,7 +2,7 @@ package main import ( "fmt" - "io/ioutil" + "io" "log" "os" ) @@ -20,7 +20,7 @@ func main() { } // os.File.Read(), io.ReadFull(), ioutil.ReadAll() ve io.ReadAtLeast() da kullanılabilir - data, err := ioutil.ReadAll(file) + data, err := io.ReadAll(file) if err != nil { log.Fatal(err) } diff --git a/impl/files-manipulation/20-QuickReadWholeFile.go b/impl/files-manipulation/20-QuickReadWholeFile.go index a25d7c1e..5b9d525e 100644 --- a/impl/files-manipulation/20-QuickReadWholeFile.go +++ b/impl/files-manipulation/20-QuickReadWholeFile.go @@ -1,7 +1,7 @@ package main import ( - "io/ioutil" + "os" "log" ) @@ -12,7 +12,7 @@ func main() { */ // Dosyadan byte dilimine okuma yapma - data, err := ioutil.ReadFile("demo.txt") + data, err := os.ReadFile("demo.txt") if err != nil { log.Fatal(err) } diff --git a/impl/files-manipulation/28-DownloadFileOverHTTP.go b/impl/files-manipulation/27-DownloadFileOverHTTP.go similarity index 100% rename from impl/files-manipulation/28-DownloadFileOverHTTP.go rename to impl/files-manipulation/27-DownloadFileOverHTTP.go diff --git a/impl/files-manipulation/27-TempFilesAndDirs.go b/impl/files-manipulation/27-TempFilesAndDirs.go deleted file mode 100644 index 0d48340f..00000000 --- a/impl/files-manipulation/27-TempFilesAndDirs.go +++ /dev/null @@ -1,47 +0,0 @@ -package main - -import ( - "fmt" - "io/ioutil" - "log" - "os" -) - -func main() { - - /* - Temporary Files and Directories - */ - - // Create a temp dir in the system default temp folder - tempDirPath, err := ioutil.TempDir("", "myTempDir") - if err != nil { - log.Fatal(err) - } - fmt.Println("Temp dir created:", tempDirPath) - - // Create a file in new temp directory - tempFile, err := ioutil.TempFile(tempDirPath, "myTempFile.txt") - if err != nil { - log.Fatal(err) - } - fmt.Println("Temp file created:", tempFile.Name()) - - // ... do something with temp file/dir ... - - // Close file - err = tempFile.Close() - if err != nil { - log.Fatal(err) - } - - // Delete the resources we created - err = os.Remove(tempFile.Name()) - if err != nil { - log.Fatal(err) - } - err = os.Remove(tempDirPath) - if err != nil { - log.Fatal(err) - } -} diff --git a/impl/files-manipulation/29-HashAndChecksum.go b/impl/files-manipulation/28-HashAndChecksum.go similarity index 88% rename from impl/files-manipulation/29-HashAndChecksum.go rename to impl/files-manipulation/28-HashAndChecksum.go index 0b5fbb55..78ab2bbb 100644 --- a/impl/files-manipulation/29-HashAndChecksum.go +++ b/impl/files-manipulation/28-HashAndChecksum.go @@ -6,7 +6,7 @@ import ( "crypto/sha256" "crypto/sha512" "fmt" - "io/ioutil" + "os" "log" ) @@ -17,7 +17,7 @@ func main() { */ // Get bytes from file - data, err := ioutil.ReadFile("test.txt") + data, err := os.ReadFile("test.txt") if err != nil { log.Fatal(err) }