Created
January 3, 2015 03:53
-
-
Save MrValdez/6d07ee0d8a807ef17380 to your computer and use it in GitHub Desktop.
Simple code to demonstrate that set is not deterministic. Discussions: https://www.facebook.com/groups/pythonph/permalink/790887134300424/ and https://www.reddit.com/r/PythonPH/comments/2r66dx/showoffpython_set_is_not_deterministic/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
random.seed(0) | |
def MakeData(): | |
Data = ["Spam"] * 5 | |
Data.extend(["Eggs"] * 2) | |
Data.extend(["Ham"] * 2) | |
return Data | |
def NonDeterministic(Data): | |
justSpam = set(Data) & set(["Spam"]) | |
IloveHam = set(Data) & set(["Ham"]) | |
TastyEgg = set(Data) & set(["Eggs"]) | |
Breakfast = justSpam | IloveHam | TastyEgg | |
DeliciousFactor = random.random() | |
return Breakfast, DeliciousFactor | |
def Deterministic(Data): | |
justSpam = [food for food in Data if food == "Spam"] | |
IloveHam = [food for food in Data if food == "Ham"] | |
TastyEgg = [food for food in Data if food == "Eggs"] | |
Breakfast = [] | |
Breakfast.extend([justSpam[0], IloveHam[0], TastyEgg[0]]) | |
DeliciousFactor = random.random() | |
return Breakfast, DeliciousFactor | |
menu = MakeData() | |
print (NonDeterministic(menu)) | |
print (Deterministic(menu)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment