-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgramming_Assignment_16.py
More file actions
68 lines (50 loc) · 2.1 KB
/
Programming_Assignment_16.py
File metadata and controls
68 lines (50 loc) · 2.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
1. **Stuttering Function**:
```python
def stutter(word):
if len(word) < 2:
return word
stuttered = word[:2] + "... " + word[:2] + "... " + word + "?"
return stuttered
```
Explanation:
- The function first checks if the input word has at least 2 characters. If not, it returns the original word.
- Otherwise, it takes the first 2 characters of the word, repeats them twice with an ellipsis and space after each, and then adds the full word with a question mark at the end.
2. **Radians to Degrees Conversion**:
```python
import math
def radians_to_degrees(radians):
degrees = radians * (180 / math.pi)
return round(degrees, 1)
```
Explanation:
- The function uses the formula `degrees = radians * (180 / π)` to convert the input radians to degrees.
- It then rounds the result to one decimal place using the `round()` function.
3. **Curzon Number Checker**:
```python
def is_curzon(num):
numerator = 2 ** num + 1
denominator = 2 * num + 1
return numerator % denominator == 0
```
Explanation:
- The function calculates the numerator and denominator as per the problem statement.
- It then checks if the numerator is divisible by the denominator using the modulo operator `%`. If the result is 0, the number is a Curzon number, so the function returns `True`. Otherwise, it returns `False`.
4. **Area of a Hexagon**:
```python
import math
def area_of_hexagon(side_length):
area = (3 * math.sqrt(3) * side_length ** 2) / 2
return round(area, 1)
```
Explanation:
- The function uses the formula `Area = (3 * √3 * s^2) / 2`, where `s` is the side length of the hexagon.
- It calculates the area and rounds the result to one decimal place using the `round()` function.
5. **Binary Representation**:
```python
def binary(decimal):
binary_str = bin(decimal)[2:]
return binary_str
```
Explanation:
- The function uses the built-in `bin()` function to convert the input decimal number to its binary representation.
- The `[2:]` slice removes the leading `"0b"` from the binary string, leaving only the binary digits.