Programming Exercises - 004: Data Structures and Collections in Python

Here is a set of questions that test the Python collections framework.

Exercise Questions

  1. File Name: molecular_weight_calculator.py
    Question: Write a program that calculates the molecular weight of a compound given its molecular formula. Use a dictionary to store the atomic weights of elements (e.g., H = 1.008, O = 15.999, etc.). The program should take a molecular formula as input and calculate the total molecular weight. Test your program with water (H2O) and carbon dioxide (CO2).

  2. File Name: quadratic_solver.py
    Question: Create a program that solves quadratic equations of the form ax2 + bx + c = 0. The program should use a tuple to store the coefficients (a, b, c) and return the roots using the quadratic formula. Ensure that your program handles cases where the roots are real, complex, or when there is only one root (i.e., when the discriminant is zero).

  3. File Name: electromagnetic_spectrum_classifier.py
    Question: Develop a program that classifies a given wavelength (in nanometers) into its corresponding category in the electromagnetic spectrum (e.g., Radio waves, Microwaves, Infrared, Visible light, Ultraviolet, X-rays, Gamma rays). Use a dictionary to map wavelength ranges to their respective categories. Test your program with several wavelengths.

  4. File Name: pH_calculator.py
    Question: Write a program to calculate the pH of a solution given its hydrogen ion concentration. The program should store a list of common substances and their corresponding pH values (e.g., Lemon juice = 2.0, Water = 7.0, Bleach = 12.6). It should then classify the solution as acidic, neutral, or basic based on the calculated pH.

  5. File Name: physics_vectors.py
    Question: Create a program that performs vector addition in 3D space. Each vector should be represented as a list of three elements [x, y, z]. The program should take two vectors as input and output their sum. Additionally, implement a function to calculate the magnitude of the resulting vector.

  6. File Name: periodic_table_lookup.py
    Question: Develop a program that allows users to look up information about elements in the periodic table. The program should store element data (e.g., atomic number, symbol, name, atomic mass) in a dictionary where the element symbol is the key. Allow the user to input an element symbol and display the corresponding information.

  7. File Name: chemical_reaction_balancer.py
    Question: Write a program that helps balance simple chemical equations. The program should take a chemical equation as input and use sets to ensure that the number of atoms of each element is the same on both sides of the equation. For example, H2 + O2 → H2O should balance to 2H2 + O2 → 2H2O.

  8. File Name: distance_formula.py
    Question: Implement a program that calculates the distance between two points in a 2D space using the distance formula. The program should take the coordinates of the two points as tuples (x1, y1) and (x2, y2) and return the distance. Additionally, the program should store a history of all distances calculated in a list and allow the user to review past calculations.

  9. File Name: thermodynamics_calculator.py
    Question: Create a program that calculates the change in internal energy of a gas using the first law of thermodynamics (ΔU = Q - W). The program should use a dictionary to store values for heat added (Q) and work done (W) for different processes. It should then calculate and display the change in internal energy.

  10. File Name: chemical_equilibrium_calculator.py
    Question: Write a program to calculate the equilibrium concentrations of reactants and products in a chemical reaction using the equilibrium constant (K). The program should use lists to store initial concentrations and a dictionary to map the stoichiometric coefficients. The user should input initial concentrations and the equilibrium constant, and the program should output the equilibrium concentrations.