ITI1120 F – Introduction to Computing 1 – Winter 2020
Assignment 1
All rights reserved. No part of this document may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, or otherwise without prior written permission from the instructor.
Section A: Important Instructions
1. Read and follow the instructions below carefully.
2. This assignment is worth 5% of your grade.
3. Submit your assignment by Sunday February 9th, 11:30 PM via Brightspace. Refer to the course syllabus to understand the policy over late assignment submissions. You can make multiple submissions, but only the last submission will be graded.
4. The goal of this assignment is to learn and practice (via programming) the concepts that we have learned so far: numbers, algebraic expressions, boolean expressions, strings, operations on strings, type conversion, variables, use of Python’s built in functions including input and output functions, designing your own functions, and testing your functions.
5. Before you can start this assignment, you need to know how to use google Colab or Jupyter notebook to develop and test your code. Refer to lecture or lab material for help.
6. Assignments must be submitted in a notebook format (file extension .ipynb) using free resources: either google Colab or Jupyter. The following information must be included at the beginning of your assignment program:
# Course: IT1 1120
# Assignment number: 1
# Family name, Given name # Student number
7. This is an individual assignment, NOT a group effort. Review and adhere to course policies and the university Plagiarism and Academic Integrity policy presented during the first lecture.
8. The assignment has 10 programming questions. Each question asks you to design a function (s). Gather all answers to all questions in ONE file only, called a1_xxxxxx.ipynb (where xxxxxx is replaced with your student number). In this one file, have a code cell
for the answer to every one of the 10 questions. Your submission should contain AT
LEAST 10 cells.
9. Your program must run without syntax errors. In particular, when grading your
assignment, TAs will first open your file a1_xxxxxx.ipynb with either google Colab or Jupyter notebook and press Run Module. If pressing Run Module causes any syntax error for any cell, the grade for that question will be zero.
10. For each of the functions below, two tests are provided to test your functions. For example, you should test question 3 by making function call reverse_int(123) in your google Colab or Jupyter notebook. To obtain a partial mark your function may not necessarily give the correct answer on these tests. But if your function gives any kind of python error when run on the tests provided below, that question will be marked with zero points.
11. To determine your grade, your functions will be tested both with examples provided in each question and with extra examples.
12. Each function has to be documented with docstrings (as was explained in the Lectures. In particular, each function has to have docstrings that specify:
a. description about what the function does (while mentioning parameter names)
b. preconditions, if any
Section B: Assignment Questions
1. Write a function repeat(string, n, delim) that returns the string repeated n times, separated by the string by a given delim. For example,
>>>repeat(“Hello”, 3, “!”)
Hello! Hello! Hello
2. Implement function points() that takes as input four numbers x1, y1, x2, y2 that are the coordinates of two points (x1;y1) and (x2; y2) in the plane. Your function should compute:
a. The slope of the line going through the points, unless the line is vertical
b. The distance between the two points.
Your function should print the computed slope and distance (rounded to 2 decimal places) in the following format. If the line is vertical, the value of the slope should be string ‘infinity’. Note: Make sure you convert the slope and distance values to a string before printing them. >>> points(0, 0, 1, 1)
Slope = 1.0
Distance = 1.41
>>> points(0, 0, 0, 1)
Slope = infinity
Distance = 1.0
3. Implement function reverse_int() that takes a three-digit integer as input and returns the integer obtained by reversing its digits. For example, if the input is 123, your function should return 321. You are not allowed to use the string data type operations to do this task. Your program should simply read the input as an integer and process it as an integer using operators such as // and %. You may assume that the input integer does not end with the 0 digit.
>>> reverse_int(123)
321
>>> reverse_int(908)
809
4. Write a function is_palindrome() that check a string “as an input” as argument and returns True or False indicating whether the string is a palindrome or not. A palindrome is a string that can be read the same way forward and backward. Your function does not need to convert between lower and upper case characters and needs to check only for an exact match including case. For example, the string ‘madam’ is a palindrome but the string ‘Madam’ is not. Your function should ask you to “Enter any String” and should print the string + is not palindrome or is a palindrome, e.g.:
>>> Enter any String: Ottawa
Ottawa is not a palindrome
5. Write the following functions.
a. all_equal(x, y, z) (returning true if the arguments are all the same)
b. all_different(x, y, z) (returning true if the arguments are all different) c. all_sorted(x, y, z) (returning true if the arguments are sorted, with the smallest one coming first).
6. A year with 366 days is called a leap year. Leap years are necessary to keep the calendar synchronized with the sun because the earth revolves around the sun once every 365.25 days. Actually, that figure is not entirely precise, and for all dates after 1582 the Gregorian correction applies. Usually years that are divisible by 4 are leap years (for example, 1996). However, years that are divisible by 100 (for example, 1900) are not leap years, but years that are divisible by 400 are leap years (for example, 2000). Implement function leap() that takes one input argument—a year—and returns True if
the year is a leap year and False otherwise.
>>> leap(2008)
True
>>> leap(1900)
False
>>> leap(2000)
True
7. Write function letter2number() that takes as input a letter grade (A, B, C, D, F, possibly with a – or +) and returns the corresponding number grade. The numeric values for A, B, C, D, and F are 4, 3, 2, 1, 0. A + increases the number grade value by 0.3 and a – decreases it by 0.3.
>>> letter2number(‘A-‘)
3.7
>>> letter2number(‘B+’)
3.3
>>> letter2number(‘D’)
1.0
8. Write a function is_nneg_float() that checks if string s denotes a non-negative floating point value (not in scientific notation). This function should return True if s contains (at most) one decimal point and one or more digits (and nothing else); and False otherwise. According to this definition an integer should return True.
>>> is_nneg_float(“2.15”)
True
>>> is_nneg_float(“3.”)
True
>>> is_nneg_float(“.5”)
True
>>> is_nneg_float(“123”)
True
>>> is_nneg_float(“-12”)
False
>>> is_nneg_float(“1e10”)
False
9. Rock, Paper, Scissors is a two-player game in which each player chooses one of three items. If both players choose the same item, the game is tied. Otherwise, the rules that determine the winner are:
a. b. c.
Rock always beats Scissors (Rock crushes Scissors) Scissors always beats Paper (Scissors cut Paper) Paper always beats Rock (Paper covers Rock)
Implement function rps() that takes the choice (‘R’, ‘P’, or ‘S’) of player 1 and the choice of player 2, and returns -1 if player 1 wins, 1 if player 2 wins, or 0 if there is a tie.
>>> rps(‘R’, ‘P’)
1
>>> rps(‘R’, ‘S’)
-1
>>> rps(‘S’, ‘S’)
0
10. Write a function alogical(n) , that takes as input a number, n, where n is bigger or equal to 1, and returns the minimum number of times that n needs to be divided by 2 in order to get a number equal or smaller than 1. For example 5.4/2=2.7. Since 2.7 is bigger than 1, dividing 5.4 once by 2 is not enough, so we continue. 2.7/2=1.35 thus dividing 5.4 twice by 2 is not enough since 1.35 is bigger than 1. So we continue. 1.35/2=0.675. Since 0.675 is less than 1, the answer is 3. In particular, these calculations determine that 5.4 needs to be divided by 2 three times minimum in order to get a number that is less than or equal to 1.