-
Notifications
You must be signed in to change notification settings - Fork 0
/
max_sum_non_neighbours.py
95 lines (84 loc) · 2.9 KB
/
max_sum_non_neighbours.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
''' This script solves the problem of calculating maximum sum
of the non-neighbours in a list and provide the elements
making this maximum sum along with the sum.
This script works for mixed (positive and negative) number list
Script doesn't do of the input parameters
It takes input from the user
'''
__version__="1.0.0"
__author__="Jayaprakash Nevara"
def list_has_negative_numbers_only(num_list):
'''This function checks if the given input list
num_list has only negative numbers
returns - True, if it has only negative numbers
False, if not
'''
ele_count = 0
for item in num_list:
if item < 0:
ele_count += 1
if ele_count == len(num_list):
return True
else:
return False
def get_max_sum_list(num_list):
'''This function takes num_list a number list as input
processes the list to find the maximum sum of the
non-neighbour numbers and the numbers that make this sum
Input : num_list - a number list
Output : (max_sum, max_sum_list) - a tuple with maximum sum
and the numbers list that make up this sum
'''
if not isinstance(num_list, list):
raise ValueError("Expecting list as input")
incl_list = []
excl_list = []
index = 0
for num in num_list:
if num > 0:
incl = num
incl_list.append(num)
break
index += 1
excl = 0
for num in num_list[index + 1:]:
if num > 0:
temp = incl
if incl < excl + num:
temp_list = incl_list.copy()
incl_list = excl_list.copy()
incl_list.append(num)
excl_list = temp_list.copy()
incl = excl + num
excl = temp
if incl == excl:
excl_list = incl_list.copy()
else:
temp = incl
incl = max(incl, excl + 0)
excl = incl
excl_list = incl_list.copy()
return max(incl, excl), incl_list
def main():
number_of_elements = int(input("Enter number of elements in the list?"))
print("Enter list elements(only integer data allowed)")
index = 0
num_list = []
while index < number_of_elements:
try:
element = int(input())
except Exception as exp:
print("Invalid input, enter integer only")
continue
num_list.append(element)
index += 1
if not list_has_negative_numbers_only(num_list):
max_sum, sum_list = get_max_sum_list(num_list)
print("Maximum sum : ", max_sum)
print("Elements making maximum sum")
print(sum_list)
else:
print("Maximum sum : ", max(num_list))
print("Elements making maximum sum")
print("[" + str(max(num_list)) + "]")
main()