A document from MCS 260 Fall 2021, instructor Emily Dumas. You can also get the notebook file.

MCS 260 Fall 2021 Worksheet 1 Solutions

  • Course instructor: Emily Dumas

Topics

This worksheet covers software setup for MCS 260 and number systems.

In general, the MCS 260 worksheet for any week's lab will cover material up to the Monday of that week; for week 1, this means only lecture 1 is included.

Instructions

  • Work on the activities below in lab.
  • Seek assistance from your TA or from fellow students if you run into trouble.
  • Start on the software setup tasks, but skip ahead to the number systems exercises if it looks like you are running out of time. It's important to get practice on both while you have a TA and other students around to offer support and assistance.

Collaboration

Collaboration on worksheets is strongly encouraged.

Resources

The main course materials to refer to for this worksheet are:

(Lecture videos are not linked on worksheets, but are also useful to review while working on worksheets. Video links can be found in the course course Blackboard site.)

Note about solutions

Unlike most future worksheets, many of the steps on this one just ask you to do things and don't have "solutions" per se. The actual solutions begin on question 9.

Part I: Software setup

These instructions assume you'll use a personal computer to do some of your coursework, and that you brought this device to lab with you.

If you are going to use UIC lab computers exclusively, and not do any course work on a personal device, you shouldn't need to install anything. Instead, follow the instructions below but skip the actual install steps, so that they become a series of checks that everything is working and that you know how to run it.

All of the software you need for MCS 260 is free. No purchase is required.

1. Install Python 3.9 (or any version 3.6 or higher)

The exact steps depend on the operating system of your computer (Windows, MacOS, Linux), and details can be found in the Getting Started section of the course web page.

For most students, this step either requires no action (recent MacOS) or is as simple as opening the Microsoft Store, finding Python 3.9, and clicking Install (Windows).

2. Open a terminal

We'll do a lot of work in MCS 260 using a text-based interface to your computer, the terminal.

The terminal is an application like any other, and is already installed on your computer. But the name depends on your operating system:

  • In Windows, you need to run Windows Powershell. Look for it in the Start Menu. It should open a window with a dark blue background. We recommend making a shortcut to this application on the desktop or in the taskbar, since you'll need to use it a lot.
  • In MacOS, run the application Terminal, which is located in the Applications menu under the submenu Utilities.
  • In Linux (with a graphical desktop), the name depends on the distribution and desktop environment. You'll need to open the application menu or launcher and look for a terminal application.

At the end of this step, you should have a window open that displays a small amount of text, and which is waiting for keyboard input.

3. Run the Python 3 interpreter in the terminal

To do this, you'll need to make the terminal the active window and then type the interpreter command name followed by the Enter key. The interpreter command name could be either python3 or python, and in some cases both will work. Try them in that order.

Success looks something like this:

PS C:\Users\ddumas> python
Python 3.9.6 (tags/v3.9.6:db3ff76, Jun 28 2021, 15:26:21) [MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>

Notice the Python 3.9.6 on the second line, which shows that Python 3.9 is running. If you see something like Python 2.7.18 instead, it means that command opened a much older version of Python that cannot be used in MCS 260. Try the command python3 instead.

At this point your terminal will be "stuck" in Python, which waits for you to type commands. You can tell that you are in Python because the prompt is >>>. If you needed to exit from Python, you would use the command exit(), but we'll stay in Python for the next step.

4. Run the traditional first Python statement

This step assumes you have a terminal running, and Python 3.x is running inside that terminal. You should see a prompt >>> with the cursor to the right of it.

Type the traditional first Python statement

print("Hello world!")

followed by the Enter key. Be careful to include the quotation marks and parentheses exactly as shown above. If you succeeded, a new line of text should appear that contains simply

Hello world!

At this point, you can exit Python by typing exit() followed by the Enter key.

5. Install a programming editor

Most students entering MCS 260 either won't have a programming editor installed, or else won't have a strong opinion about which one is best. If that applies to you, then you should install our recommended one, Visual Studio Code. It's a free program by Microsoft available from this URL:

Note that Visual Studio Code is very different from Visual Studio. Both are made by Microsoft, but we'll only use the first one.

If you already have a different programming editor installed, and prefer to use that, you may do so. However, we probably can't offer technical support on other editors.

Also, programs like Microsoft Word (on Windows) or Pages (on MacOS) are document editors, not text editors, and they won't work for MCS 260.

6. Create a file in Visual Studio Code

Open Visual Studio Code (from the start menu or application launcher) and create a new file. You can use Control-N or select File>New from the menus.

In that file, type the following text:

print("Hello world, this is a script!")

Save the file on the desktop (Control-S or File>Save), giving it the name hello.py. The extension .py identifies this file as a collection of Python statements, i.e. a script.

7. Find the desktop in the terminal

Switch back to the terminal or open it again, as applicable. Change the current working directory to the Desktop. In most terminals, the following command will do that:

cd ~/Desktop

8. Run the script you created

Run the script hello.py that you created in step 6 by typing the command

python hello.py

or

python3 hello.py

in the terminal. Success is indicated by

Hello world, this is a script!

appearing after you press Enter.

Note 1: This command needs to run in the terminal, and not in Python itself. If your terminal shows >>> next to the cursor, it means Python is running and you need to exit. The command exit() will exit Python and return to the terminal.

Note 2: This simple script will run properly even if you accidentally use the Python 2 interpreter. In the future, however, running scripts related to MCS 260 under Python 2 will create major problems. Thus it is important to make sure you are actually running Python 3. You can edit the script hello.py to add a check, as follows:

import sys
print("Hello world, from Python version",sys.version)

This will print a greeting that includes the Python version. For example, on Windows it might show:

Hello world, from Python version 3.9.6 (tags/v3.9.6:db3ff76, Jun 28 2021, 15:26:21) [MSC v.1929 64 bit (AMD64)] on win32

9. Experiment with Python as a calculator

Run python in the terminal again, to get the >>> prompt. Now, experiment with using it as a calculator. For example, try these commands:

  • 2+3
  • 2*36
  • 2**5
  • 11/2
In [1]:
2+3
Out[1]:
5
In [2]:
2*36
Out[2]:
72
In [3]:
2**5
Out[3]:
32
In [4]:
11/2
Out[4]:
5.5

And a couple of bonus calculations:

In [6]:
# 2 to the power 260
2**260
Out[6]:
1852673427797059126777135760139006525652319754650249024631321344126610074238976
In [7]:
# Is 10,000 days an exact number of weeks?
10_000 % 7
Out[7]:
4

(What this answer means: No, after 10,000 days the weekday will be 4 days later than at the start, e.g. 10,000 days after a Monday it will be a Friday.)

In [9]:
# How many seconds will you spend in MCS 260 this semester?
# 43 lectures, 50 minutes each + 15 labs, 110 minutes each
43*50*60 +  15*110*60
Out[9]:
228000

Part II. Number systems

Work out these number conversion problems by hand. This document doesn't provide space to write, so use a separate sheet of paper, a blank page in your tablet's writing application, etc.. If you can figure out a way to check them with a calculator or with Python, do so afterward, but this isn't required.

The purpose of working a few conversions by hand is to make sure you understand the basic principles of different number systems.

10. Convert 0b010110001 to decimal.

The place values with 1s in them are 1, 16, 32, 128. So the number represents 1 + 16 + 32 + 128 = 177 in decimal.

11. Convert 0xB00 to decimal.

In hexadecimal, the first few place values are 1, 16, and 16*16=256. Since B represents 11 and is the only nonzero hex digit in this number, appearing in the 256s place, the value is

0xB00 = 11 * 256 = 2816

12. Convert the decimal number 99 to hexadecimal.

Here is a table showing the conversion in the way we discussed in class (repeated integer division and remainder):

x   x//16   x%16
99  6       3
6   0       6

Note we stop when x//16 is zero. In binary tables, the last line is always 1 0 1, but for hex tables the last line may have a number different from 1 in the other columns.

In any case, we read the right column from bottom to top to conclude 99 = 0x63.

13. Convert 0b10010110 to hexadecimal.

Splitting it into blocks of 4 digits we have 0b1001_0110. The block 1001 on its own represents the number 8+1=9, while 0110 represents 4+2=6. Therefore 0b10010110 = 0x96.

14. Convert the decimal number 56 to binary.

Here is a table showing the conversion in the way we discussed in class (repeated integer division and remainder):

x   x//2   x%2
56  28     0
28  14     0
14  7      0
7   3      1
3   1      1
1   0      1

Therefore, 56 = 0b111000.

15. What is the decimal value of the largest number that can be written with 2 hexadecimal digits?

The largest digit in hex is F, with value 15. So the largest 2-digit hex number is 0xFF with value

0xFF = 15*16 + 15*1 = 255

16. Which is bigger: The largest number you can write in decimal with 4 digits, or the largest number you can write in binary with 13 digits?

The largest decimal 4-digit number is 9999. The largest binary 13-digit number is 0b1_1111_1111_1111, so the question is really to determine the decimal value of this number.

That value turns out to be 8191. One way to see this is to notice that adding one to 0b1_1111_1111_1111 must give the smallest 14-digit binary number 0b10_0000_0000_0000, which is just 2**13 = 8192, so we can subtract one to get the value of 0b1_1111_1111_1111. Another way is to compute

0b1_1111_1111_1111 = 1 + 2 + 4 + 8 + ... + 2048 + 4096 = 8191,

a sum you can do manually or using the formula for the sum of a geometric series.

Since 9999 > 8191, we conclude that the largest 4-digit decimal number is larger.

Revision history

  • 2021-08-26 Initial release