Two Pointer Problem

Introduction

Technically indexes and pointers are not the same thing, but in certain circumstances they can be considered the same for naming purposes (imprecise/sloppy). In the diagram below there are two list indexes (A and B). In this circumstance you might use the name index and pointer interchangeable.

image missing

Project #1

Given the diagram above (list and pointers/indexes), find all of the integer pairs that sum to 6.

Use the following algorithm (The list must be sorted in ascending order)

idxa = 0 idxb = len(nums) - 1 target = 6 while idxa < idxb: total = nums[idxa] + nums[idxb] if total == target: print-the-match-info if total >= target: move-an-index else: move-an-index

Turn the algorithm into a function. Turn it into a generator?

Write a program that calls the function with the information above. Print each pair and the total number of pairs.

Question: Why must the list be sorted for this algorithm?

Project #2

Create a program that allows a user to enter a list and a target sum.

Questions:

Project #3

Given a string, use two pointers/indexes to find the longest sequence of the same character. Display the character and its location in the list.

For example:
string = 'aghrsssssdsfgggghaannozzxzttrrrrvvvvvvzzzz' ... ... ... print the longest sequence character is "V" print the location is index 32 to 37 (inclusive)

Create a program that allows a user to enter a string. Find the longest substring matching a single character. Display the character and its location.

Project #4

See Project #3.

Do the same thing but find the longest sequence of non-matching characters.

Create a program that allows a user to enter a string. Find the longest substring of non-matching characters. Display the characters and their location in the list.