Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 [2] 3

Author Topic: The Place For Any Programs You Wrote That Aren't Really Worthy Of Anything  (Read 4835 times)

Berserker

  • Bay Watcher
    • View Profile
Re: The Place For Any Programs You Wrote That Aren't Really Worthy Of Anything
« Reply #15 on: December 12, 2009, 02:34:06 pm »

I made a self-modifying Python program.

Code: [Select]
A = 1

selfname = 'program.py'

print 'Self-modifying program'
print 'This program has been started '+str(A)+' times.'

# Read the program into a list
prog = [line.rstrip() for line in open(selfname)]

# Modify the first line
prog[0] = "A = "+str(A+1)

# Write the modified program back to the file
out = open(selfname,'w')
for line in prog:
    out.write(line + '\n')

out.close()

raw_input("Press any key to quit.")

Logged

alfie275

  • Bay Watcher
    • View Profile
Re: The Place For Any Programs You Wrote That Aren't Really Worthy Of Anything
« Reply #16 on: December 12, 2009, 09:15:15 pm »

SKYNET!!! Kill it with fire!!!!!
Logged
I do LP of videogames!
See here:
http://www.youtube.com/user/MrAlfie275

timmeh

  • Bay Watcher
    • View Profile
    • My Portfolio
Re: The Place For Any Programs You Wrote That Aren't Really Worthy Of Anything
« Reply #17 on: December 12, 2009, 11:48:07 pm »

SKYNET!!! Kill it with fire magma!!!!!
Fix'd, this is the DF forum after all :P

That's awesome though Berserker!  What would be really crazy would be making the program recursive.  I.E. It starts itself again after it modifies itself, until A==5 or something.  I'll have to get python set back up on my computer once I get it back from repairs, I can think of some really weird things to do with that sort of program...
Logged
On the Wall is a Masterfully engraved carving of Urist McHardcastle and Goblins. Urist McHardcastle is surrounded by the Goblins. The Golbins are stamping on Urist McHardcastle. Urist McHardcaste is laughing at the Goblins. The carving related to the prolonged and bloody death of Urist McHardcastle in the Fall of 1659, the Winter of 1659, and the Spring of 1660. On the engraving is an image of Cheese.

Berserker

  • Bay Watcher
    • View Profile
Re: The Place For Any Programs You Wrote That Aren't Really Worthy Of Anything
« Reply #18 on: December 13, 2009, 05:01:07 am »

Executing code on someone else's computer.

Logged

alfie275

  • Bay Watcher
    • View Profile
Re: The Place For Any Programs You Wrote That Aren't Really Worthy Of Anything
« Reply #19 on: December 13, 2009, 09:02:08 am »

This program would probably break itself on first run:

Code: [Select]
selfname = 'program.py'
import random

print 'Self-modifying program'


# Read the program into a list
prog = [line.rstrip() for line in open(selfname)]

max = random.randrange(0, 5)
num = 0
while num < max:
# Modify a random line
line = random.randrange(0,len(prog)
prog[line][random.randrange(0,len(prog[line]))] = random.randrange(32,125)
num = num + 1

# Write the modified program back to the file
out = open(selfname,'w')
for line in prog:
    out.write(line + '\n')

out.close()

raw_input("Press any key to quit.")

[/quote]
Logged
I do LP of videogames!
See here:
http://www.youtube.com/user/MrAlfie275

eerr

  • Bay Watcher
    • View Profile
Re: The Place For Any Programs You Wrote That Aren't Really Worthy Of Anything
« Reply #20 on: December 14, 2009, 08:39:03 pm »

This program would probably break itself on first run:

Code: [Select]
selfname = 'program.py'
import random

print 'Self-modifying program'


# Read the program into a list
prog = [line.rstrip() for line in open(selfname)]

max = random.randrange(0, 5)
num = 0
while num < max:
# Modify a random line
line = random.randrange(0,len(prog)
prog[line][random.randrange(0,len(prog[line]))] = random.randrange(32,125)
num = num + 1

# Write the modified program back to the file
out = open(selfname,'w')
for line in prog:
    out.write(line + '\n')

out.close()

raw_input("Press any key to quit.")

[/quote]
Yet with a minimum of modification it probably meets the skynet possibilities minimum (tm)
Logged

Alexhans

  • Bay Watcher
  • This is toodamn shortto write something meaningful
    • View Profile
    • Osteopatia y Neurotonia
Re: The Place For Any Programs You Wrote That Aren't Really Worthy Of Anything
« Reply #21 on: December 15, 2009, 08:35:54 am »

qwertyuiopas, I'm curious about this... http://www.bay12games.com/forum/index.php?topic=46025.msg915819#msg915819
I don't really understand what is it supposed to do and how would it work.
Executing code on someone else's computer.


This is so cool... I need to learn sockets and TCP/IP sometime (amongst other networking stuff).
Logged
“Eight years was awesome and I was famous and I was powerful" - George W. Bush.

Berserker

  • Bay Watcher
    • View Profile
Re: The Place For Any Programs You Wrote That Aren't Really Worthy Of Anything
« Reply #22 on: December 15, 2009, 11:43:24 am »

Here is a program that lets the user write itself:

Code: [Select]
print '1. Modify this program'
print '2. Run this program'
choice = int(raw_input('> '))

selfname = 'writeself.py'

if choice == 1:
    # Read the first 22 lines into a list
    prog = [line.rstrip() for line in open(selfname)][:21]

    # Let the user write the rest
    line = raw_input()
    while line != 'end':
        prog.append(' '*4+line)
        line = raw_input()

    f = open(selfname, 'w')
    map(lambda x: f.write(x+'\n'), prog)
    f.close()

if choice == 2:
    # Choice 1 starts editing from here
    pass
Logged

qwertyuiopas

  • Bay Watcher
  • Photoshop is for elves who cannot use MSPaint.
    • View Profile
    • uristqwerty.ca, my current (barren) site.
Re: The Place For Any Programs You Wrote That Aren't Really Worthy Of Anything
« Reply #23 on: December 15, 2009, 06:06:40 pm »

qwertyuiopas, I'm curious about this... http://www.bay12games.com/forum/index.php?topic=46025.msg915819#msg915819
I don't really understand what is it supposed to do and how would it work.

It's more of a fragment, also looks somewhat sloppy, but it DOES get around having to either use a goto or copy code unnessecarily.
Logged
Eh?
Eh!

winner

  • Bay Watcher
    • View Profile
Re: The Place For Any Programs You Wrote That Aren't Really Worthy Of Anything
« Reply #24 on: December 17, 2009, 10:30:13 pm »

a c++ program I wrote to study positive assortive breeding, it runs from the command line.
Spoiler (click to show/hide)
Logged
The great game of Warlocks!

alway

  • Bay Watcher
  • 🏳️‍⚧️
    • View Profile
Re: The Place For Any Programs You Wrote That Aren't Really Worthy Of Anything
« Reply #25 on: December 20, 2009, 08:50:40 pm »

I have a genetic algorithm I wrote in TI Basic on my TI84 Plus Silver calculator which find a min or max for a function (or the zero point if you put in the absolute value of the function). Although it takes a few minutes to come up with the ideal solution.

That was my lesson in why variables should always be named descriptively (I used somewhere around 25 or 26 of the available letters, since variable names are 1 character long on the calculator). And why you should never use TI Basic to program anything.
Logged

Bricks

  • Bay Watcher
  • Because you never need one brick.
    • View Profile
Re: The Place For Any Programs You Wrote That Aren't Really Worthy Of Anything
« Reply #26 on: December 20, 2009, 11:51:51 pm »

How did that work, alway?  Did you take a population of test points and then "evolve" them?
Logged
EMPATHY - being able to feel other peoples' stuff.

alway

  • Bay Watcher
  • 🏳️‍⚧️
    • View Profile
Re: The Place For Any Programs You Wrote That Aren't Really Worthy Of Anything
« Reply #27 on: December 21, 2009, 08:11:30 pm »

Yep, took a population of I think 8 points (stored in matrices, limiting the number available, since the calculator only has 10 available, at least 2 of which needed to be kept empty for use in the selection stage to avoid programming headach or unintentional loss of specimens), ran em through the function provided and used that number to do fitness testing, highest/lowest 3 were caried on to next step. These three were then reproduced with one another; one decimal place was a single gene, with it being programmed for 4 decimals of accuracy, making it 5 total genes. Reproduction involved cloning of the top 3, then mutating those clones, as well as combining pairs of the three w/ mutation. It had elitism as well, keeping a copy of the best solution unmutated.
3 best: A, B, C
after reproduction: A, Am, Bm, Cm, ABm, ACm, BCm, ABCm*

It did have some major problems though brought about by the way it was programmed. Since each decimal was a gene, going from 0.9999 to a correct solution of 1 is improbable; likewise, from .0999 to .1, ect are also improbable. However, I never bothered fixing it since it runs to slowly on a calculator to be of much practical use anyway, except in very rare cases. One generation takes about 1 to 2 seconds to go through, which is why it takes so long to find a solution.

The way I wrote it, the genetic algorithm itself is a massive multi-hundred line code, with the function being tested put into a seperate program for ease of access. Without too much alteration, it could probably be used for other genetic algorithm-y purposes, albiet ones which would take a very long time.

Oh, and I nearly forgot another experiment I carried out with it... You see, it was taking quite a while to come up with the correct answers, as I have stated before. So, I rigged up a much more simple genetic algorithm which would run that genetic algorithm over and over, modifying its mutation rate. Overall, it took about 8 hours to run through it completely, but by the end of running it three times the ideal mutation rate was nearly identical(margin of difference of about 2%), and so I now use that as my mutation rate (about 26% btw, although it would certainly vary from one GA to another).  ;D

*threesome on a calculator, bow chicka bow wow
« Last Edit: December 21, 2009, 08:24:24 pm by alway »
Logged

Lord Dullard

  • Bay Watcher
  • Indubitably.
    • View Profile
    • Cult: Awakening of the Old Ones
Re: The Place For Any Programs You Wrote That Aren't Really Worthy Of Anything
« Reply #28 on: December 22, 2009, 03:52:09 am »

I've just recently started learning Python this week. As a little trial for myself, I wrote a tiny program that takes a year (any year will work, it doesn't matter if it's one, four, or twenty digits long) and turns it into a Roman numeral.

For those without Python, it's here as a compiled executable (22kb).

For those who are interested in the code:

Spoiler (click to show/hide)

Before anyone points it out to me, I know this code is remarkably inefficient. For example, I could have turned the functions to parse years, decades, and centuries into a single more all-purpose function quite easily (by adding just a few more variables to it). I also could have eliminated quite a lot of the code using regular expressions. However, I can't really be bothered to mess with it anymore - this was just a very simple exercise for me to see if I could master the language syntax.

If it helps anyone who's trying to learn, or if anyone would like to practice on it by slimming it down, feel free.
« Last Edit: December 22, 2009, 03:54:53 am by Lord Dullard »
Logged

shengii

  • Bay Watcher
    • View Profile
Re: The Place For Any Programs You Wrote That Aren't Really Worthy Of Anything
« Reply #29 on: December 22, 2009, 07:04:28 pm »

Simple Dice Rolling Program.
Kind of messy if you decide to go above ~20 sides.



Spoiler: Source code (click to show/hide)
Logged
Pages: 1 [2] 3