func ReadInt(s *bufio.Scanner) int { s.Split(bufio.ScanWords) s.Scan() result, _ := strconv.Atoi(s.Text()) return result }
func SolveMonk(scanner *bufio.Scanner) string { var buffer bytes.Buffer scanner.Split(bufio.ScanWords) scanner.Scan() trials, _ := strconv.Atoi(scanner.Text()) for i := 0; i < trials; i++ { scanner.Scan() nums, _ := strconv.Atoi(scanner.Text()) var tmp int min, freq := math.MaxInt32, 0 for j := 0; j < nums; j++ { scanner.Scan() tmp, _ = strconv.Atoi(scanner.Text()) if tmp == min { freq++ } else if tmp < min { min = tmp freq = 1 } } if freq%2 == 0 { buffer.WriteString("Unlucky\n") } else { buffer.WriteString("Lucky\n") } } return buffer.String() }
func ReadInts(s *bufio.Scanner, total int) []int64 { s.Split(bufio.ScanWords) a := make([]int64, total) for i := 0; i < total; i++ { s.Scan() a[i], _ = strconv.ParseInt(s.Text(), 10, 64) } return a }
func ReadInts(s *bufio.Scanner, total int) []int { s.Split(bufio.ScanWords) a := make([]int, total) for i := 0; i < total; i++ { s.Scan() a[i], _ = strconv.Atoi(s.Text()) } return a }
func parseBody(scanner *bufio.Scanner, contentLength int64) []byte { bytes := make([]byte, contentLength) scanner.Split(bufio.ScanBytes) var i int64 for i = 0; i < contentLength; i++ { scanner.Scan() bytes[i] = scanner.Bytes()[0] } return bytes }
func ReadInts(s *bufio.Scanner) []int { s.Split(bufio.ScanWords) s.Scan() n, _ := strconv.Atoi(s.Text()) a := make([]int, n) for i := 0; i < n; i++ { s.Scan() a[i], _ = strconv.Atoi(s.Text()) } return a }
func parseTicketData(s *bufio.Scanner) (price, sold float64, err error) { s.Split(bufio.ScanLines) if scannable := s.Scan(); scannable == false { err = errors.New("EOL Reached; Nothing else scannable") return } values := strings.Split(s.Text(), " ") price, _ = strconv.ParseFloat(values[0], 64) sold, _ = strconv.ParseFloat(values[1], 64) return }
// Parses header text out of a bufio.Scanner, should be called immediately after parseStatusLine. func parseHeaders(scanner *bufio.Scanner, method HttpMethod) []string { headers := make([]string, 0) scanner.Split(bufio.ScanLines) for scanner.Scan() { line := scanner.Text() // Break if we hit a empty line. if len(line) == 0 { break } headers = append(headers, line) } return headers }
func getPrinter(scanner *bufio.Scanner, lang language.Tag) (*message.Printer, error) { catalog := message.DefaultCatalog scanner.Split(bufio.ScanLines) for scanner.Scan() { s := scanner.Text() line := strings.SplitN(s, "=", 2) if len(line) != 2 { return nil, fmt.Errorf("Invalid line in messages file: %v", line) } catalog.SetString(lang, line[0], line[1]) } if err := scanner.Err(); err != nil { return nil, err } return catalog.Printer(lang), nil }
func scanMessagePart(scanner *bufio.Scanner, sep string) (string, bool) { scanner.Split(bufio.ScanLines) var parts []string for scanner.Scan() { // TODO: consistent de-indent by first-line detection line := strings.TrimLeft(scanner.Text(), " ") if len(line) == 0 { return strings.Join(parts, sep), true } parts = append(parts, line) } if len(parts) > 0 { return strings.Join(parts, sep), true } return "", false }
func prepareTasks() (*[]string, error) { var tasks []string var scanner *bufio.Scanner var err error if *urlFile == "-" { scanner = bufio.NewScanner(os.Stdin) } else { file, err := os.Open(*urlFile) if err != nil { return nil, err } defer file.Close() scanner = bufio.NewScanner(file) } if *isJSON { var jsonInput []byte scanner.Split(bufio.ScanBytes) for scanner.Scan() { for _, b := range scanner.Bytes() { jsonInput = append(jsonInput, b) } } if err := scanner.Err(); err != nil { return nil, err } if err = json.Unmarshal(jsonInput, &tasks); err != nil { return nil, err } } else { for scanner.Scan() { tasks = append(tasks, scanner.Text()) } if err := scanner.Err(); err != nil { return nil, err } } return &tasks, err }
func BinaryQueries(scanner *bufio.Scanner) string { var buffer bytes.Buffer scanner.Split(bufio.ScanWords) scanner.Scan() length, _ := strconv.Atoi(scanner.Text()) scanner.Scan() lines, _ := strconv.Atoi(scanner.Text()) arr := make([]int, length) for i := 0; i < length; i++ { scanner.Scan() arr[i], _ = strconv.Atoi(scanner.Text()) } var cmd, X, R int for i := 0; i < lines; i++ { scanner.Scan() cmd, _ = strconv.Atoi(scanner.Text()) if cmd == 1 { scanner.Scan() X, _ = strconv.Atoi(scanner.Text()) if arr[X-1] == 0 { arr[X-1] = 1 } else { arr[X-1] = 0 } } else { scanner.Scan() _, _ = strconv.Atoi(scanner.Text()) scanner.Scan() R, _ = strconv.Atoi(scanner.Text()) if arr[R-1] == 1 { buffer.WriteString("ODD\n") } else { buffer.WriteString("EVEN\n") } } } return buffer.String() }
func readMatrix(s *bufio.Scanner) [][]byte { s.Split(bufio.ScanWords) s.Scan() r, _ := strconv.Atoi(s.Text()) s.Scan() c, _ := strconv.Atoi(s.Text()) s.Split(bufio.ScanLines) g := make([][]byte, r) for i := 0; i < r; i++ { s.Scan() t := s.Text() g[i] = make([]byte, c) for j := 0; j < c; j++ { g[i][j] = t[j] - '0' } } return g }
func main() { var ( cst customers c customer ex bool err error text []byte command string flagEndofLoop bool = true scanner *bufio.Scanner = bufio.NewScanner(os.Stdin) fio, acc string money float64 time int64 fioArr []string ) // читаем файл text, err = ioutil.ReadFile(file) if err != nil { fmt.Println(err) } //преобразовываем файл в массив структур err = json.Unmarshal(text, &cst) if err != nil { fmt.Println(err) } for flagEndofLoop { // пишем команду fmt.Println("Введите команду и параметр через пробел (для более подробной информации наберите help)") scanner.Split(bufio.ScanWords) scanner.Scan() command = scanner.Text() switch command { case "fio": // ищем по фамилии scanner.Split(bufio.ScanWords) scanner.Scan() v := scanner.Text() fmt.Printf("%v\n", v) fioArr = append(fioArr, string(v)) scanner.Scan() v = scanner.Text() fmt.Printf("%v\n", v) fioArr = append(fioArr, string(v)) fio = strings.Join(fioArr, " ") fio = strings.Trim(fio, " ") ex, c = cst.FindFio(fio) if ex { fmt.Printf("%v\n", c) } else { fmt.Println("нет такого") } case "acc": // ищем по счету scanner.Split(bufio.ScanWords) scanner.Scan() acc = scanner.Text() ex, c = cst.FindAccount(acc) if ex { fmt.Printf("%v\n", c) } else { fmt.Println("нет такого") } case "money": // ищем по деньгам scanner.Split(bufio.ScanWords) scanner.Scan() money, _ = strconv.ParseFloat(scanner.Text(), 32) ex, c = cst.FindMoney(money) if ex { fmt.Printf("%v\n", c) } else { fmt.Println("нет такого") } case "time": // ищем по сроку scanner.Split(bufio.ScanWords) scanner.Scan() time, _ = strconv.ParseInt(scanner.Text(), 10, 0) ex, c = cst.FindTime(time) if ex { fmt.Printf("%v\n", c) } else { fmt.Println("нет такого") } case "help": // помощь по командам fmt.Println("Возможные команды:") fmt.Println("fio строка : поиск по фамилии и имени") fmt.Println("acc строка: поиск по счету") fmt.Println("money число: поиск по кол-ву денег") fmt.Println("time число: поиск по сроку ") fmt.Println("end: Выход из программы") case "end": // если надоело то выходим fmt.Println("пока") flagEndofLoop = false } } }