Sinisterly
[Golfing] Filter - Printable Version

+- Sinisterly (https://sinister.ly)
+-- Forum: Coding (https://sinister.ly/Forum-Coding)
+--- Forum: Coding (https://sinister.ly/Forum-Coding--71)
+--- Thread: [Golfing] Filter (/Thread-Golfing-Filter)

Pages: 1 2


[Golfing] Filter - Inori - 08-26-2015

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!


RE: [Golfing] Filter - Shebang - 08-26-2015

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.


RE: [Golfing] Filter - Eclipse - 08-26-2015

Not a fan of regex, I'll attempt it without using it in a bit.

EDIT: I used regex. Sorry.


RE: [Golfing] Filter - Inori - 08-26-2015

(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]


RE: [Golfing] Filter - Shebang - 08-26-2015

(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]


RE: [Golfing] Filter - null - 08-27-2015

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]


RE: [Golfing] Filter - Inori - 08-27-2015

(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.


RE: [Golfing] Filter - null - 08-27-2015

(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


RE: [Golfing] Filter - Eclipse - 08-28-2015

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.


RE: [Golfing] Filter - Shebang - 08-28-2015

(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