← Computer Programming I

Problem 1: Shopping Cart Calculator

Problem Statement: Create a shopping cart system that calculates prices with various discounts. Your program should:

  • Define a function calculate_item_total(quantity, unit_price) that returns the total price for an item
  • Define a function apply_bulk_discount(total, quantity) that:
    • Returns 10% discount if quantity >= 10
    • Returns 5% discount if quantity >= 5
    • Returns 0 otherwise
  • Define a function calculate_tax(subtotal, tax_rate) that calculates tax amount
  • Define a function is_eligible_for_free_shipping(subtotal) that returns True if subtotal >= 50
  • Define a function process_order(item_name, quantity, unit_price, tax_rate) that:
    • Calculates item total
    • Applies bulk discount
    • Calculates tax
    • Checks shipping eligibility
    • Prints a formatted receipt
  • Test with these orders:
    • “Notebooks”: 12 units at $3.50 each, 8% tax
    • “Pens”: 6 units at $1.25 each, 8% tax
    • “Paper”: 3 reams at $4.99 each, 8% tax

Expected Output:

SHOPPING CART CALCULATOR
========================================
Order Receipt for: Notebooks
  Quantity: 12 @ $3.50 each
  Item Total: $42.00
  Bulk Discount: -$4.20
  Subtotal: $37.80
  Tax (8%): $3.02
  Final Total: $40.82
  Need $12.20 more for free shipping
----------------------------------------
Order Receipt for: Pens
  Quantity: 6 @ $1.25 each
  Item Total: $7.50
  Bulk Discount: -$0.38
  Subtotal: $7.12
  Tax (8%): $0.57
  Final Total: $7.69
  Need $42.88 more for free shipping
----------------------------------------
Order Receipt for: Paper
  Quantity: 3 @ $4.99 each
  Item Total: $14.97
  Subtotal: $14.97
  Tax (8%): $1.20
  Final Total: $16.17
  Need $35.03 more for free shipping
----------------------------------------

Problem 2: Student Grade Calculator

Problem Statement: Create a comprehensive grade calculation system. Your program should:

  • Define a function calculate_average(score1, score2, score3) that returns the average of three scores
  • Define a function drop_lowest(score1, score2, score3) that returns the average of the two highest scores
  • Define a function calculate_weighted(assignments, midterm, final) that calculates weighted average:
    • Assignments: 30%
    • Midterm: 30%
    • Final: 40%
  • Define a function determine_grade(average) that returns letter grade:
    • A: 90-100, B: 80-89, C: 70-79, D: 60-69, F: below 60
  • Define a function needs_improvement(current_avg, target_grade) that:
    • Returns True if current average is below the minimum for target grade
    • target_grade is a letter (‘A’, ‘B’, ‘C’, ‘D’)
  • Test with this student data:
    • Assignment scores: 85, 78, 92
    • Midterm: 88
    • Final: 82
    • Calculate both regular average and average with lowest dropped
    • Calculate weighted average
    • Check if student needs improvement to get an ‘A’

Expected Output:

STUDENT GRADE CALCULATOR
========================================
Assignment Scores: 85, 78, 92
Midterm Score: 88
Final Score: 82
----------------------------------------
Regular Assignment Average: 85.00
Average with Lowest Dropped: 88.50
Using Better Average: 88.50

Weighted Course Average: 85.95
Letter Grade: B

Needs improvement for an 'A': Yes
Points needed: 4.05
Already meets or exceeds 'B' grade requirement

Problem 3: Time Converter and Scheduler

Problem Statement: Create a time management system that converts between different time formats. Your program should:

  • Define a function hours_to_minutes(hours) that converts hours to minutes
  • Define a function minutes_to_seconds(minutes) that converts minutes to seconds
  • Define a function total_seconds(hours, minutes, seconds) that calculates total seconds
  • Define a function format_time(total_minutes) that:
    • Takes total minutes as input
    • Returns a formatted string “X hours and Y minutes”
    • Example: 135 minutes returns “2 hours and 15 minutes”
  • Define a function can_fit_task(available_hours, task_hours, task_minutes) that:
    • Returns True if a task can fit within available time
    • Convert everything to minutes for comparison
  • Define a function schedule_summary(task_name, hours, minutes) that prints:
    • Task name and duration
    • Total time in minutes
    • Total time in seconds
    • No return value (action function)
  • Test your system:
    • Convert 2.5 hours to minutes
    • Calculate total seconds for 1 hour, 45 minutes, 30 seconds
    • Format 200 minutes into hours and minutes
    • Check if a 3 hour 20 minute task fits in 3.5 hours available
    • Create schedule summaries for:
      • “Study”: 2 hours 30 minutes
      • “Exercise”: 0 hours 45 minutes

Expected Output:

TIME CONVERTER AND SCHEDULER
========================================
Converting 2.5 hours to minutes: 150.0 minutes

Total seconds for 1 hour, 45 minutes, 30 seconds: 6330 seconds

Formatting 200 minutes: 3 hours and 20 minutes

Can a 3 hour 20 minute task fit in 3.5 hours?
  Yes, the task fits!

SCHEDULE SUMMARIES:
------------------------------
Task: Study
  Duration: 2 hours, 30 minutes
  Total Minutes: 150.0
  Total Seconds: 9000.0

Task: Exercise
  Duration: 0 hours, 45 minutes
  Total Minutes: 45
  Total Seconds: 2700

Problem 4: Collatz Sequence Analyzer
  • Problem Statement: Create a program that analyzes the Collatz sequence (also known as 3n+1 sequence). Your program should:
    • Define a function next_collatz(n) that returns the next number in the sequence:
      • If n is even, return n/2
      • If n is odd, return 3n + 1
    • Define a function collatz_length(n) that counts how many steps to reach 1:
      • Start with n and keep applying the Collatz rule
      • Count steps until reaching 1
      • The sequence always reaches 1 (unproven but true for all tested numbers)
    • Define a function max_in_collatz(n) that finds the maximum value reached in the sequence starting from n
    • Define a function print_collatz_sequence(n, max_steps) that prints the sequence:
      • Print each number in the sequence
      • Stop after max_steps or when reaching 1, whichever comes first
    • Test your functions:
      • Find sequence length for starting values: 6, 7, 27
      • Find maximum value reached starting from 27
      • Print first 10 steps of sequence starting from 19
      • Compare sequence lengths for 15, 16, and 17

Expected Output:

Collatz Sequence Analyzer
----------------------------------------
Steps to reach 1 from 6: 8
Steps to reach 1 from 7: 16
Steps to reach 1 from 27: 111

Maximum value in sequence from 27: 9232

Collatz sequence starting from 19:
  Step 0: 19
  Step 1: 58
  Step 2: 29
  Step 3: 88
  Step 4: 44
  Step 5: 22
  Step 6: 11
  Step 7: 34
  Step 8: 17
  Step 9: 52
  Step 10: 26

Sequence length comparison:
  Starting from 15: 17 steps
  Starting from 16: 4 steps
  Starting from 17: 12 steps

Problem 5: Number System Converter
  • Problem Statement: Create a comprehensive number system converter that works with decimal, binary, and hexadecimal numbers. Your program should:
    • Define a function decimal_to_binary(n) that converts a decimal number to binary string
      • Use repeated division by 2
      • Build the binary string from remainders (in reverse order)
      • Return the binary representation as a string
    • Define a function binary_to_decimal(binary_str) that converts a binary string to decimal
      • Multiply each digit by appropriate power of 2
      • Return the decimal value
    • Define a function decimal_to_hex_digit(n) that converts a single decimal (0-15) to hex character
      • 0-9 remain as digits
      • 10-15 become A-F
    • Define a function is_valid_binary(binary_str) that checks if a string is valid binary
      • Return True only if all characters are ‘0’ or ‘1’
    • Define a function count_ones_in_binary(binary_str) that counts the number of 1s
    • Test your functions:
      • Convert decimal 25 to binary
      • Convert binary “11011” to decimal
      • Check if “1010”, “1012”, “1111” are valid binary strings
      • Count ones in binary “110101”
      • Convert decimal values 9, 10, 15 to hex digits

Expected Output:

Number System Converter
----------------------------------------
Decimal 25 to binary: 11001
Binary 11011 to decimal: 27

Is '1010' valid binary? True
Is '1012' valid binary? False
Is '1111' valid binary? True

Number of 1s in '110101': 4

Decimal 9 to hex: 9
Decimal 10 to hex: A
Decimal 15 to hex: F