Site icon Tutor Bin

UCSD Basic Python Worksheet

UCSD Basic Python Worksheet

Description

In this exercise you will be defining several different functions

  1. For each function below, a) define the function, then b) evaluate and print out its return value to test that it gives the correct output. There are also some additional constraints:
    • All variable and parameter names should use snake_case for names with multiple words.
    • All print()ing should happen outside the functions.

Exercise Functions

  1. bool_to_int

    Define a lambda function and assign it to the bool_to_int variable. This function should have one parameter named value, which will receive a boolean argument. The function should convert that boolean argument to an integer and return it.

    Examples:
    bool_to_int(True) # returns 1
    bool_to_int(False) # returns 0
  2. get_smaller

    Define a lambda function and assign it to the get_smaller variable. This function should have two parameters named a and b, which will each receive a numeric argument. The function should return the smaller of those two arguments.

    Examples:
    get_smaller(16, 31) # returns 16
    get_smaller(253, 223) # returns 223
  3. cube

    Define a def function named cube. This function should have one parameter named base, which will receive a numeric argument. The function should return the value of the argument raised to the 3rd power. Call it with positional arguments only.

    Examples:
    cube(2) # returns 8
    cube(5) # returns 125
  4. absolute_difference

    Define a def function named absolute_difference. This function should have two parameters named a and b, which will each receive a numeric argument. The function should return the absolute value of the difference of the arguments. Call it with positional arguments only.

    Examples:
    absolute_difference(14, 11) # returns 3
    absolute_difference(13, 40) # returns 27
  5. squared_difference

    Define a def function named squared_difference. This function should have two parameters named a and b, which will each receive a numeric argument. The function should return the square of the difference of the arguments. Call it with positional arguments only.

    Examples:
    squared_difference(14, 11) # returns 9
    squared_difference(13, 40) # returns 729
  6. hours_to_minutes

    Define a def function named hours_to_minutes. This function should have one parameter named hours, which will receive a numeric argument. The function should return the number of minutes equal to argument’s number of hours. Call it with keyword arguments only.

    Examples:
    hours_to_minutes(hours = 3.5) # returns 210.0
    hours_to_minutes(hours = 12) # returns 720
  7. get_circumference

    Define a def function named get_circumference. This function should have one parameter named radius, which will receive a numeric argument. The function should return the circumference of a circle with that radius. Call it with keyword arguments only.

    Examples:
    get_circumference(radius = 1) # returns 6.283185307179586
    get_circumference(radius = 9.2) # returns 57.8053048260521912
  8. linear_transform

    Define a def function named linear_transform. This function should have three parameters named x, slope, and intercept; which will each receive a numeric argument. The function should return the vertical position of a straight line with slope slope and vertical-axis-intecept intercept at horizontal position x (in other words, it should evaluate a linear equation). Call it with keyword arguments only, and use a different argument order with each function call.

    Examples:
    linear_transform(x=5.0, slope=3.0, intercept=-8.5)
    # returns 6.5
    linear_transform(slope=-2.1, intercept=17.0, x=2.5)
    # returns 11.75
  9. standardize

    Define a def function named standardize. This function should have three parameters named x, x_center, and scale_size; which will each receive a numeric argument, and scale_size will always be positive. The function should return the difference between x and x_center, divided by the scale_size. Call it with keyword arguments only, and use a different argument order with each function call.

    Examples:
    standardize(x=8.2, x_center=13.8, scale_size=4.83)
    # returns -1.1594202898550727
    standardize(scale_size=24.63, x=2.89, x_center=-72.813)
    # returns 3.073609419407227

Have a similar assignment? "Place an order for your assignment and have exceptional work written by our team of experts, guaranteeing you A results."

Exit mobile version