Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitb2388f3

Browse files
committed
Longest continuous odd subsequence using DP
1 parente05075b commitb2388f3

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Author: OMKAR PATHAK
2+
3+
# this program will illustrate an example for finding the longest CONTINUOUS ODD subsequence
4+
5+
# INPUT: [2, 6, 8, 3, 9, 1, 5, 6, 1, 3, 5, 7, 7, 1, 2, 3, 4, 5]
6+
# OUTPUT: [1, 3, 5, 7, 7, 1] 6
7+
8+
# in short we have to find the elements and length of the longest continuous odd subsequence
9+
10+
deflongest_continuous_odd_subsequence(array):
11+
final_list= []
12+
temp_list= []
13+
foriinarray:
14+
# we want to find whether the element is odd
15+
ifi%2==0:
16+
# if element is even and our temp_list is not empty then only append it to out result list
17+
iftemp_list!= []:
18+
final_list.append(temp_list)
19+
temp_list= []
20+
else:
21+
# if element is odd, append it to our temp_list
22+
temp_list.append(i)
23+
24+
# if temp_list is not empty at the last iteration, add it to the final_list
25+
iftemp_list!= []:
26+
final_list.append(temp_list)
27+
28+
# print the maximum list based on its length
29+
result=max(final_list,key=len)
30+
print(result,len(result))
31+
32+
if__name__=='__main__':
33+
array= [2,6,8,3,9,1,5,6,1,3,5,7,7,1,2,3,4,5]
34+
longest_continuous_odd_subsequence(array)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp