3
\$\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"]]
asked Mar 8, 2012 at 23:44
\$\endgroup\$
1
  • \$\begingroup\$ Thanks for the edits, Wes. And again, welcome to CodeGolf.SE. \$\endgroup\$ Commented Mar 9, 2012 at 22:38

3 Answers 3

10
\$\begingroup\$

just grep is enough...

grep -Ff s h
answered Mar 9, 2012 at 0:15
\$\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!

answered Mar 9, 2012 at 0:24
\$\endgroup\$
0
\$\begingroup\$
while read -r;do grep "$REPLY" --h>"$REPLY";done<s

YEAAAAAAAH!!1

answered Mar 9, 2012 at 0:02
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.