Skip to main content
deleted 2 characters in body
Source Link
Carcigenicate
  • 16.3k
  • 3
  • 35
  • 80

This code is more efficient because every function involved here is "lazy": they doesdo only as much work as they need to. reversed, zip, islice, and the generator expression all return an iterator that can produce elements (but do little work up front). all also exits as soon as it gets a Falsey result, so it's comparable to a for loop that contains a break in some branches. This is key here because we only want to do as much work as is necessary to determine whether not they're palindromes. Making two full copies of the list does a large amount of work; far more than is required to check if the string is a palindrome.

This code is more efficient because every function involved here is "lazy": they does only as much work as they need to. reversed, zip, islice, and the generator expression all return an iterator that can produce elements (but do little work up front). all also exits as soon as it gets a Falsey result, so it's comparable to a for loop that contains a break in some branches. This is key here because we only want to do as much work as is necessary to determine whether not they're palindromes. Making two full copies of the list does a large amount of work; far more than is required to check if the string is a palindrome.

This code is more efficient because every function involved here is "lazy": they do only as much work as they need to. reversed, zip, islice, and the generator expression all return an iterator that can produce elements (but do little work up front). all also exits as soon as it gets a Falsey result, so it's comparable to a for loop that contains a break in some branches. This is key here because we only want to do as much work as is necessary to determine whether not they're palindromes. Making two full copies of the list does a large amount of work; far more than is required to check if the string is a palindrome.

added 13 characters in body
Source Link
Carcigenicate
  • 16.3k
  • 3
  • 35
  • 80

This is an extremely expensive implementation. lst[:] is needlessly creating a copy of lst, then lst[::-1] creates a second complete copy of lst (but reversed). There are many ways of approaching this, but I'dI would go for something like this:

Then checks to see if all the pairs are equal. If they are, the list is a palindrome. This method is still inefficient becausethough since only half of each needs to be checked. This couldIt can be further improved by introducing itertools.islice to get a "view" of the first half of the zipped lists:

This code is more efficient because every function involved here is "lazy": they does only as much work as they need to. reversed, zip, islice, and the generator expression all return an iterator that can produce elements (but do little work up front). all also exits as soon as it gets a Falsey result, so it's comparable to a for loop that contains a break in some branches. This is key here because we only want to do as much work as is necessary to determine whether not they're palindromes. Making two full copies of the list does a large amount of work; far more than is required to check if the string is a palindrome.

This is an extremely expensive implementation. lst[:] is needlessly creating a copy of lst, then lst[::-1] creates a second complete copy of lst (but reversed). There are many ways of approaching this, but I'd go for something like this:

Then checks to see if all the pairs are equal. If they are, the list is a palindrome. This is still inefficient because only half of each needs to be checked. This could be further improved by introducing itertools.islice to get a "view" of the first half of the zipped lists:

This code is more efficient because every function involved here is "lazy": they does only as much work as they need to. reversed, zip, islice and the generator expression all return an iterator that can produce elements (but do little work up front). all also exits as soon as it gets a Falsey result, so it's comparable to a for loop that contains a break in some branches. This is key here because we only want to do as much work as is necessary to determine whether not they're palindromes. Making two full copies of the list does a large amount of work; far more than is required to check if the string is a palindrome.

This is an extremely expensive implementation. lst[:] is needlessly creating a copy of lst, then lst[::-1] creates a second complete copy of lst (but reversed). There are many ways of approaching this, but I would go for something like this:

Then checks to see if all the pairs are equal. If they are, the list is a palindrome. This method is still inefficient though since only half of each needs to be checked. It can be further improved by introducing itertools.islice to get a "view" of the first half of the zipped lists:

This code is more efficient because every function involved here is "lazy": they does only as much work as they need to. reversed, zip, islice, and the generator expression all return an iterator that can produce elements (but do little work up front). all also exits as soon as it gets a Falsey result, so it's comparable to a for loop that contains a break in some branches. This is key here because we only want to do as much work as is necessary to determine whether not they're palindromes. Making two full copies of the list does a large amount of work; far more than is required to check if the string is a palindrome.

added 503 characters in body
Source Link
Carcigenicate
  • 16.3k
  • 3
  • 35
  • 80

This code is more efficient because every function involved here is "lazy": they does only as much work as they need to. reversed, zip, islice and the generator expression all return an iterator that can produce elements (but do little work up front). all also exits as soon as it gets a Falsey result, so it's comparable to a for loop that contains a break in some branches. This is key here because we only want to do as much work as is necessary to determine whether not they're palindromes. Making two full copies of the list does a large amount of work; far more than is required to check if the string is a palindrome.

This code is more efficient because every function involved here is "lazy": they does only as much work as they need to. reversed, zip, islice and the generator expression all return an iterator that can produce elements (but do little work up front). all also exits as soon as it gets a Falsey result, so it's comparable to a for loop that contains a break in some branches. This is key here because we only want to do as much work as is necessary to determine whether not they're palindromes.

This code is more efficient because every function involved here is "lazy": they does only as much work as they need to. reversed, zip, islice and the generator expression all return an iterator that can produce elements (but do little work up front). all also exits as soon as it gets a Falsey result, so it's comparable to a for loop that contains a break in some branches. This is key here because we only want to do as much work as is necessary to determine whether not they're palindromes. Making two full copies of the list does a large amount of work; far more than is required to check if the string is a palindrome.

added 503 characters in body
Source Link
Carcigenicate
  • 16.3k
  • 3
  • 35
  • 80
Loading
deleted 1 character in body
Source Link
Carcigenicate
  • 16.3k
  • 3
  • 35
  • 80
Loading
Source Link
Carcigenicate
  • 16.3k
  • 3
  • 35
  • 80
Loading