1 2 3 4 5 6
# 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 !!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
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.
[ruby]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
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
1 2 3 4 5 6 7 8 9 10 11
# 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
1 2 3 4 5 6 7 8 9 10 11
# 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?
1 2 3 4 5 6 7 8 9 10 11 12
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 :)
1 2 3
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#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(); }
Probably better ways to do this, quickly wrote it up after a joke in IRC..