Skip to content

Add solution for Challenge 17 #161

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

Merged
merged 9 commits into from
Jul 30, 2025
25 changes: 25 additions & 0 deletions challenge-1/submissions/ZaharBorisenko/solution-template.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package main

import (
"fmt"
)

func main() {
var a, b int
// Read two integers from standard input
_, err := fmt.Scanf("%d, %d", &a, &b)
if err != nil {
fmt.Println("Error reading input:", err)
return
}

// Call the Sum function and print the result
result := Sum(a, b)
fmt.Println(result)
}

// Sum returns the sum of a and b.
func Sum(a int, b int) int {
// TODO: Implement the function
return a + b
}
47 changes: 47 additions & 0 deletions challenge-17/submissions/ZaharBorisenko/solution-template.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package main

import (
"fmt"
"regexp"
"strings"
)

func main() {
// Get input from the user
var input string
fmt.Print("Enter a string to check if it's a palindrome: ")
fmt.Scanln(&input)

// Call the IsPalindrome function and print the result
result := IsPalindrome(input)
if result {
fmt.Println("The string is a palindrome.")
} else {
fmt.Println("The string is not a palindrome.")
}
}

// IsPalindrome checks if a string is a palindrome.
// A palindrome reads the same backward as forward, ignoring case, spaces, and punctuation.
func IsPalindrome(s string) bool {
if s == "" {
return true
}

reg := regexp.MustCompile(`[^a-zA-Z0-9]`)
s = reg.ReplaceAllString(strings.ToLower(s), "")

left := 0
right := len(s) - 1

for left <= right {
if s[left] == s[right] {
left++
right--
} else {
return false
}
}

return true
}
36 changes: 36 additions & 0 deletions challenge-18/submissions/ZaharBorisenko/solution-template.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package main

import (
"fmt"
"math"
)

func main() {
celsius := 25.0
fahrenheit := CelsiusToFahrenheit(celsius)
fmt.Printf("%.2f°C is equal to %.2f°F\n", celsius, fahrenheit)

fahrenheit = 68.0
celsius = FahrenheitToCelsius(fahrenheit)
fmt.Printf("%.2f°F is equal to %.2f°C\n", fahrenheit, celsius)
}

// CelsiusToFahrenheit converts a temperature from Celsius to Fahrenheit
// Formula: F = C × 9/5 + 32
func CelsiusToFahrenheit(celsius float64) float64 {
fahrenheit := (celsius * 9 / 5) + 32
return Round(fahrenheit, 2)
}

// FahrenheitToCelsius converts a temperature from Fahrenheit to Celsius
// Formula: C = (F - 32) × 5/9
func FahrenheitToCelsius(fahrenheit float64) float64 {
celsius := (fahrenheit - 32) * 5 / 9
return Round(celsius, 2)
}

// Round rounds a float64 value to the specified number of decimal places
func Round(value float64, decimals int) float64 {
precision := math.Pow10(decimals)
return math.Round(value*precision) / precision
}
96 changes: 96 additions & 0 deletions challenge-19/submissions/ZaharBorisenko/solution-template.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package main

import (
"fmt"
)

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) == 0 {
return 0
}

maxEl := numbers[0]
for _, num := range numbers[1:] {
if num > maxEl {
maxEl = num
}
}

return maxEl

}

// RemoveDuplicates returns a new slice with duplicate values removed,
// preserving the original order of elements.
func RemoveDuplicates(numbers []int) []int {
if len(numbers) == 0 {
return []int{}
}

m := make(map[int]bool, len(numbers))
uniqueNumbers := []int{}

for _, number := range numbers {
if _, val := m[number]; !val {
m[number] = true
uniqueNumbers = append(uniqueNumbers, number)
}
}

return uniqueNumbers
}

// ReverseSlice returns a new slice with elements in reverse order.
func ReverseSlice(slice []int) []int {
if len(slice) == 0 {
return []int{}
}
reverseSlice := []int{}

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

return reverseSlice
}

// FilterEven returns a new slice containing only the even numbers
// from the original slice.
func FilterEven(numbers []int) []int {
if len(numbers) == 0 {
return []int{}
}

evenNumbers := []int{}

for _, number := range numbers {
if number%2 == 0 {
evenNumbers = append(evenNumbers, number)
}
}

return evenNumbers
}
36 changes: 36 additions & 0 deletions challenge-2/submissions/ZaharBorisenko/solution-template.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package main

import (
"bufio"
"fmt"
"os"
)

func main() {
// Read input from standard input
scanner := bufio.NewScanner(os.Stdin)
if scanner.Scan() {
input := scanner.Text()

// Call the ReverseString function
output := ReverseString(input)

// Print the result
fmt.Println(output)
}
}

// ReverseString returns the reversed string of s.
func ReverseString(s string) string {
if s == "" {
return s
}
result := ""
data := []byte(s)

for i := len(data) - 1; i >= 0; i-- {
result += string(data[i])
}
return result
}

92 changes: 92 additions & 0 deletions challenge-21/submissions/ZaharBorisenko/solution-template.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package main

import (
"fmt"
)

func main() {
// Example sorted array for testing
arr := []int{1, 3, 5, 7, 9, 11, 13, 15, 17, 19}

// Test binary search
target := 7
index := BinarySearch(arr, target)
fmt.Printf("BinarySearch: %d found at index %d\n", target, index)

// Test recursive binary search
recursiveIndex := BinarySearchRecursive(arr, target, 0, len(arr)-1)
fmt.Printf("BinarySearchRecursive: %d found at index %d\n", target, recursiveIndex)

// Test find insert position
insertTarget := 8
insertPos := FindInsertPosition(arr, insertTarget)
fmt.Printf("FindInsertPosition: %d should be inserted at index %d\n", insertTarget, insertPos)
}

// BinarySearch performs a standard binary search to find the target in the sorted array.
// Returns the index of the target if found, or -1 if not found.
func BinarySearch(arr []int, target int) int {
left := 0
right := len(arr) - 1

for left <= right {
mid := (left + right) / 2

if arr[mid] == target {
return mid
}

if arr[mid] < target {
left = mid + 1
} else if arr[mid] > target {
right = mid - 1
}

}

return -1
}

// BinarySearchRecursive performs binary search using recursion.
// Returns the index of the target if found, or -1 if not found.
func BinarySearchRecursive(arr []int, target int, left int, right int) int {
if left > right {
return -1
}
mid := left + (right-left)/2

if arr[mid] == target {
return mid
}

if arr[mid] < target {
return BinarySearchRecursive(arr, target, mid+1, right)
} else {
return BinarySearchRecursive(arr, target, left, mid-1)
}
}

// FindInsertPosition returns the index where the target should be inserted
// to maintain the sorted order of the array.
func FindInsertPosition(arr []int, target int) int {
left := 0
right := len(arr) - 1

for left <= right {
mid := left + (right-left)/2

if arr[mid] == target {
return mid
}

if arr[mid] > target {
right = mid - 1
} else {
left = mid + 1
}

}

return left
}

Loading
Loading