Login Register






[Golfing] Filter filter_list
Author
Message
[Golfing] Filter #1
Oh boy! More golfing challenges!

You'll be given a bunch of text for your program to process line-by-line, and said text will be full of words and randomly generated non-word characters.

With that text in a separate file, your code needs to go through it one line at a time, sorting out all the random crap and outputting the words, separated by spaces.

Spoiler: Input
Code:
[.;'/Hello,';`~world (*^Oh'.>:$fuck>":}what?>)#happened>:?$%here {">?Alrighty>:%&then

Spoiler: My solution
Ruby, as usual

129 bytes. I feel like I could've done better, but I got stumped trying to get any smaller.
Code:
File.open(ARGV[0]).each_line{|l|x=[];l.each_char{|c|/[A-z]/.match(c) ? x<<c : x[-1]==" "||x[-1]==nil ? nil : x<<" "};puts x.join}

Spoiler: Output
Code:
Hello world Oh fuck what happened here Alrighty then

Spoiler: BIG tip
Regex (or Regexp) is your friend here.


Leaderboard

@0xDEAD10CC - 48B - J
@null - 53B - Bash
@null - 59B - Ruby
@Eclipse - 79B - Python
@Shebang - 102B - Python
@"Chitoge" - 129B - Ruby

Good luck!
It's often the outcasts, the iconoclasts ... those who have the least to lose because they
don't have much in the first place, who feel the new currents and ride them the farthest.

Reply

RE: [Golfing] Filter #2
Code:
print'\n'.join(' '.join(`[[' ',c][c.isalpha()]for c in l]`[2::5].split())for l in list(open('w','r')))

Haven't tested it, but I'm pretty confident It works. 102 Bytes.
[Image: CDUAq9d.png]

Reply

RE: [Golfing] Filter #3
Not a fan of regex, I'll attempt it without using it in a bit.

EDIT: I used regex. Sorry.

Reply

RE: [Golfing] Filter #4
(08-26-2015, 09:59 PM)Shebang Wrote:
Code:
print'\n'.join(' '.join(`[[' ',c][c.isalnum()]for c in l]`[2::5].split())for l in list(open('w','r')))

Haven't tested it, but I'm pretty confident it works.

Python, right?
[Image: HBLphXa.png]
It's often the outcasts, the iconoclasts ... those who have the least to lose because they
don't have much in the first place, who feel the new currents and ride them the farthest.

Reply

RE: [Golfing] Filter #5
(08-26-2015, 10:21 PM)Chitoge Wrote: Python, right?
[Image: HBLphXa.png]

'w' is the file containing the strings, haha Tongue You have to put the file in the same location as the script. I just tested, seems to work:

[Image: p7LtjSu.png]
[Image: CDUAq9d.png]

[+] 1 user Likes Shebang's post
Reply

RE: [Golfing] Filter #6
Do you guys allow bash?

53
Code:
while read l;do echo ${l//[^a-zA-Z0-9]/' '} done < t

file name is t

Output:
[Image: rIQCTF6.png]


Why not use gsub for Ruby?
Code:
IO.foreach('t')do|l|p l.gsub(/[^a-zA-Z0-9]+/,' ')end

[Image: nsm5eSG.png]

Reply

RE: [Golfing] Filter #7
(08-27-2015, 06:13 AM)null Wrote: Why not use gsub for Ruby?
Code:
IO.foreach('t')do|l|p l.gsub(/[^a-zA-Z0-9]+/,' ')end

[Image: nsm5eSG.png]

I was thinking about gsub, but it gives me spaces before and after the desired string. That's the only reason I needed ||x[-1]==nil.
It's often the outcasts, the iconoclasts ... those who have the least to lose because they
don't have much in the first place, who feel the new currents and ride them the farthest.

Reply

RE: [Golfing] Filter #8
(08-27-2015, 03:57 PM)Chitoge Wrote: I was thinking about gsub, but it gives me spaces before and after the desired string. That's the only reason I needed ||x[-1]==nil.

Oh I did not notice. Try this.
Code:
IO.foreach('t')do|l|p l.gsub(/[^a-zA-Z0-9]+/,' ')[1..-2]end

So that is ~60

[1..-2] will make it so it prints the from index 1 to the second to last index. Confusing shit. For example the string is this
" Hello world \n"
so index one is H and index -2 is d so it will print from H to d.

Run it here https://repl.it/BEC6

[+] 1 user Likes null's post
Reply

RE: [Golfing] Filter #9
Code:
import re for x in open('i'):print' '.join(re.sub('[^a-zA-Z\n]',' ',x).split())

79 bytes

Holy fuck I beat @Shebang.

EDIT: re.sub shit isn't mine btw. I suck at regex. It's a thingy I found on stack overflow that I modified to suit my needs. I can't take credit for it.

Reply

RE: [Golfing] Filter #10
(08-28-2015, 12:24 AM)eclipse Wrote:
Code:
import re for x in open('i'): print' '.join(re.sub('[^a-zA-Z\n]',' ',x).split())

84 Bytes

Holy fuck I beat @Shebang.

What the hell are you doing putting the print on another line? Save your bytes!

Also, in my defense I've never learned Regex Tongue
[Image: CDUAq9d.png]

Reply