python - Space complexity of list creation -


could explain me space complexity of beyond program, , why it?

def is_pal_per(str):  s = [i in str] nums = [0] * 129  in s:     nums[ord(i)] += 1  count = 0  in nums:     if != 0 , / 2 == 0:         count += 1  print count  if count > 1:     return false else:     return true 

actually, i'm interested in lines of code. how influence on space complexity of above program?

s = [i in str] 

nums = [0] * 129

i'm unclear you're having trouble this. s list of individual characters in str. space consumption len(s).

nums constant size, dominated o(n) term.

is code wrote, or has been handed you? programming style highly not "pythonic".


as code, start collapse:

count = 0  char in str:     val = ord[char] + 1     if abs(val) == 1:         count += 1  print count  return count == 0 

first, replaced single-letter variables (s => char; i => val). cut out most of intermediate steps, leaving in couple read code. finally, used straightforward boolean value return, rather convoluted statement of original.

i did not use python's counting methods -- shorten function more. way, have print count of unity values, or need boolean return? if it's return value, can make shorter.


Comments

Popular posts from this blog

javascript - Thinglink image not visible until browser resize -

firebird - Error "invalid transaction handle (expecting explicit transaction start)" executing script from Delphi -

mongodb - How to keep track of users making Stripe Payments -