Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Challenge 6

this is a big jump from the previous challenges, take your time with it

You are working on a database system for controller input and have been asked to make a series of variable queries to the system. Being the smart programmer you are, you decide to write a script that will interpret the data and return what is relevant.

controlScheme is a list of dictionaries which each have the following schema

{
	"id": int,        // the controller id of the control
	"button": int,    // the button id of the control
	"descripton": str // a description of what the control is for
}

A schema is just a way of saying what you can expect the data to be

Problem 1

Using higher order functions list map and filter, create a function called getDesc that takes a controlScheme and returns all of its descriptions in a list. You are given sample dataset below , but are encouraged to expand and try other customized datasets.

controlScheme1 = [
 {"id": 2, "button": 1, "description": "fire"}, 
 {"id": 2, "button": 2, "description": "intake"},
 {"id": 1, "button": 4, "description": "climb"},
 {"id": 3, "button": 2, "description": "fudge up"},
 {"id": 3, "button": 3, "description": "fudge down"}
]
controlScheme2 = [
 {"id": 0, "button": 1, "description": "up"},
 {"id": 0, "button": 2, "description": "left"},
 {"id": 0, "button": 3, "description": "down"},
 {"id": 0, "button": 4, "description": "right"},
 {"id": 1, "button": 1, "description": "jump"},
 {"id": 1, "button": 2, "description": "shoot"},
 {"id": 1, "button": 3, "description": "duck"},
 {"id": 1, "button": 4, "description": "run"}
]

def getDesc(controls) -> list[str]:
	pass

Problem 2

Like above, except expand on to return a set of ids. Note that a set enforces that there are no duplicates to the id. Also write a function that returns a sorted list of all the buttons. If there are duplicate buttons, keep them.

For sorting a list, you can mutably sort a list with li.sort() example below:

li = [2, 1, 3, 4, 0]
li.sort()
print(li) # [0, 1, 2, 3, 4]

use the following starter for the problem:

controlScheme1 = [
 {"id": 2, "button": 1, "description": "fire"}, 
 {"id": 2, "button": 2, "description": "intake"},
 {"id": 1, "button": 4, "description": "climb"},
 {"id": 3, "button": 2, "description": "fudge up"},
 {"id": 3, "button": 3, "description": "fudge down"},
]
controlScheme2 = [
 {"id": 0, "button": 1, "description": "up"},
 {"id": 0, "button": 2, "description": "left"},
 {"id": 0, "button": 3, "description": "down"},
 {"id": 0, "button": 4, "description": "right"},
 {"id": 1, "button": 1, "description": "jump"},
 {"id": 1, "button": 2, "description": "shoot"},
 {"id": 1, "button": 3, "description": "duck"},
 {"id": 1, "button": 4, "description": "run"}
]

def getIds(controls) -> set[int]:
	pass

def getButtons(controls) -> list[int]:
	pass

Problem 3

implement a moving weighted moving average generator function. The function has type signature: (terms: float) => ((term: float) => float)

This means that the function should take as input a number of terms to average at most, and then that returned function should take an argument to add to the moving average, and return the moving average.

def movingGenerator(terms: float):
	def average(term: float):
		pass
	return average

avg5 = movingGenerator(5)
print(avg5(1)) # => 1
print(avg5(3)) # => 2
for i in range(7):
	print(avg5(i)) # should print out 1.33, 1.25, 1.4, 1.8, 2.0, 3.0, 4.0

Contributors: