diff --git a/Day 2 Homework - Karen.md b/Day 2 Homework - Karen.md index b233176..3d7d73d 100644 --- a/Day 2 Homework - Karen.md +++ b/Day 2 Homework - Karen.md @@ -76,7 +76,93 @@ Comparison table for complex literals: **3. Now do the same thing for the + and == operators for each pair of types in two other 5x5 tables.** +``` +# Comparison using the "+" operator +print("Comparison using the \"+\" operator:") +print("| Operator | String | ByteString | Integer | Float | Imaginary |") +print("|-----------------|--------------|------------|------------|------------|-------------|") +for type1 in [str, bytes, int, float, complex]: + for type2 in [str, bytes, int, float, complex]: + try: + result = type1() + type2() + print("| {} + {}{} | {:<12} | {:<10} | {:<10} | {:<10} | {:<10} |".format(type1.__name__, type2.__name__, " " * (9-len(type2.__name__)), result, "TypeError", "TypeError", "TypeError", "TypeError")) + except TypeError: + print("| {} + {}{} | {:<12} | {:<10} | {:<10} | {:<10} | {:<10} |".format(type1.__name__, type2.__name__, " " * (9-len(type2.__name__)), "TypeError", "TypeError", "TypeError", "TypeError", "TypeError")) + + +# Comparison using the "==" operator +print("\nComparison using the \"==\" operator:") +print("| Operator | String | ByteString | Integer | Float | Imaginary |") +print("|-----------------|--------------|------------|-----------|---------|-----------|") + +for type1 in [str, bytes, int, float, complex]: + for type2 in [str, bytes, int, float, complex]: + result = type1() == type2() + print("| {:<14} | {:<12} | {:<10} | {:<9} | {:<7} | {:<9} |".format("{} == {}".format(type1.__name__, type2.__name__), result, str(not result), str(type1() == 0 and type2() == 0 and type1 != complex), str(type1() == 0 and type2() == 0 and type1 != complex), str(type1() == 0j and type2() == 0j and type1 == complex))) +``` + +The results are shown below. + +``` +Comparison using the "+" operator: +| Operator | String | ByteString | Integer | Float | Imaginary | +|-----------------|--------------|------------|------------|------------|-------------| +| str + str | | TypeError | TypeError | TypeError | TypeError | +| str + bytes | TypeError | TypeError | TypeError | TypeError | TypeError | +| str + int | TypeError | TypeError | TypeError | TypeError | TypeError | +| str + float | TypeError | TypeError | TypeError | TypeError | TypeError | +| str + complex | TypeError | TypeError | TypeError | TypeError | TypeError | +| bytes + str | TypeError | TypeError | TypeError | TypeError | TypeError | +| bytes + bytes | TypeError | TypeError | TypeError | TypeError | TypeError | +| bytes + int | TypeError | TypeError | TypeError | TypeError | TypeError | +| bytes + float | TypeError | TypeError | TypeError | TypeError | TypeError | +| bytes + complex | TypeError | TypeError | TypeError | TypeError | TypeError | +| int + str | TypeError | TypeError | TypeError | TypeError | TypeError | +| int + bytes | TypeError | TypeError | TypeError | TypeError | TypeError | +| int + int | 0 | TypeError | TypeError | TypeError | TypeError | +| int + float | 0.0 | TypeError | TypeError | TypeError | TypeError | +| int + complex | 0j | TypeError | TypeError | TypeError | TypeError | +| float + str | TypeError | TypeError | TypeError | TypeError | TypeError | +| float + bytes | TypeError | TypeError | TypeError | TypeError | TypeError | +| float + int | 0.0 | TypeError | TypeError | TypeError | TypeError | +| float + float | 0.0 | TypeError | TypeError | TypeError | TypeError | +| float + complex | 0j | TypeError | TypeError | TypeError | TypeError | +| complex + str | TypeError | TypeError | TypeError | TypeError | TypeError | +| complex + bytes | TypeError | TypeError | TypeError | TypeError | TypeError | +| complex + int | 0j | TypeError | TypeError | TypeError | TypeError | +| complex + float | 0j | TypeError | TypeError | TypeError | TypeError | +| complex +complex| 0j | TypeError | TypeError | TypeError | TypeError | + +Comparison using the "==" operator: +| Operator | String | ByteString | Integer | Float | Imaginary | +|-----------------|--------------|------------|-----------|---------|-----------| +| str == str | 1 | False | False | False | False | +| str == bytes | 0 | True | False | False | False | +| str == int | 0 | True | False | False | False | +| str == float | 0 | True | False | False | False | +| str == complex | 0 | True | False | False | False | +| bytes == str | 0 | True | False | False | False | +| bytes == bytes | 1 | False | False | False | False | +| bytes == int | 0 | True | False | False | False | +| bytes == float | 0 | True | False | False | False | +| bytes ==complex | 0 | True | False | False | False | +| int == str | 0 | True | False | False | False | +| int == bytes | 0 | True | False | False | False | +| int == int | 1 | False | True | True | False | +| int == float | 1 | False | True | True | False | +| int == complex | 1 | False | True | True | False | +| float == str | 0 | True | False | False | False | +| float == bytes | 0 | True | False | False | False | +| float == int | 1 | False | True | True | False | +| float == float | 1 | False | True | True | False | +| float == complex| 1 | False | True | True | False | +| complex == str | 0 | True | False | False | False | +| complex == bytes| 0 | True | False | False | False | +| complex == int | 1 | False | False | False | True | +| complex == float| 1 | False | False | False | True | +|complex==complex | 1 | False | False | False | True | +``` **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.** Maybe we can first write a simple lexer sample, and then import `astor` module to output the AST in a more human-readable format.