Practice Diagnostic

Good studying The problem order and structure of this diagnostic will match the format of diagnostic 2. The actual diagnostic will be delivered through BlueBook. Learn more about the second diagnostic. We will release a BlueBook version by Wed Oct 28th.
ProTip: Your actual diagnostic will be in a 1 hour class period. Try taking this proactice diagnostic in the same amount of time.

1. Debugging & Tracing

Consider the following (buggy) code:

Suppose that you have been assigned to take over a project from another programmer who has just been dismissed for writing buggy code:

def insert_value(value, value_list) :
   """
   Inserts a new value into a sorted array of integers by
   creating a new array that is one element larger than the
   original array and then copying the old elements into the
   new array, inserting the specified value at its proper
   position.
   >>> insert_value([1,4,7,9], 3)
   [1,3,4,7,9]
   >>> insert_value([], 7)
   [7]
   >>> insert_value([10,20,40,50], 45)
   [10,20,30,40,45,50]
   """
   result = []
   for i in range(len(value_list) + 1):
      result.append(0)

   for i in range(len(result)):
      result[i] = value_list[i]

   pos = 0
   for i in range(len(value_list)):
      if value > value_list[i]:
         pos = i
         break

   i = len(result)
   while i >= pos:
      result[i] = result[i - 1]
      i -= 1

   result[pos] = value
   return result

Unfortunately, the code contains several bugs. Point out the bugs in the implementation and write a short sentence explaining the precise nature of each problem you identify.

2. Strings

Given a string s. For each '@' in s, parse out the substring of zero or more alphabetic chars which immediately follow the '@', which we will call a "word". Return a list of all the word substrings in the order they appear in s, like these examples:
'x @abc @xyz x' -> ['abc', 'xyz']
'xx@xx$$' -> ['xx']
'@a @ @c' -> ['a', '', 'c']
def parse1(s):

3. Lists and Files

We want to write a program that performs some calculations on a text file. Specifically, the program is given a file that contains a list of integers (one per line), which are separated into groups by either a "+" or "*" operator. An example of such a file (named data.txt) is shown here:

data.txt
10
23
1
+
6
2
*
7
+
14
4
5
+
3
*
2
2
2
*

Your program needs to process this file so that it will compute either the sum or product of each group of numbers separated by an operator, based on what operator comes at the end of the group of numbers. For example, in data.txt above, the first group of numbers includes: 10, 23, and 1. Since there is a "+" operator following this group of numbers, the numbers should all be added to produce the sum 34 (which is then printed to the console). The next group of numbers in the file is: 6 and 2, followed by a "*" operator. The "*" operator indicates that we should compute the product of the numbers in that group (namely, 6 * 2) giving us the value 12. The next group of numbers includes just a single 7 followed by a "+" operator. Here, we take the sum comprised simply of the number 7, which gives us the number 7. Similarly, if we had a single number to take the product of (such as the example of the fifth group of numbers in the data file to right, which just contains a single 3 followed by a "*" operator), the result would simply be that number itself (i.e., 3, in this example). So, for the file data.txt on the right, your program should produce the following output:

> python3 compute.py
34
12
7
23
3
8

You can assume that each line of the data file will contain either a single integer or a "+" or "*". You can also assume that there will always be at least one integer in each group of numbers (i.e., you'll never have two operators ("+" or "*") right after each other in the data file). Finally, you can assume that there will always be an operator after a group of numbers. The file name will be in a constant INPUT_FILE

4. 2D Lists

Say we have a 2-dimensional array of ints (which we’ll refer to as a variable named grid) that contains 0 in every element, except for one element, which contains a 1. Such a grid is shown below. Note the single 1 (which is bold-faced in the grid for clarity), which is in the second row, fifth column of the grid – that would be position grid[1][4].

0 0 0 0 0 0 0
0 0 0 0 1 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0

We would like to create a diagonal line of 1’s (going down and to the right) through the grid, which includes the initial 1 in the grid and extends to the edges (but, obviously does not go outside) of the grid. So, starting with the grid above, we would end up with the following final grid (again, the 1’s in the grid are bold-faced for clarity):

0 0 0 1 0 0 0
0 0 0 0 1 0 0
0 0 0 0 0 1 0
0 0 0 0 0 0 1
0 0 0 0 0 0 0
0 0 0 0 0 0 0

The initial grid we are given can have any number of rows and columns (but it will be guaranteed to have at least one of each). You can assume that every element in the grid you are given, except for one, contains 0’s and the remaining element has a 1. Your task in this problem is to write a function:

diagonal_line(my_grid)

which is passed a 2-dimensional array of ints representing the grid (as descibed above). When the function is done, the grid passed in should now contain a “diagonal” line of 1’s that includes the 1 initially in the grid and all other elements in the grid should still be 0’s. Note that this function does not return anything, it changes the grid in place

Below, we give two additional examples of initial/final grids to help further clarify specific cases

Example 2
Initial grid
0 0 0
0 0 0
0 0 0
0 1 0
Final grid
0 0 0
0 0 0
1 0 0
0 1 0
Example 3
Initial grid
0 0 1
0 0 0
0 0 0
Final grid
0 0 1
0 0 0
0 0 0

5. Dictionary Prefix:

We'll say that the "prefix" of a word is its first 2 chars, in lowercase form. Given a list of words, we want to count how many times each prefix appears. So for example if the words are

['baby', 'Aardvark', 'basketball']
We'd count that the prefix 'ba' appears 2 times, and 'aa' 1 time. count_prefix(words): given a "words" list of word strings. Build a dict to count how many times each prefix appears across all the words. Ignore words with length less than 2. Once the dict is complete, print out, in any order, each prefix followed by its count, 1 per line, like this:
aa 6
ab 17
ax 1
ba 12
...

Note that the counts are case insensitive with regard to words / prefixes and all prefixes should be displayed in lowercase. Here are a few more examples:

Example 2:

list2 = ['atom', 'atol', 'at', 'a', 'big', 'small', 'bicycle']
count_prefix(list2)
# will print
at 3
bi 2
sm 1

Example 3:

list3 = ['a', 'b', 'c']
count_prefix(list3)
# will print nothing as there are no prefixs 
# from words of at least two characters

def count_prefix(words):