Author Topic: Python Help  (Read 1926 times)

Offline Doomkiller

  • Ultra Heavyweight
  • Posts: 2112
  • Rep: 1
  • I HAS DOUBLE PISTOLS
    • View Profile
    • Awards
Python Help
« 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
"I make death fun!"
Quote from: NerdCubed
OH MY GOD IT'S JAWS!

Offline Pwnator

  • *
  • Posts: 6676
  • Rep: 15
  • Awards BOTM Winner
    • View Profile
    • http://pwnator.tumblr.com
    • Awards
  • See profile for gamer tags: Yes
Re: Python Help
« Reply #1 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?
Clash Cubes 1 - Grey Matter (Runner-Up)
King of Karnage - Sideshow Freak (Runner-Up, Best Engineered)
Rust In Pieces - Paper Cut 3 (Grand Champion, Most Dangerous Bot)
Wheely Tag Tournament - Ion Thruster (Grand Champion, along with Ounces' DiSemboweLment)
UK vs USA - Dark Striker (Grand Champion)
Rust In Pieces 2 - Claymore (Runner-Up, Favourite Bot)
BBEANS 6 - Infection 4 (Runner-Up)
RA2 Team Championships - Serious Business, Skeksis (Runner-Up, along with Scrappy, S_M, and Badnik)
RA2 Team Championships 2 - The Other Stig (Runner-Up, along with Scrappy, S_M, Badnik, 090901, and R1885)
Replica Wars 3 - Abaddon (Runner-Up, Luckiest Bot)
BroBots - wheebot & yaybot (Runner-Up)
Robo Zone 2 - Dipper (4th place, Survival Champion, & Best Axle Bot)
ARBBC - The Covenant (3rd place, BW Rumble Winner, Most Feared BW)

Offline Doomkiller

  • Ultra Heavyweight
  • Posts: 2112
  • Rep: 1
  • I HAS DOUBLE PISTOLS
    • View Profile
    • Awards
Re: Python Help
« Reply #2 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?
"I make death fun!"
Quote from: NerdCubed
OH MY GOD IT'S JAWS!

Offline Pwnator

  • *
  • Posts: 6676
  • Rep: 15
  • Awards BOTM Winner
    • View Profile
    • http://pwnator.tumblr.com
    • Awards
  • See profile for gamer tags: Yes
Re: Python Help
« Reply #3 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. >.<
Clash Cubes 1 - Grey Matter (Runner-Up)
King of Karnage - Sideshow Freak (Runner-Up, Best Engineered)
Rust In Pieces - Paper Cut 3 (Grand Champion, Most Dangerous Bot)
Wheely Tag Tournament - Ion Thruster (Grand Champion, along with Ounces' DiSemboweLment)
UK vs USA - Dark Striker (Grand Champion)
Rust In Pieces 2 - Claymore (Runner-Up, Favourite Bot)
BBEANS 6 - Infection 4 (Runner-Up)
RA2 Team Championships - Serious Business, Skeksis (Runner-Up, along with Scrappy, S_M, and Badnik)
RA2 Team Championships 2 - The Other Stig (Runner-Up, along with Scrappy, S_M, Badnik, 090901, and R1885)
Replica Wars 3 - Abaddon (Runner-Up, Luckiest Bot)
BroBots - wheebot & yaybot (Runner-Up)
Robo Zone 2 - Dipper (4th place, Survival Champion, & Best Axle Bot)
ARBBC - The Covenant (3rd place, BW Rumble Winner, Most Feared BW)

Offline Doomkiller

  • Ultra Heavyweight
  • Posts: 2112
  • Rep: 1
  • I HAS DOUBLE PISTOLS
    • View Profile
    • Awards
Re: Python Help
« Reply #4 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

:(

"I make death fun!"
Quote from: NerdCubed
OH MY GOD IT'S JAWS!

Offline nightcracker

  • *
  • Posts: 505
  • Rep: 7
  • Script kiddo
    • View Profile
    • NC Labs
    • Awards
  • Skype: orsonpeters
Re: Python Help
« Reply #5 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)

Offline Doomkiller

  • Ultra Heavyweight
  • Posts: 2112
  • Rep: 1
  • I HAS DOUBLE PISTOLS
    • View Profile
    • Awards
Re: Python Help
« Reply #6 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 = []
"I make death fun!"
Quote from: NerdCubed
OH MY GOD IT'S JAWS!

Offline nightcracker

  • *
  • Posts: 505
  • Rep: 7
  • Script kiddo
    • View Profile
    • NC Labs
    • Awards
  • Skype: orsonpeters
Re: Python Help
« Reply #7 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

Offline Doomkiller

  • Ultra Heavyweight
  • Posts: 2112
  • Rep: 1
  • I HAS DOUBLE PISTOLS
    • View Profile
    • Awards
Re: Python Help
« Reply #8 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
"I make death fun!"
Quote from: NerdCubed
OH MY GOD IT'S JAWS!

Offline nightcracker

  • *
  • Posts: 505
  • Rep: 7
  • Script kiddo
    • View Profile
    • NC Labs
    • Awards
  • Skype: orsonpeters
Re: Python Help
« Reply #9 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.

Offline Doomkiller

  • Ultra Heavyweight
  • Posts: 2112
  • Rep: 1
  • I HAS DOUBLE PISTOLS
    • View Profile
    • Awards
Re: Python Help
« Reply #10 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)

"I make death fun!"
Quote from: NerdCubed
OH MY GOD IT'S JAWS!

Offline nightcracker

  • *
  • Posts: 505
  • Rep: 7
  • Script kiddo
    • View Profile
    • NC Labs
    • Awards
  • Skype: orsonpeters
Re: Python Help
« Reply #11 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.

Offline Doomkiller

  • Ultra Heavyweight
  • Posts: 2112
  • Rep: 1
  • I HAS DOUBLE PISTOLS
    • View Profile
    • Awards
Re: Python Help
« Reply #12 on: September 03, 2011, 08:36:12 AM »
Ohh ok thanks

Also, yeah im used to actionscript atm, hence the // // comments :P
"I make death fun!"
Quote from: NerdCubed
OH MY GOD IT'S JAWS!