# generate_bf.rb # (copyleft) Matt Gauger 2008 # Randomly generates a brainfuck program # Commandline argument is used as the length of the program # Otherwise, the program defaults to 140 characters in length to fit on Twitter
length = 140
program = ""
ARGV.each do |n|
if n != nil and n != 0
length = n.to_i
end
end
length.downto(1) { |n|
choice = rand(7)
case choice
when 0
program.concat('>')
when 1
program.concat('<')
when 2
program.concat('+')
when 3
program.concat('-')
when 4
program.concat('.')
when 5
program.concat(',')
when 6
program.concat('[')
when 7
program.concat(']')
else
end
}
puts program
Refactorings
No refactoring yet !
JonM1827
July 31, 2008, July 31, 2008 20:20, permalink
I'm sure there are much better ways to do this. I never use ruby, so I don't really know the ins and outs of it. Just off the top of my head though, this will put it down from 35 lines to 15 lines.
length = 140
program = ""
chars = ['>','<','+','-','.','[',']']
ARGV.each do |n|
if n != nil and n != 0
length = n.to_i
end
end
length.downto(1) { |n|
program.concat(chars[rand(7)])
}
puts program
Maciej Piechotka
July 31, 2008, July 31, 2008 20:31, permalink
A little bit different working - but in 3 lines
# generate_bf.rb
# (copyleft) Matt Gauger 2008
# Randomly generates a brainfuck program
# Commandline argument is used as the length of the program
# Otherwise, the program defaults to 140 characters in length to fit on Twitter
# generated Brainfuck code not guaranteed to do anything !!
length = ARGV.last.to_i || 140
chars = %w{< > + - . , [ ]}
puts Array.new(length) {chars[rand(chars.length)]}.join
JonM1827
July 31, 2008, July 31, 2008 20:37, permalink
and that is why I need to learn ruby at some point :)
edit:
Just one change to make it work...
@Maciej ARGV.last is nil, but ARGV.last.to_i is 0...
-Jon
# generate_bf.rb
# (copyleft) Matt Gauger 2008
# Randomly generates a brainfuck program
# Commandline argument is used as the length of the program
# Otherwise, the program defaults to 140 characters in length to fit on Twitter
# generated Brainfuck code not guaranteed to do anything !!
length = ARGV.last ? ARGV.last.to_i : 140
chars = %w{< > + - . , [ ]}
puts Array.new(length) {chars[rand(chars.length)]}.join
mattgauger
July 31, 2008, July 31, 2008 20:57, permalink
Cool, I'm learning lots in a very short time span! :)
This is a superficial hack to add some of the example 'patterns' from the Brainfuck Wikipedia article, for more interesting generated programs. Of course, this breaks the nice ability to specify output length.
Any suggestions?
length = ARGV.last ? ARGV.last.to_i : 140
chars = %w{< > + - . , [ ] [-] ,[.,] >,[.>,] [->+<] >[-]<[->+<]}
# added 'patterns':
# [-] cell clear
# ,[.,] simple loop
# >,[.>,] moving the pointer
# [->+<] addition, destructive
# >[-]<[->+<] byte copy
puts Array.new(length) {chars[rand(chars.length)]}.join
JonM1827
July 31, 2008, July 31, 2008 21:11, permalink
I'm sure there is a much more elegant way to do this as it is generating far to many things, but there is always the lazy way of doing things... substring :)
length = ARGV.last ? ARGV.last.to_i : 140
chars = %w{< > + - . , [ ] [-] ,[.,] >,[.>,] [->+<] >[-]<[->+<]}
puts Array.new(length) {chars[rand(chars.length)]}.join[0, length]
abhi
September 5, 2008, September 05, 2008 15:07, permalink
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int a,i,n;
double b=0;
printf("enter a no.");
scanf("%d",&n);
for(i=0;i<=6;i++)
{
a=n%2;
n/=2;
b+=a*pow(10,i);
}
printf("%7.0f",b);
getch();
}
sigs
January 6, 2010, January 06, 2010 17:26, permalink
indent intended.
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int a,i,n;
double b=0;
char *s = "mehmeh";
printf("enter a no.");
scanf("%s",&s);
if (strcmp(s, "no"))
printf("You didn't enter a no )):");
else
printf("kthxmaet");
getch();
}
Cees Timmerman
November 20, 2011, November 20, 2011 05:36, permalink
Python 3 one-liner for 140 bytes of random bf code.
import random; print(''.join(random.choice('<>+-.,[]') for i in range(140)))
Cees Timmerman
November 20, 2011, November 20, 2011 05:37, permalink
Python 3 one-liner for 140 bytes of random bf code.
import random; print(''.join(random.choice('<>+-.,[]') for i in range(140)))
Cees Timmerman
November 20, 2011, November 20, 2011 05:38, permalink
Python 2.7+ one-liner for 140 bytes of random bf code.
import random; print(''.join(random.choice('<>+-.,[]') for i in range(140)))
Probably better ways to do this, quickly wrote it up after a joke in IRC..