Introduction to Computational Thinking Abstraction & Data Structures (C) Dennis Kafura 2016
Abstraction & Data Structures identifying the information properties of an entity relevant for a given stakeholder; each property has a value. computer property --> value abstraction property --> value data structure state property --> value algorithm organizing the properties and their values so that they can be processed by an algorithm executed by a computer. (C) Dennis Kafura 2016
Abstraction An abstraction can be depicted by a table Example: an abstraction of a weather report properties City Temperature “Blacksburg, VA” 77 values (C) Dennis Kafura 2016
Dictionary: a data structure A dictionary is a data structure used to represent a single instance A dictionary is organized as a collection of property-value pairs In Python the term key-value pair is used City Temperature “Blacksburg, VA” 77 key-value pair key-value pair temps = { “City” : “Blacksburg, VA”, “Temperature” : 77} key value (C) Dennis Kafura 2016
Dictionary operations temps = { “ City” :“Blacksburg, VA” , “Temperature” : 77} retrieval cityTemp = temps[“Temperature”] cityName = temps[“City”] update temps[“Temperature”] = 82 (C) Dennis Kafura 2016
For today Work in your cohorts on the class work problems for 30 minutes More discussion of abstraction and dictionaries to follow (C) Dennis Kafura 2016
Abstracting multiple entities Similar abstracted entities (e.g., weather reports for different locations) have the same properties but different values. They are called instances. properties City Temperature “Blacksburg, VA” 77 “New York, NY” 85 “San Jose, CA” 75 “Miami, FL” 88 instances (C) Dennis Kafura 2016
Representing an abstraction City Temperature “Blacksburg, VA” 77 “New York, NY” 85 “San Jose, CA” 75 “Miami, FL” 88 list multiple instances, one property dictionary multiple properties one instance (C) Dennis Kafura 2016
Representing Abstractions Abstractions are represented using data structures Dictionary a collection of key-value pairs used to represent multiple properties of a single instance of an abstraction List a collection of similar things used to represent one property of multiple instances of an abstraction (C) Dennis Kafura 2016
An Example City Temperature “Blacksburg, VA” 77 “New York, NY” 85 “San Jose, CA” 75 “Miami, FL” 88 report = [ { “ City” : “Blacksburg, VA” , “Temperature” : 77} , { “ City” : “New York, NY” , “Temperature” : 85} , { “ City” : “San Jose, CA” , “Temperature” : 75} , { “ City” : “Miami, FL” , “Temperature” : 88} ] (C) Dennis Kafura 2016
Dictionaries/Lists and iteration reports = [ { “ City” : “Blacksburg, VA” , “Temperature” : 77} , { “ City” : “New York, NY” , “Temperature” : 85} , { “ City” : “San Jose, CA” , “Temperature” : 75} , { “ City” : “Miami, FL” , “Temperature” : 88} ] # find average temperature total = 0 number= 0 for report in reports: total= total + report[“Temperature”] number = number + 1 average = total / number (C) Dennis Kafura 2016