Identify whether an individual character is a numeric digit

Assignment Help Python Programming
Reference no: EM131215005

Problem Description

The previous assignment assumes its input includes spaces between every token in an expression, but many programmers tend not to use so many spaces, if the expression is unambiguous. For example, both of the following expressions would be considered to be equivalent:

1 + 2 * 3 1 + 2*3

If the expression on the right was provided as input to the first homework assignment, the lack of spaces would cause the "2*3" to be interpreted as a single token, so it would not perform any multiplication, and might not make any sense of the token at all.

Of course, it is a simple concept to imagine adding functionality to take a full character string, and then to create a new version that has all the spaces included. However, that requires both the time to create that new version, but also the memory to hold it. Any programmer intending for maximal efficiency and minimal time should like to eliminate that extra step.

In fact, the previous homework itself had an intermediate step creating a new variable -- it took a single character string, and created a list of shorter character strings, and then used a subscript variable to visit each element of that list.

This goal of this assignment is to eliminate all of those middle steps and to interpret the numeric expression directly from the original character string, without taking any time allocating new variables.

Identifying Tokens

The first phase of this project is to take the input string and divide it up into tokens (individual numbers and operators). The goal here is to let a caller ask for each token in turn and to supply it on demand.

The simplest approach is one presented in a recitation assignment that only requires implementing a single function -- a generator that yields one token at a time as it finds it in the input string.

Here is an intentionally incomplete illustration from the instructor's solution, as it stands at this time of writing:
(in newsplit.py)

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 short

To 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 instructor's 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 language's iteration. In fact, the instructor's 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 Specifications

The 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 instructor's 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

def eval_infix_sum(iterator)

"""evaluate sum expression (zero or more additions or subtractions), using an iterator"""

..... code that no longer uses array subscripting

def eval_infix_iter(iterator)

"""evaluate an expression, given an iterator to its tokens"""

return eval_infix_sum(Peekable(iterator))

def eval_infix(expr)

"""accept a character string, split it into tokens, then evaluate"""
return eval_infix_iter(new_split_iter(expr))
Specification Details

You may assume:

• Correctly formed expressions consisting of integers and operators
• All digits within an integer are adjacent ("12" is valid, but "1 2" is not)
• All operators are single characters (accepting "//" is not required)
• There will be no division by zero operation in given expressions

You may not assume:

• anything about how many spaces appear between tokens (may be 0, 1, or more)
• anything about how many space characters appear at beginning and end of input
• anything about how many tokens may appear within a given input string
• any assumptions disallowed in the previous assignment about expressions

Unit Testing

Another feature of the Python language that has no equivalent in C++ is the ability to embed test code within each file, which may be used to test the functionality of that file. It includes a conditional test that determines whether that file is being run by itself (for testing) or is being used in a larger project.

Here is a portion of the instructor's infix2.py, following the code shown above:

if __name__ == "__main__":
print ( eval_infix("5 ") )
print ( eval_infix("15 ") )
print ( eval_infix( " 2 * 3 + 1 " ) )
print ( eval_infix( " 2 + 3 * 1" ) )

If the Python environment is told to run infix2.py directly, it will attempt these print statements. On the other hand, the Python environment is told to run some other file, this code is skipped.

Along the same lines, here is a test statement in the instructor's newsplit.py testing the iterator's results:
print (list( new_split_iter( "3+4 * 5" )))

Reference no: EM131215005

Questions Cloud

What professions comprise your career field workplace : In the workplace, many industry cultures may work in the same company or organization. Each of these groups may have different cultures based on the type of work that is done and experiences that they have. Each culture may have a different approa..
Find the coefficient of kurtosis of x : Find the mean of X ;-  Find the variance of X ; -  Find the coefficient of skewness of X ; -  Find the coefficient of kurtosis of X .
Key concept in information systems : Use the Internet or the Strayer Library to research one (1) concept in information systems that you have studied in this course. You may select a topic of your choice or you may use one (1) of the following:
Find a crowded area where there are people : Find a crowded area where there are people for at least fifteen minutes (e.g., mall, restaurant, baseball game, or waiting room). Create a research question and hypothesis that has to do with people's behaviors in this environment. Identify what s..
Identify whether an individual character is a numeric digit : 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).
What is the third moment of this stream : Compute the surprise number (second moment) for the stream 3, 1, 4, 1, 3, 4, 2, 1, 2. What is the third moment of this stream?
Is restorative justice truly victim-centred discuss : Restorative justice Proponents of restorative justice argue for the introduction of restorative justice measures in the modern criminal justice system on the grounds that it is victim-centred. However, is restorative justice truly victim-centred? ..
Calculate the data rate : Most CD music has two channels. Calculate the data rate, in kbps, for each channel of CD music based on its sampling rate and bit depth. If CD music (two channels) is converted to MP3 files at 256 kbps, what is the compression ratio? Show your wor..
Find the conditional mean and the conditional variance : An exponential random variable has a PDF given by fx(x) = exp(-x)u(x).- Find the conditional mean and the conditional variance given that X > 1 .

Reviews

Write a Review

Python Programming Questions & Answers

  Write a python program to implement the diff command

Without using the system() function to call any bash commands, write a python program that will implement a simple version of the diff command.

  Write a program for checking a circle

Write a program for checking a circle program must either print "is a circle: YES" or "is a circle: NO", appropriately.

  Prepare a python program

Prepare a Python program which evaluates how many stuck numbers there are in a range of integers. The range will be input as two command-line arguments.

  Python atm program to enter account number

Write a simple Python ATM program. Ask user to enter their account number, and print their initail balance. (Just make one up). Ask them if they wish to make deposit or withdrawal.

  Python function to calculate two roots

Write a Python function main() to calculate two roots. You must input a,b and c from keyboard, and then print two roots. Suppose the discriminant D= b2-4ac is positive.

  Design program that asks user to enter amount in python

IN Python Design a program that asks the user to enter the amount that he or she has budget in a month. A loop should then prompt the user to enter his or her expenses for the month.

  Write python program which imports three dictionaries

Write a Python program called hours.py which imports three dictionaries, and uses the data in them to calculate how many hours each person has spent in the lab.

  Write python program to create factors of numbers

Write down a python program which takes two numbers and creates the factors of both numbers and displays the greatest common factor.

  Email spam filter

Analyze the emails and predict whether the mail is a spam or not a spam - Create a training file and copy the text of several mails and spams in to it And create a test set identical to the training set but with different examples.

  Improve the readability and structural design of the code

Improve the readability and structural design of the code by improving the function names, variables, and loops, as well as whitespace. Move functions close to related functions or blocks of code related to your organised code.

  Create a simple and responsive gui

Please use primarily PHP or Python to solve the exercise and create a simple and responsive GUI, using HTML, CSS and JavaScript.Do not use a database.

  The program is to print the time

The program is to print the time in seconds that the iterative version takes, the time in seconds that the recursive version takes, and the difference between the times.

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd