Skip to main content
Code Review

Return to Answer

added 4 characters in body; added 2 characters in body
Source Link
200_success
  • 145.6k
  • 22
  • 190
  • 479
  1. Like @Mast mentioned already mentioned you should use proper variable named in your program. A good variable name makes the program so much easier to read, specially if you're reading it after couple of months.

    There are only two hard things in Computer Science: cache invalidation and naming things. -- Phil Karlton

  2. e = int(len(nl))

    e = int(len(nl))

    The call to int() is redundant, length is always an integer.

  3. Given integer operands you can get floored division in both Python 2 and 3 using //// operator. This prevents the int() conversions as we are not dealing with floats here.

  4. Return your result instead of printing it. That way you can import this function in other modules as well, making it re-usable.

New version of your program:

def median(seq):
 new_list = sorted(seq)
 length = len(seq)
 if length % 2 != 0:
 index = length // 2
 median = new_list[index]
 else:
 index_right = length // 2
 index_left = index_right - 1
 median = (new_list[index_left] + new_list[index_right]) / 2.0
 return median
  1. Like @Mast mentioned already mentioned you should use proper variable named in your program. A good variable name makes the program so much easier to read, specially if you're reading it after couple of months.

    There are only two hard things in Computer Science: cache invalidation and naming things. -- Phil Karlton

  2. e = int(len(nl))

    The call to int() is redundant, length is always an integer.

  3. Given integer operands you can get floored division in both Python 2 and 3 using // operator. This prevents the int() conversions as we are not dealing with floats here.

  4. Return your result instead of printing it. That way you can import this function in other modules as well, making it re-usable.

New version of your program:

def median(seq):
 new_list = sorted(seq)
 length = len(seq)
 if length % 2 != 0:
 index = length // 2
 median = new_list[index]
 else:
 index_right = length // 2
 index_left = index_right - 1
 median = (new_list[index_left] + new_list[index_right]) / 2.0
 return median
  1. Like @Mast mentioned already mentioned you should use proper variable named in your program. A good variable name makes the program so much easier to read, specially if you're reading it after couple of months.

    There are only two hard things in Computer Science: cache invalidation and naming things. Phil Karlton

  2. e = int(len(nl))

    The call to int() is redundant, length is always an integer.

  3. Given integer operands you can get floored division in both Python 2 and 3 using // operator. This prevents the int() conversions as we are not dealing with floats here.

  4. Return your result instead of printing it. That way you can import this function in other modules as well, making it re-usable.

New version of your program:

def median(seq):
 new_list = sorted(seq)
 length = len(seq)
 if length % 2 != 0:
 index = length // 2
 median = new_list[index]
 else:
 index_right = length // 2
 index_left = index_right - 1
 median = (new_list[index_left] + new_list[index_right]) / 2.0
 return median
Formatting and typo
Source Link
rolfl
  • 98.1k
  • 17
  • 219
  • 419
  1. Like @Mast mentioned already mentioned you should use proper variable named in your program. A good variable name makes the program so much easier to read, specially if you're reading it after couple of months.

    Like @Mast mentioned already mentioned you should use proper variable named in your program. A good variable name makes the program so much easier to read, specially if you're reading it after couple of months.

    There are only two hard things in Computer Science: cache invalidation and naming things. -- Phil Karlton

  2. e = int(len(nl))

    The call to int() is redundant, length is always an integer.

  3. Given integer operands you can get floored division in both Python 2 and 3 using // operator. This prevents the int() conversions as we are not dealing with floats here.

  4. Return your result instead of printing it. That way you can import this function in other modules as well, making it re-usable.

There are only two hard things in Computer Science: cache invalidation and naming things. -- Phil Karlton

  1. e = int(len(nl))

The call to int() is redundant, length is always an integer.

  1. Given integer operands you can get floored division in both Python 2 and 3 using // operator. This prevents the int() conversions as we are not dealing with floats here.

  2. Return your result instead of printing it. That was you can import this function in other modules as well, making it re-usable.

New version of your program:

def median(seq):
 new_list = sorted(seq)
 length = len(seq)
 if length % 2 != 0:
 index = length // 2
 median = new_list[index]
 else:
 index_right = length // 2
 index_left = index_right - 1
 median = (new_list[index_left] + new_list[index_right]) / 2.0
 return median
  1. Like @Mast mentioned already mentioned you should use proper variable named in your program. A good variable name makes the program so much easier to read, specially if you're reading it after couple of months.

There are only two hard things in Computer Science: cache invalidation and naming things. -- Phil Karlton

  1. e = int(len(nl))

The call to int() is redundant, length is always an integer.

  1. Given integer operands you can get floored division in both Python 2 and 3 using // operator. This prevents the int() conversions as we are not dealing with floats here.

  2. Return your result instead of printing it. That was you can import this function in other modules as well, making it re-usable.

New version of your program:

def median(seq):
 new_list = sorted(seq)
 length = len(seq)
 if length % 2 != 0:
 index = length // 2
 median = new_list[index]
 else:
 index_right = length // 2
 index_left = index_right - 1
 median = (new_list[index_left] + new_list[index_right]) / 2.0
 return median
  1. Like @Mast mentioned already mentioned you should use proper variable named in your program. A good variable name makes the program so much easier to read, specially if you're reading it after couple of months.

    There are only two hard things in Computer Science: cache invalidation and naming things. -- Phil Karlton

  2. e = int(len(nl))

    The call to int() is redundant, length is always an integer.

  3. Given integer operands you can get floored division in both Python 2 and 3 using // operator. This prevents the int() conversions as we are not dealing with floats here.

  4. Return your result instead of printing it. That way you can import this function in other modules as well, making it re-usable.

New version of your program:

def median(seq):
 new_list = sorted(seq)
 length = len(seq)
 if length % 2 != 0:
 index = length // 2
 median = new_list[index]
 else:
 index_right = length // 2
 index_left = index_right - 1
 median = (new_list[index_left] + new_list[index_right]) / 2.0
 return median
Source Link
  1. Like @Mast mentioned already mentioned you should use proper variable named in your program. A good variable name makes the program so much easier to read, specially if you're reading it after couple of months.

There are only two hard things in Computer Science: cache invalidation and naming things. -- Phil Karlton

  1. e = int(len(nl))

The call to int() is redundant, length is always an integer.

  1. Given integer operands you can get floored division in both Python 2 and 3 using // operator. This prevents the int() conversions as we are not dealing with floats here.

  2. Return your result instead of printing it. That was you can import this function in other modules as well, making it re-usable.

New version of your program:

def median(seq):
 new_list = sorted(seq)
 length = len(seq)
 if length % 2 != 0:
 index = length // 2
 median = new_list[index]
 else:
 index_right = length // 2
 index_left = index_right - 1
 median = (new_list[index_left] + new_list[index_right]) / 2.0
 return median
lang-py

AltStyle によって変換されたページ (->オリジナル) /