-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathq23.py
More file actions
54 lines (45 loc) · 1.45 KB
/
q23.py
File metadata and controls
54 lines (45 loc) · 1.45 KB
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
def LIS(seq): #Longest Increasing subsequence.
list = []
for i in range (0,n):
j = 0
elemupdte = False
for j in range(0, len(list)):
if list[j] > seq[i]:
list.insert(j,seq[i])
#print list
list.pop(j+1)
elemupdte = True
break
if not elemupdte:
list.insert(j,seq[i])
return sorted(list)
def LDS(seq): #Using dynamic programming to determine the longest decreasing subsequence.
temparr = [0] * len(seq)
lenseq = len(seq)
for i in range(lenseq - 2, -1, -1):
for j in range(lenseq - 1, i, -1):
if temparr[i] <= temparr[j] and seq[i] > seq[j]:
temparr[i] = temparr[j] + 1
max_value = max(temparr)
result = []
for i in range(len(temparr)):
if max_value == temparr[i]:
result.append(seq[i])
max_value -= 1
return result
if __name__ == "__main__":
f = open("rosalind_lgis.txt",'r') #To extract input from the file
inputs = f.read().split('\n')
#print inputsrosalind_lgis
n = int(inputs[0])
#print n
seq = map(int,inputs[1].split())
#print seq
lis = LIS(seq)
for i in lis:
print i,
print
dis = LDS(seq) #Calling LDS function as define above.
for i in dis:
print i,
#refrence: www.hackerrank.com