Skip to content Skip to sidebar Skip to footer

How To Do About "array / List" In Order To Make "(int, List) -> Float" In Python2.7

I have this function: def findScore(g): gLetter = ['A', 'B', 'C', 'D', 'F'] gPoint = [4.0, 3.0, 2.0, 1.0, 0.0] for i, v in enumerate(gLetter): if v==g:

Solution 1:

Use a dictionary for the grade scores, then simply do lookups for each grade in the list passed in, sum the divide by the number of grades:

defgrd_dict():
    gLetter = ["A", "B", "C", "D", "F"]
    gPoint = [4.0, 3.0, 2.0, 1.0, 0.0]
    returndict(zip(gLetter, gPoint))


deffind_gpa(num, grds):
    scores = grd_dict()
    returnsum((scores[g] for g in grds),0.0) / num

Output:

In[2]: find_gpa(4, ['B', 'B', 'C', 'B'])
Out[2]: 2.75In[3]: find_gpa(3, ['A', 'B', 'C'])
Out[3]: 3.0

Personally I would do it all in a single function and just take a list of grades, you can get the number of subjects from the list passed in so num is not really needed.

A dict is the most efficient way to do your lookups but if you have to use lists maybe then you can use a the logic closely related to an example in the biect docs:

from bisect import bisect_left

defgrade(g):
    g_point = [4, 3, 2, 1, 0]
    grades = 'ABCDF'
    i = bisect_left(grades, g)
    return g_point[i]


deffind_gpa(grds):
    returnsum((grade(g) for g in grds), 0.0) / (len(grds) or1)

(len(grds) or 1) will also handle an empty list:

In[44]: find_gpa(['F', 'F', 'F'])
Out[44]: 0.0In[45]: find_gpa(['B', 'B', 'C', 'B'])
Out[45]: 2.75In[46]: find_gpa(['A', 'B', 'C'])
Out[46]: 3.0In[47]: find_gpa([])
Out[47]: 0.0

Using bisect you can search a sorted structure in log(n) time as opposed to potentially looking at every element.

Solution 2:

findGPA doesn't actually need its first argument like you've proposed, because you could just find the length of the list of grade letters.

You can use your findScore function with a list comprehension to work out the score for each grade, and then sum these with the sum function. Finally, this is divided by the length of the scores list, found with the len function.

This can be done like so:

deffindGPA(grades):
    returnsum([findScore(x) for x in grades]) / max(len(grades), 1)

This then gives your desired output:

>>>findGPA(['A', 'B', 'C'])
3.0
>>>findGPA(['B', 'B', 'C', 'B'])
2.75

Solution 3:

Actually you don't need two functions for this also you can completly remove the first argument to your find_gpa function and use the built-in len function.

>>>gLetter = ["A", "B", "C", "D", "F"]>>>gPoint = [4.0, 3.0, 2.0, 1.0, 0.0]>>>mapped_values = dict(zip(gLetter, gPoint))>>>deffind_gpa(score):...returnsum(mapped_values[g] for g in score) / len(score)...>>>find_gpa(['A', 'B', 'C'])
3.0

If you are on Python 3.4 or newer you can use the mean function from the statistics module.

Demo:

>>>from statistics import mean>>>deffind_gpa(score):...return mean(mapped_values[g] for g in score)...>>>find_gpa(['B', 'B', 'C', 'B'])
2.75
>>>find_gpa(['A', 'B', 'C'])
3.0

Post a Comment for "How To Do About "array / List" In Order To Make "(int, List) -> Float" In Python2.7"