-1

Trying to solve leetcode questions with python3. This question is just asking for some simple stat calculations, but I can't even get the code to run because I don't understand what the problem is. The names are spelled the same, the function is defined before it is called, is it something with the parameter?

 class Solution:
   def minFinder(nums):
       min = nums[0]
       for num in nums:
           if (num < min):
               min = num
       return min
               
   def maxFinder(nums):
       max = nums[0]
       for num in nums:
           if (num > min):
               max = num
       return max
   
   def meanFinder(nums):
       sum = 0
       for num in nums:
           sum += num
       mean = sum/(len(nums))
       return mean
   
   def medFinder(nums):
       if (len(nums) % 2 == 0):
           med = nums[((len(nums)/2 - 1) + (len(nums)/2)) / 2]
       else:
           med = nums[len(nums) // 2]
       return med
           
   def modeFinder(nums):
       index = 0
       freq = 0
       for num in nums:
           if nums.count(num) > freq:
               freq = nums.count(num)
               index = nums.index(num)
       mode = nums[index]
       return mode
               
   def sampleStats(self, count: List[int]) -> List[float]:
       fltr_cnt = []
       for num in count:
           if (num != 0):
               fltr_cnt.append(num)
       stats = []
       stats.append(minFinder(fltr_cnt) * 1.0)
       stats.append(maxFinder(fltr_cnt))
       stats.append(meanFinder(fltr_cnt))
       stats.append(medFinder(fltr_cnt))
       stats.append(modeFinder(fltr_cnt))
       return stats
name 'minFinder' is not defined

Advice to make my code less complex also appreciated, like is there a faster way to do .append() 5 times?

2 Answers2

0

Since you are using classes you will have to use the self while defining and accessing the methods, attributes of a class, please try below code:

 class Solution:
   def minFinder(self, nums):
       min = nums[0]
       for num in nums:
           if (num < min):
               min = num
       return min
               
   def maxFinder(self, nums):
       max = nums[0]
       for num in nums:
           if (num > min):
               max = num
       return max
   
   def meanFinder(self, nums):
       sum = 0
       for num in nums:
           sum += num
       mean = sum/(len(nums))
       return mean
   
   def medFinder(self, nums):
       if (len(nums) % 2 == 0):
           med = nums[((len(nums)/2 - 1) + (len(nums)/2)) / 2]
       else:
           med = nums[len(nums) // 2]
       return med
           
   def modeFinder(self, nums):
       index = 0
       freq = 0
       for num in nums:
           if nums.count(num) > freq:
               freq = nums.count(num)
               index = nums.index(num)
       mode = nums[index]
       return mode
               
   def sampleStats(self, count: List[int]) -> List[float]:
       fltr_cnt = []
       for num in count:
           if (num != 0):
               fltr_cnt.append(num)
       stats = []
       stats.append(self.minFinder(fltr_cnt) * 1.0)
       stats.append(self.maxFinder(fltr_cnt))
       stats.append(self.meanFinder(fltr_cnt))
       stats.append(self.medFinder(fltr_cnt))
       stats.append(self.modeFinder(fltr_cnt))
       return stats
JayantSeth
  • 348
  • 1
  • 12
0

self represents the instance of the class. By using the self keyword we can access the attributes and methods of the class in python. It binds the attributes with the given arguments. So, your member functions were not able to access them. To know more about self read this article


class Solution:
    def minFinder(self, nums):
        min = nums[0]
        for num in nums:
            if (num < min):
                min = num
        return min

    def maxFinder(self, nums):
        max = nums[0]
        for num in nums:
            if num > max:
                max = num
        return max

    def meanFinder(self, nums):
        sum = 0
        for num in nums:
            sum += num
        mean = sum / (len(nums))
        return mean

    def medFinder(self, nums):
        if len(nums) % 2 == 0:
            med = nums[((len(nums) / 2 - 1) + (len(nums) / 2)) / 2]
        else:
            med = nums[len(nums) // 2]
        return med

    def modeFinder(self, nums):
        index = 0
        freq = 0
        for num in nums:
            if nums.count(num) > freq:
                freq = nums.count(num)
                index = nums.index(num)
        mode = nums[index]
        return mode

    def sampleStats(self, count: list):
        fltr_cnt = []
        for num in count:
            if (num != 0):
                fltr_cnt.append(num)
        stats = []
        stats.append(self.minFinder(fltr_cnt) * 1.0)
        stats.append(self.maxFinder(fltr_cnt))
        stats.append(self.meanFinder(fltr_cnt))
        stats.append(self.medFinder(fltr_cnt))
        stats.append(self.modeFinder(fltr_cnt))
        return stats

Also, there is mistake in maxFinder function. You have used min instead of max.

   def maxFinder(nums):
       max = nums[0]
       for num in nums:
           if (num > min):
               max = num
       return max