In the Ants problem, two ants can occupy the same place if one is a container ant that can contain the other; the add_to method must check if either the existing ant can contain the new ant or vice versa, and if so, update the place's ant attribute to point to the container ant and have it contain the other ant; if neither can contain the other, raise an error.
Deep Dive
Prerequisite Knowledge
- No data available.
Where to go next
- No data available.
Deep Dive
[CS 61A SU26] Ants Problem Getting Started
Added:welcome to the getting started video for question 8B of ants so this is continuing off the question a a um now that we've implemented hopefully the container and class we want to actually allow ants um to have two ants occupying the same place at the same time because remember with the container in it you can have one ant instead of another ant in the same place so what we're going to do is we're going to modify the add to method in the ant class to allow those two ends to occupy the same place but only if these rules are applied so you go to the end class in the code here and scroll down to the add to Method um this section of the code here is where you're going to write in your code um as a reminder the add to method for ants is going to be the method that adds the and to a place so uh originally what just happened is as long as there isn't an and in that place we would just add that in and then otherwise we would err but now we want to only raise this error we want to assert this when um the container ants um isn't a thing like when the ant that's already there isn't a container then we should say okay these two answers can't be there but otherwise if these conditions are fulfilled then we should make it possible for those two ends to be in that place and so if we go through the rules here um it says if the ant that was originally in that place is a container and that can contain the other ends then both atoms can occupy the place and the original ant is going to have to contain the unit so you're going to store that ants in the original ants if the ad that you're adding to that place can contain the end that was already there then you can have both of them occupied a space and then you just have the ant that was already there being placed into the new name but if neither of them can contain each other you're just going to raise this here and assert that there can't be two in here um and then importantly when you're handling this and when you're if you're putting two ants in a specific place then you need to make sure that the and attribute of um whatever place we're in right now so the instance attribute of that place needs to refer to the container Ant and the container and needs to contain the non-container in so if the ant that was already there is the container ant then um you don't need to change anything because that's the places and but if you're the and if the ants added is the container and then you need to change the places and to be the new ant because that's the container so um that's the one that's going to be actually into place um uh the ant attribute um I stand for this when you're checking whether an ant can be a container ant you can just use the can contain method that you wrote in question 8A and check if that and can contain the other answer that way you don't have to write any repeating code so to kind of write out what I want in my code um this is all going to be in my all statement because I know already um if this was checked and there's already an n in that place now in this else case assuming there's already an ant there I need to check if one of them can contain the other hand the first what plant size um if the ants already there can contain the new so remember the ant already there we can get that with um by using the place so Place Dot and that's an attribute that tells us this is the ant that's already in that place so the answer already there already there we can get that with race Dot the new ant is actually ourselves it's self because um this ants Clause we're implementing a method for an ant to add itself to a place so the new ant is ourselves itself and using that information think about how you would call can contain like what Aunt would you call can contain with and what and would you pass in remember can contain says that ant can or cannot contain the ant that you pass in so if you're checking the ant already there can contain the new ants um that means the ante there should be the container and the new and should be the and that goes inside right okay so I'm gonna put these information top right and so if the ant already there can contain the new ants then make sure both ants occupy the place okay and make sure that the container and [Music] so the original one the ant already there is going to uh contain the ants added so the ant already which is place that ends needs to contain the new ant which is ourselves um and there should already be a method to do that for us in our containerian clock um and on the other hand the other case is it the ants added can contain the original ion so remember and being added as us if the self so the ants right now can add the band that's already there then again both and occupy the place and uh container and so the new ants contain the ants added uh which is the ant originally there so the original okay and then to note with both of these cases the answer attribute of the place and since may or may not need to be updated so um if the ant already there is the container ants no need to update the um ant attribute oh right because that's already the gonna be placed on hand um is a container again but if you're saying the new n is the container ant then the new ant needs to be the place stand because it is the container hand so you need to make sure to update and attribute of place okay so that's an extra piece of code for this case um and once you've handled that right if neither of these cases were true so if neither of these ants can't contain the other ants then you need to um raise the same error as before and so all you need to do is just make sure this case um happens in all other cases so if these two cases weren't true then make sure just this line is the line B okay otherwise just a certain statement is what it's right that should be a four question eight beat and note this note here that any unreachable code warning received for this question um doesn't really matter you know you can feel free during the ones
Related Videos

TOP 15 Data compression Interview Questions and Answers 2019 Part-2 | Data compression | Wisdom jobs
wisdomjobs
281 views•2019-06-28

CTS 158: 802.11w Management Frame Protection
ClearToSend
4K views•2019-02-04

NDSS 2019 Send Hardest Problems My Way: Probabilistic Path Prioritization for Hybrid Fuzzing
NDSSSymposium
496 views•2019-04-02

How realistic is Cities: Skylines?
CityBeautiful
159K views•2019-02-14

GUIs & TUIs: Choosing a User Interface for Your Python Project | Real Python Podcast
realpython
2K views•2025-04-04

The OSI Model - Explained by Example
hnasr
225K views•2019-05-12

Cloud Computing - Introduction
elithecomputerguy
98K views•2019-10-07

From Traveler's Dilemma to Dynamic Routing | Demystifying Networking
IITBombayJuly
5K views•2019-08-04
Trending

WOW! Judge TURNS THE TABLES on Trump in His OWN $10B LAWSUIT!!!
MeidasTouch
197K views•2026-07-23

Playstation NO DISC/NO BUY Fight Is Over...
DavidJaffeGames
4K views•2026-07-23

Steam and Xbox Just Dropped The Hammer On PlayStation
OhNoItsAlexx
9K views•2026-07-23

Americans Confused in Australia for 17 Minutes Straight
IWrocker
17K views•2026-07-23