Sections 14 and 15
Notes and Hacks
Notes
- Libraries simplify difficult programs for the programmer
- APIs specify how procedures in libraries should behave and be utilized.
- Documentation for APIs/Libraries are necessary to gain proper understanding into how to use them.
- Documentation: Text that explains the what, how, or why of your code.</li>
- Libraries: A collection of prewritten code or procedures that coders can use to maximize their efficiency</li>
- Software libraries contain procedures used in creating novel programs.
- Existing code segments can be derived from internal or external sources: Libraries, Previously written code.
- API:A type of software through several computers are able to communicate information amongst eachother</li>
- make maximize efficiency in novel code, it is best to use previously made procedures
- RANDOM(a, b): which generates and returns a random integer from a to b, inclusive. Each result is equally likely to occur
</ul> </div> </div> </div>(1) Reflection
From today's lesson on Sections 14 and 15, I learned about libraries and the different functions I can use to create randomization. I learned that libraries are useful in terms of simplfying a program and being more efficient when doing so. Using documentation is helpful when writing code because you can easily refer to it and understand it easily for yourself or even when helping others. There are multiple random. functions which help generate values in different. The random. functions are random.item, shuffle, and randint, which all have a different function and are used in different cases. In terms of how the lesson, I think I grasped the topics easily and understood the content thoroughly due to how the lesson was organized and taught. It was taught in a organized way and
(2) Multiple Choice
- 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. </p>
C. A random word from variable a to variable b exclusive.
D. A random word from variable a to variable b inclusive.
- 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 </p>
C. x = stop, y = start, z = step
D. x = step, y = start, z = stop
- Which of the following is NOT part of the random library?
A. random.item
B. random.random Explanation: This is not part of the random library because you cannot random a random.</p>
C. random.shuffle
D. random.randint
</div> </div> </div>(3) Short Answer Questions
- What is the advantage of using libraries?
The advantage of using libraries is that libraries help simplify complex programs and they also collect prewritten code and procedures. Libraries also help maximize their efficiency too.
- Write a thorough documentation of the following code.
import random #random library imported in order to use it's functions names_string = input("Give me everybody's names, seperated by a comma.") #inputs names to make a list of names names = names_string.split(",") #splits everyone's names with commas num_items = len(names) #creates a list of names random_choice = random.randint(0, num_items - 1) #pick a random choice from a list of strings person_who_will_pay = names[random_choice] #chooses random name print(f"{person_who_will_pay} is going to buy the meal today!") #output received
(4) Coding Challenges!
REQUIRED: Create programs in python to complete the two task</p>
- Create a program to pick five random names from a list of at least 15 names
import random names = ["Bob", "Bill", "Billy", "Bobby", "Sally", "Sean", "John", "Johnny", "Jonny", "Jonathan", "Joe", "Joel", "Jon", "June", "Jill"] for i in range(5): print(random.choice(names)) print("These are your 5 random names!")
- Create a program to simulate a dice game where each player rolls two fair dice (6 sides); the player with the greater sum wins
def dice(n): dicerolls = [] for i in range(n): dice = random.randint(1, 6) + random.randint(1, 6) dicerolls.append(dice) return dicerolls
</div>EXTRA-CREDIT OPPORTUNITY:
- Create a program to randomly generate one of those 5x5 square CollegeBoard robot courses frequently seen in these lessons. Here are the criteria:
-
No need to create a graphical user interface (output the course in graphical form); that would be too demanding. You can use store your randomizations in variables to be printed, after which you can sketch one instance of your output using any means (handdrawn, google drawings, etc.) to attach to your submission as an image.
-
Your illustration can differ graphically from CollegeBoard's illustrations as long as the robot, its direction, the wall squares, and the goal square are fairly self-evident (ie you can make the goal a present or food instead of a gray square)
You must randomize the following:
- Initial direction of the robot (up, down, left, right)
- Initial Position of the robot
- Goal position
- Positions of at least 12 wall/obstacle squares
The latter three must be mutually exclusive (on different squares). This prevents you from generating a course where the robot is on the goal, on a wall square, or the goal is in a wall square
EXTRA X 2: Put safeguards in your program to make sure the robot course is actually possible to complete. This one is tough, so if you manage to do it you're pretty much guaranteed to get 1.0 points. This will not likely be the only way to get a perfect score, however.
- Libraries: A collection of prewritten code or procedures that coders can use to maximize their efficiency</li>