CSC108H Exercise 1

Due Date: TBA, depending on when the automarking is set up.

The purpose of this exercise is to become comfortable with integers, integer division and the remainder operation (commonly known as mod in computer science). Solving this exercise requires understanding how positional notation works. We will only be dealing with converting positive integers, not fractional or negative numbers. These operations come up often in programming, in part due to the face that while we use a decimal system computer are built with a binary system of numbers, because it is much easier to encode something that can take only 2 values in hardware than something that can take 10 values.

In a file called e1.py, write the following functions.

Function name:
(Parameter types) -> Return type
Description
larger_remainder:
(int, int, int) -> bool
Return True if the remainder of the first integer when divided by the third integer is larger than the remainder of the second integer when divided by the third integer. Return False otherwise.
nth_digit:
(int, int) -> int
The first parameter is an arbitrary integer that you can think of as being base 10. The second parameter is an integer that specifies a digit in the first parameter. This function returns that digit. Digits are counted from right to left, so that 0 represents the 0s digit, 1 represents the 10s digit, 2 represents the 100s digit and so on. So nth_digit(123,0) should return 3, nth_digit(123,1) should return 2, and nth_digit(123,2) should return 1.

You can assume the first number is positive, and the second number refers to a digit that is explicitly written. So you will not need to handle something like nth_digit(123,4).
nth_digit_w_base:
(int, int, int) -> int
Here we expand nth_digit to handle arbitrary bases. The first integer is still the integer that we are looking at, the second integer is still the digit that we want, the last integer represent the base that we view the first integer in. So nth_digit_w_base(x, y, 10) should behave similarly to nth_digit(x, y).

You may still assume that x is positive; however you may no longer assume that the digit is explicitly represented. If it is not, you should return -1. So nth_digit(123, 4, 10) should return -1. 123 is 1111011 in base 2, so nth_digit_w_base(123, 4, 2) should return 1. You should also return -1 if the base is not a positive integer larger than 1.

Content of e1.py: Your e1.py should contain only the three function definitions (larger_remainder, nth_digit, and nth_digit_w_base). Do not include any print statements. We will call your functions to test them and printing may cause your code to fail the tests!

To make things a tad easier, we are giving you some starter code here. Each of the three functions is at various levels of completion, although none of them are complete.

How you should verify your code: Using the editor area of Wing101, define the functions in e1.py and then click "Run". In the shell, call your functions to verify that they work as specified.

Pre-marking: We will "pre-mark" the exercise by getting your most recent submission from the online submission system (MarkUs), running our tests on it, and giving you the results (in a file called test_e1.out on MarkUs). This will be done at scheduled times (see the Exercises webpage). You may resubmit as often as you like; the version in MarkUs at the deadline is what your exercise mark is based on.

Note, that automarking is not yet setup.

What To Hand in: Submit e1.py, according to the instructions on the course website. Remember that spelling of filenames and function names, including case, counts: your files and functions must be named exactly as specified above.