Bottom Page

Thread Rating:
  • 2 Vote(s) - 4.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Kevin Bacon game with breath-first search
I am more-less new at Python and I am working on Kevin Bacon game.

I have "movies.txt" text file that looks like:

Quote:Apollo 13 (1995);Bill Paxton;Tom Hanks;Kevin Bacon
Begyndte ombord, Det (1937);Aage Schmidt;Valso Holm
Bersaglio mobile (1967);Dana Young;Bebe Drake
Bezottsovshchina (1976);Yelena Maksimova;Lev Prygunov
Dark, The (1979);Angelo Rossitto;William Devane

So in first row we have movie name and in other rows we have actors for that movie.

I am trying to make Kevin Bacon game with breath-first search.

My source code (Python 3.X):

class cvor:
    __slots__ = ('ime','susjed')
def kreiranjeCvora(ime):
    n = cvor()
    n.ime = ime
    n.susjed = []
    return n

def pronadiCvor(cvorlist, ime):
    for n in cvorlist:
        if n.ime == ime:
            return n
def ucitajGraf(file):  
    graph = []
    for line in file:
        imeGlumaca = []
        mojaLinija = line.split(";")
        imeFilma = mojaLinija[0]
        for i in range (1,len(mojaLinija)):
            imeGlumaca.insert(len(imeGlumaca), mojaLinija[i])           

        cvorFilm = pronadiCvor(graph, imeFilma)
        if cvorFilm == None:
            cvorFilm = kreiranjeCvora(imeFilma)
        for glumac in imeGlumaca:
            glumacCvor = pronadiCvor(graph,glumac)
            if glumacCvor == None:
                glumacCvor = kreiranjeCvora(glumac)
    return graph

def main():
    f = open("movies.txt")
    graf = ucitajGraf(f)
    print (graf)    
My problem is that when I print graph with "print (graph)" I am getting:

Quote:"[<__main__.cvor object at 0x000001475275EBE0>, <__main__.cvor object at 0x000001475275EEF0>, <__main__.cvor object at 0x000001475275EFD0>, <__main__.cvor object at 0x000001475275EE80>, <__main__.cvor object at 0x000001475275EB70>, <__main__.cvor object at 0x000001475275ED68>,..."

And I know why (I think) but I don't know how to fix it and get correct "names" there.

What would be the best way to perform breath-first search between two entered names and what should be my next step?
What you are seeing is the default text representation (repr) of the object you have created. That may be the correct answer, but it's not showing you the information to determine that. You need to override the __repr__ method of cvor so that it gives you better info. Your cvor (I'm not sure if this is the actor or the film) class is very bare bones, I would expand it:

class cvor(object):

    def __init__(self, ime):
        self.ime = ime
        self.susjed = []

    def __repr__(self):
        return '<cvor {} ({})>'.format(self.ime, len(self.susjed))
Now you don't need kreiranjeCvora. You can just do cvorFilm = cvor(imeFilma). Also, the output you get should be more intelligible.

BTW, it's breadth-first search, with a d. Breath is taking in air through your mouth and nose, breadth is how wide something is.
Craig "Ichabod" O'Brien -
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures

There are a couple ways to correct this. The first way is calling the cvor's attribute that stores the name (ime, I believe):

for movie in graf:
To simplify that, you could create a function that does the same thing and use map():

def return_movie_name(movie):
    return movie.ime

print(map(return_movie_name, graf))
Or, you can add __str__() and __repr__() methods to cvor to return the movie name when the object is printed:

class cvor:
    __slots__ = ('ime','susjed')

    def __str__(self):
        return self.ime

    def __repr__(self):
        return self.__str__()


Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Breadth-First-Search MontyPython 2 376 Dec-01-2018, 11:23 PM
Last Post: MontyPython
  Help With Function to search a list johnissa 2 498 Apr-24-2018, 08:08 AM
Last Post: johnissa
  Word search in grid Skelbo 9 1,045 Dec-22-2017, 09:36 PM
Last Post: Terafy
  program that search string in text file and do something alon30 1 856 Aug-04-2017, 08:10 AM
Last Post: buran
  Linear Search just_started_python 2 1,322 Nov-17-2016, 12:05 PM
Last Post: heiner55

Forum Jump:

Users browsing this thread: 1 Guest(s)