faca - 1 year ago 86
Python Question

# Python problems w/ code

I would like to ask for some help with python ask since im new and im trying to learn it.
Write the following functions.

empty_list() returns an empty "list" of songs. This doesn't have to be a list in a Python sense of a word: the function can return a list, a tuple, dictionary, string, None ... or even a number if you think this is a good idea. (Hint: it's not. :) Return something that will be useful un the following functions.

play(a_list, song) somehow adds the given song (a string) to the list. How it does it depends on what your list is (a list, dictionary...).

song_plays(a_list, song) returns the number of times the song was played.

Say we do this:

new_list = empty_list()
play(new_list, "Yesterday")
play(new_list, "Tomorrow")
play(new_list, "Yesterday")
play(new_list, "Yesterday")

After this, a call song_plays(new_list, "Yesterday") returns 3, call song_plays(new_list, "Tomorrow") returns 1, and call song_plays(new_list, "Today") returns 0.

First part is done but now i need to write 4 more functions which i dont know where to start.

In the following functions we assume that we have two lists.

1. number_of_common(list1, list2) returns the number of songs that appear on both lists.

2. repertoire_size(list1, list2) returns the number of songs that appear on one (or even both) lists.

3. similarity(list1, list2) returns the similarity computed as the quotient of the above two numbers. If both lists are empty, the similarity is 0.

4. Write a function rang(a_list, n) that returns a list of the n most played songs. If two songs share equal number of plays, sort them alphabetically. If the list contains less than n song, the returned list will be shorted (but still ordered).

This is what i have for now:

``````    def empty_list():
list = {}
return list

def play(list, song):
if song in list:
list[song] += 1
else:
list[song] = 1

def song_plays(list, song):
if song not in list:
return 0
else:
return list[song]

def total_songs(list):
return len(list)

def total_plays(list):
totalplays = 0
for name in list:
a = list[name]
totalplays += a

def favourite(list):
if list == {}:
return None
else:
max_name = ''
max_a = 0
for name in list:
a = list[name]
if max_a < a:
max_a = a
max_name = name
return max_name

def number_of_common(list1,list2):

def repertoire_size(list1,list2):

def similarity(list1,list2):

def rang(list,n):
``````

For the list datatype, I'd recommend a `Counter`, then everything gets pretty compact:

``````from collections import Counter as empty_list

def play(a_list, song):
a_list[song] += 1

def song_plays(a_list, song):
return a_list[song]

def number_of_common(list1, list2):
return len(set(list1) & set(list2))

def repertoire_size(list1, list2):
return len(list1 + list2)

def similarity(list1, list2):
try:
return number_of_common(list1, list2) / repertoire_size(list1, list2)
except ZeroDivisionError:
return 0

def rang(a_list, n):
ranks = sorted(a_list.most_common(n), key = lambda x: (-x[1],x[0]))
return [song for song, times in ranks]
``````
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download