Working with graphs - Kevin Bacon game
MRAB
python at mrabarnett.plus.com
Wed Jan 9 06:00:33 EST 2019
On 2019-01-09 09:53, jskako at gmail.com wrote:
> I am working on Kevin Bacon game.
>
> I have "movies.txt" text file that looks like:
>
> 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
>
It would be better if a function that returns a value _always_
explicitly returns a value:
return None
>
> def ucitajGraf(file):
> graph = []
> for line in file:
These lines:
> imeGlumaca = []
> mojaLinija = line.split(";")
> imeFilma = mojaLinija[0]
> for i in range (1,len(mojaLinija)):
> imeGlumaca.insert(len(imeGlumaca), mojaLinija[i])
>
do the same as:
mojaLinija = line.split(";")
imeFilma = mojaLinija[0]
imeGlumaca = mojaLinija[1 : ]
> 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)
It's recommended to use 'with':
with open("movies.txt") as f:
graf = ucitajGraf(f)
because it'll close the file immediately.
> print (graf)
>
> main()
>
>
> My problem is that when I print graph with "print (graph)" I am getting:
>
> "[<__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 but I don't know how to fix it and get "name" there.
>
> What would be the best way to perform breath-first search between two entered names?
>
More information about the Python-list
mailing list