Reference no: EM133087563
Functions, Methods and Objects
Introduction
In each lab you'll work through a series of exercises that help you to understand and be able to apply and use the knowledge and techniques learnt in the associated weeks' lecture.Remember - you aren't expected to do everything from memory - sometimes it's best to do a little research / reading / re-reading of materials and come up with a great, and correct, answer rather than just 'taking a stab' at a question or exercise!
Exercises
Question 1. Write a program with a function called abba that takes two strings as input (a and b), and returns a string which consists of:
• The string a,
• Then the string b,
• Then the string b again,
• And finally the string a again.
So for example, if we passed in the strings 'a' and 'b' the function would return 'abba', andif we passed in the strings 'cat' and 'dog', the function would return 'catdogdogcat'. In your program make a few calls to the function and print out the returned value to ensure it's all working properly.
Question 2. Write a program with two functions called find_firstand find¬_all, each of which take two strings called source and target.as input.
The first_first function takes the two strings and input and returns the location of the target string within the source string as an integer - so for example, if we passed called find_firstwith the source string of "Roses are red, violets are blue" and a target string of "are", it would return the value 6 because that's the index of the first occurrence of the target string within the source string.
The find_allfunction works the same as the find_firstfunction, but it returns a list of all the locations of the target string within the source string - again, passing in "Roses are red, violets are blue" would return the list [6, 23] because the two instances of the substring start at those indices in the source string.
As before, make a few calls to the functions and print out the return values to ensure everything's working correctly.
Question 3. Write a short program with a function called bigger that takes two numbers as input parameters and returns the bigger of the two numbers. For example, if we called bigger(3, 5) it would return the value 5 because 5 is more than 3.
Once you have that working, write a function called biggest that takes three numbers and returns the biggest of the three values. An easy way to accomplish this is by using your bigger function within your biggest function - if you do it this way then it can be accomplished in just a single line of code!
Question 4. Write a program containing a function called swap that takes two parameters called first and second. You should be able to run the function passing it these two parameters and returning a 'swapped' version of these values. So, for example you could execute the function like this:
a = 123
b = 456
a, b = swap(a, b)
print(a, b) # Prints 456, 123 (i.e. the a variable now has b's value, and b now has a's value)
It's perfectly possible to do this with a single line of code within your function.
Attachment:- Functions, Methods.rar