Search for question
Question

2/14/24, 4:25 AM { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Review practice problems\n", "\n", "Problems curated from [Python Programming And Numerical Methods: A Guide For Engineers And Scientists] (https://pythonnumericalmethods.berkeley.edu/notebooks/Index.html)\n", "\n", "**Each question 3 pts**\n", ] }, { "\n", "## Skills review Week 1\n", "\n", "1. Use the print function to generate the following strings.\n", 11 The world 'Engineering' has 11 letters.\n", The word 'Book' has 4 letters.\n", "2. "3. 11 11 Assign the value 2 to the variable x and the value 3 to the variable y. \n", Let x = 10 and y = u = $x + y$\n", V = $xy$\n", W = $x/y$\n", Z = $sin(x)$\n", r S $5sin(xy)$\n", р $x^y$\n", "4. Compute the area of a triangle with base 10 and height 12. Recall that the area of a triangle is half the base times the height.\n", "5. Compute the surface area and volume of a cylinder with radius 5 and height 3.\n", - = mediatb.blob.core.windows.net/media/65bae9d6bc66b89155934145/questions/06.6practice_1707856090803.ipynb = $8sin(x) $\n", 3. Write a line of code that will make each of the following assignments.\n", Recall that the distance between points in two dimensions is $\\sqrt{(x_2-x_1)^2 + (y_2_y_1)^2}$\n", "8. The hyperbolic sin or sinh is defined in terms of exponentials as $sinh(x) = \\frac{e^x-e^{-x}}{2}$\n", module.\n", Compute sinh for x=2 using exponentials. Verify that the result is indeed the hyperbolic sin using Python's function sinh in the math Verify that $sin^2(x)+cos^2(x)=1$ for x=ï, µ2, î4, µ6\n", Compute the $sin(87°)$" "6. Compute the slope between the points (3,4) and (5,9)\n", Recall that the slope between points (x1,y¹) and (x2,y2) is $\\frac{y_2 - y_1}{x_2 - x_1}$\n", "7. Compute the distance between the points (3,4) and (5,9)\n", "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "# Week 1 answers\n", https://mediatb.blob.core.windows.net/media/65bae9d6bc66b89155934145/questions/06.6practice_1707856090803.ipynb 1/7 2/14/24, 4:25 AM "\n", "# 1\n", "\n", "\n", "# 2\n", ] }, { "\n", "\n", "# 3\n", "\n", "\n", "# 4\n", "\n", "\n", "# 5\n", "\n", "\n", "# 6\n", "\n", "\n", "# 7\n", "\n", "\n", "# 8\n", "\n", "\n" "cell_type": "markdown", "metadata": {}, "source": [ mediatb.blob.core.windows.net/media/65bae9d6bc66b89155934145/questions/06.6practice_1707856090803.ipynb "## Skills review Week 2\n", "\n", "1. Get the last word 'great' from 'Python is great!' (hint: treat the string as a list of characters and use indexing/slicing to get the characters)\n", "2. Assign list [1, 8, 9, 15] to a variable list_a. Append 4 to the list_a using the append method.\n", "3. Sort the list_a in ascending order.\n", "4. Turn 'Python is great!' to a list.\n", "5. Create one tuple with elements 'One', and 1; Assign it to tuple_a\n", "6. Get the 2nd element in the tuple_a.\n", "7. Get the unique element from (2, 3, 2, 3, 1, 2, 5).\n", "8. Create array x and y, where x = [1, 4, 3, 2, 9, 4] and y=[2, 3, 4, 1, 2, 3].\n", 11 - u = x + y\n", - V = xy\n", - W = x/y\n", Z = sin(x)\n", r = 8*sin(x) \n", https://mediatb.blob.core.windows.net/media/65bae9d6bc66b89155934145/questions/06.6practice_1707856090803.ipynb 2/7 2/14/24, 4:25 AM ] }, { S = 5*sin(x*y)\n", Р x**y\n", q = x @ y\n", "9. Generate an array with size 100 evenly spaced between -10 to 10 using linspace function in Numpy.\n", "10. Create a zero array with size (2, 4).\n", "11. Change the 2nd column in the above array to 1.\n", "12. Create this array and calculate the determinate of the array.\n", $$y = \\left[\n", \\begin{matrix}\n", "3 & 2 & 3\\\\\n", "5 & 2 & 8\\\\\n", "3 & 5 & 9\n", "\\end{matrix} \n", "\\right]\n", "$$\n", "13. Write a matrix multiplication using two vectors of numbers 0 to 12, to generate a 13x13 matrix representing a multiplication table\n", "14. Plot a line chart of x equals 0 to 20 with 100 values and y = 3x + 14" "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "# Week 2 answers\n", "\n", "# 1\n", "\n", "\n", "# 2\n", "\n", "\n", "# 3\n", "\n", "\n", "# 4\n", "\n", "\n", "# 5\n", "\n", "\n", "# 6\n", mediatb.blob.core.windows.net/media/65bae9d6bc66b89155934145/questions/06.6practice_1707856090803.ipynb "\n", "\n", "# 7\n", "\n", https://mediatb.blob.core.windows.net/media/65bae9d6bc66b89155934145/questions/06.6practice_1707856090803.ipynb 3/7 2/14/24, 4:25 AM "\n", "# 8\n", "\n", "\n", }, { "# 9\n", "\n", "\n", "# 10\n", "\n", "\n", "# 11\n", ] "\n", "\n", "# 12\n", "\n", "\n", "# 13\n", "\n", "\n", "# 14\n" "cell_type": "markdown", "metadata": {}, "source": [ mediatb.blob.core.windows.net/media/65bae9d6bc66b89155934145/questions/06.6practice_1707856090803.ipynb "## Skills review Week 3\n", "\n", "1. Let array_a be an array [-1, 0, 1, 2, 0, 3]. Write a command that will return an array consisting of all the elements of array_a that are larger than zero. Hint: Use logical expression as the index of the array.\n", "2. A year is considered to be 365 days long. However, a more exact figure is 365.24 days. As a consequence, if we held to the standard 365- day year, we would gradually lose that fraction of the day over time, and seasons and other astronomical events would not occur as expected. A leap year is a year that has an extra day, February 29, to keep the timescale on track. Leap years occur on years that are exactly divisible by 4, unless it is exactly divisible by 100, unless it is divisible by 400. For example, the year 2004 is a leap year, the year 1900 is not a leap year, and the year 2000 is a leap year.\n", Compute the number of leap years between the years 1583 and 2024 with initial 4 year rule.\n", Compute the number of leap years between the years 1583 and 2024 with all the rules\n", "3. Assign the string 'HELLO' to the variable s1 and the string 'hello' to the variable s2. Use the == operator to show that they are not equal. Use the == operator to show that s1 and s2 are equal if the lower method is used on s1. Use the == operator to show that s1 and s2 are equal if upper method is used on s2.\n", "4. Check if 'Python' is in 'Python is great!'.\n", "5. Write a code, where bill is the total cost of a meal and party is the number of people in the group. The tip should be calculated as 15% for a party strictly less than six people, 18% for a party strictly less than eight, 20% for a party less than 11, and 25% for a party 11 or more. Test code with each of the below (leave only the last test in the code submitted):\n", bill $109.29, party 3\n", bill $109.29, party 7\n", bill $109.29, party 9\n", 11 https://mediatb.blob.core.windows.net/media/65bae9d6bc66b89155934145/questions/06.6practice_1707856090803.ipynb 4/7 2/14/24, 4:25 AM ] }, { "y = 0\n", "for i in range (1000): \n", bill $109.29, party 12\n", "6. What will the value of y be after the following code is executed?\n", \n", for j in range (1000): \n", if i == j:\n", y += 1\n", \n", "7. Write a nested for loop to generate a 13x13 matrix representing a multiplication table of numbers 0 to 12\n", "8. Repeat with list comprehension\n", "9. Write a while loop to calculate the factorial of 10\n", "10. Write a lambda function y_func for $y = 5x^3 + 4x^2 + 3x + 2$.\n", "11. Make a numpy array x from -10 to 10 with 100 increments. Use the lambda function to find y and plot it. "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "# Week 3 answers\n", "\n", "# 1\n", "\n", "\n", "# 2\n", "\n", "\n", "# 3\n", "\n", "\n", "# 4\n", "\n", "\n", "# 5\n", mediatb.blob.core.windows.net/media/65bae9d6bc66b89155934145/questions/06.6practice_1707856090803.ipynb "\n", "\n", "# 6\n", "\n", "\n", "# 7\n", "\n", "\n", "# 8\n", "\n", https://mediatb.blob.core.windows.net/media/65bae9d6bc66b89155934145/questions/06.6practice_1707856090803.ipynb "I 5/7