Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

Randomizing BBCode

Name: Anonymous 2011-08-09 9:58

https://gist.github.com/1132093

How can I improve this?

WARNING: FIOC

Name: Anonymous 2011-08-09 10:38

Excuse my ignorance, but why the html?

Name: Anonymous 2011-08-09 10:52

>>2
This. I don't get what he was trying to achieve.

Name: Anonymous 2011-08-09 11:43

>>2,3
My bad, I was using it on a webpage and I just lifted the code from there.

Name: Anonymous 2011-08-09 11:59

Warning: Valid Perl code

use strict;
use warnings;
my @bbcodes = ( "u", "o", "sup", "sub", "s", "i", "b", "code" );
my @chars = split(//, $ARGV[0]);
foreach (@chars) {
  print &fibrs($_);
}
print "\n";

sub fibrs {
  my $elem = $bbcodes[rand @bbcodes];
  my $composed = "[" . $elem . "]" . $_[0] . "[/" . $elem . "]";
}

Name: Anonymous 2011-08-09 12:11

>>5
So much better.

Name: Anonymous 2011-08-09 12:17

testing


hello

Name: Anonymous 2011-08-09 12:18

Forgot to quote the string. What an amateur mistake.


hello world

Name: Anonymous 2011-08-09 12:48

OP here, glad someone made a better version because I've only just started really learning programming, and so I'm sure my version is full of ``amateur mistakes".
Anyway, this is fun but it sure can't beat MANUAL HANDCRAFTED EXPERT BBCODE.

Name: Anonymous 2011-08-09 12:58

I like OP's version better. It looks more like a ransom note.

Name: Anonymous 2011-08-09 14:15

Tidied up your FIOC a bit, then rewrote most of it:

import random, sys
tags = 'b u i o sup sub spoiler m aa s'.split()

ransom = ''
for c in sys.stdin.read():
    amount = random.randint(0, 5)
    for tag in random.sample(tags, amount):
        c = '[{0}]{1}[/{0}]'.format(tag, c)
    ransom += c
print ransom

Name: Anonymous 2011-08-09 14:54

>>11
Not that it matters too much for something like this, but it's considered bad practice to have a loop that concatenates to a string in Python. It reallocates a new string each iteration and it's slow. This is the way that won't get you bitched at when working with a team of Pythonistas.

import random, sys
tags = 'b u i o sup sub spoiler m aa s'.split()

ransom = []
for c in sys.stdin.read():
    amount = random.randint(0, 5)
    for tag in random.sample(tags, amount):
        c = '[{0}]{1}[/{0}]'.format(tag, c)
    ransom.append(c)
print ''.join(ransom)

Name: Anonymous 2011-08-09 19:51

THAT WAS VIP QU[spoiler]A/spoiler]LITY

Name: Anonymous 2011-08-09 20:07

>>13
Just how did you fuck this up?

Name: Anonymous 2011-08-09 20:19

>>12
Wait, Python is slow????

Name: Anonymous 2011-08-09 20:26

from random import randint as r,sample as s;from sys import argv as a,stdin as i
print ''.join(map(lambda c,f=lambda c,t,f=lambda c,t,f:c if not t else '[{0}]{1'
'}[/{0}]'.format(t.pop(),f(c,t,f)): f(c,t,f):f(c,s('b u i o sup sub spoiler m a'
'a s'.split(),r(0,5))),' '.join(a[1:]) or i.read()))

Name: Anonymous 2011-08-09 20:39

>>15
Don't you have some useless C code that does nothing to optimize?

Name: Anonymous 2011-08-09 20:42

>>17
Python is slow, and I hate C.

Name: Anonymous 2011-08-09 21:10

Waiting for the ``in Lisp" version.

Name: Anonymous 2011-08-10 1:38

hax my anus

Name: Anonymous 2011-08-10 3:07

ONE WORD VALID FORCED ONE-LINER TO PREVENT FORCED INDENTATION OF CODE THREAD OVER[b]

Name: ᴍʀ. ᴠɪʟᴇ !PRoGN.piPE 2011-08-11 8:10

    map{$a=qw(u o sup sub s i b code)[rand 7];print"[$a]$_[/$a]"}split//=>"@ARGV"

shorter perl version

Name: ᴍʀ. ᴠɪʟᴇ !PRoGN.piPE 2011-08-11 8:26

improved.


map{$c=$_;@a=qw(u o sup sub s i b code);map{$b=splice@a,rand@a,1;print"\[$b]$c\[/$b]"}rand@a..$#a}split//=>"@ARGV"

Name: Anonymous 2011-08-11 9:15

haxm[o][i]y[/i][/o] a[i][u][o][s]n[/s][/o][/u][/i][o][sub][s][i][u]u[/u][/i][/s][/sub][/o][b]s[/b]

Name: ᴍʀ. ᴠɪʟᴇ !PRoGN.piPE 2011-08-11 9:16

map{my$i=$_;@a=qw(u o sup sub s i b);map{$b=splice@a,rand@a,1;$i = "\[$b]$i\[/$b]"}@a[rand@a..$#a];print$i}split//=>"@ARGV"

fixed the code repeating and not stacking like it was intended

Name: Anonymous 2011-08-11 9:29

Hello, world!

Name: Anonymous 2011-08-11 9:39

i suck cocks

Name: Anonymous 2011-08-11 13:51

PERL VS PYTHON: BBCODE EDITION

Name: Anonymous 2011-08-11 15:46

>>25
Have a Susscoin on me.

Name: Anonymous 2011-08-11 16:11

>>25
Okay, now with proper indentation please.

Name: Anonymous 2011-08-11 16:15

>>19
Keep waiting. Lisp may be ``Turing Complete'', but it's completely useless, and its champions can't program anything besides Fibonacci.

Name: Anonymous 2011-08-11 16:44

>>31
Keep practicing. Your ``trolls" are complete shit and you can't accept that Lisp is the superior language.

Name: Anonymous 2011-08-11 18:25

>>32
haha go fuck yourself faggot

Name: ᴍʀ. ᴠɪʟᴇ !PRoGN.piPE 2011-08-11 22:44

>>30

map {
    $a = $_;
    @_ = qw(u o sup sub s i b);
    map {
        $b = splice @_, rand @_, 1;
        $a ="[$b]$a\[/$b]"
        } @_[rand @_ .. $#_];
    print $a
} split//,"@ARGV"

proper spacing/indentation, and it is now strict-compatible

Name: Anonymous 2011-08-11 22:56

c
a
t

m
y

a
n
u
s

Name: ᴍʀ. ᴠɪʟᴇ !PRoGN.piPE 2011-08-11 23:05


map {$\=$_;@_=qw(u o sup sub s i b);map{$\="[$_]$\\[/$_]"}splice@_,rand@_,1while@_;print''}split//,<>;


102 chars, unfortunately this version will randomly order the bbcode 8 times, not a random amount from 1 to 8

(still strict-compatible)

Name: Anonymous 2011-08-11 23:45

PROGRAM MY ANUS

Name: Anonymous 2011-08-12 3:04

COOL STORY, BRO

Name: NIGGER 2011-08-12 3:06

>>36
MR.VILE IS A FAGGOT

Don't change these.
Name: Email:
Entire Thread Thread List