www.robowars.org

RoboWars Australia Forum Index -> Off-Topic

python help
Goto page Previous  1, 2

Post new topic   Reply to topic
  Author    Thread
Valen
Experienced Roboteer


Joined: 07 Jul 2004
Posts: 4436
Location: Sydney


 Reply with quote  

the rules .txt file is text only
you need to read through that text file and parse the lines for the variables yourself
I'd do something kind of like this

(just the juicy parts, all this needs wrapping

code:


#initalise the dictionary
variables = {} #create dictionary to hold text variables
read through rules.txt
split each line on " "
for each word in the split array, see if it starts with a "<"
if it does then
    variables{word} = ""


#actual program stuff
    convert input string to lower case and remove all punctuation
    split input string into array on space  (use split function)
    call it user_input

    open rules.txt
    building = True
    while building = true
        read line from file
        split line into array on |  #convert input line into request and response, one sets variables one gets them
        request = split(inputline[0]," ")  # split lines into words
        response = split(inputline[1]," ")
        wordnum = 0
        comparing = True
        match = False
        while  comparing #this part deals with the request
              if userinput[currentword] == request[currentword]
                   print "matched %s" % (currentword)
              elseif the first letter of word is "<"   # then its a variable
                   if variables[word] = "" then
                        variable[word] = input[currentword] #set the variable to the thing the user entered
               else
                    print "It didnt match"
                    building = False
       if current_word = len(user_input) then
            print "Matched the whole string"
            building = False
            match = True
       current_word +=1




if you get out of that with match = true then response holds what you need to give back, you then just need to check each word as it goes out for a < and if it is append the variable to the output string rather than the word itself.
_________________
Mechanical engineers build weapons, civil engineers build targets

Post Sun Aug 31, 2008 11:22 am 
 View user's profile Send private message Visit poster's website AIM Address Yahoo Messenger MSN Messenger ICQ Number
cerberus3112



Joined: 05 Dec 2005
Posts: 497
Location: Mt Druitt,Sydney,NSW


 Reply with quote  

The python challenge is over i ended on 189/200 couldn't get that last one I was so close but ran out of time Crying or Very sad

code:
# section that finds the variables in the rule file
import re

variables = {} #create dictionary to hold text variables
for line in open("rules.txt"):
    line = line.rstrip('\n')
    stripped_text = "" 
    for c in line:
        if c in '!"#$%&\'()*+,-./:;=?@[\\]^_`{}~':
            c = ""
        stripped_text += c
    words = stripped_text.split(" ")
    for word in words:
        m = re.findall('<.*?>', word)
        m = ''.join(m)
        if m not in variables:
            variables[m] = ''

# user_input loop

running = True
while running  == True:
    user_input = raw_input(">")
    if user_input == "":
        break
    words = user_input.split(" ")
    z = 0
    answer = []
    ended = False

    # this is the compairing and output section
    for line in open("rules.txt"):
        built = False
        matched = 0
        while not built :
            line = line.rstrip('\n')
            split = line.split(" | ")
            request = split[0].split(" ")
            response = split[1].split(" ")
            compaired = False
            matched = 0
            currentword = 0
            while not compaired :
                m = re.findall('<.*?>', request[currentword])
                m = ''.join(m)
                if words[currentword] == request[currentword]:
                    if m != '':
                        variables[m] = words[currentword]
                    matched += 1
                elif m != '':
                    variables[m] = words[currentword]
                    matched += 1
# this is where it checks wether the whole thing matched and if the varialbes are not empty it also makes the final answer
                if currentword == len(words)-1:
                    built = True
                    compaired = True
                    broken = False
                    if matched == len(request):
                        answer = []
                        for word in response:
                            if not broken:
                                m = re.findall('<.*?>', word)
                                m = ''.join(m)
                                if m == '':
                                    answer.append(word)
                                else:
                                    if variables[m] == '':
                                        broken = True
                                        compaired = True
                                    else:
                                        h = re.sub('<.*?>',variables[m], word)
                                        answer.append(h)       
                        print ' '.join(answer)
                        z = 1
                        built = True
                        compaired = True
                    built = True
                    compaired = True
                elif currentword == len(request)-1 or len(request) != len(words):
                    built = True
                    compaired = True
                else:
                    currentword += 1
    if z != 1:
        print "Please go on"
         






works fine except it prints all the ones that match the request pattern and that it has trouble handling the variables
_________________
A journey of a million miles begins with a single step followed by a hell of a lot of other steps so get walking

Post Mon Sep 01, 2008 12:22 am 
 View user's profile Send private message Send e-mail
  Display posts from previous:      

Forum Jump:
Jump to:  

Post new topic   Reply to topic
Page 2 of 2

Goto page Previous  1, 2

Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Last Thread | Next Thread  >
Powered by phpBB: © 2001 phpBB Group
millenniumFalcon Template By Vereor.