Lab #10

Before Kattis

This lab relates to topics discussed in class 13 and class 13b.

Let’s make a small program with classes. We’ll make a program that will store a contact list of people.

We want 2 classes, and another script to interact with the objects we create.

Person

Start by making a simple object to store information for a Person. Create a Person class.

If using PyCharm or Spyder, open an empty python file in your working directory/project. Call it Person.py

Attributes

  • A first name

  • A last name

  • An email

There, that’s it for now. We can add more later if we want.

Methods

  • Constructor

  • Getters/setters for the attributes.
    • eg: getFirstName(self), setFirstName(self, newFirstName)

  • The repr method (__repr__)
    • You decide what should be returned here to give us a string representation of the object. Just come up with something reasonable.

    • My output looked like this

      Smith, Greg:  gsmith@xfts.ca

  • The equals method (__eq__)
    • You decide what it is for two Person objects to be equivalent.

That’s about it for the Person object. It’s pretty simple.

ContactList

Now let’s make a class called ContactList to hold onto a list of Person objects. If using PyCharm or Spyder, open another empty python file in your working directory/project. Call it ContactList.py. Be sure to import the Person class at the top of this file.

Attributes

  • A list of friends

That’s all I think we’ll need for now.

Methods

  • The constructor
    • We don’t really need to give this any parameters actually (except we need self).

    • Here we could pass it a list I guess, but let’s not.

    • We do want a list attribute though.

  • addFriend — a method to add a Person object to the contact list.
    • See that. We need to create a Person OBJECT.

    • Here we have a design decision to make. Which parameters should the method get?
      • Should we pass a reference to a Person object to the method

        def addFriend(self, aPerson):

      • Should we pass the details to make a person to the method?

        def addFriend(self, fName, lName, email):

    • Hard to really say actually. For now let’s go with the 2nd option.

    • The choice will really impact how we interface with the objects.

  • __repr__
    • You decide what string should be returned. Just be sure to use the __repr__ for the Person objects.
      • How do we convert something to a str?

    • My output looked like this, but do whatever you want really (as long as it makes sense)

    Contact List
    0 Sehguh, Semaj:        ssehguh@xfts.ca
    1 Smith, Greg:  gsmith@xfts.ca
    

If you are stuck on any of this, you should have a look here.

Using Them Together

Create ANOTHER file and put this in it:

1#from ContactList import *      # Only need if using multiple files
2
3friendsList = ContactList()
4friendsList.addFriend('Semaj', 'Sehguh', 'ssehguh@xfts.ca')
5friendsList.addFriend('Greg', 'Smith', 'gsmith@xfts.ca')
6
7print(friendsList)

Everything should work. If not, ask for help.

More Special Functions

Add these to the ContactList class.

  • __len__ — A method that returns the length of the ContactList (the length of the list of friends)
    • I wonder how we can then use this to get the len of the object?

    • Try adding this to the script we’re running to test it out

      print(len(friendsList))

    • len calls the __len__ method.

  • __getitem__ — A method that returns a Person object from a given index in the list of friends.
    • Try adding this to the script we’re running to test it out

      print(friendsList[1])

    • indexing with [x] calls the __getitem__ method.

Testing

You should be able to run the below code and everything should work correctly. If not, ask for help.

 1# Only need these if using multiple files
 2#from Person import *
 3#from ContactList import *
 4
 5friendsList = ContactList()
 6friendsList.addFriend('Semaj', 'Sehguh', 'ssehguh@xfts.ca')
 7friendsList.addFriend('Greg', 'Smith', 'gsmith@xfts.ca')
 8
 9print(friendsList)
10print(len(friendsList))
11aFriend = friendsList[1]
12print(aFriend)
13
14# This just makes sure that aFriend is
15# pointing to a a Person object.
16# If it is, nothing special happens
17# If it's not, it will crash the program
18assert isinstance(aFriend, Person)
19
20print(aFriend.getFirstName())
21print(aFriend.getLastName())
22print(aFriend.getEmail())
23aFriend.setFirstName('Not')
24aFriend.setLastName('A')
25aFriend.setLastName('Thing')
26
27print(friendsList)

Make sure it makes sense to you why when we print out friendslist we now wee an altered person.

Add Some Things

Now that you have everything working, go add some additional attributes to the Person class, update methods, add new methods, use the methods in the ContactList class, etc. Basically I just want you to go nuts and see what you can do.

Maybe go even loop up other special python methods and see if you can hac them to work in weird ways.

Kattis Problems

Can you do these with something other than lists? In fact, you might have to for the runtime requirements. You may come up with a perfect solution that will be correct 100% of the time; however, a correct solution is not necessarily a good solution.

  1. https://open.kattis.com/problems/everywhere

  2. https://open.kattis.com/problems/babelfish

  3. https://open.kattis.com/problems/oddmanout

  4. https://open.kattis.com/problems/securedoors

  5. https://open.kattis.com/problems/modulo

LeetCode Problems

The following problem is a classic CS programming problem.

  1. https://leetcode.com/problems/two-sum/

If you finish the lab, go back and work on incomplete problems from previous labs.

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.

If you have somehow finished everything so far, go check out LeetCode. Sort the problems by Acceptance (click the table header) and start seeing if you can solve some of these problems.

ENSURE WE HAVE RECORDED YOUR COMPLETION. FAILURE TO DO SO WILL RESULT IN A GRADE OF 0!