4 Replies - 2317 Views - Last Post: 04 September 2019 - 08:22 AM Rate Topic: -----

#1 uwl   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 02-August 18

Finding Median and Mode using reduce

Posted 22 September 2018 - 02:56 PM

I am trying to find the Median and the Mode using Python's reduce() function.

from functools import reduce

def sumAB(a, B)/>: 
      c = a + b 
      return c

def meanMedianMode(numbers):

    calculate = {   
          'mean': reduce(sumAB, numbers) / len(numbers), 
          # count how many numbers, add 1 then divide by 2
          'median': reduce(), 
          # count number occurs most often
          'mode': reduce()}

    return calculate

mmm_dict = meanMedianMode([1, 2, 6, 7, 8, 9, 3, 4, 5, 10, 10])
print(mmm_dict)



How do I do this, do I need to create another function?

Can someone walk me through accomplishing this?

This post has been edited by uwl: 22 September 2018 - 02:58 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Finding Median and Mode using reduce

#2 DK3250   User is offline

  • Pythonian
  • member icon

Reputation: 560
  • View blog
  • Posts: 1,777
  • Joined: 27-December 13

Re: Finding Median and Mode using reduce

Posted 22 September 2018 - 03:11 PM

Why use reduce() ?

Mean is ok, but simpler is: sum(numbers)/len(numbers)
Median is the middle value when numbers are sorted >> sort and use list index.
Mode (do you mead the most frequent number..?) >> itertools.groupby or collections.Counter or home-build loop.
Was This Post Helpful? 0
  • +
  • -

#3 wbyker   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 25-August 19

Re: Finding Median and Mode using reduce

Posted 25 August 2019 - 03:47 PM

i am doing this same thing for the lambda school, the best results I have came up with are in this video link on youtube.com
https://www.youtube....6vJGiE8I&t=782s
do you still have a copy of how you solved this?
Was This Post Helpful? 0
  • +
  • -

#4 DK3250   User is offline

  • Pythonian
  • member icon

Reputation: 560
  • View blog
  • Posts: 1,777
  • Joined: 27-December 13

Re: Finding Median and Mode using reduce

Posted 03 September 2019 - 09:06 AM

@wbyker: You know, asking for code is not in compliance with the rules here.

I'll be happy to help. Show your code and explain the associated problem; providing a set of sample data and expected output is also helpful.
Was This Post Helpful? 0
  • +
  • -

#5 baavgai   User is offline

  • Dreaming Coder
  • member icon


Reputation: 7492
  • View blog
  • Posts: 15,527
  • Joined: 16-October 07

Re: Finding Median and Mode using reduce

Posted 04 September 2019 - 08:22 AM

Hmm... len seems like cheating. This actually works:
xs = [1, 2, 6, 7, 8, 9, 3, 4, 5, 10, 10]
print((lambda tot_sz: tot_sz[0] / tot_sz[1])(reduce(lambda acc, x: ((acc[0] + x), (acc[1] + 1)), xs, (0, 0))))



Because, honestly, if you were reducing you'd probably be counting. It's a rather functional thing to do. :P

You can, theoretically, use reduce for most everything lispy:
print(reduce(lambda x, y: x / y, reduce(lambda acc, x: ((acc[0] + x), (acc[1] + 1)), xs, (0, 0))))



Along the same lines, you could build a dict for that median. Though you won't be able to use a lambda, you'll need a spare function for that. You could actually reduce all the way there in other languages, but I don't know that python has the syntax for it in a lambda.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1