In this test you will be required to write two functions digitval(n)
and digitpos(n)
. Use a single .py
file to develop your functions and submit it via Blackboard. Make sure that you use the function names exactly as specified. You are free to write other functions which can be used by digitval(n)
and digitpos(n)
, but avoid having any code outside functions. All testing should be done in a main()
function.
To get started, copy and paste the following lines as a template for your code and fill out the fields in the docstring:
"""
Full name: Peter Pan
StudentId: 123456
Email: peter.pan@student.manchester.ac.uk
"""
def digitval(n):
# TODO: Your code goes here
return # TODO: return the required value
def digitpos(n):
# TODO: Your code goes here
return # TODO: return the required position
def main():
# TODO: Test your functions here
# For example, like so:
print(digitval(-273))
main()
Problem 1. Write a function digitval(n)
which accepts an integer argument n
. The function has to return (not print!) the value of the largest digit in n
as an integer.
For example, the function call digitval(-273)
should return the integer value $7$.
Problem 2. Write a function digitpos(n)
which accept an integer argument n
. The function has to return (not print!) the position of the first largest digit in n
as counted from the left. The first position has index 1, the second index 2, and so on.
For example, the function call digitpos(1767234)
should return the integer value 2
, as the largest digit 7
appears first in the second position as counted from the left.