-
-
Notifications
You must be signed in to change notification settings - Fork 984
Add solution for Challenge 10 by shansing #1436
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
Open
shansing
wants to merge
4
commits into
RezaSi:main
Choose a base branch
from
shansing:challenge-10-shansing
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
170141e
Add solution for Challenge 10
go-interview-practice-bot[bot] 06608bd
Add solution for Challenge 10
go-interview-practice-bot[bot] 7cf1e06
Update challenge-10/submissions/shansing/solution-template.go
shansing c8503a8
Fix SortByArea to use correct slice copy and comparison
shansing File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,161 @@ | ||
| // Package challenge10 contains the solution for Challenge 10. | ||
| package challenge10 | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "math" | ||
| "sort" | ||
| // 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("width and height must be positive") | ||
| } | ||
| return &Rectangle{width, height}, nil | ||
| } | ||
|
|
||
| // Area calculates the area of the rectangle | ||
| func (r *Rectangle) Area() float64 { | ||
| return r.Height * r.Width | ||
| } | ||
|
|
||
| // Perimeter calculates the perimeter of the rectangle | ||
| func (r *Rectangle) Perimeter() float64 { | ||
| return (r.Height + r.Width) * 2 | ||
| } | ||
|
|
||
| // String returns a string representation of the rectangle | ||
| func (r *Rectangle) String() string { | ||
| return fmt.Sprintf("Rectangle(width=%v, height=%v)", 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("radius must be positive") | ||
| } | ||
| return &Circle{radius}, nil | ||
| } | ||
|
|
||
| // Area calculates the area of the circle | ||
| func (c *Circle) Area() float64 { | ||
| return math.Pi * math.Pow(c.Radius, 2) | ||
| } | ||
|
|
||
| // Perimeter calculates the circumference of the circle | ||
| func (c *Circle) Perimeter() float64 { | ||
| return 2 * math.Pi * c.Radius | ||
| } | ||
|
|
||
| // String returns a string representation of the circle | ||
| func (c *Circle) String() string { | ||
| return fmt.Sprintf("Circle(radius=%v)", 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 <= 0 || b <= 0 || c <= 0 { | ||
| return nil, fmt.Errorf("a, b, c must be positive") | ||
| } | ||
| if a+b <= c || a+c <= b || b+c <= a { | ||
| return nil, fmt.Errorf("not a valid triangle") | ||
| } | ||
| return &Triangle{a, b, c}, nil | ||
| } | ||
|
|
||
| // Area calculates the area of the triangle using Heron's formula | ||
| func (t *Triangle) Area() float64 { | ||
| return math.Sqrt( | ||
| ((t.SideA + t.SideB + t.SideC) / 2.0) * | ||
| (((t.SideA + t.SideB + t.SideC) / 2.0) - t.SideA) * | ||
| (((t.SideA + t.SideB + t.SideC) / 2.0) - t.SideB) * | ||
| (((t.SideA + t.SideB + t.SideC) / 2.0) - 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(sides=%v,%v,%v)", t.SideA, t.SideB, t.SideC) | ||
| } | ||
|
|
||
| // ShapeCalculator provides utility functions for shapes | ||
| type ShapeCalculator struct{} | ||
|
|
||
| // NewShapeCalculator creates a new ShapeCalculator | ||
| func NewShapeCalculator() *ShapeCalculator { | ||
| return &ShapeCalculator{} | ||
| } | ||
|
|
||
| // PrintProperties prints the properties of a shape | ||
| func (sc *ShapeCalculator) PrintProperties(s Shape) { | ||
| fmt.Println(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 { | ||
| if len(shapes) == 0 { | ||
| return nil | ||
| } | ||
| largestArea := 0.0 | ||
| var lastShape Shape | ||
| for _, shape := range shapes { | ||
| if shape.Area() > largestArea { | ||
| largestArea = shape.Area() | ||
| lastShape = shape | ||
| } | ||
| } | ||
| return lastShape | ||
| } | ||
|
|
||
| // SortByArea sorts shapes by area in ascending or descending order | ||
| func (sc *ShapeCalculator) SortByArea(shapes []Shape, ascending bool) []Shape { | ||
| result := make([]Shape, len(shapes)) | ||
| copy(result, shapes) | ||
| if ascending { | ||
| sort.Slice(result, func(i, j int) bool { return result[i].Area() < result[j].Area() }) | ||
| } else { | ||
| sort.Slice(result, func(i, j int) bool { return result[i].Area() > result[j].Area() }) | ||
| } | ||
| return result | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.