You are given a document of text. The number of lines is not given, so you must read until the input ends. Each line of the document is either an empty line with no characters at all, or a line of words made of lowercase English letters, separated by single spaces. A run of one or more consecutive lines that are not empty is called a paragraph. That is, a paragraph is a longest run of non-empty lines, bounded at each end by an empty line, the start of the input or the end of the input. Several empty lines in a row only separate paragraphs; empty lines alone never form a paragraph. On the first line, print the number of lines in the input (empty lines included) and the number of paragraphs, separated by one space. On the second line, print for each paragraph the number of words in it, in the order they appear in the document, separated by single spaces. If there is no paragraph at all, print - (a single minus sign) on the second line.
Input
The input has 1 to 2,000 lines, and the number of lines is not given. Each line has 0 to 80 characters. A line that is not empty consists of words made only of lowercase English letters, separated by single spaces, and does not start or end with a space (so no line is made of spaces only). Every line, including the last, ends with a newline
Output
The number of lines and the number of paragraphs on the first line; the word count of each paragraph on the second line (- if there is no paragraph)
Example 1 · stdin
the cat sat
on the mat
dogs bark
birds sing
loudly
stdout
8 3
6 2 3
Example 2 · stdin
hello world
stdout
3 1
2
main.py
Error List00F8 ↓ · ⇧F8 ↑
Input (stdin)
When you run, these lines are fed to input()/stdin in order (the program doesn't stop to ask).
$ Ready
Open this on a computer to write and run code. On mobile you can read the problem and the solutions.
In other languagesShow solutions by language · 8 languages
Since the number of lines is not given, you must tell 'the input has ended' and 'this line is empty' apart as two different signals. Java's BufferedReader.readLine() and C#'s Console.ReadLine() give "" for an empty line and null when the input has ended. C++'s getline(cin, line) gives an empty string for an empty line and turns false at the end, which stops while (getline(cin, line)). Craykon's Console.ReadLine() likewise gives "" for an empty line and null at the end, so the loop is while (line != null). Python's for raw in sys.stdin: and C's while (fgets(buf, sizeof buf, stdin)) stop by themselves at the end, but the line they give still has its newline, so remove it with raw.rstrip("\n") or buf[strcspn(buf, "\n")] = '\0'. JS and TS read the whole input and cut it with split("\n"). Trap one: because every line ends with a newline, split("\n") in JS and TS leaves one more empty string after the last newline. Counting it makes the first line of example 1 9 3, so throw it away with pop() (the trailing empty string never forms a paragraph, so the second line stays right). Trap two: if you do not remove the newline from a line of Python's sys.stdin or from C's fgets buffer, an empty line is "\n" rather than "" and does not look empty. On example 1, Python sees the whole document as one paragraph and prints 8 1 and 11 (the C solution, which counts spaces, counts one extra word for each empty line and prints 14). Trap three: the common C# loop while (!string.IsNullOrEmpty(line = Console.ReadLine())) stops not only at null but also at an empty line. It stops reading at the first empty line, and example 1 becomes 2 1 and 6. Trap four: if a paragraph is closed only when an empty line arrives, the last paragraph is lost when the document ends without an empty line. Close it once more after the loop; otherwise example 1 becomes 8 2 and 6 2. Trap five: closing a paragraph at every empty line creates paragraphs with 0 words where empty lines follow each other. Example 1 becomes 8 4 and 6 2 0 3, so close a paragraph only when it has words.
Pythonmain.py · 19 lines
import syslines = 0counts = [] # 문단마다 단어 수words = 0 # 지금 읽고 있는 문단의 단어 수(0이면 문단 밖)for raw in sys.stdin: # 입력이 끝나면 반복이 저절로 멈춘다 line = raw.rstrip("\n") # sys.stdin의 줄에는 줄 바꿈이 붙어 있다 lines += 1 if line == "": if words > 0: # 빈 줄이 문단을 닫는다 counts.append(words) words = 0 else: words += len(line.split())if words > 0: # 입력이 빈 줄 없이 끝나면 마지막 문단을 여기서 닫는다 counts.append(words)print(lines, len(counts))print(" ".join(map(str, counts)) if counts else "-")
JavaScriptmain.js · 20 lines
// 입력을 통째로 읽어 줄 바꿈으로 나눈다const lines = require("fs").readFileSync(0, "utf8").split("\n");lines.pop(); // 모든 줄이 줄 바꿈으로 끝나므로, 마지막 줄 바꿈 뒤에 생긴 빈 문자열을 버린다const counts = [];let words = 0; // 지금 읽고 있는 문단의 단어 수(0이면 문단 밖)for (const line of lines) { if (line === "") { if (words > 0) { counts.push(words); words = 0; } } else { words += line.split(" ").length; }}if (words > 0) counts.push(words); // 빈 줄 없이 끝난 마지막 문단console.log(`${lines.length} ${counts.length}`);console.log(counts.length > 0 ? counts.join(" ") : "-");
TypeScriptmain.ts · 22 lines
import { readFileSync } from "node:fs";// 입력을 통째로 읽어 줄 바꿈으로 나눈다const lines: string[] = readFileSync(0, "utf8").split("\n");lines.pop(); // 모든 줄이 줄 바꿈으로 끝나므로, 마지막 줄 바꿈 뒤에 생긴 빈 문자열을 버린다const counts: number[] = [];let words = 0; // 지금 읽고 있는 문단의 단어 수(0이면 문단 밖)for (const line of lines) { if (line === "") { if (words > 0) { counts.push(words); words = 0; } } else { words += line.split(" ").length; }}if (words > 0) counts.push(words); // 빈 줄 없이 끝난 마지막 문단console.log(`${lines.length} ${counts.length}`);console.log(counts.length > 0 ? counts.join(" ") : "-");
JavaMain.java · 37 lines
import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.util.ArrayList;import java.util.List;import java.util.StringJoiner;public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); List<Integer> counts = new ArrayList<>(); int lines = 0, words = 0; String line; // 빈 줄은 ""로, 입력이 끝나면 null이 온다 while ((line = br.readLine()) != null) { lines++; if (line.isEmpty()) { if (words > 0) { counts.add(words); words = 0; } } else { words += line.split(" ").length; } } if (words > 0) counts.add(words); // 빈 줄 없이 끝난 마지막 문단 System.out.println(lines + " " + counts.size()); if (counts.isEmpty()) { System.out.println("-"); } else { StringJoiner sj = new StringJoiner(" "); for (int c : counts) sj.add(String.valueOf(c)); System.out.println(sj); } }}
C#Main.cs · 33 lines
using System;using System.Collections.Generic;class Program{ static void Main() { var counts = new List<int>(); int lines = 0, words = 0; string line; // 빈 줄은 ""로, 입력이 끝나면 null이 온다 while ((line = Console.ReadLine()) != null) { lines++; if (line == "") { if (words > 0) { counts.Add(words); words = 0; } } else { words += line.Split(' ').Length; } } if (words > 0) counts.Add(words); // 빈 줄 없이 끝난 마지막 문단 Console.WriteLine($"{lines} {counts.Count}"); Console.WriteLine(counts.Count > 0 ? string.Join(" ", counts) : "-"); }}
C++main.cpp · 38 lines
#include <iostream>#include <sstream>#include <string>#include <vector>using namespace std;int main() { string line; vector<int> counts; int lines = 0, words = 0; // getline은 줄 바꿈을 떼어 주고, 입력이 끝나면 거짓이 되어 반복이 멈춘다 while (getline(cin, line)) { lines++; if (line.empty()) { if (words > 0) { counts.push_back(words); words = 0; } } else { istringstream in(line); string w; while (in >> w) words++; } } if (words > 0) counts.push_back(words); // 빈 줄 없이 끝난 마지막 문단 cout << lines << ' ' << counts.size() << '\n'; if (counts.empty()) { cout << "-\n"; } else { for (size_t i = 0; i < counts.size(); i++) { if (i > 0) cout << ' '; cout << counts[i]; } cout << '\n'; } return 0;}
Cmain.c · 36 lines
#include <stdio.h>#include <string.h>int main(void) { char buf[128]; static int counts[1000]; /* 문단은 많아야 1,000개 */ int lines = 0, paragraphs = 0, words = 0; /* fgets는 입력이 끝나면 NULL을 준다 */ while (fgets(buf, sizeof buf, stdin)) { buf[strcspn(buf, "\n")] = '\0'; /* fgets가 담아 준 줄 바꿈을 지운다 */ lines++; if (buf[0] == '\0') { if (words > 0) { counts[paragraphs++] = words; words = 0; } } else { words++; /* 단어 수 = 빈칸 수 + 1 */ for (char *p = buf; *p; p++) { if (*p == ' ') words++; } } } if (words > 0) counts[paragraphs++] = words; /* 빈 줄 없이 끝난 마지막 문단 */ printf("%d %d\n", lines, paragraphs); if (paragraphs == 0) { printf("-\n"); } else { for (int i = 0; i < paragraphs; i++) { printf(i == 0 ? "%d" : " %d", counts[i]); } printf("\n"); } return 0;}
Craykonmain.cray · 40 lines
줄수 = 0;문단 = []; // 문단마다 단어 수단어 = 0; // 지금 읽고 있는 문단의 단어 수(0이면 문단 밖)줄 = Console.ReadLine(); // 빈 줄은 "", 입력이 끝나면 빈값while (줄 != null){ 줄수 += 1; if (줄 == "") { if (단어 > 0) { 문단.Add(단어); 단어 = 0; } } else { 단어 += 줄.Split(" ").Count; } 줄 = Console.ReadLine();}if (단어 > 0){ 문단.Add(단어); // 빈 줄 없이 끝난 마지막 문단}Console.WriteLine(줄수 + " " + 문단.Count);if (문단.Count == 0){ Console.WriteLine("-");}else{ 결과 = "" + 문단[0]; for (i = 1; i < 문단.Count; i++) { 결과 = 결과 + " " + 문단[i]; } Console.WriteLine(결과);}