Friday, January 27, 2012

Homework 3: Magic

For this homework you will write a program that guesses what number the user is thinking of. Below is a sample transcript:
Think of a number between 1 and 255.
I will guess the number you are thinking of.
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 201 203 205 207 209 211 213 215 217 219 221 223 225 
227 229 231 233 235 237 239 241 243 245 247 249 251 253 255 
Is it in the list above? (yes or no): yes

2 3 6 7 10 11 14 15 18 19 22 23 26 27 30 31 34 35 38 39 42 43 46 47 50 51 54 55 
58 59 62 63 66 67 70 71 74 75 78 79 82 83 86 87 90 91 94 95 98 99 102 103 106 
107 110 111 114 115 118 119 122 123 126 127 130 131 134 135 138 139 142 143 146 
147 150 151 154 155 158 159 162 163 166 167 170 171 174 175 178 179 182 183 186 
187 190 191 194 195 198 199 202 203 206 207 210 211 214 215 218 219 222 223 226 
227 230 231 234 235 238 239 242 243 246 247 250 251 254 255 
Is it in the list above? (yes or no): no

4 5 6 7 12 13 14 15 20 21 22 23 28 29 30 31 36 37 38 39 44 45 46 47 52 53 54 55 
60 61 62 63 68 69 70 71 76 77 78 79 84 85 86 87 92 93 94 95 100 101 102 103 108 
109 110 111 116 117 118 119 124 125 126 127 132 133 134 135 140 141 142 143 148 
149 150 151 156 157 158 159 164 165 166 167 172 173 174 175 180 181 182 183 188 
189 190 191 196 197 198 199 204 205 206 207 212 213 214 215 220 221 222 223 228 
229 230 231 236 237 238 239 244 245 246 247 252 253 254 255 
Is it in the list above? (yes or no): yes

8 9 10 11 12 13 14 15 24 25 26 27 28 29 30 31 40 41 42 43 44 45 46 47 56 57 58 
59 60 61 62 63 72 73 74 75 76 77 78 79 88 89 90 91 92 93 94 95 104 105 106 107 
108 109 110 111 120 121 122 123 124 125 126 127 136 137 138 139 140 141 142 143 
152 153 154 155 156 157 158 159 168 169 170 171 172 173 174 175 184 185 186 187 
188 189 190 191 200 201 202 203 204 205 206 207 216 217 218 219 220 221 222 223 
232 233 234 235 236 237 238 239 248 249 250 251 252 253 254 255 
Is it in the list above? (yes or no): yes

16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 48 49 50 51 52 53 54 55 56 57 
58 59 60 61 62 63 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 112 113 114 
115 116 117 118 119 120 121 122 123 124 125 126 127 144 145 146 147 148 149 150 
151 152 153 154 155 156 157 158 159 176 177 178 179 180 181 182 183 184 185 186 
187 188 189 190 191 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 
223 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 
Is it in the list above? (yes or no): no

32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 
58 59 60 61 62 63 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 160 161 162 163 
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 
184 185 186 187 188 189 190 191 224 225 226 227 228 229 230 231 232 233 234 235 
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 
Is it in the list above? (yes or no): no

64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 192 193 194 195 
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 
Is it in the list above? (yes or no): yes

128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 
248 249 250 251 252 253 254 255 
Is it in the list above? (yes or no): no

You are thinking of 77

The program prints out 8 sets of numbers. Notice the pattern:
  1. starting at 1 print the number, skip the next one, print the next one, skip the next one, etc.
  2. starting at 2 print the next 2 numbers, skip the next 2, print the next 2, skip the next 2, etc.
  3. starting at 4 print the next 4 numbers, skip the next 4, print the next 4, skip the next 4, etc.
  4. starting at 8 print the next 8 numbers, skip the next 8, print the next 8, skip the next 8, etc.
  5. etc

You will implement this using nested for loops to print out the 8 sets of numbers (that is, you cannot just cut-n-paste these numbers into your program).

You can then calculate the number the person is thinking of by simply adding up the first number in each one of the lists the user said "yes" to. In the example above this would be 1+4+8+64 = 77.

The homework is due on Monday, February 6 @noon at the dropbox.cse.sc.edu as usual. Yes, this is the same time as your Test 1.

2 comments:

Jose Vidal said...

Its not a big deal but, yes, cutting off the lines of numbers (as shown) is part of the requirements. That is, if you just print each set of numbers in one big line you will not get a 10 (probably a 9).

paspossible said...

No offense, but that's kind of unfair to post on February 4th without an email to the whole class or anything. A lot of people did this homework before then and it wasn't actually a part of the instructions. It's only by chance that I saw this now and it's a bit too late to sit up and figure that out at this point.