Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
4 views

Python Paper

Uploaded by

Rehan Mahmood
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Python Paper

Uploaded by

Rehan Mahmood
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

PYTHON PROGRAMMING PAPER Instructions: The paper is not about

how well are you at programming, it is to check whether the approach


that you are using is correct or not. Beginner Level Problems NOTE:
Don’t leave any question. Attempt in any language that you want.
Preferred language is python but if the logic is good, you will get
marks for formal languages such as using English or Urdu.
Working with Dictionaries

You're tracking inventory for a small store. You have a dictionary, stock, where keys are
product names and values are quantities:

stock = {"Apples": 20, "Oranges": 12, "Bananas": 0, "Milk": 5}

Write functions to:

• add_stock(product, quantity): Adds a new product or increases the quantity of an


existing item.
• check_availability(product): Returns True if a product is in stock (quantity > 0),
otherwise False.
• fulfill_order(product, quantity): Decreases stock if enough quantity is available.
Returns True if the order was fulfilled, False otherwise.

Question : Boolean Logic and Customer Preferences

Suppose a pizza ordering system uses these boolean variables:

• pepperoni: True if the customer wants pepperoni


• extra_cheese: True if the customer wants extra cheese
• mushrooms: True if the customer wants mushrooms

Write Python functions to represent the following scenarios:

• wants_vegetarian(): Returns True if the customer wants a vegetarian pizza (no


pepperoni).
• simple_order(): Returns True if the customer wants only one topping.
Question : If-Else Logic and Number Properties

Write a Python function is_interesting(number) that determines if an integer has the


following properties:

• It's a multiple of 3 or a multiple of 5.


• It does not contain the digit 7.
• Task: Come up with two test cases: One where the function should return True
and one where it should return False.

Question 5: While Loops and User Input

Write a Python function guess_the_number() with the following logic:

• The computer secretly selects a random number between 1 and 100.


• The user continuously inputs guesses.
• Using a while loop, the function provides feedback after each guess ("Too high,"
"Too low") until the user gets it right.
• Once the number is guessed, the function indicates success.

Boolean Logic and Scenarios

Question 1: An event registration system uses the variables is_member, paid_early, and
has_guest. Write functions for these situations:

• is_eligible_for_discount(): Member gets a discount, OR they paid early, OR they


have a guest.
• requires_additional_ticket(): The person is NOT a member AND they have a guest.

Question 2: Three booleans track order status: is_shipped, is_delivered,


is_refunded. Write functions for:

• can_review(): Order is delivered but NOT refunded.


• needs_customer_action(): The order is not shipped OR requires a refund.

Question 3: Function: calc_shipping(weight, distance, is_priority).


Shipping rules:

• Base cost: $5
• IF weight > 10 lbs, add $5 per pound over 10
• IF distance > 500 miles, add $7
• IF is_priority is True, double the total cost.

Task: Provide test cases demonstrating how each rule triggers.

Question 4: Function: grade_assignment(score).

• IF score >= 90, return "A"


• ELIF score >= 80, return "B"
• ELIF score >= 70, return "C"
• ELIF score >= 60, return "D"
• ELSE return "F"
• Task: Find test cases on the edge of each grade boundary, plus one invalid score
that should cause an error.

Question 5: compute_paycheck(hours, wage, has_overtime).

• Regular pay: Hours up to 40 multiplied by wage


• IF has_overtime is True, anything over 40 hours is 1.5 times the wage
• Task: Test regular hours, overtime hours, and a case with no overtime.

Question 6: count_occurrences(data, target): Find how many times target appears


in the list data.

• Task: Test with target at the start, in the middle, multiple times, and NOT in the
list.

Question 7: filter_positives(numbers):

• Return a new list with only the positive values from numbers.
• Task: Test cases with all positives, all negatives, a mix, and an empty list.

Question 8: find_duplicates(items):

• Return a list with only the elements that appear more than once in items.
• Task: Tests with multiple repeated items, items repeating only twice, and no
duplicates.

Question 9: is_special(num):
• Return True if a number is EITHER divisible by 4 OR ends in the digit 2; but NOT
both.

Question 10: has_even_divisor(num):

• Return True if a number has ANY divisor (besides 1) that is even.

You might also like