def new_split_iter( expr )
divide a character string into individual tokens, which need not be separated by spaces (but can be!)
also, the results are returned in a manner similar to iterator instead of a new data structure
expr = expr + ; # append new symbol to mark end of data, for simplicity
pos = 0 # begin at first character position in the list
while expr[pos] != ; # repeat until the end of the input is found
- to be filled in by the student, using yield as necessary
yield ; # inform client of end of data
You may identify whether an individual character is a numeric digit via expr[pos].isdigit() There are similar funtions isalpha for letters and isalnum for alphanumerics (letters or digits).Where Python iterators fall shortTo qualify as an iterator in Python, it is sufficient to support two operations: get started (usually through a call to iter()) obtain the next element, (through an explicit or implicit call for next())Unfortunately, this is insufficient for the parser in this assignment. Consider the example 1 + 2 * 3. The function that parses products will use the next operation to obtain the plus sign, and determine that it is not a multiplication operator, returning control to the function that parses sums. But if that function were to ask for the next symbol, it would move forward to the 2 and not see the plus sign. One could consider having every function pass its last scanned symbol to its caller, but that would likely make the interface
look very clumsy.The better solution, in the instructors opinion, is to extend the definition of the iterator to allow it to examine data without moving forward. In fact, other programmers believe the same thing there is a proposed package online called itertools that wishes to add a great deal more functionality to the default languages iteration. In fact, the instructors solution was based partly on seeing the proposed interface to the more powerful iterator (but without seeing any of the implementation).The file peekable.py is provided free for this course and for student use within the course. It is expected to never be modified by the students or submitted in solutions. This example follows the second model from the recitation for an iterator, but can its functionality to any other variety of iterator.Assignment SpecificationsThe student submission will is to consist of two files: A completed newsplit.py based on what was given above, defining a function new_split_iter that accepts a character string, and continually uses the yield statement to return the individual tokens as character strings. A file named infix2.py that very much resembles the infix1.py from Homework 1. The primary difference is that the function parameters will no longer consist of a list and an integer subscript, but will instead receive all data through an iterator.Here appear a very few select lines from the instructors solution, as it appears at the time of this writing:# import peek functionality for iterators
# and maybe the splitter, if you need it
from peekable import Peekable, peek
from newsplit import new_split_iter