Homework 9

pdf

School

San Jose State University *

*We aren’t endorsed by this school

Course

30

Subject

Computer Science

Date

Feb 20, 2024

Type

pdf

Pages

7

Uploaded by MinisterBravery13388

Report
09 HW - Lists and Tuples Problem 9.1 - List Modification Write a function called chop2 that takes a list and returns a new list that contains all but the first two elements, or returns a message that the list isn’t long enough. For example, if a list t = [1, 2, 3, 4] : chop2(t) returns [3, 4] [3, 4] Problem 9.2 - Comma Code You have the following list: spam = ['apples', 'bananas', 'tofu', 'cats'] Write a function that takes a list value as an argument and returns a string with all the items separated by a comma and a space, with and inserted before the last item. For example, passing the previous spam list to the function would return 'apples, bananas, tofu, and cats' . Your function should work with any list value passed to it, not just this one. 'apples, bananas, tofu, and cats' Problem 9.3 - List Iteration Write a function called d_nested_sum that takes a list of lists of integers and adds up the elements from all of the nested lists and halves it. For example: In [1]: def chop2 ( List ): if ( len ( List ) <= 2 ): return "List isn’t long enough." else : list1 = [] for i in range ( 2 , len ( List )): list1 . append ( List [ i ]) return list1 t = [ 1 , 2 , 3 , 4 ] chop2 ( t ) Out[1]: In [2]: def func ( List ): string = "" length = len ( List ) for i in range ( length ): if i == length - 1 : string += "and " + List [ i ] else : string += List [ i ] + ", " return string spam = [ 'apples' , 'bananas' , 'tofu' , 'cats' ] func ( spam ) Out[2]:
if t = [[1, 2], [3], [4, 5, 6]] then the call d_nested_sum(t) returns 10.5 10.5 Problem 9.4 - Character Picture Grid Say you have a list of lists where each value in the inner lists is a one-character string, like this: grid=[ ['.', '.', '.', '.', '.', '.'], ['.', 'O', 'O', '.', '.', '.'], ['O', 'O', 'O', 'O', '.', '.'], ['O', 'O', 'O', 'O', 'O', '.'], ['.', 'O', 'O', 'O', 'O', 'O'], ['O', 'O', 'O', 'O', 'O', '.'], ['O', 'O', 'O', 'O', '.', '.'], ['.', 'O', 'O', '.', '.', '.'], ['.', '.', '.', '.', '.', '.']] You can think of grid[x][y] as being the character at the x- and y-coordinates of a “picture” drawn with text characters. The (0, 0) origin will be in the upper-left corner, the x-coordinates increase going right, and the y- coordinates increase going down. Copy the previous grid value, and write code that uses it to print the image. . . O O . O O . . . O O O O O O O . . O O O O O O O . . . O O O O O . . . . . O O O . . . . . . . O . . . . Hint : You will need to use a loop in a loop in order to print grid[0][0] , then grid[1][0] , then grid[2] [0] , and so on, up to grid[8][0] . This will finish the first row, so then print a newline. Then your program should print grid[0][1] , then grid[1][1] , then grid[2][1] , and so on. The last thing your program will print is grid[8][5] . Also, remember to pass the end keyword argument to print() if you don’t want a newline printed automatically after each print() call. In [3]: def d_nested_sum ( List ): sum = 0 for i in List : for j in i : sum += j return sum / 2.0 t = [[ 1 , 2 ],[ 3 ],[ 4 , 5 , 6 ]] d_nested_sum ( t ) Out[3]: In [4]: grid = [[ '.' , '.' , '.' , '.' , '.' , '.' ], [ '.' , 'O' , 'O' , '.' , '.' , '.' ], [ 'O' , 'O' , 'O' , 'O' , '.' , '.' ], [ 'O' , 'O' , 'O' , 'O' , 'O' , '.' ], [ '.' , 'O' , 'O' , 'O' , 'O' , 'O' ], [ 'O' , 'O' , 'O' , 'O' , 'O' , '.' ], [ 'O' , 'O' , 'O' , 'O' , '.' , '.' ], [ '.' , 'O' , 'O' , '.' , '.' , '.' ], [ '.' , '.' , '.' , '.' , '.' , '.' ]] def PictureGrid ( grid ):
. . O O . O O . . . O O O O O O O . . O O O O O O O . . . O O O O O . . . . . O O O . . . . . . . O . . . . Problem 9.5 - Variable Number of Arguments Write a function called prod_all that takes any number of arguments and returns their product. Test the function by using these two tuples (separately) as arguments: (1, 2, 3, 4) & (4, 6, 8): prod_all(1, 2, 3, 4) # The answer should be 24 prod_all(4, 6, 8) # The answer should be 192 24 192 Problem 9.6 - Cumulative Sum Write a function called cumsum_twice() that takes a list of numbers and returns twice the cumulative sum; that is, a new list where the ith element is twice the sum of the first i + 1 elements from the original list. For example: t = [1, 2, 3] cumsum_twice(t) returns [2, 6, 12] [2, 6, 12] for y in range ( len ( grid [ 0 ])): for x in range ( len ( grid )): print ( grid [ x ][ y ], end = " " ) print () PictureGrid ( grid ) In [5]: def prod_all ( * args ): tupl = ( args ) total = 1 for n in tupl : total *= n return total print ( prod_all ( 1 , 2 , 3 , 4 )) print ( prod_all ( 4 , 6 , 8 )) In [8]: def cumsum_twice ( inList ): outList = [] cumsum = 0 for i in range ( len ( inList )): cumsum += inList [ i ] outList . append ( 2 * cumsum ) return outList cumsum_twice ([ 1 , 2 , 3 ]) Out[8]:
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
CPX Problem 9.7 Assume you don't know how many LEDs you have and you want to have a direct control on the intensities of each LED. You are given a list of intensities: intensities = [0.0025, 0.005, 0.01, 0.02, 0.04, 0.08, 0.16, 0.32, 0.64, 1.0] This is the fraction of brightness, so 1.0 means the brightness will be set to 255, and a value of 0.01 translates to 3 - int(2.55), the RGB values have to be integers. The length of the intensities list also determines the number of LEDs you have - in this case the number is 10. Your program should work for any number of LEDs. Here's what you need to do: Red Cycle : Cycle through all LEDs starting from cp.pixels[0] , turning each one red and using the intensities list to select the intensity. For example the cp.pixels[5] should have the RGB values of (20, 0, 0) - int(0.08*255) = 20. A new LED should be turned on every 0.5 seconds. Green Cycle : Now cycle back in reverse order, but turning the LEDs green instead of red. The intensity mapping will be the same as before with cp.pixel[0] at the lowest intensity. Blue Cycle : Repeat in the same order as the red cycle but turning them blue. Put the above cycles in an infinite loop. Remember not to hard code the number of LEDs anywhere. In [ ]: import time from adafruit_circuitplayground import cp intensities = [ 0.004 , 0.006 , 0.01 , 0.02 , 0.04 , 0.08 , 0.16 , 0.32 , 0.64 , 1.0 ] l = len ( intensities ) while True : for led in range ( l ): cp . pixels [ led ] = ( int ( intensities [ led ] * 255 ), 0 , 0 ) time . sleep ( 0.5 ) for led in range ( l - 1 , - 1 , - 1 ): cp . pixels [ led ] = ( 0 , int ( intensities [ led ] * 255 ), 0 ) time . sleep ( 0.5 ) for led in range ( l ): cp . pixels [ led ] = ( 0 , 0 , int ( intensities [ led ] * 255 )) time . sleep ( 0.5 )
Problem 9.8 Use the accelerometer to change the tone of the speaker output. The command x, y, z = cp.acceleration gives you a tuple with x, y and z components of acceleration. Calculate the magnitude of acceleration: mag_acc = math.sqrt(x*x + y*y + z*z) and use that to increase the tone of the speaker output. Higher the acceleration, higher the tone. Use your experience from previous exercises. In [ ]: import time import math from adafruit_circuitplayground import cp while True : x , y , z = cp . acceleration mag_acc = math . sqrt ( x * x + y * y + z * z ) cp . play_tone ( 2 * mag_acc , .3 ) time . sleep ( 1 )
Problem 9.9 Use to accelerometer to drive the RGB LEDs (neoPixels). Your program should be such that the color and intensity of the LEDs depends on how you shake the board. Use cp.pixels.fill(R, G, B) and x, y, z = cp.acceleration and remember that R, G, B are positive integers only, whereas x, y and z are floating point numbers that can be positive or negative, so you will first need to make the appropriate transformation. Are the LEDs lit when the board is lying flat without being shaken? If so, why? Because even when the board is lying flat it still has a value of x, y, z which then turns it into an RGB signal In [ ]: while True : if cpx . switch : print ( "Slide switch off!" ) cpx . pixels . fill (( 0 , 0 , 0 )) continue else : R = 0 G = 0 B = 0 x , y , z = cpx . acceleration print (( x , y , z )) if x : R = abs ( int ( x )) if y : G = abs ( int ( y )) if z : B = abs ( int ( z )) cpx . pixels . fill (( R , G , B ))
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
Submission Submit both a .pdf and a .ipynb file via Canvas. It should include a video of problem 9.7 through 9.9.