Adapted from slides by Marty Stepp and Stuart Reges

Slides:



Advertisements
Similar presentations
Def f(n): if (n == 0): return else: print(“*”) return f(n-1) f(3)
Advertisements

Adapted from slides by Marty Stepp and Stuart Reges
Adapted from slides by Marty Stepp and Stuart Reges
CSc 110, Autumn 2016 Lecture 26: Sets and Dictionaries
Adapted from slides by Marty Stepp and Stuart Reges
CSc 110, Autumn 2017 Lecture 16: Fencepost Loops and Review
CSc 110, Spring 2017 Lecture 12: Random Numbers
CSc 110, Autumn 2017 Lecture 19: While loops and File Input
CSc 110, Autumn 2017 Lecture 20: Line-Based File Input
CSc 110, Spring 2017 Lecture 29: Sets and Dictionaries
CSc 110, Spring 2017 Lecture 18: Line-Based File Input
CSc 110, Autumn 2017 Lecture 21: Line-Based File Input
Adapted from slides by Marty Stepp and Stuart Reges
Adapted from slides by Marty Stepp and Stuart Reges
CSc 110, Autumn 2016 Lecture 27: Sets and Dictionaries
CSc 110, Autumn 2017 Lecture 24: Lists for Tallying; Text Processing
CSc 110, Autumn 2017 Lecture 18: While loops and File Input
Adapted from slides by Marty Stepp and Stuart Reges
CSc 110, Autumn 2016 Lecture 13: Random Numbers
CSc 110, Spring 2017 Lecture 28: Sets and Dictionaries
CSc 110, Autumn 2017 Lecture 5: The for Loop and user input
CSc 110, Spring 2018 Lecture 32: Sets and Dictionaries
CSc 110, Autumn 2017 Lecture 31: Dictionaries
CSc 110, Autumn 2017 Lecture 30: Sets and Dictionaries
CSc 110, Autumn 2016 Lecture 12: while Loops, Fencepost Loops, and Sentinel Loops Adapted from slides by Marty Stepp and Stuart Reges.
Adapted from slides by Marty Stepp and Stuart Reges
CSc 110, Spring 2018 Lecture 33: Dictionaries
Adapted from slides by Marty Stepp, Stuart Reges & Allison Obourn.
Adapted from slides by Marty Stepp and Stuart Reges
CSc 110, Spring 2017 Lecture 5: Constants and Parameters
CSc 110, Spring 2018 Lecture 16: Fencepost Loops and while loops
CSc 110, Spring 2018 Lecture 17: while Loops and decomposition
Lecture 23: Tuples Adapted from slides by Marty Stepp and Stuart Reges
Adapted from slides by Marty Stepp and Stuart Reges
CSc 110, Autumn 2017 Lecture 9: Graphics and Nested Loops
CSc 110, Autumn 2017 Lecture 27: Lists that change size and Tuples
CSc 110, Autumn 2016 Lecture 5: Loop Figures and Constants
CSc 110, Spring 2017 Lecture 13: Random numbers and Boolean Logic
CSc 110, Spring 2017 Lecture 17: Line-Based File Input
Adapted from slides by Marty Stepp and Stuart Reges
Adapted from slides by Marty Stepp and Stuart Reges
Adapted from slides by Marty Stepp and Stuart Reges
CSc 110, Spring 2018 Lecture 14: Booleans and Strings
CSc 110, Spring 2017 Lecture 4: Nested Loops and Loop Figures
CSc 110, Autumn 2016 Lecture 20: Lists for Tallying; Text Processing
Adapted from slides by Marty Stepp and Stuart Reges
Adapted from slides by Marty Stepp and Stuart Reges
Adapted from slides by Marty Stepp and Stuart Reges
Lecture 23: Tuples Adapted from slides by Marty Stepp and Stuart Reges
CSc 110, Spring 2018 Lecture 7: input and Constants
CSc 110, Spring 2018 Lecture 5: The for Loop and user input
Adapted from slides by Marty Stepp and Stuart Reges
CSc 110, Spring 2018 Lecture 23: lists as Parameters
Adapted from slides by Marty Stepp and Stuart Reges
CSc 110, Autumn 2016 Lecture 17: Line-Based File Input
CSc 110, Spring 2018 Lecture 22: Line-Based File Input
CSc 110, Spring 2017 Lecture 20: Lists for Tallying; Text Processing
CSc 110, Spring 2018 Lecture 5: Loop Figures and Constants
CSc 110, Spring 2018 Lecture 27: Lists that change size and File Output Adapted from slides by Marty Stepp and Stuart Reges.
CSc 110, Spring 2018 Lecture 8: input and Parameters
CSc 110, Spring 2018 Lecture 10: More Graphics
CSc 110, Spring 2018 Lecture 21: Line-Based File Input
CSc 110, Autumn 2016 Programming feel like that?
Adapted from slides by Marty Stepp and Stuart Reges
Adapted from slides by Marty Stepp and Stuart Reges
Adapted from slides by Marty Stepp and Stuart Reges
CSc 110, Autumn 2017 Lecture 8: More Graphics
Adapted from slides by Marty Stepp and Stuart Reges
CSc 110, Autumn 2016 Lecture 20: Lists for Tallying; Text Processing
Adapted from slides by Marty Stepp and Stuart Reges
Presentation transcript:

Adapted from slides by Marty Stepp and Stuart Reges CSc 110, Spring 2017 Lecture 31: 2D Structures Adapted from slides by Marty Stepp and Stuart Reges

Exercise Write a program that allows a user to ask the distance between two people in a network of friends. If person 1 and person 2 are friends then they are at distance 1 If person 2 is friends with a friend of person 2 they are at distance 2

graph { Ashley -- Christopher Ashley -- Emily Ashley -- Joshua Christopher -- Andrew Emily -- Joshua Jacob -- Christopher Jessica -- Ashley Sarah -- Andrew Sarah -- Christopher Sarah -- Emily Stuart -- Jacob } Name 2 friends at distance 1. Which two people are at the greatest distance?

Ashley Christopher Emily Christopher Ashley Ashley Emily Jacob Joshua Joshua Andrew Sarah Jessica Sarah Note: not all sets of friends shown. graph { Ashley -- Christopher Ashley -- Emily Ashley -- Joshua Christopher -- Andrew Emily -- Joshua Jacob -- Christopher Jessica -- Ashley Sarah -- Andrew Sarah -- Christopher Sarah -- Emily Stuart -- Jacob }

# Reads in a dot file with friendship data # Version 0: Asks if two people are friends def main(): file = open("friends.dot") lines = file.readlines() friends = create_dict(lines) for name in friends: print(name, " : ", friends[name]) name1 = input("Enter a name: ") name2 = input("Enter a name: ") #Are name1 and name2 friends?

# creates and returns a dictionary mapping each person to a # set of their friends. Creates an entry for name1 to name2 # and name2 to name1. def create_dict(lines): friends = {} # skip the first and lst lines as they have dot syntax for i in range(1, len(lines) - 1): line = lines[i].split() name1 = line[0] name2 = line[2] if (name1 not in friends): friends[name1] = set() friends[name1].add(name2) if (name2 not in friends): friends[name2] = set() friends[name2].add(name1) return friends

friends dictionary The content of the friends dictionary is: { Stuart : {'Jacob'} Jacob : {'Stuart', 'Christopher'} Ashley : {'Christopher', 'Emily', 'Joshua', 'Jessica'} Sarah : {'Christopher', 'Andrew', 'Emily'} Jessica : {'Ashley'} Andrew : {'Christopher', 'Sarah'} Emily : {'Ashley', 'Joshua', 'Sarah'} Joshua : {'Ashley', 'Emily'} Christopher : {'Jacob', 'Ashley', 'Andrew', 'Sarah'} }

Pseudocode for finding the distance – Version1 initialize a current set of friends to name1 initialize distance to zero while name2 not found in current set of friends increment the distance make a new set of friends from the current set using the dictionary to reference the sets of friends set the current set of friends to the union of the current set and new set of friends print the distance

Sarah to Joshua This works but what if we looked for someone out of the friend network? What is the problem with current_friends? new_friends {'Christopher', 'Andrew', 'Emily'} current_friends {'Christopher', 'Sarah', 'Andrew', 'Emily'} {'Sarah', 'Ashley', 'Andrew', 'Emily', 'Jacob', 'Joshua', 'Christopher'} {'Ashley', 'Jacob', 'Joshua', 'Sarah', 'Andrew', 'Emily', 'Christopher'} distance is: 2 We are never removing names that we have already seen.

Pseudocode for finding the distance – Version2 initialize a current set of friends to name1 Initialize a set of already seen friends to name1 initialize distance to zero while name2 not found in current set of friends and length of current friends not zero increment the distance make a new set of friends from the current set using the dictionary to reference the sets of friends already seen friends is assigned to the union of itself and current friends set the current set of friends to the new set of friends minus the already seen friends if the length of the current set of friends is not zero print the distance else print not connected

# Reads in a dot file with friendship data – Version2 def main(): file = open("friends.dot") lines = file.readlines() friends = create_dict(lines) name1 = input("Enter a name: ") name2 = input("Enter a name: ") #Are name1 and name2 friends? current_friends = {name1} already_seen = {name1} distance = 0 # stops when the friend is found or there is no possibility of a connection while(name2 not in current_friends and len(current_friends) != 0): distance += 1 new_friends = set() # builds up a set of the friends of the current friends for friend in current_friends: new_friends = new_friends | friends[friend] already_seen = already_seen | current_friends # replaces current friends and gets rid of friends looked at before current_friends = new_friends - already_seen if(len(current_friends) != 0): print("found at distance " + str(distance)) else: print("sorry they are not connected")