SET A Flashcards
Enumerate the three levels of data modeling and briefly explain each.
- Conceptual Model - High-level overview of entities and relationships
- Logical Model - Detailed model design including attributes and keys
- Physical Model - implementation leveling, includes data types and indexes
List four advantages of having a well-structured ER diagram in database design.
-It organizes data effectively
-Ensures database efficiency
-Prevents redundancy
-Enhances communication with shareholders
Enumerate the key steps involved in converting a conceptual data model to a logical
data mode
- Identify entity and relationships
- Define attributes for each entity
- Choose primary keys for entities
-Add foreign keys and map relationships
-Normalize the data
-Define constraints
-Review and refine model
Consider a Python function that processes a sequence of grades to compute the
average
-Error Handling for Empty Strings: If grades_str is an empty string, it would
result in division by zero causing a runtime error. Add a condition to check if there
are no grades before dividing.
-Handling Non-integer inputs (Can be combined with the third one): If
grades_str contains non-integer values, the int() conversion will raise a
ValueError. Use a try-except block to catch invalid inputs.
-Better Input Validation (Can be combined with the second one): The function
doesn’t handle invalid characters, negative grades, or grades that exceed a
reasonable limit (like 100). Add a logic to validate the grades.
Explain the difference between logical and physical data models and provide a
scenario where a logical data model may differ significantly from the physical model.
-A Logical Model primarily defines the attributes and keys of entities while the
Physical Model defines its data types and indexes.
- A logical model might show a customer’s full name as a single attribute,
but the physical model could split it into separate first_name and
last_name columns for easier sorting and searching. This change makes
data manipulation more efficient at the database level. - In a logical model, a product’s price might be represented as a simple
number, but in the physical model, it could be stored as an integer in cents
to avoid floating-point precision issues. For example, $10.99 would be
stored as 1099. - A logical model could represent a user’s address as one entity, while the
physical model might normalize it into separate tables for addresses,
cities, states, and countries to reduce data redundancy. This allows for
more efficient storage and easier updates of shared information.
Reason how Python’s flexibility with data types can impact the design of algorithms
and functions. Provide an example where this flexibility can lead to errors or
inefficiencies.
-Python’s dynamic typing allows variables to hold different types of data, which
can make code more versatile but also requires careful consideration in algorithm
design.
- Possible Examples:
- A function designed to calculate the average of numbers might
accidentally include a string in the input list, causing a TypeError when
attempting to perform arithmetic operations. For instance, calculating the
average of [1, 2, ‘3’, 4] would raise an error because ‘3’ is a string, not a
number. - A sorting algorithm might behave unexpectedly when given a list
containing both numbers and strings, as Python’s default sorting
compares strings lexicographically. This could lead to counterintuitive
results, such as [1, 2, 10, 20] being sorted as [1, 10, 2, 20] if the numbers
were initially stored as strings. - A function that concatenates user input might work fine with strings but fail
if given a number, requiring explicit type conversion. For example, “Hello”
+ 5 would raise a TypeError, while “Hello” + str(5) would work as expected.
Write a Python function that accepts a number and prints the square of all numbers
from 1 to that number.
Input: 4
Output:
1
4
9
16
def print_squares(n):
for i in range(1, n+1):
print (i*i)
Write a Python function that accepts a number and prints the square of all numbers
from 1 to that number.
Input: 4
Output:
1
4
9
16
def is_palindrome(ans):
num = str(ans)
return num == num[:: -1]