Python Forum
Kevin Bacon game with breath-first search - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: Kevin Bacon game with breath-first search (/thread-15237.html)



Kevin Bacon game with breath-first search - gamingingrs - Jan-09-2019

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
etc,...

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)
            graph.append(cvorFilm)
        for glumac in imeGlumaca:
            glumacCvor = pronadiCvor(graph,glumac)
            if glumacCvor == None:
                glumacCvor = kreiranjeCvora(glumac)
                graph.append(glumacCvor)
            glumacCvor.susjed.append(cvorFilm)
            cvorFilm.susjed.append(glumacCvor)
    return graph


def main():
    f = open("movies.txt")
    graf = ucitajGraf(f)
    print (graf)    
    
main()    
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?


RE: Kevin Bacon game with breath-first search - ichabod801 - Jan-09-2019

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.


RE: Kevin Bacon game with breath-first search - stullis - Jan-09-2019

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:
    print(movie.ime)
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__()

print(graf)