# Median Solution – Intro to Computer Science One way to solve this problem, is to look at the possibilities. We know that there is a bigger number, the median, and a smaller number. And that we have 3 inputs– Let’s call them “a,” “b,” and “c.” Let’s say, for the sake of example– that we figure out that “b” is the bigger number. We could solve this using the bigger function that you defined in lecture.>From here, we only need to figure out what is the median and the smaller number. We know “b” can’t be the median, because we just decided that it was the bigger number.>From here, all we have to do is compare “a” and “c.” There are two cases at this point. One is where “a” is greater than “c.” If “a” is greater than “c,” we know “c” is the smaller number– and “a” is the median. If “c” is greater than “a,” we know that “a” is the smaller number– and “c” is the median. We can use this strategy to solve every possible case when defining our function. So let’s take a crack at it by going right to the interpreter. So, first let’s define our function. As specified in the problem, it’s called median–and we take in 3 parameters. As I did before, we are going to call them “a,” “b,” and “c.” As I described earlier, we want to find the biggest number first, let’s call it “big.” Here’s the perfect time to use the function that we defined in class, “biggest.” “Biggest” will give us the biggest number, as the name describes– by simply passing in those 3 parameters.>From here, we enumerate the possible cases. As I described earlier, if “a” is the biggest number, you want to return the bigger number of “b” and “c.” If “b” is the biggest number, we want to return the bigger number of “a” and “c.” And lastly, if “c” is the biggest number, which is satisfied by the else here, we want to return the bigger number of “a” and “b.”

## 4 thoughts on “Median Solution – Intro to Computer Science”

1. Sometimes you have to think simpler and more straightforward… I did it the hard way (
Well, it's two lines shorter though 🙂

def median(a,b,c):
if biggest(a,b,c) == bigger(b,c):
not_biggest = b + c – bigger(b,c)
return bigger(a, not_biggest)
else:
return bigger(b,c)
You can make it even one more line shorter.

2. 3. wow it takes me 15 line
but he just do it with 7

i wish someday i could make it short and simple

def median(a,b,c):
if a == biggest(a,b,c):
# find the mid
if b == bigger(b,c):
return b
else :
return c
elif b == biggest(a,b,c):
if a == bigger(a,c):
return a
else:
return c
else :
# is the first number bigeer than the last number
if a == bigger(a,b):
return a
else:
return b

4. I'm actually retarded.