Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 80 additions & 0 deletions challenge-19/submissions/iamsurajmandal/solution-template.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package main

import (
"fmt"
"math"
)

func main() {
// Example slice for testing
numbers := []int{3, 1, 4, 1, 5, 9, 2, 6}

// Test FindMax
max := FindMax(numbers)
fmt.Printf("Maximum value: %d\n", max)

// Test RemoveDuplicates
unique := RemoveDuplicates(numbers)
fmt.Printf("After removing duplicates: %v\n", unique)

// Test ReverseSlice
reversed := ReverseSlice(numbers)
fmt.Printf("Reversed: %v\n", reversed)

// Test FilterEven
evenOnly := FilterEven(numbers)
fmt.Printf("Even numbers only: %v\n", evenOnly)
}

// FindMax returns the maximum value in a slice of integers.
// If the slice is empty, it returns 0.
func FindMax(numbers []int) int {
if (len(numbers) < 1) {
return 0
}
max := int(math.Inf(-1))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🔴 Critical

Fix: Avoid converting math.Inf to int.

Converting math.Inf(-1) (negative infinity) to int is problematic—the behavior is implementation-defined and can produce unexpected results. Initialize max with the first element after confirming the slice is non-empty, or use math.MinInt.

Apply this diff to use a clearer initialization:

 func FindMax(numbers []int) int {
-    if (len(numbers) < 1) {
+    if len(numbers) == 0 {
         return 0
     }
-	max := int(math.Inf(-1))
-    for i := 0; i < len(numbers); i++ {
-        if (numbers[i] > max) {
-            max = numbers[i]
-        }
-    }	
+	max := numbers[0]
+    for _, num := range numbers {
+        if num > max {
+            max = num
+        }
+    }
 	return max
 }

Committable suggestion skipped: line range outside the PR's diff.

🤖 Prompt for AI Agents
In challenge-19/submissions/iamsurajmandal/solution-template.go around line 35,
the code sets max := int(math.Inf(-1)) which converts negative infinity to int
(implementation-defined) — replace this by first checking the slice is non-empty
and initializing max from the first element (e.g., max := arr[0]) before
looping, or alternatively initialize max to math.MinInt (math.MinInt64 or
math.MinInt depending on types) to avoid converting from math.Inf.

for i := 0; i < len(numbers); i++ {
if (numbers[i] > max) {
max = numbers[i]
}
}
return max
}

// RemoveDuplicates returns a new slice with duplicate values removed,
// preserving the original order of elements.
func RemoveDuplicates(numbers []int) []int {
// TODO: Implement this function
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Remove TODO comment.

The function is fully implemented—remove the placeholder TODO comment.

-	// TODO: Implement this function
 	hs := map[int]bool{}
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
// TODO: Implement this function
hs := map[int]bool{}
🤖 Prompt for AI Agents
In challenge-19/submissions/iamsurajmandal/solution-template.go around line 47,
remove the leftover "// TODO: Implement this function" comment because the
function is already implemented; simply delete that comment line so the source
no longer contains a misleading TODO placeholder.

hs := map[int]bool{}
result := []int{}
for _, value := range numbers {
_, exists := hs[value]
if !exists {
hs[value] = true
result = append(result, value)
}
}
return result
}

// ReverseSlice returns a new slice with elements in reverse order.
func ReverseSlice(slice []int) []int {
result := []int{}
// TODO: Implement this function
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Remove TODO comment.

The function is fully implemented—remove the placeholder TODO comment.

-	// TODO: Implement this function
 	for i := len(slice) - 1; i >= 0 ; i-- {
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
// TODO: Implement this function
for i := len(slice) - 1; i >= 0 ; i-- {
🤖 Prompt for AI Agents
In challenge-19/submissions/iamsurajmandal/solution-template.go around line 63,
remove the leftover "// TODO: Implement this function" comment because the
function has been implemented; delete that single-line placeholder and ensure
the file is saved (run gofmt / go vet as needed) so no stray TODO remains.

for i := len(slice) - 1; i >= 0 ; i-- {
result = append(result, slice[i])
}
return result
}

// FilterEven returns a new slice containing only the even numbers
// from the original slice.
func FilterEven(numbers []int) []int {
result := []int{}
for _, value := range numbers{
if (value % 2 == 0 ) {
result = append(result, value)
}
}
return result
}
Loading