2
\$\begingroup\$

This is a code golf problem: Say you have two files, one file s and one file h. The problem is that for each line l of s you should produce a list that contains all lines of h that contain l. By "contain" I mean a substring, so, for example the line "foobar12baz" contains either foo, bar, foobar, 12, 1 or baz, etc...

You may use any programming language or any program to accomplish this in the least number of characters possible. Instead of a list you may print an array, or other sequence type structure.

Here is some Haskell I wrote that does it in 103 characters, and assumes you have the following modules imported

Data.List

Control.Monad

Control.Applicative

let f = (<$>) lines . readFile in (\(a,b)-> [[d|d<-b, isInfixOf c d]|c<-a]) <$> liftM2 (,) (f "s") (f "h")

Example files:

"h"

asdf1
asd2asdf
s3adsf

"s"

1
2
3

Output:

[["asdf1"],["asd2asdf"],["s3adsf"]]
\$\endgroup\$
1
10
\$\begingroup\$

just grep is enough...

grep -Ff s h
\$\endgroup\$
1
\$\begingroup\$

Better:

puts IO.readlines('h').grep /#{IO.readlines's'}/

In ruby, 53 characters, outputs an array and not a file:

p IO.readlines('h').each{|m|m=~/#{IO.readlines's'}/}

This reads file 'h' for every line of 's' to save a variable. Not ideal.

Happy golfing!

\$\endgroup\$
-1
\$\begingroup\$
while read -r;do grep "$REPLY" --h>"$REPLY";done<s

YEAAAAAAAH!!1

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.