Update page 'Day2 Homework -Karen'

2023-03-31 17:27:54 +02:00
parent ac1e94554d
commit a3e97c2493
+134
@@ -0,0 +1,134 @@
**1. Annotate the following python expressions with the specific types of each literal, as well as the overall return value:**
```
i = 3 # the literal "3" and the overall return "i" are both integer as `int`
f = 3.0 # the literal "3.0" and the overal return "f" are both floating-point literal of type `float`
c = 3+3i # the literal "3" is an integer as `int` while the literal "3i" is a complex literal with 0 as the real part and the 3i as the imaginary part; the overal return c is a complex number of type `complex`
b = b'abcdefg' # the literal "b'abcdefg'" and the overall return "b" are both bytes literal of type `bytes`
s = 'abcdefg' # the literal "'abcdegf'" and the overall return "s" are both string literal of type `str`
s = "abcdefg" # the literal ""abcdegf"" and the overall return "s" are both string literal of type `str`
```
❓ I have a question for the function 'c' as it gives a syntax error when I enter it in Python saying invalid decimal literal, so how can we enter such kind of complex number of type `complex` in python?
**2. For each of the types string, bytestring, integer, floating point, and imaginary literals, construct a 5x5 table showing what happens when the character "0" or number 0 is compared to each other type.**
```ruby
# Define literals
str_literal = "12345"
bytes_literal = b"12345"
int_literal = 12345
float_literal = 123.45
imag_literal = 123+45j
# Define comparison function
def compare(literal):
# Create table header
print(f"\nComparison table for {type(literal).__name__} literals:\n")
print(f"{'':10} {'0':^5} {literal[0]:^5} {literal[1]:^5} {literal[2]:^5} {literal[3]:^5} {literal[4]:^5}")
# Compare each element in the literal to 0
for i, elem in enumerate(literal):
is_equal = "==" if elem == 0 else "!="
print(f"{elem:<10} {is_equal:^5} {0:^5} {is_equal:^5} {i:^5} {is_equal:^5} {i+1:^5} {is_equal:^5} {i+2:^5} {is_equal:^5} {i+3:^5} {is_equal:^5} {i+4:^5}")
# Call comparison function for each type of literal
compare(str_literal)
compare(bytes_literal)
compare(int_literal)
compare(float_literal)
compare(imag_literal)
Comparison table for str literals:
0 1 2 3 4
1 != == != != !=
2 != != == != !=
3 != != != == !=
4 != != != != ==
5 != != != != !=
Comparison table for bytes literals:
0 1 2 3 4
49 != == != != !=
50 != != == != !=
51 != != != == !=
52 != != != != ==
53 != != != != !=
Comparison table for int literals:
0 1 2 3 4
12345 != != != != !=
Comparison table for float literals:
0 1 2 3 4
123.45 != != != != !=
Comparison table for complex literals:
0 1 2 3 4
(123+45j) != != != != !=
```
**3. Now do the same thing for the + and == operators for each pair of types in two other 5x5 tables.**
**4. Write a small program that uses the AST module to open the lexer/parser example, and dumps the resulting AST. See if there are any arguments to the parse method which make the output easier to read.**
**5. What does this do? `print("\007")` Can you figure out why?**
**6. What does this do? `print("abcdefg\rhijklmnop")` Can you figure out why?**
**7. This is an HTTP server. Run it and connect to http://localhost:8000 . Why doesn't this do the same thing as what you see above? (don't forget to shut down the HTTP server afterward.)**
```
import http.server
import socketserver
from urllib.parse import urlparse, parse_qs
class MyRequestHandler(http.server.SimpleHTTPRequestHandler):
def do_GET(self):
self.send_response(200)
self.send_header('Content-type', 'text/plain')
self.end_headers()
output_string = "abcdefg\rhijklmnop"
self.wfile.write(output_string.encode())
PORT = 8000
Handler = MyRequestHandler
httpd = socketserver.TCPServer(("", PORT), Handler)
print(f"Serving on port {PORT}")
httpd.serve_forever()
```
**8. Think of a way someone might try to commit a crime or hide an embezzlement scheme using the answers to the above two questions. Hint: applications usually output on web pages, but developers and system administrators usually troubleshoot in the console.**
**9. Think of how you would do input validation to prevent such a crime. Replace the code REPLACE ME here so the tests pass:**
```
def validate(str):
for char in str:
if REPLACE ME:
raise ValueError("Crime detected!")
try:
validate("abcdefg")
print("✅ test1 pass!")
except:
print("❌ test1 pass!")
try:
validate("abcdefg\rhijklmnop")
print("❌ test2 fail!")
except ValueError as err:
print("✅ test2 pass!")
```