E
E
Eugene2020-05-16 02:10:01
go
Eugene, 2020-05-16 02:10:01

How to complete a task in Yandex.Contest?

When solving a training task, I encountered a Presentation error.

The testing system cannot check the output data, because their format does not match the one described in the task conditions.

Although, on the local machine, there are no problems with the output.

The task itself:
Given two numbers A and B. You need to calculate their sum A+B. In this task, you can use both files and streams to work with input and output data at your discretion.

Input: standard input or input.txt
Output: standard output or output.txt

package main

import (
  "fmt"
  "io"
  "os"
  "strconv"
  "strings"
)

func main() {
  input, err := os.Open("input.txt")
  if err != nil {
    fmt.Println(err)
    return
  }
  defer input.Close()

  file, err := os.Create("output.txt")
  if err != nil {
    fmt.Println(err)
    return
  }
  defer file.Close()

  output, err := os.OpenFile("output.txt", os.O_APPEND|os.O_WRONLY, 0777)
  if err != nil {
    fmt.Println(err)
    return
  }
  defer output.Close()

  data := make([]byte, 64)

  for {
    n, err := input.Read(data)
    if err == io.EOF {
      break
    }

    arr := strings.Split(string(data[:n]), " ")

    a, err := strconv.Atoi(arr[0])
    if err != nil {
      fmt.Println(err)
      return
    }

    b, err := strconv.Atoi(arr[1])
    if err != nil {
      fmt.Println(err)
      return
    }

    if _, err = output.WriteString(strconv.Itoa(a + b)); err != nil {
      fmt.Println(err)
      return
    }

    _, err = os.Open("output.txt")
    if err != nil {
      fmt.Println(err)
      return
    }
  }
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander Pavlyuk, 2020-05-16
@you_are_enot

Your code will break when it sees an input file that ends with a line break.
When it breaks, it will write an error to STDOUT, which will most likely be taken by the verification system as a result output. The result, of course, will not be in that format.

M
msl97, 2020-05-18
@msl97

I racked my brains over this problem, in the end it turned out that I needed to remove newline char at the end of the data variable . That is, replace witharr := strings.Split(string(data[:n]), " ")

arr := strings.Split(strings.TrimSuffix(string(data[:n]), "\n"), " ")

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question