Data structures organize the data used by a program to efficiently solve a problem. Different data structures exist to support the implementation of a broad variety of algorithms. The choice of a data structure depends on what type of problem is being solved and how the data is represented. It is important to note that a data structure is a programming construct. It is not necessarily a physical component of the computer, nor an organized information management tool such as a database.
The following examples covered in the PD material use strings or lists in Python:
Examples of Using Strings
Loops in Python:
- Example 2: Given two strings, find all characters appearing in both.
- This example illustrates how to use the in operator and loops to compare elements in two strings.
- Example 5: Palindromes
- This example illustrates how to use the in operator and loops to determine whether a word is palindrome.
- Example 9: Converting a for-loop into a while loop
- This example illustrates how to use two different ways to iterate a string.
- Example 11: Sum up the digits in an integer
- This example illustrates how to use the in operator and loops to sum up the digits in an integer
Recursion – Algorithms:
- Example 4: Reversing a string
- This example illustrates how to use string indices and the len() function to write a recursive program reversing a string .
Examples of Using Lists
Algorithms – Solving Problems:
- Example 1: Making a profit
- This example illustrates how to use a list to hold values and use built-in Python list methods: list.sort() and list.reverse().
- Example 4: Searching for an item in a list
- This example illustrates how to search for an item in a sorted list and describes a solution based on binary search
- Example 5: Coin changing
- This example demonstrates how to create an empty list, append elements to a list, and print elements of a two-dimensional list.
- Example 6: Arranging the pancakes
- This example demonstrates how to use list slicing and concatenation in a sorting algorithm.
Recursion – Algorithms:
- Example 3: Binary Search
- This example illustrates how to search for an item in a sorted list and describes a solution based on recursive version of binary search.