Docsity

String Manipulation - Introduction to Computing Using Python - Lecture Slides, Slides of Introduction to Computing

Partial preview of the text

Download String Manipulation - Introduction to Computing Using Python - Lecture Slides and more Slides Introduction to Computing in PDF only on Docsity!

Prelim 1 Review

Spring 2013

CS 1110

String Manipulation

def make_netid(name,n): """Returns a netid for name with suffix n Netid is either two letters and a number (if the student has no " middle name) or three letters and a number (if the student has " a middle name). Letters in netid are lowercase. Example: make_netid('Walker McMillan White',2) is 'wmw2' Example: make_netid('Walker White',4) is 'ww4' Precondition: name is a string either with format ' " ' or ' '; " names are separated by spaces. n > 0 is an int.""" Prelim 1 Review 5

String Manipulation

def make_netid(name,n): """Returns a netid for name with suffix n.""” name = name.lower() # switch to lower case fpos = name.find(' ') # find first space first = name[:fpos] last = name[fpos+1:] mpos = last.find(' ') # see if there is another space if mpos == -1: return first[0]+last[0]+n # remember, n is not a string else : middle = last[:mpos] last = last[mpos+1:] return first[0]+middle[0]+last[0]+n Prelim 1 Review 7

Call Stack Example

  • Given functions to right  Function fname() is not important for problem  Use the numbers given
  • Execute the call: lname_first('John Doe')
  • Draw entire call stack when helper function lname completes line 1  Draw nothing else def lname_first(s): """ Precondition : s in the form" """ " first = fname(s) last = lname(s) return last + ',' + first " def lname(s): """ Prec : see last_name_first""" end = s.find(' ') return s[end+1:] Prelim 1 Review 8 1 2 3 1 2

Call Stack Example: lname_first('John Doe')

def lname_first(s): """ Precondition : s in the form" """ " first = fname(s) last = lname(s) return last + ',' + first " def lname(s): """ Prec : see last_name_first""" end = s.find(' ') return s[end+1:] Prelim 1 Review 10 1 2 3 1 2 s first last s end 4 Omitting this is okay. Line 2 is not complete. 'John Doe' 'John' 'John Doe' lname_first: 1 2 lname: 1 2

Line 1 is complete. Counter is next line.

Example with a Mutable Object

def shift(p): """Shift coords left Precondition: p a point""" temp = p.x p.x = p.y p.y = p.z p.z = temp

  • May get a function on a mutable object >>> p = Point(1.0,2.0,3.0) >>> shift(p)
  • You are not expected to come up w/ the “folder”  Will provide it for you  You just track changes Prelim 1 Review 11 1 2 3 4

Objects: example from A

  • Type: RGB in colormodel.py  Constructor call: colormodel.RGB(r,g,b) --- assuming prior line import colormodel, and r, g, b are ints in interval 0.. Prelim 1 Review 13 Attribute Invariant red int, within range 0.. green int, within range 0.. blue int, within range 0..

Function that Modifies Object

def increase10(rgb):

"""Increase each attribute by 10% (up to 255)

Precondition: rgb an RGB object"""

pass # implement me

Prelim 1 Review 14

Sample step – answer in bold

store in t the value of rgb's red attribute

Which of these is correct? What do the others do?

t = colormodel.RED # refers to something in colormodel t = rgb.red() # call to function "in" rgb t = rgb.r # attribute r of rgb, but there's no such attribute t = rgb.red # . is the way to access t = colormodel.rgb.red # refers to something in rgb in #colormodel Prelim 1 Review 16

Should increase10 have return statement?

Prelim 1 Review 17

Function that Modifies Object

def increase10(rgb): """Increase each attribute by 10% (up to 255)""" red = rgb.red # puts red attribute value in local var red = 1.1*red # increase by 10% red = int(round(red)) # convert to closest int rgb.red = min(255,red) # cannot go over 255

Do the others in one line

rgb.green = min(255,int(round(1.1rgb.green))) rgb.blue = min(255,int(round(1.1rgb.blue))) Prelim 1 Review 19 Procedure: no return

Prelim 1 Review 20 def multcap(x): """Returns: min of nearest int to x1.1 and 255. Precond: x a number""" return min(int(round(x1.1)), 255) def increase10(rgb): """Increase each attribute by 10% (up to 255)"""

alternate solution with massive map

alist = map(multcap, [rgb.red, rgb.green, rgb.blue]) rgb.red = alist[0] rgb.green = alist[1] rgb.blue = alist[2] Procedure: no return

Prelim 1 Review 22

  • Type: Length in module ell  Constructor call: ell.Length(ft,in) --- assuming prior line import ell and ft and in are ints, given: (^) Attribute Invariant feet int, non-negative, = 12 in inches int, within range 0.. inclusive def difference(len1,len2): """Returns: Difference between len1 and len Result is returned in inches Precondition: len1 and len2 are length objects" len1 is longer than len2""" pass # implement me

Function that Does Not Modify Object

def difference(len1,len2): """Returns: Difference between len1 and len Result is returned in inches Precondition: len1 and len2 are length objects" len1 is longer than len2""" feetdif = (len1.feet-len2.feet)* inchdif = len1.inches-len2.inches # may be negative return feetdif+inchdif Prelim 1 Review 23