Accumulation Pattern
Problem 2
Consider the code below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
labs = ['lab1', 'lab2', 'lab3', 'lab4', 'lab5', 'lab6', 'lab7', 'lab8', 'lab9'] graded = '' for lab in labs: lab_num = int(lab[3]) if lab_num < 4: graded = graded + lab + ' is simple\n' elif lab_num < 7: graded = graded + lab + ' is ok\n' else: graded = graded + lab + ' is complex\n'
print(labs) print(graded) |
Type it it the Active Code Scratchpad. Run it to check the outputs of the print() calls on lines 11, 12.
Part A. Analyze the application of the accumulation pattern and answer the following questions.
#1. A) What is the accumulator variable? ________________b. What's its data type? ________________
#2.c) What's the purpose of it?
#3.a) What's the iterator or loop variable? _____________ 2.b) What's its data type? ________________
#4. c ) What's the purpose of it?
#5 How many iterations does the for loop have? _________
#6 Write the line(s) of code where the accumulator "accumulates" something into it ________________
#7 What gets accumulated into the accumulator? Explain in English.
Part B Modify the value of labs variable such that it has the 10th element, 'lab10'. Run the program again.
#1 What do you get? Copy and paste the output from Scratchpad in here.
#2 Explain the result.
#1. A) What is the accumulator variable?
graded
b. What's its data type?
String
#2.c) What's the purpose of it?
It is a variable that adds something to itself
#3.a) What's the iterator or loop variable?
labs
2.b) What's its data type?
String array
#4. c ) What's the purpose of it?
For looping, controls how many times a loop
runs
#5 How many iterations does the for loop have?
9
#6 Write the line(s) of code where the accumulator "accumulates"
something into it
graded = graded + lab + ' is simple\n'
graded = graded + lab + ' is ok\n'
graded = graded + lab + ' is complex\n'
#7 What gets accumulated into the accumulator? Explain in
English.
Whether the lab is simple / ok / complex
OUTPUT
#1 What do you get? Copy and paste the output from Scratchpad in here.
OUTPUT
#2 Explain the result.
Takes lab[3] which is 1 for lab10 and prints that it is simple.
--------------------------------------
Hit the thumbs up if you are fine with the answer. Happy
Learning!
Accumulation Pattern Problem 2 Consider the code below. 1 2 3 4 5 6 7 8...
1. Questions Python Code: For each line of code, write the type of error (syntax, semantics, runt-time), cause of the error, and your fix. (1) def fun(num letter) (2) num == 7 + num (3) return 'num' - letter (4) result = fun(5, x) 2. Question Python Code: Run the code below to check the outputs of the print() call on lines 12. And : A) Analyze the application of the accumulation pattern and answer the following questions: #1. a....
JAVA LAB 1 2 5 7 6 9 3 8 . Write code to implement an adjacency matrix (2d matrix) which represents the graph. Your code should contain a function called addEdgelint i, int j). Use this function to add the appropriate edges in your matrix. Write code to implement Depth-First-Search (DFS) and Breadth-First-Search (BFS) of your graph. Let 0 be the source Node . Traverse the graph using DFS, print the Nodes as they are visited. . Traverse the...
Consider the following code segment. int[]arr={1, 2, 3, 4, 5, 6, 7, 8}; for(int k=3; k<arr.length-1; R++ arr[k]-arr[k+1]; What are the contents of arr as a result of executing the code segment? a. {1, 2, 3, 5, 6, 7, 8, 8) b. {2, 2, 4, 5, 6, 7, 8, 8} C. {2, 4, 6, 5, 6, 7, 8,8} d. {4, 2, 4, 4, 6, 7, 8, 8) e. {6, 6, 4, 5, 6, 7, 8, 8} int) arr={7, 2.5, 3.0,...