1. Reflection

  • I learned about documentation, libraries, and APIs. Documentation is a part of the AP Exam project part and it requires explaining what and how your code works. Libraries help make your coding experience more efficient because they are pre-written and you can reference them throughout your code. I researched one library that utilizes Python: numPy, which works with arrays.
import numpy as np

arr = np.array([1, 2, 3, 4, 5])

print(arr)

print(type(arr))
[1 2 3 4 5]
<class 'numpy.ndarray'>
# to calculate how many axes there were in a huge list, we wouldn't need to count. 
import numpy as np

a = np.array(42)
b = np.array([1, 2, 3, 4, 5])
c = np.array([[1, 2, 3], [4, 5, 6]])
d = np.array([[[1, 2, 3], [4, 5, 6]], [[1, 2, 3], [4, 5, 6]]])

# dimensions are called axes in numpy 
print(a.ndim) # there are no axes in a, there are no [] 
print(b.ndim) # there is one axes, there is one []
print(c.ndim) # there are two axes 
print(d.ndim) # there would only be 3 axes in this instance because one axes is repeated twice [1,2,3]
0
1
2
3
  • Finally, APIs are a type of software that allows computers to communicate with each other. We utilized APIs before last trimester, we used RapidAPI.
  • I also learned about the random function, which generates and returns a random integer from the parameters given, inclusive. Each integer in the range has an equal chance of occurring. We can also use random libraries like random.choice and random.shuffle
  • randrange can be written like randrange(start, stop, step) where start is the minimum value, stop is the maximum value (like the randint function), and step is the incriment the values can be

2. Multiple Choice

  1. What does the random(a,b) function generate?

A. A random integer from a to be exclusive

B. A random integer from a to b inclusive.

  • Explanation: because the random function mean a random integer from the parameters set will be chosen. Because a,b is the parameters, a random integer from a to b will be given, including a and b.

C. A random word from variable a to variable b exclusive.

D. A random word from variable a to variable b inclusive.

  1. What is x, y, and z in random.randrange(x, y, z)?

A. x = start, y = stop, z = step

  • the randrange function has the parameters so that the the start, x is the minimum value. Y is the stop and maximum value, and z is the step, or the increment the values can be.

B. x = start, y = step, z = stop

C. x = stop, y = start, z = step

D. x = step, y = start, z = stop

  1. Which of the following is NOT part of the random library?

A. random.item

  • the item() function is used in dictionaries to find an item in the dictionary, it does not have any parameters and therefore is not a part of the random library. You can use this function without the import random funcion.

B. random.random

C. random.shuffle

D. random.randint

random = {
  "brand": "Ford",
  "model": "Mustang",
  "year": 1964
}

x = random.items()

random["year"] = 2018

print(x)
dict_items([('brand', 'Ford'), ('model', 'Mustang'), ('year', 2018)])

Short Answer Questions

1.What is the advantage of using libraries?

  • Using libraries is advantageous because it is more efficient and can reduce coding errors. You have reusable functions that can be used throughout the code without re-defining them, so you don't need to write code for complex functions.

2.Write a thorough documentation of the following code.

import random 

names_string = input("Give me everybody's names, separated by a comma.")
names = names_string.split(",")

num_items = len(names)

random_choice = random.randint(0, num_items - 1)

person_who_will_pay = names[random_choice]

print(f"{person_who_will_pay} is going to buy the meal today!")
bob is going to buy the meal today!

Documentation: Using the library random, I set the variable names_string equal to the the names the user inputs. the split function will separate the names with a comma. num_items will give each name given a number and using the random function randint, it randomly select a number that corresponds to a name. Finally, the procedure returns the name and the sentence.

4 Coding Challenge

  1. Create a program to pick five random names from a list of at least 15 names
import random 

namesList = ["Enid", "Tyler", "Gomez", "Wednesday", "Morticia", "Thornhill", "Weems", "Pugsley", "Thing", "Lurch", "Fester", "Xavier", "Bianca", "Kinbott", "Galpin"]

sample_namesList = random.sample(namesList, k=5) # here, k = number of items to select
print(f"you got these 5 random names: {sample_namesList}!")
you got these 5 random names: ['Fester', 'Wednesday', 'Gomez', 'Morticia', 'Galpin']!

First I created a list that had all the names I wanted to use, I chose to use characters from the show Wednesday. Then I set a variable equal to a function that would pick 5 different random names. I used the random library function choices and set k equal to 5 which would choose 5 options from the list. Finally, I printed the list. The random.sample function allows me to get 5 unique names.

  1. Create a program to simulate a dice game where each player rolls two fair dice (6 sides); the player with the greater sum wins
import random 

p1dice1 = random.randrange(1,6)
p1dice2 = random.randrange(1,6)
print("Player 1 rolled a", p1dice1, "and", p1dice2, "for a sum of:", p1dice1+p1dice2)

p2dice1 = random.randrange(1,6)
p2dice2 = random.randrange(1,6)
print("Player 2 rolled a", p2dice1, "and", p2dice2, "for a sum of:", p2dice1+p2dice2)

p1sum = p1dice1 + p1dice2
p2sum = p2dice1 + p2dice2

if p1sum > p2sum:
    print("Player 1 wins!")
elif p1sum == p2sum:
    print("Roll again! You both rolled the same sum.")
else: 
    print("Yay! Player 2 wins!")
Player 1 rolled a 3 and 2 for a sum of: 5
Player 2 rolled a 5 and 4 for a sum of: 9
Yay! Player 2 wins!

Code Explanation:

I used the random library, then I set variables equal to a random number 1-6, which would mimic rolling the dice. I named the variables based on who rolled and what number they rolled it. Then I added the numbers together, whichever player had the higher sum wins.

Extra Credit Attempt

  1. Initial direction of the robot (up, down, left, right)
  2. Initial Position of the robot
  3. Goal position
  4. Positions of at least 12 wall/obstacle squares
import random 

directions = ["up", "down", "left", "right"]
sample_directions = random.choices(directions)
print(f"Initial direction of the robot: {sample_directions}")

print(f"the robot starts on square number {random.randrange(1,25)}")

print(f"goal position at square number {random.randrange(1,25)}")

squareNumbers = [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]

sample_namesList = random.sample(squareNumbers, k=12) 
print(f"obstacles are on these numbers:{sample_namesList}")
Initial direction of the robot: ['up']
the robot starts on square number 5
goal position at square number 10
obstacles are on these numbers:[22, 18, 15, 13, 9, 5, 23, 14, 6, 2, 12, 8]

I tried but I couldn't figure out how to get the numbers to not overlap every time it's run.