3.14 3.15 hacks and notes
for week 4
notes
libraries contain prewritten code/procedures that you can use for coding.
HINT: libraries can simplify complex problems and using them can make a problem easier to solve
APIs (application program interfaces) specify how procedures in libraries should behave and be utilized.
Documentation is an explanation of the code
HINT: look at Documentation for code if you don't understand something
The APCSP EXAM REFRENCE SHEET could come in handy later. it is essentially a library provided by College Board.
you may need expressions for generating random values.
EX: random number generator, dice roll, etc.
RANDOM(a, b) generates and returns a random integer from a to b, inclusive. Each result is equally likely to occur.
randint(start, stop) generates random integer between start and stop.
randrange(start, stop, step) generates random number where start is min value, stop is max value, and step is the multiples the rng generates.
If start = 0 and step = 5 all the values that can by outputed are 0 and multiples of 5
HACKS
reflection
libraries and APIs are essentially prewritten code that can help you on complex problems. These libraries and APIs can also be used to not only write code easier and not develop everything on projects but also be the entire foundation of the code itself. I also learned to use documentation to understand code segments and libraries/APIS. Documentation should be looked at before using a library or tinkering with some code, as it gives you an understanding of the code you are working with and how you could work with it. lastly, understand that random expressions and the use of random algorithms will be tested on the AP EXAM. It is especially important to understand the random syntax for pseudocode on the AP Exam because there will definetly be problems on the exam with different random expressions.
multiple choice
Q 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.
C. A random word from variable a to variable b exclusive.
D. A random word from variable a to variable b inclusive.
ANS: B. this is the definition given earlier and makes most sense
Q 2: What is x, y, and z in random.randrange(x, y, z)?
A. x = start, y = stop, z = step
B. x = start, y = step, z = stop
C. x = stop, y = start, z = step
D. x = step, y = start, z = stop
ANS: A, because x and y determine min and max values, and z is supposed to determine the steps the random values take
Q 3: Which of the following is NOT part of the random library?
A. random.item
B. random.random
C. random.shuffle
D. random.randint
ANS: A because random.item sounds like it is just random.choice, which does exists. it is logical to believe that random.item can't exist because a similar existing random function already exists that could probably do what that intended to do.
Q 3: What is the advantage of using libraries?
Using libraries can make your projects and code much more simpler and take less time to write, as libraries essentially provide you with prewritten code that you can build upon for projects or use for their algorithms to avoid writing your own algorithms/code for everything. basically, libraries make writing code more efficient, as they give you prewritten algorithms that you can use for your code. Even if you want to write everything yourself, its important to know that libraries also reduce the size of your code, as it means you don't need to write a bunch of code that you could easily replicate using a library.
Q 4: Write a thorough documentation of the following code:
import random # import random library
names_string = input("Give me everybody's names, seperated by a comma.") # takes user input
names = names_string.split(",") # splits names_string into list. each item in list is name separated by commas
num_items = len(names) # number of names
random_choice = random.randint(0, num_items - 1) # picks random name, uses num_items to pick random item from the list
person_who_will_pay = names[random_choice] # determine person who is paying
print(f"{person_who_will_pay} is going to buy the meal today!") # prints who will pay
(4) Coding Challenges!
REQUIRED: Create programs in python to complete the two task
1: Create a program to pick five random names from a list of at least 15 names
import random
names = ["red", "joe", "joeseph", "john", "jamie", "jack", "joselyn", "justin", "blue", "eric", "zack", "harry", "lamb", "four", "five"]
# above is ls of names
i = 0
while i != 5: # repeat 5 times
rand_names = random.choice(names) # picks random name
i = i + 1 # needed for while loop
print(rand_names) # prints random name
2: 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
player1 = 0 # initial player's score
player2 = 0
i = 0
while i != 2: # while loop rolls the "dice"
x = random.randint(1, 6)
y = random.randint(1, 6) # both of these are the die rolls
player1 = player1 + x # adds die roll to the player score
player2 = player2 + y
i = i + 1 # repeat 2 times
print("player 1 score: ", player1)
#these 2 print out scores
print("player 2 score: ", player2)
if player1 > player2: # determine who won
print("player 1 wins!")
else:
print("player 2 wins!")
import random
row1 = [0, 0, 0, 0, 0]
row2 = [0, 0, 0, 0, 0]
row3 = [0, 0, 0, 0, 0] # building the board
row4 = [0, 0, 0, 0, 0]
row5 = [0, 0, 0, 0, 0]
element = [ "x", "y"] # board elements. x is movable space, y is wall
start = ["s"] # start space
end = ["e"] # end space
bot_face = ["north", "east", "south", "west"]
i = 0
a = 0
y = random.choice(bot_face)
while i != 5:
row1[a] = random.choice(element)
row2[a] = random.choice(element)
row3[a] = random.choice(element) # randomize board
row4[a] = random.choice(element)
row5[a] = random.choice(element)
a = a + 1
i = i + 1
r = random.randint(0, 5)
row1[r] = random.choice(start) # apply start and end
row5[r] = random.choice(end)
print(row1) # print out the board
print(row2)
print(row3)
print(row4)
print(row5)
print("the bot faces:", y)
although this isn't very functional, you could at least build a board with this. I didn't build a board because it would be very time consuming for me, but you could technically look at the graph created to determine the layout of the board to some extent.