Advertisement
Guest User

[habitica] Programming challenge July 2019

a guest
Jul 18th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 1.17 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4.     "strconv"
  5.     "strings"
  6.     "os"
  7.     "fmt"
  8.     "bufio"
  9. )
  10.  
  11. func main() {
  12.     reader := bufio.NewReader(os.Stdin)
  13.     fmt.Print("Input your problem: ")
  14.     input, _ := reader.ReadString('\n')
  15.     problem := strings.TrimSuffix(strings.TrimSuffix(string(input), "?\n"), "\n")
  16.     problemSlice := strings.Split(problem, " ")
  17.  
  18.     var result *int
  19.  
  20.     for i, el := range problemSlice {
  21.         switch el {
  22.         case "plus":
  23.             if result == nil {
  24.                 a, _ := strconv.Atoi(problemSlice[i-1])
  25.                 result = &a
  26.             }
  27.             b, _ := strconv.Atoi(problemSlice[i+1])
  28.             x := *result + b
  29.             result = &x
  30.         case "minus":
  31.             if result == nil {
  32.                 a, _ := strconv.Atoi(problemSlice[i-1])
  33.                 result = &a
  34.             }
  35.             b, _ := strconv.Atoi(problemSlice[i+1])
  36.             x := *result - b
  37.             result = &x
  38.         case "multiplied":
  39.             if result == nil {
  40.                 a, _ := strconv.Atoi(problemSlice[i-1])
  41.                 result = &a
  42.             }
  43.             b, _ := strconv.Atoi(problemSlice[i+2])
  44.             x := *result * b
  45.             result = &x
  46.         case "divided":
  47.             if result == nil {
  48.                 a, _ := strconv.Atoi(problemSlice[i-1])
  49.                 result = &a
  50.             }
  51.             b, _ := strconv.Atoi(problemSlice[i+2])
  52.             x := *result / b
  53.             result = &x
  54.         }
  55.     }
  56.     fmt.Println(*result)
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement