Let’s start with something simple. Write a function to do a linear search on a list. So, write a function called
find_element(thing, inList) that will search for
inList. Have this function return
True if it exists in the list, and have it return
Create a new class called
SomeClass. This class will be really simple to start.
Make it the class
Write a constructor that literally does nothing. Like, no parameters other than
self. Then, I don’t know, make the constructor print out a message. Something like “I’m code running in the constructor”. This is just so when you test it you know it’s running.
There, that’s it for that class for now.
To finish off part 2, just create an instance of the object in the original script you created. Make sure the message is printed out. If it didn’t something is wrong and you need to ask for help.
Now we’re going to do something very contrived. If you’re wondering But… Why… after this one, you’re normal. Just do what I’m saying at this point.
find_element(thing, inList) function to within the class
SomeClass. Just copy and paste it in. There is one key thing you’ll have to change. What do all methods in a class need as their first parameter? Make that change.
Go back to your original script (the not-class one) and call the
find_element method from the
SomeClass object you created.
For this bit, we’re going to update the constructor for
- Add 2 parameter to the constructor
Now, call the function
find_elementfrom within the constructor for
SomeClass. Be sure to just pass along the parameters. Be sure to save the result into some attribute called
You might have a hard time doing this. If so, slow down and think about this…
Assume in part 3 we had:
1# from SomeClass import * 2 3anObject = SomeClass() 4anObject.find_element(5, [1,2,3,4,5])
anObjectis a pointer to a SomeClass object
Whenever we call a method for an object, we need a pointer to the object to access the attributes/methods.
So, when we move calling the method
find_elementto within the class, guess how we access the method
Write a getter for the only class attribute.
Go back to the not-class part of the code and alter it to be able to still effectively call the
find_element method and then get the result. To do this, you’ll just need to call the constructor. The constructor will call the method for us. We then need to ask the object about the
_was_it_there attribute through the getter.
If this seemed contrived, it was. If you’re wondering what the point was, and how this makes no sense, you’re not really wrong. I’m just showing you how to use these things in weird ways.
Here’s the plan for this part. Instead of doing a linear search on a list, let’s do a linear search through the file.
To do this, we’ll add some file IO to this constructor. Remember, a constructor is just a method/function, so we can basically do anything we want in here that we can do with any other method/function.
inListparameter from the constructor.
Give the constructor a new parameter, call it
fileNameor something. This will be the name of a file we want to open. This file will contain the information for the we want to see if something is in.
1# Order doesn't matter here. 2def __init__(self, fileName, thing): 3 ...
We’ll use the
fileNameparameter to open up a file. If you forget how to do this, good news, it’s in some of the lecture material.
There are a bunch of ways we can go about the next bit, but let’s do it this way. Instead of just searching through the file, let’s just load all the information into a list. Then after it’s in the list, we’ll just search the list.
Create an empty list that we will put the contents of the file in.
Both the file and list objects do not need to be attributes. They can just be local variables (so, like very other variable we’ve used so far). We could make them attributes, but there is no need to here.
- Read the file line by line and put the contents in the list. You’ll probably want to turn these from strings to ints.
Don’t know how to read a file line by line? Good thing Google exists then.
find_element, a method that needs a
inList? We can just call this method from within the constructor again but give it the list we just created.
Be sure we’re setting the attribute
Call the getter for the attribute.
print out the result.
Go back and work on Kattis problems you have yet to solve. I’m betting there are A LOT of the early ones you got stuck on that you could not demolish.
Remember, the Kattis problems are great for practice, and practice is the only way to get good at programming.
At this point, many of the not-so-difficult problems are totally doable by you now. If you’re looking for more problems, or want more practice for tests, etc. sort the Kattis problems by difficulty and have fun.
ENSURE WE HAVE RECORDED YOUR COMPLETION. FAILURE TO DO SO WILL RESULT IN A GRADE OF 0!
If you are in the online section, you must submit the .py (python scripts), not the .ipynb (notebook files). To get the python scripts from Colab, simply select File and in te dropdown menue, hit Download .py.