1
\$\begingroup\$

I am beginner in python and I am learing about syntax and other functions in python by writing Algorithm and Data Structure programs.

How can this code be improved?

def get_input(): #to get input from user
 input_str = input("Enter elements to be sorted: ")
 try:
 lst = list(map(int, input_str.split())) #make a list of integers from input string
 except:
 raise TypeError("Please enter a list of integers only, seperated by a space!!")
 return lst
def max_heapify(thelist, lst_size, idx):
 largest = idx
 left_child = (2 * idx) + 1
 right_child = (2 * idx) + 2
 if left_child < lst_size and thelist[left_child] > thelist[largest]:
 largest = left_child
 if right_child < lst_size and thelist[right_child] > thelist[largest]:
 largest = right_child
 if largest != idx:
 thelist[idx], thelist[largest] = thelist[largest], thelist[idx]
 max_heapify(thelist, lst_size, largest)
def build_max_heap(thelist, lst_size):
 for curr_idx in range(lst_size // 2 - 1, -1, -1):
 max_heapify(thelist, lst_size, curr_idx)
def heap_sort(thelist):
 if len(thelist) == 0:
 print("Empty list!!")
 elif len(thelist) == 1:
 print("Only one element!!")
 else:
 build_max_heap(thelist, len(thelist))
 for curr_idx in range(len(thelist) -1, 0, -1):
 thelist[curr_idx], thelist[0] = thelist[0], thelist[curr_idx] #swapping
 max_heapify(thelist, curr_idx, 0)
if __name__ == '__main__':
 input_list = get_input()
 heap_sort(input_list)
 print(*input_list, sep = ", ")
asked Jul 6, 2019 at 10:36
\$\endgroup\$
1
  • \$\begingroup\$ (I neither considered seriously inspecting your Quick Sort code nor this Heapsort for the same reason: both are "not" documented/commented.) Heed the Style Guide for Python Code \$\endgroup\$ Commented Jul 6, 2019 at 12:51

1 Answer 1

2
\$\begingroup\$

Use standard docstrings

This comment:

to get input from user

is best placed in a docstring:

def get_input():
 """
 get input from user
 """

Consider using type hints

You're best to google this, because there's a wealth of information about it, but as an example: the idx argument would be idx: int.

Operator precedence

(2 * idx) + 1

doesn't need parens, because multiplication has stronger association than addition.

Never except:

At the least, you should write except Exception instead of except. The latter can prevent user break (Ctrl+C) from working. If possible, replace Exception with something more specific.

Use a comprehension

map is a little difficult to read. Instead, how about

lst = [int(e) for e in input_str.split()]

Variable naming

lst isn't helpful - rather than naming things based on what type they are, you should be naming them based on what they actually mean to the program - in this case, perhaps "elements".

answered Jul 6, 2019 at 13:16
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.