⬆️ ⬇️

How prone are you to stupid, and a bit holywar

I accidentally heard the story of one colleague to another that everything in the scripting languages ​​is very cool and convenient. The narrator promoted perl as something very cool, simple and straightforward.

I couldn’t resist and intercepted, saying that python will be more understandable, and it provides no less opportunities.

Then I was asked how things are in python with regular expressions, and as a result we came to the following problem:



There is a line, you must display all the words in it that occur N times.



The problem is of course completely trivial, and writing a solution takes a few minutes, but this tupnyak has absorbed everyone for at least a few hours. If you succumb to stupid, then please roll



')

Here are the solutions:



test1.py writing 3 min. "War and peace" this decision is not mastered

#!/usr/bin/env python import re n = 5 with open("./input_file.txt", "r") as f: s = f.read() l = re.findall(r'\w+',s) print repr([x for x in l if l.count(x) == n]) 




test2.pl writing 30 min while working on "war and peace": 0m0.282s

 #!/usr/bin/perl open FH, "<input_file.txt"; local $/; $str = <FH>; $n = 5; %h = (); $h{$1}++ while $str =~ /(\w+)/g; print '['.(join ", ", grep {$h{$_} == 5} keys %h).']'; 




the work time of the first solution did not satisfy at all, so this was written:

test3.py writing 3 min. while working on "war and peace": 0m0.285s

 #!/usr/bin/env python import re n = 5 with open("./input_file.txt", "r") as f: s = f.read() d={} for x in re.findall(r'\w+',s): if x in d: d[x] += 1 else: d[x] = 1 print repr([k for k,v in d.items() if v == n]) 




test4.pl writing 30 minutes (again, very long). while working on "war and peace": 0m0.221s

But alas, the result is incorrect

 #!/usr/bin/perl open FH, "<./input_file.txt"; local $/; $str = <FH>; $n = 5; chomp($str); foreach(split(/ /, $str)){$h{$_}++;} my $res ="["; foreach(keys(%h)) {if($h{$_} == $n){$res .= "$_, ";}} $res .= "]"; print $res; 




test5.hs writing 10 min. while working on "war and peace": 0m2.948s

The man built it to say “I can also on Haskel”, but the result was not impressed

 import Data.List import Data.Char main = interact -- IO $ unlines -- combine meta-data into string . map (\(n, w) -> if (n == 5) then show w else "") . sort -- sort meta-data by occurances . map (\s -> (length s, head s)) -- transform to sublist meta-data . group -- break into sublists of unique words . sort -- sort words . words -- break into words . map (\c -> -- simplify chars in input if isAlpha c then toLower c else ' ') 




Well, if so, then in C ++ we also implement

test6.cpp writing 5 min. while working on "war and peace": 0m0.640s

 #include <stdlib.h> #include <stdio.h> #include <errno.h> #include <string> #include <iostream> #include <set> std::multiset<std::string> words; void prepare() { char *p = 0; int err = 1; while ( 1 == scanf("%as", &p)) { words.insert(p); free(p); } if (errno) { perror("scanf"); abort(); } } void output(int n) { std::multiset<std::string>::iterator it(words.begin()); std::string out; for(;it!=words.end(); it = words.upper_bound(*it)) { if ( words.count(*it) == n ) out.append(" ," + *it); } if (out.find(" ,") == 0 ) out.erase(0, 2); std::cout<<"["<<out<<"]\n"; } int main(int argc, char **argv) { prepare(); output(atoi(argv[1])); return 0; } 




Suggest your solutions in the comments if you are prone to tupnyak and can do better in sane time;)

this file was used as input_file.txt , this is “War and Peace” from the Gutenberg Library

Source: https://habr.com/ru/post/163467/



All Articles