Now that HW 4 is closed, can someone explain 4.7 PLEASE??

1
1

I have an idea how the record_user_click should work, but I'm still hung up on trying to make sure the index structure is correct in the first place. Would someone be so kind as to help me with this part, please?

asked 20 Mar '12, 20:06

Chris%20Allan's gravatar image

Chris Allan
30521023
accept rate: 0%

edited 20 Mar '12, 20:07


4 Answers:
def record_user_click(index,keyword,url):
for entry in index:
    if entry[0]==keyword:
        for e in entry[1]:
            if e[0]==url:
                e[1]=e[1]+1

and

def add_to_index(index, keyword, url):
    for entry in index:
        if entry[0] == keyword:
            entry[1].append([url,0])
            return
    # not found, add new keyword to index
    index.append([keyword,[[url,0]]])
link

answered 20 Mar '12, 20:11

Tom%20Vandenbosch's gravatar image

Tom Vandenbosch
6.7k3982132

Thank you so much! :D I wasn't adding the extra set of [ ] around url,0 which caused me to have problems.

(20 Mar '12, 20:14) Chris Allan Chris%20Allan's gravatar image

Interesting, I also forgot to add them initially, but then I got an error with some tests.

(20 Mar '12, 20:17) Tom Vandenbosch Tom%20Vandenbosch's gravatar image

I've been banging my head against the wall for 3 days because in my head, I knew how I wanted the structure to be, but was having a problem writing it in Python code.

(20 Mar '12, 20:19) Chris Allan Chris%20Allan's gravatar image

In a way, you should get at least 60% of the marks for such an answer. But yes, a grading bot is a grading bot.

(20 Mar '12, 20:22) Tom Vandenbosch Tom%20Vandenbosch's gravatar image

Kinda reminds me of my 7th grade math teacher in that aspect, then.. LOL

(20 Mar '12, 20:30) Chris Allan Chris%20Allan's gravatar image

LOL - and thanks for being so generous with karma @atomicxblue

(20 Mar '12, 20:32) Tom Vandenbosch Tom%20Vandenbosch's gravatar image

You're quite welcome! You helped me so, I felt I should return the favor.

(20 Mar '12, 21:38) Chris Allan Chris%20Allan's gravatar image

Tom's answer is the same as mine, be sure to watch the solution video (click Next on 4.7) if this still doesn't make sense.

link

answered 20 Mar '12, 20:13

zyrcster-4's gravatar image

zyrcster-4
1.8k1332

one of the possible ways to implement an index structure is as in hw4.1:

[ [ keyword, [ [url, count], [url, count], … ] ], … ]

link

answered 20 Mar '12, 20:10

Evaldas's gravatar image

Evaldas
16736

Just a slight variation to avoid duplication of key words & urls in the index:

def record_user_click(index,keyword,url):
    for e in index:
        if e[0] == keyword:
            for x in e[1]:
                if x[0] == url:
                    x[1] = x[1] + 1
    return index

def add_to_index(index, keyword, url):
    exclude = True
    for entry in index:
        count = 0
        if entry[0] == keyword:
            exclude = False
            for x in entry [1]:
                if x[0] == url:
                    count = count + 1
            if count == 0:        
                entry[1].append([url,0])
                return
    if exclude:            
        index.append([keyword, [[url,0]]])
link

answered 20 Mar '12, 20:34

Arvind%20Deogirikar-2's gravatar image

Arvind Deogi...
6571727

Your answer
Question text:

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "Title")
  • image?![alt text](/path/img.jpg "Title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Tags:

×15,212
×60

Asked: 20 Mar '12, 20:06

Seen: 271 times

Last updated: 20 Mar '12, 21:38