Task: in the input stream a number is given - the number of sentences containing words and, possibly, some sort of separating garbage; after the number on each line are the sentences themselves; followed by a number - the number of keywords; after it on each line are the words themselves.

You need to find the number of occurrences of each keyword in each sentence.

Definition of the word:

Or the word under (the ascii value 95).

We define a substring as follows.

It is a part of a word. The given substring must be preceded by letters or numerics or an underscore. There is no need for a word on the other side of the line.

Can I improve my code? Make it shorter and more efficient in the number of operations. Perhaps, with the help of functional programming, you can cut it all in half? Pearl has this.

#!/usr/bin/perl use strict; chomp(my $lines_count = <>); # read the number of centences my @words; for (1..$lines_count) { my $line = <>; push @words, split /[^\w]/, $line; } my $pattern_count = <>; for (1..$pattern_count) { chomp(my $input = <>); my $result = 0; foreach (@words) { if (/\w+$input\w+/) { $result++; } } print $result, "\n"; } 

Sample Input

1 existing pessimist optimist this is 1 is

Sample output

3

Explanation

'existing' has 'is' as a substring and is both preceded and succeeded by words as defined above.

'pessimist' has 'is' as a substring for the same argument as above.

'optimist' has 'is' as a substring for the same argument as above.

'this' though has 'is' as a substring and it has been followed by a blank space, which is non-letter and non-underscore

 'is' is not included as it is preceded and succeeded by a [blank space] which is non-letter, non-numeric and non-underscore. 
  • And explain the phrase "We need to find the number of occurrences of each keyword in each sentence.". This is the total amount of the key in the text as a whole or in each sentence separately. And by the way, + to /\w+$input absolutely useless; you just need to make sure that before and after $ input is \ w - Mike
  • Added explanation to the end. One word is searched for in all sentences. The number of matches is summed. Then go to the next key. - typemoon
  • What does perl have to do with functional programming? Functional programming, as far as I understand, a different paradigm has been accomplished, for which there are different languages ​​... - Mike
  • In the pearl, you can use functional features. And in si you can, if you implement the missing one yourself OP allows you to write something shorter than in imperative languages. - typemoon

1 answer 1

 #!/usr/bin/perl use strict; chomp(my $lines_count = <>); # read the number of centences my $text=""; $text.=<> for (1..$lines_count); # Собираем весь входной текст в единую строку my $pattern_count = <>; for (1..$pattern_count) { chomp(my $input = <>); print $text=~s/(\w$input\w)/$1/g, "\n"; } 

The main line is of course print. In it, we make the replacement of suitable parts of the text on itself. Replacement operator =~s/// returns the number of replacements made, so it returns just the number of places where the substring was encountered.

In principle, it is possible to shorten even more, but the meaning of what is written begins to slip away ...:

 #!/usr/bin/perl use strict; my $text=join('',map {$_=<>} 1..<>); print map {chomp($_=<>); $text=~s/\w$_\w/$&/g, "\n"} 1..<>;