Skip to content
Snippets Groups Projects
Commit c1a62e7d authored by hdd29's avatar hdd29
Browse files

First edition of lab 05

parent 0433e074
Branches
No related tags found
No related merge requests found
#!/usr/bin/env python3
#
# parsePhoneNrs.py - an example of 'grouping' - extracting parts of a match
#
# Kurt Schmidt
# 7/06
#
# Python 3.5.2
# on Linux 4.4.0-36-generic x86_64
#
# Demonstrates: regexp, re, search, groups
#
# Usage: By default, reads telNrs.txt . You may supply a different filename
#
# Notes:
# The pattern:
# Note that it is not perfect, but allows a bit of leeway in how we
# write a phone #. No extensions.
# Of course, only handles US-style numbers
#
# EDITOR: cols=120, tabstop=2
#
import sys
import re
stderr = sys.stderr
DEF_A_CODE = "None"
def usage() :
print( "Usage:" )
print( "\t" + sys.argv[0] + " [<file>]" )
def searchFile( fileName, pattern ) :
fh = open( fileName, "r" )
for l in fh :
l = l.strip()
# Here's the actual search
match = pattern.search( l )
if match :
nr = match.groups()
# Note, from the pattern, that 0 may be null, but 1 and 2 must exist
if (nr[0] is None):
aCode = DEF_A_CODE
else:
aCode = nr[0]
if nr[3] is None:
ext = "None"
else:
ext = nr[3]
print( "area code: " + aCode + \
", exchange: " + nr[1] + \
", trunk: " + nr[2] + \
", extension: " + ext)
else :
print( "NO MATCH: " + l )
fh.close()
def main() :
# stick filename
if len( sys.argv ) < 2 : # no file name
# assume telNrs.txt
fileName = "telNrs.txt"
else :
fileName = sys.argv[1]
# for legibility, Python supplies a 'verbose' pattern
# requires a special flag (re.VERBOSE)
#patString = '(\d{3})*[- .)]*(\d{3})[- .]*(\d{4})'
patString = r'''
# don't match beginning of string (takes care of 1-)
(\d{3})? # area code (3 digits) (optional)
[- .)]* # optional separator (any # of space, dash, or dot,
# or closing ')' )
(\d{3}) # exchange, 3 digits
[- .]* # optional separator (any # of space, dash, or dot)
(\d{4}) # number, 4 digits
[x .-]* # optional separator (any # of space, dash, x, or dot)
(\d{1,4})?
'''
# Here is what the pattern would look like as a regular pattern:
#patString = r'(\d{3})\D*(\d{3})\D*(\d{4})'
# Instead of creating a temporary object each time, we will compile this
# regexp once, and store this object
pattern = re.compile( patString, re.VERBOSE )
searchFile( fileName, pattern )
main()
1.1 Defining True = 0 and False = 1 is unconventional and may cause misunderstanding in later codes if the reader skim through the code and not noticing the define line. Possible confusion is having the very conventional forever loop: while True to not work at all.
1.2 For this snippet of code, I believe that the author tried to write a function that returns 1 when the left argument is smaller than the right argument, and 0 when left_argument is larger or equal to right_argmuent
I suggest writing it this way:
int isSmaller(char *leftArg, char *rightArg)
{
return (strcmp(leftArg, rightArg) < 0);
}
1.3 I think the name of the variables in the code snippets is unacceptable as it is too long, hard to read and impossible to write without autofill. The variables mentioned are: SMRHSHSCRTCH, S_IFEXT, MAXRODDHSH
1.4
a. if ((c != 'y') && (c != 'Y'))
return;
b. if length >= BUFSIZE
length = BUFSIZE;
c. flag = !flag
or
if (flag == 0)
flag = 1;
else
flag = 0;
d. quote = (*line == '"')\
e. bit = val & 1
1.5
the read(&val) and read(&ch). The order in which these will be read is unpredictable and may cause confusion when running the code if read(&ch) is evaluate first while the arguments are unconsciously assumed by the reader to be read left to right
1.6
2 3
1.7
a. if (!stty(stdin) && !istty(stdout) && !istty(stderr))
return 0
b. return retval
c. for (k=0; k<4; k++) {
scanf("%lf", &dx);
x += dx;
}
1.8
for (int count=0; count<total; count++) {
if (this.getName(count) == nametable.userName()) {
return (true);
}
}
1.9
As stated a few lines above from the book, there should be parentheses around the variable, in this case, c, to avoid it to be evaluated multiple times throughout the macro. Meaning: everytime c is > or = to 0 then c will be discarded and another written c will be evaluated if they are <= 9
1.10
#define MPerFt 0.3048
#define FtPerM 3.2084
#define FtPerMile 5280.0
#define MilePerKm 1.609344
#define Mile2PerKm2 2.589988
1.11
comment 1: totally wrong since it is declared a "void" function, therefore the function will not return anything
comment 2: comment contradicts the purpose of test. The condition to be met is either when the number is larger than the maximum or if it is an ODD number, not even number. And also did not specify what max is supposed to be
comment 3: the comment inside the function is duplicate of each other: does not specify the differece between the 2 commands,and also a repetition of the comment before the function
+01 (800) 638-2772 Consumer Product Safety Commision
800 296 1996 EPA, Ozone Protection Hotline
(800) 621-FEMA x3362 FEMA, assistance (good luck!)
TTY (800) 462-7585 FEMA, assistance (good luck!)
895-2000 Drexel Switchboard
+1 (212) 371-6965-296 Fictional extension at the Ukrainian Embassy, NYC
(215) 895-2000 x2669 (eg. of extension)
215.895.2000 x7 (eg. of extension)
202-456-1414 White House Switchboard
What a country! Can get the time for free, but not my president!
TTY/TDD 202-456-6213 White House - Comments
+48 225 042 000 US Embassy, Warsaw, Poland
+357 (800) 638-2772 Consumer Product Safety Commision
###800 296 1996 EPA, Ozone Protection Hotline
###(800) 621-FEMA x3362 FEMA, assistance (good luck!)
###TTY (800) 462-7585 FEMA, assistance (good luck!)
###895-2000 Drexel Switchboard
###(215) 895-2000 x2669 (eg. of extension)
###215.895.2000 x7 (eg. of extension)
###202-456-1414 White House Switchboard
### What a country! Can get the time for free, but not my president!
###TTY/TDD 202-456-6213 White House - Comments
###+48 225 042 000 US Embassy, Warsaw, Poland
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment