Pre-class assignment for Monday, Feb. 29th

In today's pre-class assignment, you are going to create a function that determines whether or not an element in a list is "happy" according to the Schelling criteria defined this past week.

Schelling's model for happiness: Recall that in a 1D line of stars and zeros (to use Schelling's terminology), an element is "happy" if at least half of its neighbors (defined as the four elements to the left and four elements to the right) are like it, and "unhappy" otherwise. For those near the end of the line the rule is that, of the four neighbors on the side toward the center plus the one, two or three outborad neighbors, at least half must be like oneself.

Your goal is to create a function called is_happy that takes in:

  1. A list of zeros and ones that is N elements long, which corresponds to the two
  2. An integer that corresponds to a position I in that list (I can range from 0 to N-1, since those are the legal elements in the list.)

This function will return True if the element at position I is happy, and False if not.

You should demonstrate that the function returns the correct value for several elements in the list provided below (test_list), including list values near both ends!


In [3]:
# this code generates a list called test_list that will be the same every time!
import random

# can change this to get different sized lists
size_of_list = 32 

# random seed is set manually to ensure that the list is always the same
random.seed(8675309)

test_list = []

for i in range(size_of_list):
    test_list.append(random.randint(0,1))

print(test_list, "list size:", len(test_list))


[1, 0, 1, 0, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 1] list size: 32

In [20]:
# Our function will loop over a range between start and end, checking neighbors.  This includes
# the value at position "index" as well, so for something to be happy more than half of the
# neighbors need to be like the value at position "index"

def is_happy(list, index):

    # do some error-checking (is the index within the allowed range?)
    if index < 0 or index > len(list)-1:
        print("you've made an indexing error!")
    
    start = index-4  # start 4 to the left
    end = index+5  # end 5 to the right (since the range() goes to 1 before end)
    
    # if the starting value is out of bounds, fix it
    if start < 0:
        start = 0
    
    # if the ending value is out of bounds, fix it.  note that we want to go to 
    # len(list), not len(list)-1, because range() goes to 1 before the end of 
    # the range!
    if end > len(list):
        end = len(list)

    # keep track of the neighbors that are like me
    neighbors_like_me = 0
    
    # keep track of total neighbors
    total_neighbors = 0
    
    # loop over the specified range
    for i in range(start,end):
        if list[i] == list[index]:  # if this neighbor is like me, keep track of that
            neighbors_like_me += 1
        total_neighbors+=1  # also keep track of total neighbors
    
    # happy if more than half are like me, unhappy otherwise
    if neighbors_like_me/total_neighbors > 0.5:
        return True
    else:
        return False

In [21]:
# test your function here!  Test several indices, including those near and exactly at the ends!
# you should look at the list provided above and determine by hand if each of the elements should
# be happy or not.

print(test_list)
print("index 0:", is_happy(test_list,0))
print("index 1:", is_happy(test_list,1))
print("index 2:", is_happy(test_list,2))
print("index 3:", is_happy(test_list,3))
print("index 4:", is_happy(test_list,4))
print("index 5:", is_happy(test_list,5))
print("index 10:", is_happy(test_list,10))
print("index 15:", is_happy(test_list,15))
print("index 20:", is_happy(test_list,20))
print("index",len(test_list)-6,":", is_happy(test_list,len(test_list)-6))
print("index",len(test_list)-5,":", is_happy(test_list,len(test_list)-5))
print("index",len(test_list)-4,":", is_happy(test_list,len(test_list)-4))
print("index",len(test_list)-3,":", is_happy(test_list,len(test_list)-3))
print("index",len(test_list)-2,":", is_happy(test_list,len(test_list)-2))
print("index",len(test_list)-1,":", is_happy(test_list,len(test_list)-1))


[1, 0, 1, 0, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 1]
index 0: True
index 1: False
index 2: False
index 3: False
index 4: True
index 5: False
index 10: False
index 15: True
index 20: False
index 26 : True
index 27 : True
index 28 : True
index 29 : True
index 30 : False
index 31 : False