### ISD Term 1 Coursework 2

(a) (15 marks)

Write a function called lifeStage, this function has 2 arguments. The first is called mammal and the second is called age they are both strings.

The function returns a tuple containing one boolean and one string.

The first element of the returned tuple is True if both inputs are valid and is False otherwise.

If there is an invalid input then the second element of the tuple is a feedback string, which is one of the following:

‘Not a valid mammal’,

‘Age is not a positive whole number”, ‘Both inputs are not valid’.

If both inputs are valid then the second element of the tuple is the computed life-stage, which is one of the following:

Examples: (Note quotation marks need to be replaced if you wish to copy to Python.)

Running the following line:

print(lifeStage("h","120")) should display the following tuple (True, ’Juvenile’)

Running the following line:

print(lifeStage("human","fifty"))

should display the following tuple

(False, ’Both inputs are not valid’)

Use the lists provided in the repl term1cw2. This will mean you can reduce the number of conditional statements you need to hardcode by replacing them with a loop.

The simpler your program (i.e.  the  less hardcoded the decisions), the more marks you will receive. This means that your program need only indices of lists to access the relevant information.

Hint, To find the correct category you can use a while loop.'

Advanced hintyou can cast a boolean to integer, i.e. int(True) is 1 and int(False) is 0.

(b) (5 marks)

Write a function called main() that has no arguments. This function reads two inputs from a user that will be used in a call to your lifeStage function.

If the user enters ’q’ in any of the entries then your program terminates.

If the user enters anything other than the ending sentinel, call your lifeStage function with the values entered by the user. If the val- ues are legitimate, display the resulting category and your program should end. If the values are not legitimate, display the feedback message returned from your lifeStage function and loop to accept another round of user entered data.

Your program should replicate the following behaviour:

Please input the either (d)og or (h)uman d Please input the age in whole months 60 Mature

An example where the user enters incorrect values then quits:

Please input the either (d)og or (h)uman dog Please input the age in whole months 35

Not a valid mammal

Please input the either (d)og or (h)uman q Please input the age in whole months 34

(c) 5 additional marks will be awarded for the clarity of your code for the above two functions, this includes the following:

• Appropriate variable, function and argument names.

2. (10 marks)

Write a function called interleaveIt that is a refactor of the following code. Your function must not use recursion, it must use instead a for loop to process the content of the lists.

## Interleave the content of two lists that are of the same length # @param a first list

# @param b second list

# @return a list containing the interleaved content

def

interleave(a,b)

: if len(a)>0:

return [a[0]] + [b[0]] + interleave(a[1:],b[1:]) else:

return []

Hint: what does interleave([1,3],[2,4])) return?

3. The following are distinction level questions.

(a) (5 marks)

Write a function called interleaveItAnyLength. This function is an amended version of interleaveIt, that  allows the  two lists to  be of different lengths. For the following code fragment

print(interleaveItAnyLength([1,3,5,7],[]))

print(interleaveItAnyLength([1,3,5,7],[2,4]))

print(interleaveItAnyLength([],[2,4]))

The output should be:

[1, 3, 5, 7]

[1, 2, 3, 4, 5, 7]

[2, 4]

(b) (5 marks)

Write a function called interleaveAnyLength. This is a refactored version of interleaveItAnyLength. Your function must use recursion.