[1] [2] [3] [4] [5] [6] [7] [8] [9]
Design a regular expression that accepts only strings of letters that begin with a capital letter followed by one or more lower-case letters.
[A-Z][a-z]+
Write a regular expression for each of the following concepts.
| a. | a string of alphabetic letters starting with the letter c |
| b. | a string of digits that doesn't start with 0 |
| a. |
c[a-zA-Z]*
|
| b. |
[1-9][0-9]*
|
Write a regular expression that describes strings containing
the lower-case letter e followed by a digit, with
possibly some intervening spaces. Examples include
3e8
,
the 100K
,
and e2
, but not e-5
.
.*e *[0-9].* (There is a space between the e
and the asterisk.)
Write a regular expression that describes all words of lower-case letters that begin with w and contain an r. Examples include weird, word, wrong, and wear, but not awkward, war-weary, or www.hendrix.edu. (The latter two are bad because they contain characters that are not lower-case letters — a hyphen and a period.)
w[a-z]*r[a-z]*
|
At right is a map of the distribution of ZIP codes within the United States. As you see, ZIP codes in Alaska range from 99500 to 99999. Design a regular expression that accepts only five-digit ZIP codes for Alaska. |
![]() Source: Wikipedia ZIP code article. Image in public domain. |
99[5-9][0-9][0-9]
Complete the Citizen class below so that
the get_id method returns the id parameter provided
when the Citizen is constructed. The run
function at right illustrates how another function might interact with the
Citizen class.
class Citizen():
def __init__(self, id):
def get_id(self):def run():
b = Citizen(2)
d = Citizen(4)
print b.get_id() # displays 2
print d.get_id() # displays 4
class Citizen():
def __init__(self, id):
self.ident = id
def get_id(self):
return self.ident
Write an Account class for tracking the balance of a bank
account. The class should include a variable named
balance for remembering the current balance, and it
should have the following constructor and method.
Account(init)(Constructor)
Constructs a bank account whose current balance is
init.
deposit(value)Increases this account's value by value, returning
the new balance.
The following fragment illustrates how a
program might use the Account class.
acct = Account(10.00)
print acct.deposit(5.00) # displays 15.0
print acct.deposit(3.00) # displays 18.0
class Account():
def __init__(self, init):
self.balance = init
def deposit(self, value):
self.balance += value
return self.balance
Write a Student class for tracking a
student's grades in CSCI 150.
The class includes two int instance variables: one named
total tracks how many points the student has earned thus
far, and another named possible tracks how many possible
points the student could have earned thus far.
The class includes one constructor and two methods.
Student()(Constructor) Constructs a student who has had completed no graded work for the class.
register(value, poss)Registers that this student has completed work with a grade of
value out of a possible poss.
get_percent()Returns the percent of possible points thus far earned. (The method returns 100.0 when no graded work has been completed.)
The following fragment illustrates how a
program might use the Student class.
robin = Student()
print robin.get_percent() # displays 100.0
robin.register(20, 30)
robin.register(10, 20)
print robin.get_percent() # displays 60.0
class Student():
def __init__(self):
self.total = 0.0
self.possible = 0.0
def register(self, value, poss):
self.total += value
self.possible += poss
def get_percent(self):
if self.possible == 0.0:
return 100.0
else:
return 100.0 * self.total / self.possible
Write a class ScoreCard providing the following constructor
and methods.
ScoreCard(par)(Constructor) Constructs a score card for tallying scores, where
par is the goal for each score.
add(score)Adds a score onto this score card. (The easiest way to solve this problem is not to remember each individual score added into the card; however, you may approach the problem that way if you wish.)
get_birdie_count()Returns the number of values added into this score card that are
below par.
get_par_difference()Returns the sum over all scores of the difference between the score and the goal. For example, if the goal is 3, and the scores added are 2 and 5, then the method returns 1, since that is the sum of 2 − 3 and 5 − 3.
The following fragment illustrates how a
program might use the ScoreCard class.
card = ScoreCard(3)
card.add(2)
card.add(3)
card.add(2)
card.add(4)
print card.get_birdie_count() # displays 2, since two scores were below 3
print card.get_par_difference() # displays -1 (= (2-3) + (3-3) + (2-3) + (4-3))
class ScoreCard():
def __init__(self, par):
self.goal = par
self.birdies = 0
self.par_diff = 0
def add(self, score):
if score < self.goal:
self.birdies += 1
self.par_diff += score - goal
def get_birdie_count(self):
return self.birdies
def get_par_difference(self):
return self.par_diff