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

/prog/ challenge: random BBCode

Name: Anonymous 2018-07-23 23:47

Write a program that iterates through a string or char array input and adds random BBCode. There are 9 tags to account for: [b], [i], [o], [u], [s], [spoiler], [code], [sup], and [sub].

Each character can have multiple BBCode tags. So you could roll a random number to determine if you want to add it to that particular character or not. I'll let you decide on the percentages for randomness. It's doesn't necessarily have to be 50%.

You could either have them all open and close for every char, or perhaps just keep track of which tags are opened, and then make sure they're all closed before the end. But the only ones that would really be interesting for multiple characters instead of just one would be code, sub, sup, and spoiler.

I made a very basic example here, but you should try to add more features:


import java.lang.Math.*;

public class BBCode {
public static void main(String[] args) {
String inputString = "here is an example";
String outputString = "";
for (Integer i = 0; i < inputString.length(); i++) {
int bold = (int) ( Math.random() * 2 + 1);
int italic = (int) ( Math.random() * 2 + 1);
int overline = (int) ( Math.random() * 2 + 1);
int underline = (int) ( Math.random() * 2 + 1);
String openingTags = "";
String closingTags = "";
//need to do openingTags in order, then closingTags in reverse order
//example: [b][i][o][u]hello[/u][/o][/i][/b]
//NOT: [b][i][o][u]hello[/b][/i][/o][/u] (that's wrong)
if (bold == 1) {
openingTags += "[b]";
}
if (italic == 1) {
openingTags += "[i]";
}
if (overline == 1) {
openingTags += "[o]";
}
if (underline == 1) {
openingTags += "[u]";
}

if (underline == 1) {
closingTags += "[/u]";
}
if (overline == 1) {
closingTags += "[/o]";
}
if (italic == 1) {
closingTags += "[/i]";
}
if (bold == 1) {
closingTags += "[/b]";
}
outputString += openingTags + inputString.charAt(i) + closingTags;

}
System.out.print(outputString);
}
}


Example output of above code:
here is an example

Name: Anonymous 2018-07-24 7:47

Not optimised, please consolidate your runs of contingent tags into a single tag.

Name: Anonymous 2018-07-24 10:50

>>2
bruh I made that example code in like 2 minutes

the point of this thread is for other people to make their own version, not nitpick over what I did

Name: Anonymous 2018-07-24 12:47

>>3
Improving the specs is a perfectly valid part of the challenge.

Name: Anonymous 2018-07-25 6:17

>>1
Holy shit that's some terrible, repetitive code.

Name: Anonymous 2018-07-25 7:43

>>5
make a better one then

back in the day, people would actually participate in /prog/ challenge threads

as in, contributing their own version

what happened?

Name: Anonymous 2018-07-27 0:29

const tags = 'b i o u s spoiler code sup sub'.split(' ');

function bbcode(s) {
const stack = [];
let res = '';

function open() {
// Hack: don't nest any tags inside [code] because it escapes them and makes the output look bad
if (stack[stack.length - 1] === 'code') {
close();
}

const tag = randomTag();
stack.push(tag);
res += `[${tag}]`;
}

function close() {
if (!stack.length) {
return;
}
const tag = stack.pop();
res += `[/${tag}]`;
}

for (const c of s) {
const rand = Math.random();

if (rand < 0.50) {
open();
} else if (rand < 0.75) {
close();
}

res += c;
}

while (stack.length) {
close();
}

return res;
}

function randomTag() {
return tags[(Math.random() * tags.length) >> 0];
}

bbcode("hax my anus. hax my anus. hax my anus. hax my anus. ");




Example output:

hax my anus. hax my anus. hax my anus. hax my anus.

Name: Anonymous 2018-07-27 0:43

>>7
nice

Name: Anonymous 2018-07-27 2:49

#!/usr/bin/env ruby

BBCODES = ["b", "i", "o", "u", "s", "spoiler", "sup", "sub"]

class BBCodeInsertion
attr_accessor :from, :to, :tag

def compatible_with? other
self.from >= other.to or self.to <= other.from
end
end

class BBCodeCompiler
def initialize s
@s = s
@bbcodes = []
end

def add bci
@bbcodes << bci if @bbcodes.all? {|x| x.compatible_with? bci}
end

def to_s
self.compile
end

def compile
open_positions = Array.new(@s.size) {Array.new}
close_positions = Array.new(@s.size + 1) {Array.new}
@bbcodes.each do |bci|
open_positions[bci.from] << bci.tag
close_positions[bci.to] << bci.tag
end
close_positions.map {|x| x.reverse}
compiled = []
@s.chars.each_with_index do |ch, idx|
compiled << close_positions[idx].map {|x| "[/#{x}]"}
compiled << open_positions[idx].map {|x| "[#{x}]"}
compiled << ch
end
compiled << close_positions[-1].map {|x| "[/#{x}]"}
compiled.join ''
end
end

if ARGV.size > 0
input = ARGV.join ' '
c = BBCodeCompiler.new input
n_tags = rand 1..(100*input.size)
for _ in 0..n_tags do
bci = BBCodeInsertion.new
bci.tag = BBCODES.sample
bci.from = rand(input.size)
bci.to = bci.from + rand(input.size - bci.from) + 1
c.add bci
end
puts c
end






Her name keke she eat my dick like its free free

Name: Anonymous 2018-07-27 2:51

>>7
function decor(text){var res=[];
var codes=['code','b', 'i', 'o', 'u', 's',
'spoiler', 'sup','sub'];
for(var i in text){
var r=0|(Math.random()*codes.length);
res[i]=`[${codes[r]}]${text[i]}[/${codes[r]}]`;
var r=0|(Math.random()*codes.length);
while(r){res[i]=`[${codes[r]}]${res[i]}[/${codes[r]}]`;var r=0|(Math.random()*codes.length); }
}
return res.join("");}

"The quick brown fox jumps over the lazy dog" Edited on 27/07/2018 03:04.

Name: Anonymous 2018-07-27 3:15

>>10
you win

Name: Anonymous 2018-07-27 3:31

#!/usr/bin/env ruby

BBCODES = ["b", "i", "o", "u", "s", "spoiler", "sup", "sub"]

input = $stdin.read
cs = input.chars
tags = Array.new
for ch in cs do
case rand 3
when 0
print ch
when 1
tag = BBCODES.sample
tags << tag
print "[#{tag}]#{ch}"
when 2
tag = tags.pop
if tag == nil
print ch
else
print "[/#{tag}]#{ch}"
end
end
end
while not tags.empty?
tag = tags.pop
print "[/#{tag}]"
end
puts





Her name keke she eat my dick like its free free

Name: Anonymous 2018-07-27 3:45

>>10
var r=0|(Math.random()*codes.length);
🤔

Name: Anonymous 2018-07-27 4:41

>>13
Its a fast Math.floor() essentially, by ORing to 0, you force JS to coerce it to integer first and OR 0 does nothing.

Name: Anonymous 2018-07-27 4:52

>>13
Also, its perfectly legal to redeclare anything with var, so copy pasting that line is ok.

Name: Anonymous 2018-07-27 7:48

benistest

Name: Anonymous 2018-08-15 17:50

using System;
using System.Text;
using System.Collections;
using System.Collections.Generic;

class Prog
{
static void Main(string[] argv)
{
var bbcodez = new List<string> { "b", "i", "o", "u", "s", "spoiler", "sup", "sub" };
var queue = new Queue();
var stack = new Stack();
var result = new StringBuilder();
var random = new Random();

foreach(char character in argv[0])
{
foreach(string code in bbcodez)
if(Convert.ToBoolean(random.Next(0, 2)))
{
queue.Enqueue(code);
stack.Push(code);
}

while(stack.Count > 0)
result.Append($"[{stack.Pop()}]");

result.Append(character);

while(queue.Count > 0)
result.Append($@"[/{queue.Dequeue()}]");
}

Console.WriteLine(result.ToString());
}
}

Name: Anonymous 2018-08-15 19:02

extern crate rand;

use std::env;
use std::fmt::Write;

use rand::Rng;

macro_rules! zero_cost_abstractions {
($it:expr, $fmt:expr, $s:expr) => {
$it.for_each(|tag| write!($s, $fmt, tag).unwrap())
};
}

fn main() {
let mut args = env::args().skip(1);
let ntags = args.next().and_then(|n| n.parse().ok()).unwrap_or(4);
let s = args
.next()
.unwrap_or_else(|| "mfw tough muddin rn fam tbh nbd smh atm btw".to_owned());

let tags = ["i", "b", "u", "spoiler", "o", "sup", "sub"];
let mut res = String::new();
let mut rng = rand::thread_rng();
let mut picked = Vec::with_capacity(ntags);

for c in s.chars() {
(1..rng.gen_range(0, ntags)).for_each(|_| picked.push(rng.choose(&tags).unwrap()));

zero_cost_abstractions!(picked.iter(), "[{}]", res);
res.push(c);
zero_cost_abstractions!(picked.iter().rev(), "[/{}]", res);
picked.clear();
}

println!("{}", res);
}

Name: Anonymous 2018-08-16 9:24

>>18
>macro_rules!
I have a sneaking suspicion they named this intentionally.

Name: Anonymous 2018-08-19 21:42

Disclaimer:
RNG Subroutine stolen from GNU docs.

PROGRAM BBCODE
IMPLICIT NONE

CHARACTER(LEN=50) :: STRING
CHARACTER(LEN=7),
& DIMENSION(9) :: BBCODES
INTEGER :: POS, I
REAL :: T

STRING="Ok, you just fuqin angered an expert programmer"
POS=1
BBCODES=["b ","i ","o ","u ","s ",
& "spoiler","code ","sup ","sub "]

CALL INIT_RANDOM_SEED()
1 CALL RANDOM_NUMBER(T)
I=INT(T*10)
IF(I==0) GO TO 1

WRITE(*,FMT="(A,A)",ADVANCE="NO") "["//TRIM(BBCODES(I))//"]",
& STRING(POS:POS)//"[/"//TRIM(BBCODES(I))//"]"

POS=POS+1
IF(POS<LEN(STRING)) GO TO 1

END PROGRAM BBCODE


SUBROUTINE init_random_seed()
INTEGER :: i, n, clock
INTEGER, DIMENSION(:), ALLOCATABLE :: seed

CALL RANDOM_SEED(size = n)
ALLOCATE(seed(n))

CALL SYSTEM_CLOCK(COUNT=clock)

seed = clock + 37 * (/ (i - 1, i = 1, n) /)
CALL RANDOM_SEED(PUT = seed)

DEALLOCATE(seed)
END SUBROUTINE


Output:
Ok, you just fuqin angered an expert programmer

Name: Anonymous 2018-08-20 4:03

>>20
Which boomerlang is this? BASIC?

Name: Anonymous 2018-08-20 4:57

>>21
Fortran ofc " IMPLICIT NONE"

Name: Anonymous 2018-08-20 14:30

>>21
spied the quiche eater.

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