Skip to content
Snippets Groups Projects
Commit 68225ba0 authored by ys554's avatar ys554
Browse files

lab 5 jawns

parent 87ca9e87
Branches
No related tags found
No related merge requests found
import sys
filename = sys.argv[1]
file = open(filename, "r")
dictID = {}
for data in file:
row = data.strip("\n")
data = row.split(" ")
id = data[0]
data.pop(0)
fullName = ""
for name in data:
fullName += name + " "
dictID[id] = fullName
#I am sorting the dictionary keys, but it is not sorting for some reason.
#I also tried making all the keys as a separate list and then sort it,
#but it kept on sorting the values in alphabetical order instead of keys.
#I am not sure why :(
sorted(dictID.keys())
for key in dictID:
print(key, " ", dictID.get(key))
lab05/l5 0 → 100644
Chapter 1 exercise
1.1 - The name and value choices are bad. ch and EOF are unclear, and should not have all capital names.
1.2 - Because it is an active function, I would change the names and add comments
int isSmalelr(char *s, char *t) // function to compare two char values and return true if so
1.3 - ... I read it
1.4 - if (! strcasecmp(c,'y'))
return:
newLength = (length < bufSize) ? length : bufSize;
flag? 0:1;
quote = (*line == null)? 1: 0;
if (val)
bit = 1;
else
bit = 0;
1.5 - Inserting chars and integers
1.6 - Possible outputs are: 1, 2, and 4
1.7 - switch(istty){
case (stdin):
break;
case (stdout):
break;
case (stderr):
break;
default:
return 0;
}
1.8 - Count is incremented first and total is not defined. If total = 0, this statement would never be executed
1.9 - The name of the definition of choice. Definition name sould not be in all caps fir an active function.
1.10 - By making definitions clearer and use variable type like float, int, double. Executing function with different variables could cause wrong output or syntax errors.
1.11 - First comment does not specify what the function does, but it just comments on return type
Comment in the center does not specify what to do in the if statement
Increment line counter does not give what the function is doing
import sys
import re
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()
match = pattern.search( l )
if match :
nr = match.groups()
if not nr[0] :
aCode = DEF_A_CODE
else :
aCode = nr[0]
if len(nr)<=3:
print("area code: " + aCode + \
", exchange: " + nr[1] + ", trunk: " + nr[2])
elif nr[3]is not None:
print("area code: " + aCode + \
", exchange: " + nr[1] + ", trunk: " + nr[2] + ", extension: " + nr[3])
else :
print("area code: " + aCode + \
", exchange: " + nr[1] + ", trunk: " + nr[2])
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
#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.\-]*
((\d{4})|(\d{3})|(\d{2})|(\d{1}))*
#[(\d{4})(\d{3})(\d{2})(\d{1})]*
#(\d)*
'''
# 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()
import sys
filename = sys.argv[1]
file = open(filename, "r")
for data in file:
data = data.strip("\n")
score = data.split(",")
print("score:", score)
name = score[0]
score.pop(0)
total = 0
for scores in score:
total += int(scores)
avg = ((total)/len(score))
print(name, ",", avg)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment