VP 20: Arithmetic in Python (10 pts + 45 Extra)

What You Need

Any Python 3 environment, such as Google Colab.

Purpose

To perform basic programming tasks, such as arithmetic and string manipulations.

Google Colab

This is the new way to get a Python environment in the cloud, including an AI to help write your code.

In a browser, go to

https://colab.research.google.com/
If you see a blue "Sign In" button at the top right, click it and log into a Google account.

From the menu, click File, "New notebook".

Integer Arithmetic

Adding and Subtracting

Execute this code:
a = 10
b = 20
print("a:", a, "b:", b)

# Add and subtract
added = a + b
subtracted = a - b
print("Added:", added, "Subtracted:", subtracted)
The results show the expected values, as shown below.

Multiplying and Dividing

Execute this code:
a = 10
b = 20
print("a:", a, "b:", b)

# Multiplying and Dividing
multiplied = a * b
divided = a / b
print("Multiplied:", multiplied, "Divided:", divided)
The results show the expected values, as shown below.

Exponentiation and Log

Execute this code:
import math
a = 2
b = 4
print("a:", a, "b:", b)

# Exponentiation and Logarithm
exponentiation = a ** b
log_a = math.log(a, 2)
log_b = math.log(b, 2)
print("a ** b:", exponentiation)
print("log(a, 2):", log_a)
print("log(b, 2):", log_b)
The results show the expected values, as shown below.

Binary and Hexadecimal

Decimal to Binary and Hexadecmial

Execute this code:
a = 7
b = 24
print("a:", a, "b:", b)

# Binary and Hex
bin_a = bin(a)
bin_b = bin(b)
hex_a = hex(a)
hex_b = hex(b)
print("In binary, a:", bin_a, "b:", bin_b)
print("In hex, a:", hex_a, "b:", hex_b)
The results show the expected values, as shown below.

Binary and Hexadecimal to Decimal

Python 3 accepts integers in binary and hex natively, so no conversion is needed.

But if they are strings, the "int" function is needed to perform the conversion.

Execute this code:

a = 0xf0
b = 0b11011
print("a:", a, "b:", b)

c = "ff"
d = "11101"
print("c:", c, "d:", d)

# Converting to Decimal

dec_c = int(c, 16)
dec_d = int(d, 2)
print("In Decimal, c:", dec_c, "d:", dec_d)
The results show the expected values, as shown below.

Flag VP 20.1: Arithmetic (5 pts)

Start with two numbers:
a: 22
b: 45
Now perform these operations in order:
  • Multiply a by b
  • Add 108
  • Divide by 9
  • Subtract 14
The flag is the answer, covered by a green rectangle in the image below.

Flag VP 20.2: Math (5 pts)

Start with two numbers:
a: 25
b: 9
Now perform these operations in order:
  • Raise a to the power b
  • Divide by 625 (use two slashes, as shown below, to make the result an integer)
  • Subtract 300
  • Display the result in hexadecimal
The flag is the answer, covered by a green rectangle in the image below.

Flag VP 20.3: Log (5 pts extra)

Start with two numbers:
a: 32
b: 8
Now perform these operations in order:
  • Raise a to the power b
  • Divide by 50
  • Subtract 3000
  • Find the log to the base 2 of the result
The flag is the answer, covered by a green rectangle in the image below.

Flag VP 20.4: Octal (10 pts extra)

Start with two numbers:
a: 35
b: 4
Now perform these operations in order:
  • Raise a to the power b
  • Divide by 5 (make sure the result is an integer)
  • Subtract 200
  • Express the result in octal
If you aren't familiar with the octal system, use a search engine to find a page explaining it and how to use it in Python.

The flag is the answer, covered by a green rectangle in the image below.

Flag VP 20.5: Find the Pair (15 pts extra)

Start with these numbers:
[7419,162,1140,4737,6266,1248,7347,3580,7103,7577,2697,69,4881,
  3013,1178,418,6740,8615,1923,5134,4128,586,483,6930,2445,7541,
  2936,5390,9482,384,4850,3708,5583,8292,67,6003,7744,7918,7737,
  8996,146,3204,7937,2063,9031,1060,9127,387,9304,6360]
Find two numbers in that list that sum to 2085.

Multiply those two numbers together. The flag is that product.

Flag VP 20.6: Find the Triplet (15 pts extra)

Start with the numbers in this file:

https://samsclass.info/COMSC132/proj/VP20a

Find three numbers in that list that sum to 2085.

Multiply those three numbers together. The flag is that product.


Posted: 8-24-24
Flag 5 added 8-31-24
Flag 6 added 9-6-24
Video added 9-11-24