TutorialEdge

Challenge 05 - Implementing a Stack

Welcome, everyone to the discussion thread for the 5th Go challenge posted to the site :tada:

Feel free to show us how you solved the challenge by posting your solutions below!

Solution:

package main

import (
	"errors"
	"fmt"
)

type Stack struct {
	Items []Flight
}

type Flight struct {
	Origin      string
	Destination string
	Price       int
}

func (s *Stack) Pop() (Flight, error) {
	if s.IsEmpty() {
		return Flight{}, errors.New("Stack is Empty")
	} else {
		lastElemIndex := len(s.Items) - 1
		flight := s.Items[lastElemIndex]
		s.Items = s.Items[:lastElemIndex]
		return flight, nil
	}
}

func (s *Stack) Push(flight Flight) {
	s.Items = append(s.Items, flight)
}

func (s *Stack) Peek() (Flight, error) {
	if s.IsEmpty() {
		return Flight{}, errors.New("Stack is Empty")
	} else {
		lastElemIndex := len(s.Items) - 1
		flight := s.Items[lastElemIndex]
		return flight, nil
	}
}

func (s *Stack) IsEmpty() bool {
	if len(s.Items) == 0 {
		return true
	}
	return false
}

func main() {
	fmt.Println("Go Stack Implementation")
}

else is a smell; you can remove all of those else branches and code readability is greatly improved. also len(s.Items) == 0 is an expression that you can return directly without if/else around it. can you show me the tests you’re using to validate this code? Checking my work on my end it looks correct, but doesn’t pass the test. thanks!

Welcome to the community!

Very true, I wonder if I should lint the code submitted by the challenges to ensure everyone is following best practices?

The source code for the tests can be found here: https://raw.githubusercontent.com/TutorialEdge/tutorialedge.net/main/content/challenges/go/implementing-a-stack.md

Linting is probably a good idea, maybe make it a button or checkbox? Thanks!

1 Like

Thanks, Elliot for sharing the knowledge! I am new to Golang and practicing problems from Tutorialedge. Here is my solution to the above problem.

package main

import “fmt”

type Flight struct {
origin string
destination string
price int
}

type Stack struct{
Items []Flight
}

func isEmpty(s *Stack) bool {
return len(s.Items)==0
}

func Push(item Flight,s *Stack) {
s.Items = append(s.Items, item)
}

func Pop(s *Stack) Flight {
if isEmpty(s) {
return Flight{}
}
lastIndex := len(s.Items)-1
top := s.Items[lastIndex]
s.Items = s.Items[0:lastIndex]
return top
}

func Peek(s *Stack) Flight {
if isEmpty(s) {
return Flight{}
}
lastIndex := len(s.Items)-1
return s.Items[lastIndex]
}

func main() {
fmt.Println(“Implementing stack”)
flight1 := Flight{origin: “India”, destination: “USA”, price: 120,}
flight2 := Flight{origin: “India”, destination: “UK”, price: 50,}

stack := Stack{}
fmt.Println("Pop item from stack", Pop(&stack))
fmt.Printf("is Stack Empty? = %t\n\n", isEmpty(&stack))
fmt.Printf("Push items in stack\n")

Push(flight1, &stack)
Push(flight2, &stack)
fmt.Printf("Stack items=%v\n\n",stack.Items)

fmt.Println("Pop item from stack", Pop(&stack))
fmt.Printf("Stack items=%v\n\n",stack.Items)

fmt.Println("Get top value", Peek(&stack))

}