Docsity

Data Structures and Algorithms Midterm 1, Exams of Data Structures and Algorithms

Toggle sidebar
bg1
Data Structures and
Algorithms: Midterm 1
.__init__() - ANSdeclare which attributes each instance of the class should
have
.ipynb - ANSThe .ipynb file extension stands for "Interactive
PythonNotebook". It is a file format used by Jupyter Notebook to store
andshare documents that contain live code, equations, visualizations,
andnarrative text.10
.py files - ANSThe Files with the .py extension contain the Python sourcecode.
** Exponent - ANSRaises a number to a power
// Integer division: - ANSDivides one number by another and gives the result
as int
% Remainder - ANSDivides one number by another and gives the remainder
A value-returning function: - ANS• Executes the statements it contains, and
then it returns a value back to the statement that called it.
• The input, int, and float, str functions are examples of value-returning
functions
A void function: - ANS• Simply executes the statements it contains and then
terminates
pf3
pf4
pf5
pf8
pf9
pfa
pfd

Partial preview of the text

Download Data Structures and Algorithms Midterm 1 and more Exams Data Structures and Algorithms in PDF only on Docsity!

Data Structures and

Algorithms: Midterm 1

.init() - ANSdeclare which attributes each instance of the class should have .ipynb - ANSThe .ipynb file extension stands for "Interactive PythonNotebook". It is a file format used by Jupyter Notebook to store andshare documents that contain live code, equations, visualizations, andnarrative text. .py files - ANSThe Files with the .py extension contain the Python sourcecode. ** Exponent - ANSRaises a number to a power // Integer division: - ANSDivides one number by another and gives the result as int % Remainder - ANSDivides one number by another and gives the remainder A value-returning function: - ANS• Executes the statements it contains, and then it returns a value back to the statement that called it.

  • The input, int, and float, str functions are examples of value-returning functions A void function: - ANS• Simply executes the statements it contains and then terminates

An anonymous (lambda) function definition: - ANSlambda parameter list: statements lambda x, y: x + y Argument: - ANSa piece of data that is sent into a function

  • When the function is called
  • Function can use argument in calculations
  • When calling the function, the argument is placed in parentheses following the function name Assignment Operation - ANSWe use = to assign a value to a variable.
  • Variable name on the left of =
  • Value on the right of =
  • This single equation is called Assignment Operation Block: - ANSa set of statements that belong together as a group BOGO Sort - ANSAlgorithm: Shuffle the list, and (hope) it is sorted; otherwise, shuffle again. Time Complexity: O(n!) Boolean Type - ANSThe result of a relational operation will be a boolean type: True or False Branching - ANSWe can perform a specific action(s) only if a condition is satisfied. Also known as Decision Structure or Selection Structure.

continue - ANSSkip the rest of the statements and jump to the next iteration. Design an algorithm to place, in an empty chessboard (8x8), 8 Queens, so that no two queens threaten each other. Include a flowchart and pseudo- code for this situation - ANSThe algorithm starts by placing a queen on the first column, then it proceeds to the next column and places a queen in the first safe row of that column. If the algorithm reaches the 8th column and all queens are placed in a safe position, it prints the board and returns true.If the algorithm is unable to place a queen in a safe position in a certain column, it backtracks to the previous column and tries a different row. The "isSafe" function checks if it is safe to place a queen on a certain row and column by checking if there are any queens in the same row, diagonal or anti-diagonal. It's worth to notice that this is just a high-level pseudocode and it might need to be adapted depending on the specific implementation and language you are using. dynamic-type programming language - ANSVariable assigned to one type can be reassigned to another type ie int -> float Every instruction (line of code) will take one second, how much time will take to execute the myFunction(10) for the following code? def myFunction(n) i= while i Exception ValueError - ANSRaised when an operation or function receives an argument that has the right type but an inappropriate value Explain Why Big O Notation is relevant for a big dataset, and it is not so relevant if you have a small set of data (for example, less than 1000) - ANSScaling with Input Size: Big O Notation primarily focuses on how an algorithm's performance scales with the size of the input data. In other words, it tells you how the algorithm's runtime increases as the dataset becomes larger. For small datasets, the differences in runtime between algorithms with different time complexities might not be noticeable or significant. When you have a small dataset, the actual runtime of even a less efficient algorithm might be fast enough for practical purposes. Therefore, the need for detailed analysis of time complexity is less critical when the dataset is small. when dealing with big datasets (often referred to as "big data"), the differences in algorithm efficiency become much more pronounced. Even small improvements in algorithmic efficiency (e.g., moving from O(n^2) to O(n log n)) can lead to substantial reductions in runtime, which is crucial for processing and analyzing massive amounts of data efficiently. Find the time complexity for a given code - ANSBig O notation, often denoted as O(), is a way to describe the upper bound or worst-case time complexity of an algorithm in computer science. It provides a theoretical analysis of how the runtime of an algorithm grows in relation to the size of its input. float - ANSfloat: a numeric value has decimal points flow chart process - ANScomputations performed , square boy

Identifiers of Variables - ANSVariables have identifiers - their names.

  • Each identifier may consist of letters (A-Z, a-z), digits (0-9), andunderscores (_).
  • Identifiers must begin with a letter or the underscore.
  • Identifiers may not begin with a digit.
  • Identifiers cannot have special characters ($, %, #, &, *, ^, etc)
  • Identifiers are case sensitive.
  • Identifiers may not be reserved keywords. Such as import, if, else,and, or, return if/else branching - ANSdecision branches to plan A and plan B and then they join on the end Insertion Sort - ANSAlgorithm: [x 1] itself is a sorted list, [x 2, ... x n ] still unsorted. Compare x 1 with x 2, if x 1 > x 2, then insert x 2 before x 1 as [x 2, x 1] [x 3... x n ],otherwise, insert x 2 after x 1 as [x 1, x 2][x 3... x n ]. Insert x 3 to the sorted list to make it sorted. Keep doing this until the last element, and we have a list sorted. Time Complexity: O(n^2) int - ANSint: a numeric value without decimal points main function: - ANScalled when the program starts
  • Calls other functions when they are needed
  • Defines the mainline logic of the program

Merge Sort - ANSMerge sort has two parts: divide and merge. Divide: A list will be divided into sublists until they are sorted by themselves (one element per list). Merge: Then sublists will be merged to be a sorted list until there is only one list. Time Complexity: O(n*log 2n) Modules: - ANSfiles that store functions of the standard library

  • Help organize library functions not built into the interpreter
  • Copied to a computer when you install PythonTo call a function stored in a module, you need to write an import statement
  • Written at the top of the program
  • Format: import module_name Object-Oriented Programming (OOP) - ANSIs a method of structuring a program Related properties and behaviors into individual objects Parameter variable: - ANSvariable that is assigned the value of an argument when the function is defined
  • The parameter and the argument reference the same value
  • General format: def function_name(parameter):
  • Scope of a parameter: the function in which the parameter is used

Keep doing this until the last element; we have a list [x m1, x m2, x m3,... x mn ], and it is sorted. Time Complexity: O(n^2) Standard library: - ANSlibrary of pre-written functions that comes withPython store end time - ANSimport time end = time.time() store starting time - ANSimport time begin = time.time() Strength of Python (Why we use Python) - ANSSmaller code length, More friendly to beginners, Extensive libraries (packages), Best for Data Science! string - ANSIt is a sequence of characters The benefits of using functions include: - ANS• Simpler code

  • Code reuse
  • Write the code once and call it multiple times
  • Better testing and debugging
  • Can test and debug each function individually
  • Faster development
  • Easier facilitation of teamwork
  • Different team members can write different functions The complexity of a Selection Search is O(n), True or False? - ANSsorting algorithm that repeatedly (n times O(n)) selects the minimum (or maximum) element from an unsorted portion of the array and moves it (O(n)) to the sorted portion. The time complexity of selection sort is O(n²) in the worst- case, average-case, and best-case scenarios. Time Complexity - ANShow much time your algorithm needs grows with the input grows efficiency or scalability total time taken - ANSimport time print(f"Total runtime is {end - begin}") What are Attributes? - ANSAttributes are defined in the class template and represent the state of an object What are classes? - ANSClasses are user-defined datatypes that act as the blueprint for individual objects, attributes, and methods. allow you to create user-defined data structures. Contain attributes and methods What are computers? - ANSElectronic devices that compute data according to instructions.• Components:• CPU(Central Processing Unit),• Memory,• Storage,• Input/output devices.
  • Use it in other functions.
  • Use it in list comprehension.