gametechmods

Off-Topic => Off-Topic Discussion => Topic started by: Doomkiller on September 03, 2011, 03:33:32 AM

Title: Python Help
Post by: Doomkiller on September 03, 2011, 03:33:32 AM
Anyone here good with python? (outside of Ra2)
I got this problem with this challenge:


Don't really want the solution, just want some help on to how to go about solving this. Did try the forum on the challenge website, but I have yet to recieve and answer :/

Also, im using python 2.6.5 in idle
Title: Re: Python Help
Post by: Pwnator on September 03, 2011, 03:43:24 AM
Your best bet is to make a flowchart. This is what I usually do when I'm trying to solve complex but recursive problems.

Also, are there any restrictions to your program?
Title: Re: Python Help
Post by: Doomkiller on September 03, 2011, 03:53:21 AM
Your best bet is to make a flowchart. This is what I usually do when I'm trying to solve complex but recursive problems.

Also, are there any restrictions to your program?

I have tried that, i know the general idea of how to do it, I just don't know how to get python to do it

Restrictions? by what do you mean?
Title: Re: Python Help
Post by: Pwnator on September 03, 2011, 04:23:33 AM
Forbidding the use of certain functions, certain data types, etc...

Our instructor pisses us off that way. >.<
Title: Re: Python Help
Post by: Doomkiller on September 03, 2011, 04:43:44 AM
Forbidding the use of certain functions, certain data types, etc...

Our instructor pisses us off that way. >.<

No restrictions as far as I know

btw, I has started a sortve code, that sortve works...
Code: [Select]
lines = int(raw_input("Number of lines: "))
start = raw_input("Start line: ")
firstline = start
nextline = []
print firstline
for i in range(lines-1):
    for j in range(len(start)-1):
        if (firstline[j-1] == "*" and firstline[j+1] == "."):
            nextline.append("*")
        elif (firstline[j-1] == "." and firstline[j+1] == "*"):
            nextline.append("*")
        else:
            nextline.append(".")
    firstline = "".join(nextline)
    print firstline
    nextline = []


It starts off good, going:
Number of lines: 8
Start line: ......*......
......*......
.....*.*....

but then:
Traceback (most recent call last):
  File "C:/Users/User/Documents/testingagain.py", line 10, in <module>
    elif (firstline[j-1] == "." and firstline[j+1] == "*"):
IndexError: string index out of range

:(

Title: Re: Python Help
Post by: nightcracker on September 03, 2011, 06:53:04 AM
It's not that hard, the problem is that the i-1 and i+1 go out of range. i-1 is already doing exactly what it should do for this program, access from the end. But if I have a list with 3 elements we want li[4] to go back to the beginning. I solve this by using the modulo operator.


Code: [Select]
def getnextrow(oldrow):
    size = len(oldrow)
    newrow = "." * size

    for i in range(0, size):
        char = "."
        if (oldrow[i - 1] == "*") != (oldrow[(i + 1) % size] == "*"): # modulo size so that the index wraps around
            char = "*"
        newrow[i] = char

    return newrow

firstrow = "..*.**.."
nextrow = getnextrow(firstrow)
print(nextrow)
Title: Re: Python Help
Post by: Doomkiller on September 03, 2011, 07:19:39 AM
Im not too sure how you did it night, care to elaborate more?

Also, I solved it anyway :P

Code: [Select]
lines = int(raw_input("Number of lines: "))
start = raw_input("Start line: ")
firstline = start
nextline = []
print firstline
for i in range(lines-1):
    for j in range(len(start)):
        if (firstline[j-1] == "*" and firstline[j-len(start)+1] == "."):
            nextline.append("*")
        elif (firstline[j-1] == "." and firstline[j-len(start)+1] == "*"):
            nextline.append("*")
        else:
            nextline.append(".")
    firstline = "".join(nextline)
    print firstline
    nextline = []
Title: Re: Python Help
Post by: nightcracker on September 03, 2011, 07:53:08 AM
Say we have a string row:

"..*.*"

Well basically, range(0, len(row) - 1) goes like this:

0 1 2 3

now, if we check their "neighbours" with -1 and +1 the range will turn into this:

-1 0 1 2 3 4

Because row[-1] equals row[len(row) - 1] it's fine. However, row[4] will give the IndexError: out of range. What do we want? We want 4 to go back to the beginning (row[0]). I did this with the modulo operator, you should read on it: http://en.wikipedia.org/wiki/Modulo_operation (http://en.wikipedia.org/wiki/Modulo_operation)
Title: Re: Python Help
Post by: Doomkiller on September 03, 2011, 08:00:26 AM
Oh, no I know what you mean by the modulo and how the neighbours worked, I meant when I looked at your code, i didn't quite understand what you were doing
Title: Re: Python Help
Post by: nightcracker on September 03, 2011, 08:14:03 AM
Oh, no I know what you mean by the modulo and how the neighbours worked, I meant when I looked at your code, i didn't quite understand what you were doing

Quote my code and add comments to the parts you don't understand and I'll try to explain it.
Title: Re: Python Help
Post by: Doomkiller on September 03, 2011, 08:16:58 AM
Code: [Select]
//what does def do?//
def getnextrow(oldrow):
    size = len(oldrow)
    newrow = "." * size

    for i in range(0, size):
        char = "."
          //and what happens here?//
        if (oldrow[i - 1] == "*") != (oldrow[(i + 1) % size] == "*"): # modulo size so that the index wraps around
            char = "*"
        newrow[i] = char

    return newrow

firstrow = "..*.**.."
nextrow = getnextrow(firstrow)
print(nextrow)

Title: Re: Python Help
Post by: nightcracker on September 03, 2011, 08:30:42 AM
Comments in python are preceded with #, not //.

Code: [Select]
def function_name(argument1, argument2):
    # code here

creates a new function that can be called and can return things. Like this:

Code: [Select]
def add_one(number):
    return number + 1

print(add_one(4)) # this will print out 5

What happens here is a bit more complicated:

Code: [Select]
if (oldrow[i - 1] == "*") != (oldrow[(i + 1) % size] == "*"):
Basically I'm first asking two questions (the two parts in the brackets): is my topleft neighbour a star? and is my topright neighbour a star? Then I check with != if the answer to those two questions is not the same.
Title: Re: Python Help
Post by: Doomkiller on September 03, 2011, 08:36:12 AM
Ohh ok thanks

Also, yeah im used to actionscript atm, hence the // // comments :P