Skip to content

Instantly share code, notes, and snippets.

@ashwanthkumar
Last active December 25, 2017 06:28
Show Gist options
  • Save ashwanthkumar/960fa84bfb644a082da6459bf93dccb1 to your computer and use it in GitHub Desktop.
Save ashwanthkumar/960fa84bfb644a082da6459bf93dccb1 to your computer and use it in GitHub Desktop.

Python Lists

The following questions were adapted from Prolog Lists, and mostly re-written to remove any prolog references or examples.

  1. Find the last element of a list
  2. Find the last but one element of a list.
  3. Find the K'th element of a list.
  4. Find the number of elements of a list.
  5. Reverse a list.
  6. Find out whether a list is a palindrome.
    A palindrome can be read forward or backward; e.g. [x,a,m,a,x].
  7. Flatten a nested list structure.
    Transform a list, possibly holding lists as elements into a 'flat' list by replacing each list with its elements (recursively).
  8. Eliminate consecutive duplicates of list elements.
    If a list contains repeated elements they should be replaced with a single copy of the element. The order of the elements should not be changed.
  9. Pack consecutive duplicates of list elements into sublists.
    If a list contains repeated elements they should be placed in separate sublists.
  10. Duplicate the elements of a list a given number of times.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment