Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
0882db5
Add solution for Challenge 1 by Ali-Fartoot
Ali-Fartoot Sep 12, 2025
0caaf6c
Add solution for Challenge 2 by Ali-Fartoot
Ali-Fartoot Sep 12, 2025
fd16b48
Add solution for Challenge 3 by Ali-Fartoot
Ali-Fartoot Sep 13, 2025
400ba13
Merge branch 'main' into main
RezaSi Sep 16, 2025
a6418bc
Add solution for Challenge 4 by Ali-Fartoot
Ali-Fartoot Sep 16, 2025
b1f7c0b
Add solution for Challenge 5 by Ali-Fartoot
Ali-Fartoot Sep 16, 2025
0c26a31
Merge branch 'main' of github.com:Ali-Fartoot/go-interview-practice
Ali-Fartoot Sep 16, 2025
970237b
Add solution for Challenge 6 by Ali-Fartoot
Ali-Fartoot Sep 16, 2025
e0d5cd1
Add solution for Challenge 7 by Ali-Fartoot
Ali-Fartoot Sep 17, 2025
44590e0
Merge branch 'main' into main
Ali-Fartoot Sep 18, 2025
8386655
Add solution for Challenge 4 by Ali-Fartoot
Ali-Fartoot Sep 18, 2025
e86aaac
Add solution for Challenge 5 by Ali-Fartoot
Ali-Fartoot Sep 18, 2025
074acc7
Add solution for Challenge 6 by Ali-Fartoot
Ali-Fartoot Sep 18, 2025
c121972
Add solution for Challenge 7 by Ali-Fartoot
Ali-Fartoot Sep 18, 2025
8433705
Add solution for Challenge 8 by Ali-Fartoot
Ali-Fartoot Sep 18, 2025
719af7f
Merge branch 'RezaSi:main' into main
Ali-Fartoot Sep 22, 2025
451da23
Merge branch 'main' of github.com:Ali-Fartoot/go-interview-practice
Ali-Fartoot Sep 24, 2025
5174961
Add solution for Challenge 18 by Ali-Fartoot
Ali-Fartoot Sep 26, 2025
521369b
Add solution for Challenge 21 by Ali-Fartoot
Ali-Fartoot Sep 26, 2025
37902de
Add solution for Challenge 22 by Ali-Fartoot
Ali-Fartoot Sep 26, 2025
1b5cd80
Add solution for Challenge 30 by Ali-Fartoot
Ali-Fartoot Sep 28, 2025
f671fae
Add solution for Challenge 16 by Ali-Fartoot
Ali-Fartoot Sep 28, 2025
f5129bd
Merge branch 'RezaSi:main' into main
Ali-Fartoot Sep 29, 2025
e23ff56
Add solution for Challenge 13 by Ali-Fartoot
Ali-Fartoot Sep 29, 2025
f008d72
Add solution for Challenge 10 by Ali-Fartoot
Ali-Fartoot Sep 29, 2025
6204b4a
Add solution for Challenge 17 by Ali-Fartoot
Ali-Fartoot Sep 29, 2025
e87f7e6
Add solution for Challenge 19 by Ali-Fartoot
Ali-Fartoot Sep 29, 2025
81fa084
Add solution for Challenge 23 by Ali-Fartoot
Ali-Fartoot Sep 29, 2025
7873118
Merge branch 'main' of github.com:Ali-Fartoot/go-interview-practice
Ali-Fartoot Sep 30, 2025
f6b5c74
Add solution for Challenge 27 by Ali-Fartoot
Ali-Fartoot Sep 30, 2025
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
184 changes: 184 additions & 0 deletions challenge-10/submissions/Ali-Fartoot/solution-template.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
// Package challenge10 contains the solution for Challenge 10.
package challenge10

import (
"fmt"
"math"
// Add any necessary imports here
)

// Shape interface defines methods that all shapes must implement
type Shape interface {
Area() float64
Perimeter() float64
fmt.Stringer // Includes String() string method
}

// Rectangle represents a four-sided shape with perpendicular sides
type Rectangle struct {
Width float64
Height float64
}

// NewRectangle creates a new Rectangle with validation
func NewRectangle(width, height float64) (*Rectangle, error) {
if width <= 0 || height <= 0 {
return nil, fmt.Errorf("zero input")
}
rectangle := Rectangle{
Width: width,
Height: height,
}
return &rectangle, nil
}

// Area calculates the area of the rectangle
func (r *Rectangle) Area() float64 {
return r.Width * r.Height
}

// Perimeter calculates the perimeter of the rectangle
func (r *Rectangle) Perimeter() float64 {
return 2 * (r.Height + r.Width)
}

// String returns a string representation of the rectangle
func (r *Rectangle) String() string {
return fmt.Sprintf("rectangle with width %.2f and height %.2f", r.Width, r.Height)

}

// Circle represents a perfectly round shape
type Circle struct {
Radius float64
}

// NewCircle creates a new Circle with validation
func NewCircle(radius float64) (*Circle, error) {
if radius <=0 {
return nil, fmt.Errorf("zero input")
}
circle := Circle {
Radius: radius,
}

return &circle, nil
}

// Area calculates the area of the circle
func (c *Circle) Area() float64 {
return c.Radius * c.Radius * math.Pi
}

// Perimeter calculates the circumference of the circle
func (c *Circle) Perimeter() float64 {
return c.Radius * 2 * math.Pi
}

// String returns a string representation of the circle
func (c *Circle) String() string {
// TODO: Implement string representation
return fmt.Sprintf("circle with radius %.2f", c.Radius)

}

// Triangle represents a three-sided polygon
type Triangle struct {
SideA float64
SideB float64
SideC float64
}

// NewTriangle creates a new Triangle with validation
func NewTriangle(a, b, c float64) (*Triangle, error) {

if a >= b + c || b >= a + c || c >= a + b {
return nil, fmt.Errorf("inequality theorem")
}
if a <= 0 || b <= 0 || c<=0 {
return nil, fmt.Errorf("zero input")
}
triangle := Triangle {
SideA: a,
SideB: b,
SideC: c,
}
return &triangle, nil
}

// Area calculates the area of the triangle using Heron's formula
func (t *Triangle) Area() float64 {
s := t.Perimeter() / 2
return math.Sqrt(s * (s - t.SideA) * (s - t.SideB) * (s - t.SideC))
}

// Perimeter calculates the perimeter of the triangle
func (t *Triangle) Perimeter() float64 {
return t.SideA + t.SideB + t.SideC
}

// String returns a string representation of the triangle
func (t *Triangle) String() string {
return fmt.Sprintf("triangle with sides %.2f, %.2f, and %.2f", t.SideA, t.SideB, t.SideC)

}

// ShapeCalculator provides utility functions for shapes
type ShapeCalculator struct{}

// NewShapeCalculator creates a new ShapeCalculator
func NewShapeCalculator() *ShapeCalculator {
shapeCalculator := ShapeCalculator{}
return &shapeCalculator
}

// PrintProperties prints the properties of a shape
func (sc *ShapeCalculator) PrintProperties(s Shape) {
s.String()
}

// TotalArea calculates the sum of areas of all shapes
func (sc *ShapeCalculator) TotalArea(shapes []Shape) float64 {
totalArea := 0.0
for _, shape := range shapes {
totalArea += shape.Area()
}
return totalArea
}

// LargestShape finds the shape with the largest area
func (sc *ShapeCalculator) LargestShape(shapes []Shape) Shape {
latgestShape := 0.0
var selectedShape Shape

for _, shape := range shapes {
if latgestShape < shape.Area(){
latgestShape = shape.Area()
selectedShape = shape
}
}
return selectedShape
}

func (sc *ShapeCalculator) SortByArea(shapes []Shape, ascending bool) []Shape {
sortedShapes := make([]Shape, len(shapes))
copy(sortedShapes, shapes)

n := len(sortedShapes)
for i := 0; i < n-1; i++ {
for j := 0; j < n-i-1; j++ {
shouldSwap := false
if ascending {
shouldSwap = sortedShapes[j].Area() > sortedShapes[j+1].Area()
} else {
shouldSwap = sortedShapes[j].Area() < sortedShapes[j+1].Area()
}

if shouldSwap {
sortedShapes[j], sortedShapes[j+1] = sortedShapes[j+1], sortedShapes[j]
}
}
}

return sortedShapes
}
Loading
Loading