-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgramming_Assignment_15.py
More file actions
92 lines (80 loc) · 4.1 KB
/
Programming_Assignment_15.py
File metadata and controls
92 lines (80 loc) · 4.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
**Question 1**:
```python
def divisible_by_5_and_7(n):
for i in range(n+1):
if i % 5 == 0 and i % 7 == 0:
yield i
n = int(input("Enter the value of n: "))
print(",".join(str(x) for x in divisible_by_5_and_7(n)))
```
Explanation:
- The `divisible_by_5_and_7` function is a generator that yields the numbers between 0 and `n` that are divisible by both 5 and 7.
- The function uses a `for` loop to iterate through the range from 0 to `n` (inclusive), and checks if the current number `i` is divisible by both 5 and 7 using the modulo operator `%`.
- If the condition is met, the function `yield`s the current number `i`.
- The `print` statement then joins all the yielded numbers into a comma-separated string using a list comprehension and the `join()` method.
**Question 2**:
```python
def even_numbers(n):
for i in range(n+1):
if i % 2 == 0:
yield i
n = int(input("Enter the value of n: "))
print(",".join(str(x) for x in even_numbers(n)))
```
Explanation:
- The `even_numbers` function is a generator that yields the even numbers between 0 and `n`.
- The function uses a `for` loop to iterate through the range from 0 to `n` (inclusive), and checks if the current number `i` is even using the modulo operator `%`.
- If the condition is met, the function `yield`s the current number `i`.
- The `print` statement then joins all the yielded numbers into a comma-separated string using a list comprehension and the `join()` method.
**Question 3**:
```python
def fibonacci(n):
if n == 0:
return 0
elif n == 1:
return 1
else:
return fibonacci(n-1) + fibonacci(n-2)
n = int(input("Enter the value of n: "))
print(",".join(str(fibonacci(i)) for i in range(n+1)))
```
Explanation:
- The `fibonacci` function is a recursive function that calculates the Fibonacci sequence up to the `n`th term.
- The function first checks the base cases: if `n` is 0, it returns 0; if `n` is 1, it returns 1.
- For all other values of `n`, the function recursively calculates the `(n-1)`th and `(n-2)`th Fibonacci numbers and adds them together to get the `n`th Fibonacci number.
- The `print` statement then creates a list of Fibonacci numbers up to the `n`th term using a list comprehension, and joins them into a comma-separated string.
**Question 4**:
```python
def extract_username(email):
# Assuming the email is in the format "<username@companyname.com>"
start = email.index('<') + 1
end = email.index('@')
return email[start:end]
email = input("Enter the email address: ")
print(extract_username(email))
```
Explanation:
- The `extract_username` function takes an email address as input and returns the username part of the email.
- The function assumes that the email address is in the format `"<username@companyname.com>"`.
- It uses the `index()` method to find the starting and ending indices of the username, and then slices the email string to extract the username.
- The `print` statement simply calls the `extract_username` function with the input email address and prints the resulting username.
**Question 5**:
```python
class Shape:
def __init__(self):
self.area = 0
def print_area(self):
print("The area of the shape is:", self.area)
class Square(Shape):
def __init__(self, length):
super().__init__()
self.length = length
self.area = length * length
# Example usage
square = Square(5)
square.print_area()
```
Explanation:
- The `Shape` class is the base class, and it has an `__init__` method that initializes the `area` attribute to 0. It also has a `print_area` method that prints the area of the shape.
- The `Square` class is a subclass of `Shape`. It has an `__init__` method that takes a `length` argument and calls the `__init__` method of the `Shape` class using `super().__init__()`. It then calculates the area of the square (length * length) and assigns it to the `area` attribute.
- In the example usage, we create a `Square` object with a length of 5, and then call the `print_area` method, which will print the area of the square (25).