ProgrammingMethodology-Lecture13.pdf

(56 KB) Pobierz
Programming Methodology-Lecture13
Instructor (Mehran Sahami): So welcome back to Week 5 of CS 106A. We're getting
so close to the middle of the class, so a couple of quick announcements before we start.
First of which, there are three handouts. Hopefully, you should pick them up. There’s this
week’s section from some string examples, and kind of some more string examples that
we're gonna go over today.
And now, since we're getting to the middle of the quarter, it’s that time for the mid-term
to be coming up. So, in fact, the mid-term is next week. It’s a week from Tuesday. I
know. The quarters go by so quickly. If you have a conflict with the mid-term, and by
conflict, I mean an unmovable academic conflict.
Like, if you have another class at the same time, not like, oh, yeah, Tuesday night, you
know, October 30, yeah, around dinnertime, I was gonna go out with some friends for
dinner, but the mid-term is really cramping my style so I can’t take it then. That’s not an
unmovable academic conflict. So if you have an unmovable academic conflict with the
mid-term time, check the schedule.
Send me email by 5:00 p.m. this Wednesday, so you have a few days to do it, 5:00 p.m.
by this Wednesday. Don’t just tell me you have a conflict. Let me know all the times you
are free next week to be able to take the alternate exam 'cause what I’m gonna do is a
little constraint satisfaction problem. I’m gonna take everyone who’s got a conflict with
the mid-term, find out the time that everyone can take the exam an alternate time.
Hopefully, there will be one, and we'll schedule an alternate time. So only email me if
you have an unmovable academic conflict. Make sure to list all the times you're free, and
let me know by 5:00 p.m. Wednesday 'cause after 5:00 p.m. Wednesday, I figure out
what the time is, and we ask for room scheduling. And that’s it. I can’t accommodate any
more requests after that. So 5:00 p.m. Wednesday, know it, live it, learn it, love it, mid-
term.
A couple of things about the mid-term you should also know, one of which is it is an
open-book exam. It will be all written so leave your laptops at home. You won’t be using
a laptop. But if you ever get a job in computing where someone says, hey, guess what,
you have to memorize, like, the Java Manual, don’t take that job. So in computer science,
we say, hey, you don’t need to memorize all this stuff 'cause people, actually, look it up
in the real world when they need it.
So it’s an open-book, open-note exam. You can bring in printouts of your programs, and I
would encourage you to do that. You can bring in your book, only the book for the class,
by the way. Don’t bring in a whole stack of books. And the reason why we make that
restriction is because we – actually, in the past, there has been bad experiences with
someone who brings in a whole stack of books. And they spend so much time trying to
look things up in a whole stack of books that they just run out of time on the exam. It
doesn’t make any sense.
All the information you're gonna need for the exam will be in your book, and in the Karel
Course Reader. So open book, that’s course text, open Karel Course Reader, open any
handouts or notes you’ve taken in the class, and printouts of your own programs. So you
can bring all that stuff in. Another thing about the mid-term is you may be wondering,
hey, what kind of stuff’s on the mid-term, so on Wednesday I will give you an actual
sample mid-term and the solutions for it.
I would encourage you to actually try to take the mid-term, the sample that I give you
under sort of time constraint conditions. So you get a sense for 1.) What kind of stuff is
on the mid-term? 2.) How much time you're actually spending thinking about the
problems versus looking stuff up in books. And that will give you some notion of what
you actually need to study to sort of remember quickly versus stuff you might want to
look up.
Another good source of mid-term-type problems is the section problems. So like, the
section handout this week, actually, has more problems than will probably be covered in
section, just to give you some more practice. So section problems are real good. I’ll give
you a practice mid-term. That will give you even more sort of examples.
And if you want still more problems to work through, I’d encourage you to work through
the problems of the exercise at the back of every chapter of the book. Those are also
similar in many cases to the kinds of problems that’ll be on exams. So you'll just have
problems up the wazoo if you want to deal with them, okay. So any questions about the
mid-term next week? We can all talk about it more as the week goes on, next week,
Tuesday.
All right, so with that said, any questions about strings? We're gonna do a bunch more
stuff today with strings and characters. But if there’s any questions before we actually
dive in to things, let me know now. And if you could use the microphone, that would be
great. One more time, take those microphones out, hold them close to your heart. Air and
gear, they're lots of fun, they're your friend. Keep the microphone with you.
Student: Actually, sorry, about the mid-term, is it going – what’s the cutoff of the mid-
term in terms of, like, caustes.
Instructor (Mehran Sahami): Right, so the mid-term for stuff you need to know, the
cutoff will be Wednesday’s class. So, basically, you'll have a whole week of material that
you won’t need to be responsible for that will be from this Wednesday up until the mid-
term. The other thing, though, is to keep in mind that a few people have asked, well, do I
need the book versus your lectures for the mid-term.
You need to know the lectures, and you need to know all the material from the book that
is covered with respect to lectures, which is most of the material from the book. But
there’s a few cases where we go over something very quickly in class, or I say refer to
this page of the book or whatever. That stuff you're responsible for knowing. Stuff that
I’ve, like, explicitly told you you don’t need to know, like, polar coordinates, aren’t
gonna be on the exam, okay.
So the exam will be more heavily geared towards stuff from lecture, but you still should
know all the stuff from the book that we've kind of referred to in lecture as we've gone
along, allrighty. All right, so let’s dive into our next great topic. Actually, it’s a
continuation of our last great topic, which is strings. And so, if we think about strings a
little bit, one of the things we might want to do with strings, is we want to do some string
processing that also involves some characters.
So how are we gonna do that? One thing we might want to do is let’s just do a simple
example to begin with, which is going through a string, and counting the number of
uppercase characters in the string. And the reason why I’m gonna harp on strings a whole
bunch – we talked about it last time – we’re gonna talk about it this time – guess what
your next assignments gonna be. It’s gonna be all about string processing. So it’s good
stuff to know, okay.
So we might want to have some function. Count uppercase. And that’s a function I’ve
actually given to you in one of the handouts, so you don’t need to worry about jotting
down all my code real quickly, but you might want to pay close attention. And what this
does, is it gets past some string, STR, and it’s gonna count how many uppercase
characters are in that string. So it’s gonna return an int.
And let’s just say this is part of some other program, so we'll call this private, although
you could make it public if it was in some class that you wanted to make available for
other people to use. So if we want to count the number of uppercase characters, what do
we want to think about doing? What’s the kind of standard idiom that we use for strings?
Anyone remember? What? We want to have a foreloop, [inaudible] somewhere over
here.
Yeah, it’s just raining candy on you. We want to have a foreloop that goes through all the
characters of the string, sort of counting through the character. So we can do that by just
saying for N2i equals zero, “i” less than the length of the string, right. So STR.length is
the method we use to get the length of the string, and then i++. And this is gonna loop
through all the characters of the string. Okay, where actually it’s gonna loop through
some number of times, which is the number of characters in the string.
Now we want to pull out each one of the characters, individually, to check to see if it’s an
uppercase character. What method might we use to do that? Get a character out of a
string in a particular position. Come on, I’m begging for it. Char at – it’s like where’d it
go? It’s just gone, char at, and we'll just for the delayed reaction, we'll do it in slow mo.
Anyone remember “The Six Million Dollar Man,” that show? No, all right. Get another
man.
I’m just getting so old, I gotta hang it up. And the thing is, I’m not that much older than
you. But it’s just amazing what big a difference a few years makes. So char CH is going
to be from this string. Were gonna pull out the char at apposition i. So now, we've
actually each. We're gonna loop through each character of the string, pulling out that
character, and we want to check to see if the character’s uppercase.
We could actually have an if statement in here to check to see if that CH is in between
uppercase A and uppercase Z, which is kind of how you saw last time we could do some
math on characters. We're gonna use the new funky way, which is to actually use one of
the methods from the character class, and just say if. And the way we use the methods
from the character class, we specify the name of the class here as opposed to the name of
an object because the methods from the character class are what we refer to as static
method.
There is no object associated with them. There are just methods that you call and pass
into character. Is uppercase because this returns a Boolean, and will pass at CH to see if
CH is an uppercase character, okay. If it is an uppercase character, okay. If it is an
uppercase character, we want to somehow keep track of the number of the uppercase
characters we have. So how might we do that? Counter, right.
So have some int count equals zero, up here, that I want initialized. Who said that? It
came from somewhere over here. Come on, raise your hand. Don’t be shy. It’s a candy
extravaganza. So if character is uppercase, CH then got count, we're just gonna add 1 to.
Otherwise we're not gonna increment the counts. It’s not an uppercase character. And
then, we end the foreloop. So this is gonna go through all the characters of the string. For
every character check seeks the uppercase.
If it is, increment our count, and at the end, what we want to do is, basically, return that
count, which tells us how many uppercase characters were actually in the string, okay. Is
there any questions about this? This is kind of like an example of the sort of vanilla string
processing you might do. You have some string. You go through all the characters of the
string. You do some kind of thing for a character of the string. In this case, were not
creating a new resulting string. We're just counting up some number of characters that
might be in the string.
So we can do something a little bit more funky. This is kind of fun, but it’s sort of like,
yeah, just basic kind of string and character stuff. Let’s see something a little bit more
funky, which is actually to do some string manipulation to break the string up into
smaller pieces. And so what we want to do is replace some occurrence of a substring in a
larger string with some other sting, sort of like when you work on [inaudible], when you
do Find/Replace.
You say, hey, find me some little string, or some little work that’s actually in my bigger
document. I’m gonna replace it with some other word. We're actually gonna implement
that as a little function, okay. So what this is gonna do, we'll call this replace occurrence
just to keep the name short, but, in fact, all we're gonna do is replace the very first
occurrence in a string. So we're gonna get past int some string, STR, and what we want to
do is, basically, have some original string, which is the thing that we want to replace,
with some replacement string.
So we're gonna get past three parameters here. Will call this RPL for replace, okay.
Which is the large string, a piece of text that I want to replace some word in, the original
word that I want to replace, and the thing that I want to replace it with, okay. And so what
I want to do because strings are immutable, right. I can’t change the string in place. I
have to actually return a new string, which has this original replaced by this string.
So, this puppy’s gonna return the string, and we'll just make this private again, although
we could have made it public if we wanted to have it in the library that other people
would use, or a class that other people would use, okay. So how might we think about the
algorithm for replacing this original string with the replacement. What’s the first thing we
might want to think about that we want to do with the original string.
Do, do, do, do, a little concentration music. We want to find it, right. We want to see if
this original string appears somewhere on that string, right because if it doesn’t we're
done. Thanks for playing, right, but that’s actually the good things for playing. It’s sort of
like you got no more work to do. And there’s, actually, some methods from the string
class that we can use to do that.
So there’s a string in the string class called “index of.” And what index of does is I can
pass it some string, like the original string I want to look up, and it will return to me a
number. That number is the index of the position of the first character of this string if it
appears in the larger string. So the larger string is the one that I’m sending the message
to, and I’m asking it do you have this original string somewhere inside you.
If you do, return me the index of its first occurrence. And if you don’t, it returns a
negative 1. So I’m gonna assign this thing to some variable I’ll call index, and first of all,
I want to check to see if I have any work to do. If index is not equal to negative 1, then I
have some work to do. If it is equal to negative 1, that means hey, you know what, you
want it to replace this original string, inside string STR. That original string doesn’t exist,
so I got no work to do.
You just called, like, find and replace in the word processor, and the thing you wanted to
find wasn’t there, okay. So in that case, all I would do is I would just return STR, right.
Sort of unchanged, if I assume that I’m not doing what’s inside the braces. If I do find
that string, though, I’m gonna get some index, which is not negative 1, which is the
position of this original string.
So let’s do a little example just to make this a little bit more clear what’s going on. So if
we were to call this function – do, do, do, do, do – and pass in the string, STR. So here’s
STR that we're gonna pass in. We'll just put it in a big box, and we'll say, at this point in
life, everyone’s just friendly. So we say Stanford loves Cal, right. Sometimes you have to
distort reality in order t make an example. All right, so we have Stanford loves Cal.
Zgłoś jeśli naruszono regulamin