Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python help thread?
#41
Fiel Wrote:Start with -1, not with 0

Code:
word = 'crap'
for index in range(-1, -len(word)-1, -1):
    print(word[index]),

That works to make it horizontal, which is the biggest problem, but I'm still getting that pesky double front letter and the traceback. Hurt

Code:
Word: How in the world?
? d l r o w   e h t   n i   w o H H

Traceback (most recent call last):
  File "G:\python\backwards.py", line 11, in <module>
    backwards_word(words)
  File "G:\python\backwards.py", line 5, in backwards_word
    letter = words[index]
IndexError: string index out of range
Reply
#42
Post your code again?
Reply
#43
Also, appreciate the help. Biggrin
Code:
def backwards_word(words):
    for index in range(-1, -len(words)-1, -1):
        print (words[index]),
    while index <= len(words):
        letter = words[index]
        print letter
        index -= 1
        
words = raw_input("Word: ")

backwards_word(words)
Reply
#44
Comment out the while loop. >_>;;;
Reply
#45
Fiel Wrote:Comment out the while loop. >_>;;;

Alright, I broke the loop. Still showing that fucking double letter pomegranate though..
Reply
#46
How is that possible? I commented it out on mine and it works fine.

POST CODE
Reply
#47
By commenting out you meant breaking the while, right?
Code:
def backwards_word(words):
    for index in range(-1, -len(words)-1, -1):
        print (words[index]),
    while index <= len(words):
        letter = words[index]
        print letter
        index -= 1
        break
        
words = raw_input("Words: ")

backwards_word(words)
Reply
#48
Nope. Use "#" before the lines. A # is a comment

Code:
def backwards_word(words):
    for index in range(-1, -len(words)-1, -1):
        print (words[index]),
#    while index <= len(words):
#        letter = words[index]
#        print letter
#       index -= 1
#        break
Reply
#49
Fiel Wrote:Nope. Use "#" before the lines. A # is a comment

Code:
def backwards_word(words):
    for index in range(-1, -len(words)-1, -1):
        print (words[index]),
#    while index <= len(words):
#        letter = words[index]
#        print letter
#       index -= 1
#        break

Hmm. Thanks! I have no clue why that works.. Time to do some research.
Reply
#50
Interestingly enough, this latest problem helps me with my new assignment: Palindromes. Have to check a file and check for perfect, imperfect, and no palindromes.
Reply
#51
JPTheMonkey Wrote:Interestingly enough, this latest problem helps me with my new assignment: Palindromes. Have to check a file and check for perfect, imperfect, and no palindromes.

That was one of the assignments in the reading I was doing. Weird.
Reply
#52
Alley Wrote:That was one of the assignments in the reading I was doing. Weird.
Excellent

I thought of SP when I checked out what was in the file we were checking for palindromes.
"No sir away, there is a papaya war on." (Or very close to that, definitely had "papaya war").
Reply
#53
I've been having some trouble trying to figure what I'm supposed to do for a portion of my program. More specifically, my program needs to be able to do "Markov Analysis."

If the term doesn't seem familiar, it essentially means my program needs to be able to dissect a sentence (or paragraph) and reassemble it using its given prefixes and suffixes. For example, the sample sentence I put up:
Quote:to be or not to be that is the question

A prefix would be the word before the next word and the suffix is the one coming after. Ex. "be" is the prefix and "or" or "that" could be the potential suffix. Which is the part I'm having problems with, I don't really have an idea of how the reassembling part should be like.

Here's my code thus far:
Code:
# Lab 9 - Markov Analysis
# CMPUT 174 W11

import random
import sys

def shift (prefix, word):
    """
    Pops a word out of a tuple and pushes another word in
    """
    return prefix[1:] + (word,)

def readWords(fin):
    """
    Reads the file into the words list
    """
    wordsList = []
    for line in fin:
        wordsList.extend(line.split(' '))
    return wordsList

def analyze(wordsList,suffixes):
    """
    Perform the Markov Analysis
    """
    possibleSuffixes = dict()
    # Your code goes here
    
    prefix = []
    prefix.append((wordsList[0],wordsList[1]))
    
    for i in range(len(wordsList)-2):
        if prefix[i] not in possibleSuffixes:
            possibleSuffixes[prefix[i]] = [wordsList[i+2]]
        else:
            possibleSuffixes[prefix[i]].append(wordsList[i+2])
        prefix.append((wordsList[i+1],wordsList[i+2]))
        
    print 'possible suffixes:',possibleSuffixes
    print 'prefixes', prefix

    return possibleSuffixes

def generate(suffixes, prefix):
    """
    Generate text based on the known suffixes
    """
    generatedText = prefix[0] + " " + prefix[1]
    prefix = prefix[0],prefix[1]
    print "First pair: ", prefix

    # Your code goes here
    i = 1
    suffix = prefix[1]
    print suffix
    while i != 0:
        if  suffix not in prefix[i]:
            i = 1
            exit()
        else:
            suffix = suffixes.get(prefix)
            if len(suffix) > 1:
                suffix = [random.choice(suffix)]
                print "Random choosen:",suffix
            prefix = shift(prefix,suffix[0])
            print "Prefixes after:",prefix
            print "Suffix:", suffix
            i += 1
            
            generatedText = generatedText + " " + suffix[0]
    return generatedText

if __name__ == "__main__":
    # The input file as a list of words in order
    #filename = sys.argv[1]
    filename = "Input.txt"
    fin = open(filename)
    words = readWords(fin);
    
    suffixes = dict()

    # Do the analysis and return the dictionary that maps every tupple into a prefix word
    suffixes = analyze(words, suffixes)
    
    # Pick a starting prefix at random
    startingPrefix = random.choice(suffixes.keys())

    # Generate the text and print it
    print generate(suffixes, startingPrefix)

Disregard what I currently have under "def generate" since my code doesn't work but it's what I have thought up so far. "def analyze" should be already complete (but hey, if you can optimize it further go ahead!).

If my explanation sucks butt, here's copypasta from the TA for the objective:
 Markov Analysis
[Spoiler=General format of how "Generate" should work]
Start with the given prefix
Select at random a possible suffix that comes after this prefix (use random.choice([collection goes here]) to pick at random)
Print or store the new word and use it to form a new prefix
Repeat

Now I don't think we're required to have it work with punctuation as well, but if you guys can get that to work too, I'd be really thankful. It'd really help with my learning process Big Grin
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)