CISC 108: Introduction to Computer Science I, Fall 2016
Lab 2
Lab 12 Goals
This week’s lab will help you to practice:
• Python syntax, for concepts in weeks 1-10.
Note: There is a new submission system
All the following problems are from previous labs. Feel free to examine the previous BSL solutions.
Lab 1
Problem 1.1 [5pt]: Define the function string_last, which extracts the last character (as a string) from a non-empty string. Don’t worry about empty strings.
Problem 1.2 [5pt]: Define the function string_join, which consumes two strings and appends them with “_” in the middle.
Lab 2
Problem 2.1 [5pt]: Design a function increment_timer that consumes a TrafficTimerValue (a NatNum Interval from [0,100]) and produces the next TrafficTimerValue. Recall that the timer resets to 0 after reaching 100. There are two possible solutions to this problem (one using if, and one using % [remainder]); use either solution.
Lab 4 (also covers Lab 3 material) Note
In BSL Lab 4, we designed a Mixture data type (a Type Union) called Mappable that could either be a Restaurant, Person, GeoFence, or simply a GeoLoc. In Python, the use of objects will not require a new data type, but instead will need to implement the “interface” functions for Mappable things in each class.
Page 1 of 4
CISC 108: Introduction to Computer Science I, Fall 2016 Lab 2
Because of the automated testing for this lab, you must use the same function names, class names, object field names, and name all selectors with the name pattern get_field()
Problem 4.1 [10pt]
Design a compound data type (in Python, a class) to represent a GeoLoc with fields [lat] and [long]. A GeoLoc should consist of a latitude [-90,90] and longitude [-180,180] in decimal degrees. Remember that the class MUST be named GeoLoc, the fields are lat and long, and the selectors get_lat() and get_long() in order for the automated grading to work.
Also design a method __str__ that produces a string representation of the given Mappable. A GeoLoc should be displayed as follows:
“<39.689071,-75.757207>”
NOTE: For all the string printing problems you must produce EXACTLY the same string!! No extra
spaces, different capitalization, every punctuation correct: the system is auto-grading your output.
Problem 4.2 [10pt]
Design a data type called Restaurant to represent a place that serves food. A Restaurant should contain fields for a name [name], a phone number [phone], a rating from 0 to 5 stars indicating the food
quality [rating], and a GeoLoc [location]. Remember that selectors/setters must be named get_name, get_phone, etc.
Also design a method __str__ that produces a string representation of the given Mappable. A Restaurant should be displayed as follows:
“[3 stars] Santa Fe Mexican Grill [302-369-2500] <39.6837546,-75.7456899>”
Problem 4.3 [10pt]
Design a data type called Person to represent your contacts. A Person should of course include a name [name], a phone number [phone], and GeoLoc [loc].
Also design a method __str__ that produces a string representation of the given Mappable. A Person should be displayed as follows:
"Dr. Siegel [302-831-5555] <39.682531,-75.754329>"
Problem 4.4 [10pt]
Design a data type called GeoFence to represent a geo-fenced area notification; this notification should be displayed if you are currently within the given radius. A GeoFence should include a GeoLoc [center]
indicating the center of the area, a radius in meters [radius], and a short textual description [description].
Also design a method __str__ that produces a string representation of the given Mappable. Page 2 of 4
CISC 108: Introduction to Computer Science I, Fall 2016 Lab 2 A GeoFence should be displayed as follows:
"Velociraptor Paddock: (1000 Meters) <39.665648,-75.749031>"
Problem 4.5 [10 pts]
Design a function distance that accepts two Mappable arguments and produces the distance in meters between them. A GeoFence should be measured from the outside of the radius, not the center. So if I am 20 meters away from (the center of) a GeoFence of radius 10, then I am a distance of 10 meters away, because I am 10 meters away from the “fence” around the center point.
The star is 10m from the fence
10m 20m
Because determining distance with latitude/longitude is, admittedly, difficult due to the Earth’s curvature and no one remembering 9th grade conic section geometry, we have provided one helper function you can import from the attached file lab12_extras.py
# place this line at the top of your solution from lab12_extras import gps_distance
"""gps_distance : Number Number Number Number —> Number Parameters
Number latA: latitude of point A Number lonA: longitude of point A Number latB: latitude of point B Number lonA: longitude of point B
Produces the approximate distance in meters between A and B"""
Problem 4.6 [5 pts]
Design a function is_nearby that consumes two Mappable arguments, and a distance in meters, and produces True if the distance between the Mappables is strictly less than the given distance.
Problem 4.7 [10 pts]
Design a function select_nearby that consumes a [ListOf Mappable], a Mappable, and a distance and produces a copy of the given [ListOf Mappable] containing only the Mappable elements that fall strictly within the given distance. Hint: Use abstract list functions…
Page 3 of 4
CISC 108: Introduction to Computer Science I, Fall 2016 Lab 2
Problem 4.8 [10 pts]
Design a function acceptable_restaurants that consumes an [ListOf Mappable] and a minimum star rating, and produces a [ListOf Mappable] that includes only those restaurants on the list, if any, that meet or exceed the given rating criteria.
Problem 4.9 [10 pts]
Design a function mappables_to_string that accepts a [ListOf Mappable] and produces a str representation of the given [ListOf Mappable] where each Mappable is displayed by __str__, separated by a newline (“\n”) and in the order that they appear in the given [ListOf Mappable].
Page 4 of 4