Variables & Control Flow


Images: Blur

          
def average(in_list):
	return sum(in_list)//len(in_list)


def get_average_neighbors(x,y, image):
	"""
	This function loops through the neighbors of the pixel and the given x,y coordinates and calculates the 
	average of the red, green, and blue values of all neighbor pixels. It returns a tuple where the first
	value is the average of the red components, the second if the average of the green components, and the third value
	is the average of the blue components. 
	"""

	#create the lists for all red, green, and blue componenets respectively 
	reds = []
	greens = []
	blues = []

	#loop through neighbors to get list of all color components for all 9 pixels
	for i in range(y-1, y+2):
		for j in range(x-1, x+2):
			if image.in_bounds(j,i):
				in_pixel = image.get_pixel(j,i)
				reds.append(in_pixel.red)
				greens.append(in_pixel.green)
				blues.append(in_pixel.blue)

	#calculate the average of each component 
	average_red = average(reds)
	average_green = average(greens)
	average_blue = average(blues)

	#return a tuple with the three color components
	return (average_red, average_green, average_blue)


def blur(image):
	#create a new image that you will update and return rather than modifying given image in place
	out_image = SimpleImage.blank(image.width, image.height)

	#loop through given image 
	for y in range(image.height):
		for x in range(image.width):
			in_image_pixel = image.get_pixel(x, y)
			out_image_pixel = out_image.get_pixel(x,y)
			blur_pixel = get_average_neighbors(x,y, image)
			out_image.set_pix(x,y,blur_pixel)

	return out_image
          
        

Drawing


Strings


Grids


Lists: Camel Casing

          
def make_first_char_upper(s):
	return s[0].upper() + s[1:].lower()

def make_camel_case(phrases):
	
	#create list to store all updated phrases
	cameled_phrases = []
	
	#loop through each phrase 
	for phrase in phrases:
		split_by_space = phrase.split()

		#store the first word because it will remain all lower case 
		lower_case_word = split_by_space[0]

		#change the first char in each remaining word to uppercase
		updated = [make_first_char_upper(s) for s in split_by_space[1:]] 
		
		#add all updated words to a single string 
		cameled_phrase = lower_case_word
		for word in updated:
			cameled_phrase += word

		#add updated phrase to the output list 
		cameled_phrases.append(cameled_phrase)
	
	return cameled_phrases
          
        

Parsing


Files and File Reading


Dictionaries


Tuples: student residences

          
def map_students_to_dorms(all_housing_assignments):
	student_living_locations = {}

	for name, dorm in all_housing_assignments:
		if name not in student_living_locations:
			student_living_locations[name] = []
		student_residences = student_living_locations[name]
		student_residences.append(dorm)

	return student_living_locations
          
        

Sorting, lambdas and List Comprehensions


More sorting: stock prices

          
def find_first_and_last(stock_prices, a,b):
	within_time_interval = sorted([pair for pair in stock_prices if pair[0] >= a and pair[0] <= b])
	first = within_time_interval[0][1]
	last = within_time_interval[len(within_time_interval)-1][1]
  return (first, last)

def find_lowest_price(stock_prices, a,b):
	lowest = min([pair for pair in stock_prices if pair[0] >= a and pair[0] <= b], key=lambda t: t[1])[0]
	return lowest
          
        

Weather Map

          
def help_weatherman(img):

	#list of all places affected 
	places_affected = []

	#loop through each pixel to check if it represents the type of weather 
	#we need to use the double for loop because we need the x,y coordinates to get the actual location
	for y in range(img.height):
		for x in range(img.width):
			pixel = img.get_pixel(x,y)
			if pixel.blue >= MINIMUM_BLUE_THRESHOLD:
				location = translate_coordinate_to_location(x,y)

				#only add each location once 
				if location not in places_affected:
					places_affected.add(location)

	#return the list of places affected 
	return places_affected