Skip to main content
Code Review

Return to Question

Commonmark migration
Source Link

I had an interview, where I was asked to solve to this question:

Given an string str, find the maximum length of palindrome subsequence.

Example: >if str = 'abcda':

maximum_length = 3 'aca'

How can I improve this code?

def maximum_length_palindrome_subsequence(a):
 a = list(a)
 n = len(a)
 table = [[0 for i in xrange(n)] for j in xrange(n)]
 for i in xrange(n):
 table[i][i] = 1
 for gap in xrange(1,n):
 for i in xrange(n-1):
 h = i+gap
 if h>=n:
 continue
 if a[i] == a[h]:
 if i+1==h:
 table[i][h] = 2
 else: 
 table[i][h] = table[i+1][h-1] + 2 
 else:
 if h<n:
 table[i][h] = max(table[i+1][h], table[i][h-1])
 return table[0][n-1] 
 
print maximum_length_palindrome_subsequence('abcbda')

I had an interview, where I was asked to solve to this question:

Given an string str, find the maximum length of palindrome subsequence.

Example: >if str = 'abcda':

maximum_length = 3 'aca'

How can I improve this code?

def maximum_length_palindrome_subsequence(a):
 a = list(a)
 n = len(a)
 table = [[0 for i in xrange(n)] for j in xrange(n)]
 for i in xrange(n):
 table[i][i] = 1
 for gap in xrange(1,n):
 for i in xrange(n-1):
 h = i+gap
 if h>=n:
 continue
 if a[i] == a[h]:
 if i+1==h:
 table[i][h] = 2
 else: 
 table[i][h] = table[i+1][h-1] + 2 
 else:
 if h<n:
 table[i][h] = max(table[i+1][h], table[i][h-1])
 return table[0][n-1] 
 
print maximum_length_palindrome_subsequence('abcbda')

I had an interview, where I was asked to solve to this question:

Given an string str, find the maximum length of palindrome subsequence.

Example: >if str = 'abcda':

maximum_length = 3 'aca'

How can I improve this code?

def maximum_length_palindrome_subsequence(a):
 a = list(a)
 n = len(a)
 table = [[0 for i in xrange(n)] for j in xrange(n)]
 for i in xrange(n):
 table[i][i] = 1
 for gap in xrange(1,n):
 for i in xrange(n-1):
 h = i+gap
 if h>=n:
 continue
 if a[i] == a[h]:
 if i+1==h:
 table[i][h] = 2
 else: 
 table[i][h] = table[i+1][h-1] + 2 
 else:
 if h<n:
 table[i][h] = max(table[i+1][h], table[i][h-1])
 return table[0][n-1] 
 
print maximum_length_palindrome_subsequence('abcbda')
edited tags
Link
200_success
  • 145.5k
  • 22
  • 190
  • 478
Tweeted twitter.com/StackCodeReview/status/857078455011352576
added 10 characters in body; edited title
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

length Length of longest palindrome subsequence

I had an interview, where I was asked to solve to this question:

Given an string str, find the maximum length of palindrome subsequence.

Given an string str, find the maximum length of palindrome subsequence.

Example: >if str = 'abcda':

maximum_length = 3 'aca'

How tocan I improve this code?

def maximum_length_palindrome_subsequence(a):
 a = list(a)
 n = len(a)
 table = [[0 for i in xrange(n)] for j in xrange(n)]
 for i in xrange(n):
 table[i][i] = 1
 for gap in xrange(1,n):
 for i in xrange(n-1):
 h = i+gap
 if h>=n:
 continue
 if a[i] == a[h]:
 if i+1==h:
 table[i][h] = 2
 else: 
 table[i][h] = table[i+1][h-1] + 2 
 else:
 if h<n:
 table[i][h] = max(table[i+1][h], table[i][h-1])
 return table[0][n-1] 
 
print maximum_length_palindrome_subsequence('abcbda')

length of longest palindrome subsequence

I had an interview, where I was asked to solve to this question:

Given an string str, find the maximum length of palindrome subsequence.

Example: >if str = 'abcda':

maximum_length = 3 'aca'

How to improve code?

def maximum_length_palindrome_subsequence(a):
 a = list(a)
 n = len(a)
 table = [[0 for i in xrange(n)] for j in xrange(n)]
 for i in xrange(n):
 table[i][i] = 1
 for gap in xrange(1,n):
 for i in xrange(n-1):
 h = i+gap
 if h>=n:
 continue
 if a[i] == a[h]:
 if i+1==h:
 table[i][h] = 2
 else: 
 table[i][h] = table[i+1][h-1] + 2 
 else:
 if h<n:
 table[i][h] = max(table[i+1][h], table[i][h-1])
 return table[0][n-1] 
 
print maximum_length_palindrome_subsequence('abcbda')

Length of longest palindrome subsequence

I had an interview, where I was asked to solve to this question:

Given an string str, find the maximum length of palindrome subsequence.

Example: >if str = 'abcda':

maximum_length = 3 'aca'

How can I improve this code?

def maximum_length_palindrome_subsequence(a):
 a = list(a)
 n = len(a)
 table = [[0 for i in xrange(n)] for j in xrange(n)]
 for i in xrange(n):
 table[i][i] = 1
 for gap in xrange(1,n):
 for i in xrange(n-1):
 h = i+gap
 if h>=n:
 continue
 if a[i] == a[h]:
 if i+1==h:
 table[i][h] = 2
 else: 
 table[i][h] = table[i+1][h-1] + 2 
 else:
 if h<n:
 table[i][h] = max(table[i+1][h], table[i][h-1])
 return table[0][n-1] 
 
print maximum_length_palindrome_subsequence('abcbda')
Source Link
Harsha
  • 1.3k
  • 1
  • 10
  • 23
Loading
lang-py

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