Back to course home
0% completed
Vote For New Content
Issues using Golang standard libraries
Kurt Medley
Aug 16, 2024
I'm not sure if I'm missing something but I'm not able to use Golang's standard libraries (fmt, strconv, etc.) in the inline problem editor which makes doing things like converting int to string difficult. Additionally, I can't print to stdout, etc. I do see fmt being used in the solution. Basically, this forced me to do some weird stuff like converting strings to bytes to avoid costly string concatenation.
```go type Solution struct{} // type Stack struct { // items []interface{} // } // func NewStack() Stack { // return Stack{} // } // func (s *Stack) Empty() bool { // return len(s.items) == 0 // } // func (s *Stack) Push(item interface{}) { // s.items = append(s.items, item) // } // func (s *Stack) Pop() (interface{}, error) { // if s.Empty() { // return nil, errors.New("stack is empty") // } // item := s.items[len(s.items)-1] // s.items = s.items[:len(s.items)-1] // return item, nil // } // func (s *Stack) Top() (interface{}, error) { // if s.Empty() { // return nil, errors.New("stack is empty") // } // return s.items[len(s.items)-1], nil // } func (s *Stack) Size() int { return len(s.items) } func (s *Solution) decimalToBinary(num int) string { if num == 0 { return "0" } stack := NewStack() for num > 0 { remainder := "0" if num%2 == 1 { remainder = "1" } stack.Push([]byte(remainder)[0]) num /= 2 } var binary []byte = make([]byte, stack.Size()) for i, n := 0, stack.Size(); i < n; i++ { val, _ := stack.Pop() binary[i] = val.(byte) } return string(binary) }
0
0
Comments
Comments
Shubham Voraa year ago
Please, check the below Solution. package main import ( "fmt" ) // Solution struct represents the solution for converting decimal to binary. type Solution struct{} // decimalToBinary converts a decimal number to its binary representation. func (s *So...
On this page