variables fixed by Stephen Castle

package main

import (

//Statement is a representation of a string asignment
type Statement struct {
\x09Variable string
\x09Number   int
\x09IsQuery  bool
\x09Reverse  bool

func main() {
\x09statements := scanner()
\x09state := map[string]int{}
\x09for _, statement := range statements {
\x09\x09if statement.IsQuery {
\x09\x09\x09if statement.Reverse {
\x09\x09\x09\x09fmt.Printf("%d=%s\n", state[statement.Variable], statement.Variable)
\x09\x09\x09} else {
\x09\x09\x09\x09fmt.Printf("%s=%d\n", statement.Variable, state[statement.Variable])
\x09\x09} else {
\x09\x09\x09state[statement.Variable] = statement.Number

func scanner() []Statement {
\x09scanner := bufio.NewScanner(os.Stdin)
\x09statements := []Statement{}
\x09for scanner.Scan() {
\x09\x09statements = append(statements, parseStatement(scanner.Text()))
\x09return statements

func parseStatement(s string) Statement {
\x09re := regexp.MustCompile("[0-9]+")
\x09sre := regexp.MustCompile("[A-z]")
\x09qre := regexp.MustCompile("[?]")
\x09num := re.FindString(s)
\x09questionMarkIndex := qre.FindStringIndex(s)
\x09variableName := sre.FindString(s)
\x09variableIndex := sre.FindStringIndex(s)
\x09if num == "" {
\x09\x09return Statement{
\x09\x09\x09Variable: variableName,
\x09\x09\x09IsQuery:  true,
\x09\x09\x09Reverse:  variableIndex[0] > questionMarkIndex[0],
\x09n, err := strconv.Atoi(num)
\x09if err != nil {
\x09statement := Statement{
\x09\x09Variable: variableName,
\x09\x09Number:   n,
\x09return statement

Note that non-ascii characters in the above source code will be escaped (such as \x9f).

To protect the system from spam, please input your favorite sport (hint: I believe its name must start with 'g', case insensitive)


return to the top page