Reference no: EM133091489
Functions and Classes
Exercises
Question 1: Write a program with a function called abbathat 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.
Question 5: Consider the following class definition:
class Vector:
def __init__(self,x,y):
self.x = x
self.y = y
def add1(self, v):
self.x = self.x + v.x
self.y = self.y + v.y
def add2(v1,v2):
v = Vector(0,0)
v.x = v1.x + v2.x
v.y = v1.y + v2.y
return v
def dot_product(v1,v2):
return v1.x*v2.x + v1.y*v2.y
def print(self):
print('({},{})'.format(self.x,self.y))
Save the class definition in a file and try to understand what this class is for and how it can be used. Run the code and experiment with the class definition.