**Recursive Function
**

**Problem 1 Recursive functions. **

To complete python assignment problem, you are asked to write three re- cursive functions. All three functions should be implemented in a module called problem1.py. Please note the following important points.

- Write a recursive function called num double letters with a single string parameter astr that returns the number of occurrences of double letters in astr. A double letter is simply a consecutive pair of the same character. For example, num double letters("Mississippi") should return 3 because double letters occur 3 times in the string ("ss", "ss", and "pp"). Note that if the same letter occurs consecutively three or more times (this doesn’t happen too often in English!), we count every double letter pair. For example, num double letters("hmmm") should return 2 because there are two double m pairs (the second and third letter, as well as the third and fourth letter), and num double letters("hmmm") or num double letters("mmhmm") should return 3.
- Write a recursive function called has repeated with a single list parameter L, that returns true if L has repeating elements (i.e., an element occurring more than once) and False otherwise. For example, has repeats([3, 2, 1, 5, 12]) should return False and has repeats([3, 5, 2, 1, 5, 12]) should return True.
- Write a recursive function called abb pattern with a single parameter astr, which is a string. The function returns True if astr is a string of the form anb2n (n a-s followed by 2n b-s, where n is a positive integer) and False otherwise. For example, abb pattern("abb"), abb pattern("aabbbb"), and abb pattern("aaaabbbbbbbb") all return True, but abb pattern("") (parameter is an empty string), abb pattern("abbabb"), and abb pattern("aaabbbbb") all return False.

**Problem 2 Straight Lines.**

In this problem, you are asked to implement a class for straight lines called StraightLine. A straight line in two dimensions is represented in standard form by the linear equation ax + by = c. In this representation, a is called the x coefficient, b is called the y coefficient, and c is called the constant coefficient. Thus the main instance attributes for straight-line objects will be the three coefficients a, b, and c.

Implement the following methods for the StraightLine class. Recall once again that self is always the first parameter for a method of the class. Your implementation should appear in a module called problem2.py.

1. init: The constructor should create three instance attributes, which are the coefficients a, b, and c of the standard form equation of the line. The parameters to the constructor are initial values for these coefficients. Provide default values for all three attributes.

2. str: This method returns a printable version of a straight line, which is a “nice” string representation of the linear equation. This means that only non-zero coefficients of x and y should be printed, and if a coefficient is negative, the - (minus sign) must be embedded in the string. For example, the straight line 2x−5y = 4 should be represented by the following string (note that a = 2, b = −5, and c = 4 for this line):

2x - 5y = 4

and not as

2x + -5y = 4

Similarly, the straight line 5x = −6 should be represented as

5x = -6

and not as

5x + 0y = -6

3. repr: This method returns a meaningful string representation of the straight line. For example, this could be the same string returned by the str method.

4. slope: This method returns the slope of the line. If the line is vertical, the slope is undefined and the method should return None in this case.

5. y-intercept: This method returns the y-intercept of the line. If the line is vertical, then return None (since there is no y-intercept for vertical lines).

6. x-intercept: This method returns the x-intercept of the line. If the line is horizontal, then return None (since there is no x-intercept for horizontal lines).

7. parallel: This method has a parameter L which is another StraightLine object. It returns True if the line is parallel to L and False otherwise.

8. perpendicular: This method has a parameter L which is another StraightLine object. It returns True if the line is perpendicular to L and False otherwise.

9. intersection: This method has a parameter L which is another StraightLine object. It returns the point of intersection between the line and L. Return the point as a 2-tuple. If L is parallel to the line, then your function should return None (since two parallel lines do not intersect).

10. eq: This method overloads the equality operator ==. We say that two StraightLine objects are equal if and only if they have the same slope and y-intercept. Make sure that your code handles vertical lines correctly; two vertical lines are equal if and only if their x-intercepts are equal.

11. ne: This method overloads the inequality operator !=.

**Solution:**

```
def num_double_letters(astr):
if len(astr) < 2:
return 0
elif astr[0] == astr[1]:
return 1 + num_double_letters(astr[1:])
else:
return num_double_letters(astr[1:])
def has_repeats(L):
if len(L) <= 1:
return False
elif L[0] == L[1] or has_repeats(L[1:]) or has_repeats([L[0]] + L[2:]):
return True
else:
return False
def abb_pattern(astr):
if len(astr) < 3:
return False
elif astr == "abb":
return True
elif astr[0] == "a" and astr[len(astr)-2:] == "bb":
return abb_pattern(astr[1:len(astr)-2])
else:
return False
if __name__ == '__main__':
print(num_double_letters("mississippi"))
print(num_double_letters("hmmm"))
print(num_double_letters("hmmmm"))
print(num_double_letters("mmhmmm"))
print(has_repeats([3, 2, 1, 5, 12]))
print(has_repeats([3, 5, 2, 1, 5, 12]))
print(abb_pattern("aabbbb"))
print(abb_pattern("aaaabbbbbbbb"))
print(abb_pattern(""))
print(abb_pattern("abbabb"))
print(abb_pattern("aaabbbbb"))
```

2.

```
class StraightLine:
def __init__(self, a = 0, b = 0, c = 0):
self.a = a
self.b = b
self.c = c
def __str__(self):
result = ""
if self.a != 0:
result += str(self.a) + "x "
if self.b < 0:
result += "- " + str(self.b) + "y = "
elif self.b == 0:
result += "= "
else:
result += "+ " + str(self.b) + "y = "
else:
if self.b != 0:
result += str(self.b) + "y = "
result += str(self.c)
return result
def __repr__(self):
return self.__str__()
def slope(self):
if self.b == 0:
return None
else:
return self.a/self.b
def yintercept(self):
if self.b == 0:
return None
else:
return self.c/self.b
def xintercept(self):
if self.a == 0:
return None
else:
return self.c/self.a
def parallel(self, L):
return self.slope() == L.slope()
def perpendicular(self, L):
if self.slope() is None or L.slope() is None:
if (L.slope() == 0.00 and self.slope() is None) or (L.slope() is None and self.slope() == 0.00):
return True
else:
return False
else:
return self.slope()*L.slope() == -1
def intersection(self, L):
if self.parallel(L):
return None
else:
return ((L.b * self.c) - (self.b * L.c)) / ((self.a * L.b) - (self.b * L.a)), ((L.c * self.a) - (self.c * L.a)) / ((self.a * L.b) - (self.b * L.a))
def __eq__(self, L):
if self.slope() is not None and L.slope() is not None:
if self.slope() == L.slope() and self.yintercept() == L.yintercept():
return True
else:
return False
elif self.slope() is None and L.slope() is None:
if self.xintercept() == L.xintercept():
return True
else:
return False
return False
def __ne__(self, L):
return not self.__eq__(L)
```