# Practice Diagnostic

The problem order and structure of this diagnostic will match the format of Wednesday's diagnostic. The actual diagnostic will be delivered through a progra called BlueBook. Learn more about BlueBook.

## 1. Debugging & Tracing (8 points)

#### Consider the following (buggy) code:


def divide_and_round(n):
"""
Divides an integer n by 2 and rounds
up to the nearest whole number
"""
if n % 2 == 0:
n = n / 2
else:
n = (n + 1) / 2

def main():
n = 42
divide_and_round(n)
print(n)  # should print 21


#### Part A (4 points):

What will be printed at the end of the program? Why?

#### Part B (4 points):

How can this program be fixed to print 21? You can make changes to both divide_and_round and main, but divide_and_round must still implement the functionality described in its comment for all possible values of its parameter. You should write a fixed version of the program with comments indicating each line you changed.

## 2. Short Programs (12 points)

#### Odd numbers (6 points)

Write a program that prints the first 100 odd numbers greater than 0. In other words print out 1 then 3, then 5 all the way up to, and including, 199.

1
3
5
7
9
11
13
15
17
19
21
23
25
27
29
31
33
35
37
39
41
43
45
47
49
51
53
55
57
59
61
63
65
67
69
71
73
75
77
79
81
83
85
87
89
91
93
95
97
99
101
103
105
107
109
111
113
115
117
119
121
123
125
127
129
131
133
135
137
139
141
143
145
147
149
151
153
155
157
159
161
163
165
167
169
171
173
175
177
179
181
183
185
187
189
191
193
195
197
199

### Can I Ride Rollercoaster (6 points)

Write a main function which asks the user for their height in meters.

If their height is less than 1 meter or greater than 2 meters, print "You can't ride the roller coaster".

If their height is between 1 meter and 2 meters print "You can ride the roller coaster".

Here are a few examples. User input is in blue

Enter height in meters: 1.2
You can ride the roller coaster.
Enter height in meters: 2.5
You can't ride the roller coaster.

## 3. Karel Problem (14 points)

Errata: in Karel problems we generally allow boolean operators (and, or, not). We updated the wording (see blue text) on Oct 4th to make this clear.

Karel has recently started a new job as a farmer, and needs you to write a program called FarmerKarel to help it gather up crops (represented as beepers, of course). Karel starts off in a world of any size, with crops (beepers) scattered around this world. Your program should have Karel, for each row in this world, gather up the beepers in that row and place them on the leftmost square of that row. Here is a before-and-after example:

Note that, in each row, Karel has gathered all beepers in that row and placed them on the leftmost square. If there are no beepers in a row, Karel should not place any beepers in that row

You may assume the following facts about the world:

• Karel starts at (1, 1) facing East, with an infinite number of beepers in its beeper bag. This means Karel cannot put down exactly the number of beepers it previously collected.
• The world may be any size, and your program should work for all world sizes.
• There may be any number of beepers, or no beepers, on a given square, and beepers may be placed anywhere in the world.
• There are no walls in the world.
• Karel’s ending location and direction do not matter
• You must not use any non-Karel features like variables (other than i in a for loop), return or break. You may use any Karel features described in the course reader. You may, however, use the three boolean operators: and, or, not.

## 4. Python Problem (14 points)

Write a program that reads in integers one by one from the user and prints out whether those integers are perfect. A perfect number is a number whose factors sum is the same as itself. For example, 28 is a perfect number, since its factors (1, 2, 4, 7 and 14) sum to 28. On the other hand, 42’s factors (1, 2, 3, 6, 7, 14, 21) sum to 58, and so it isn’t a perfect number. Your program should print an error message if the number entered is negative, and should end when the user types 0. A sample run of the program is below. User input is in blue

Your number: -12
28 is perfect!
42 is not perfect!
Your number: 0

## 5. Lists Problem (12 points)

Write a list function
def make_ascending(old_list):

which returns a new list, which has all ascending elements in a list of numbers. An element is ascending, if it is greater-that-or-equal-to the number before it. The first element should always be included in the new list.

Here are a few examples:

make_ascending([1, 2, 3]) should return [1, 2, 3]

make_ascending([1, 2, 3, 2, 5]) should return [1, 2, 3, 5]

make_ascending([-1, 3, 5]) should return [-1, 3, 5]

make_ascending([5, 3, 1]) should return [5]

make_ascending([]) should return []