Organize Transcript
Start visual description. The instructor’s screen is shared where he shows how to prepare and write code for a particular program. He demonstrates the steps as he describes each aspect. The instructor can be seen in the top right-hand corner in a small box. End visual description.
[00:00:00]
Instructor: Alright, let’s go through the organized activity together. So we’re
going to write a program that organizes user input, right? So organized in our
case means that all the characters are reordered into this sequence.
[00:00:14]
We have the lowercase letters, uppercase letters, the digits, and then everything
else with all the white space removed. So, hello, what is your name becomes
ellowhatisyournameH comma question mark, right? BYU is my favorite school…
ismyfavoriteschoolBYU. 3.14159 is a loose approx for PI…
isalooseapproxforPI314159 dot dot dot.
[00:00:37]
So we need to essentially like group letters from each category together. So, how
can we do this? Sketching things out. First we see we need a place to store all the
different letters we find in each group. We’re going to be going through each
letter at a time.
[00:01:02]
Well that, you know, we are familiar with this, you know, four letter in string, and
then we have a given letter and we just need to put it in a different place, right?
Let’s see. You know, so we got the string of letters and then for each one, one will
go one place, one will go to another, one to go to that place. Maybe somewhere
else, right? The third spot.
[00:01:26]
So we’re going to kind of sort things out so we know how to step through all the
letters. And then we need to add those to different places. Well, what could we
use for each to represent these different groups? In this case, I would just use
different strings, right? Here’s the string that we add all these two, here’s a string,
we add these other ones to, et cetera, right? So let’s sketch that out in in code
here. Let’s go to organize.py.
[00:01:59]
When you have an empty file, start with the main, pass. If name equals main.
Great. And then in here, we want to look through the text and then we want to
organize the pieces, right? Organize the letters into their respective categories.
There we go. So this is what it means to organize. Organize means this.
[00:02:46]
Now the overall function of the program, let’s come back and look at this again,
right? We have this pattern here, grabbing text and then printing something out.
I’m grabbing text and printing some out, right? So that pattern, we’ve just got
this loop. You give me an input and I’ll print your input out with some change to
it.
[00:03:07]
So overall we’re going to say loop on inputs. Print input with modifications. So
those are the basic ideas here. Well, in main, let’s do the overall piece, so we can
say all true phrase or text, text equals input. What prompt do we want to use
here? Text. Text. And then we want to print out the text that’s been modified
because we call that function anything we want, maybe we can call it organize.
And organize that text and print it out.
[00:03:51]
But if you don’t give me text, if not text, this is just another way of saying this. If
text equals empty, same thing, but just another way that Python lets you do it. If
not text, break, we’re done. Otherwise organize the text.
[00:04:13]
What does organize mean? Let’s create a function. Let’s move our comment here
up to here. It’s not going to let me. Here we go. So what does organize mean
here? We’re going to loop through the text and organize their letters into
respective categories. And then concatenate them, concatenate the categories
together. In what order did we want them? We wanted lower, upper, digits,
everything else. Lower, upper, digits, and else.
[00:05:00]
Well, I know how to loop through the text. For a letter in text, we’re going to do
something. Great. We need categories. So before we start looping, let’s say
lowers equals empty, uppers equals empty, digits equals empty, and then others
equals empty. I can’t use else because that’s a keyword in Python. It’s highlighted
blue. I can’t use that as a variable and so I have to come up with something else.
[00:05:31]
So I got my lowers, my uppers, my digits, my others. So for letter in text, now I’ve
got a given letter. Which category does it go in? Well, if letter dot is lower, then
lowers plus equals letter. Elif letter dot is upper, uppers plus equals letter. There
we go.
[00:06:03]
That’s what we want. Elif letter dot is digit. Upper…no, not uppers, I’m sorry.
Digits plus equals letter. Good, others. Everything else, right? Then what about
the white space? We’re supposed to remove the white spaces that are coming up
here in respective categories, but remove white space.
[00:06:34]
So elif letter dot is space something, maybe we’ll get back to this, otherwise
others plus equals letter and now we can come back to this something. Think
about that for a second. If the letter is space, what we’re supposed to do with it,
well, not copy it over, right? Drop it, forget it.
[00:06:59]
So actually pass is a perfectly good use case right here. We’d say letter is space
and then we just pass, skip that letter, don’t do anything with it. Otherwise we
append this in. Great.
[00:07:10]
Another word. Yeah, we’ll just stick at that. And then once we’re all done, we’ve
gone through all of our letters, we’ve added them to lowers, uppers, digits, and
others, we want to concatenate them.
[00:07:22]
How do we concatenate two strings? We’ll use the plus sign, right? So I can
simply say return lowers plus uppers plus digits plus others. And we’ve brought it
all together.
[00:07:37]
Let’s give it a try.
[00:07:41]
So text, let’s get some numbers and some symbols and some letters, and now
the letters in front, then the numbers and symbols, right? We, I guess we needed
some uppercase. Apple. Pear. 1234. 8910, whatever. Go Team!
[00:08:04]
Ok. And now all the lowercase, all the uppercase, all the digits, all the
punctuation. There you have it. That’s how you can do the organized problem.