0

Hello I was given a problem where I had to take a list of lists and find individual bands within those lists and see if they all have a common favorite band. If so I am supposed to output true. I need to follow this method of programming where I modularize my code, but I cannot seem to get it. Here is my code so far. Thank you for all help that you can give.

favoriteBandLists = [["Metallica","Linkin Park","Alice In Chains","Nirvana", "Soundgarden"],
    ["Pink Floyd","Alice In Chains","Soundgarden","Metallica","Linkin Park"],
    ["Audioslave","Offspring","The Beatles", "Soundgarden"]]

def commonFavoriteBand(favoriteBandLists):

    thereExists= False
    for i in (favoriteBandLists[2]):
        if(commonFavoriteBandA(favoriteBandLists)):
            thereExists = True
    return (thereExists)

def commonFavoriteBandA(favoriteBandLists):

    foundCounterExampleYet = False
    for band in favoriteBandLists[2]:
        if not(band == favoriteBandLists[0:1]):
            foundCounterExampleYet = True
    return not foundCounterExampleYet

print(commonFavoriteBand(favoriteBandLists))
MattDMo
  • 100,794
  • 21
  • 241
  • 231

2 Answers2

1

Use intersect from the set object

set(["Metallica","Linkin Park","Alice In Chains","Nirvana", "Soundgarden"]).intersection(["Pink Floyd","Alice In Chains","Soundgarden","Metallica","Linkin Park"])
set(['Linkin Park', 'Alice In Chains', 'Soundgarden', 'Metallica'])

EDIT

If the list needs to be traversed you can use any of the list traversing functions like, map, filter or reduce.

favoriteBandLists = [["Metallica","Linkin Park","Alice In Chains","Nirvana", "Soundgarden"],
    ["Pink Floyd","Alice In Chains","Soundgarden","Metallica","Linkin Park"],
    ["Audioslave","Offspring","The Beatles", "Soundgarden"]]
reduce(lambda a, b: a.intersection(b), (set(a) for a in favoriteBandLists))
set(['Soundgarden'])
markcial
  • 9,041
  • 4
  • 31
  • 41
  • I am not able to do that, the lists given at the top must stay the way they are. – Nota4rizzle Dec 18 '14 at 17:08
  • Try: `bool(reduce(lambda x, y: x.intersection(y), [set(l) for l in favoriteBandsList]))` – tmr232 Dec 18 '14 at 17:10
  • You don't need to redefine the lists, and you can use them to create sets. You can do `set(favoriteBandLists[0])` to get the first list as a set, for example – David Reeve Dec 18 '14 at 17:11
0

If you really want to write something that shows that you're modularizing your code, first make a function that returns the common element in two lists:

def commonBand(L1, L2):
    answer = []
    for band in L1:
        if band in L2:
            answer.append(band)
    return answer

Now, call that function many times, repeatedly:

def main(listOfLists):
    i = 1
    answer = listOfLists[0]
    while i<len(listOfLists):
        answer = commonBand(answer, listOfLists[i])
        if not answer:
            break
        i += 1
    return answer

Output:

In [193]: main(favoriteBandLists)
Out[193]: ['Soundgarden']

Note: this seemed like a homework question to me, so my code is conducive to that. I would otherwise have gone with the set intersection method that has been discussed in other responses here

inspectorG4dget
  • 110,290
  • 27
  • 149
  • 241