Preparations

Read this week's course notes.

Read the first part of Chapter 2 (up to -while loop statements-) in Automate the Boring Stuff with Python.

Task 1

In this task, you will create a function find_longest_words with three parameters in the file uke_02_oppg_1.py. The function prints the longest words. If several words have the longest length, the program must print all the words.

For example, a call to find_longest_words(-Game-, -Action-, -Champion-) will print

while a call to find_longest_words(-apple-, -carrot-, -pineapple-) will print

and a call to find_longest_words(-Four-, -Five-, -Nine-) will print

Task 2

The rule for calculating whether a year is a leap year or not is as follows:

Usually, a year divisible by 4 is a leap year (for example, 1996 was a leap year);

except for years that are also divisible by 100 (for example, 1900 is not a leap year);

but if the year that is divisible by 100 is also divisible by 400, then it is still a leap year (for example, 2000 is a leap year).

n the file uke_02_oppg_2.py create a function is_leap_year(year) which takes in a year number and returns True if it is a leap year, and False if not.

Test your code by adding these lines to the bottom of the file:

Hint

Use the modulus operator (%) to determine whether an integer is divisible by another. Check course notes about the operator %.

PS: If everything is as it should be, the program will print

Testing is_leap_year... OK

Task 3

It is usually said that one human year corresponds to 7 dog years. This does not take into account that dogs become adults when they are about 2 years old. Therefore, it may be better to count both the first 2 human years as 10.5 dog years each, and after that count each human year as 4 dog years.

In the file uke_02_oppg_3.py, create the function human_to_dog_years with one parameter representing the number of human years. Let the function return how many dog years this corresponds to.

Test your code by adding these lines to the bottom of the file:

PS: If an assert fails, it might be a good idea to see what the result actually was. Add a print statement that displays the result of human_to_dog_years before the asserts

Task 4

Here we will do almost the same thing as in task 1 again, but with a small difference. In the file uke_02_oppg_4.py, you must create a function find_first_longest_word which takes in 3 strings as parameters and prints out the longest word. But if several words have the longest length, the program should only print the first word that has the longest length.

Example run:

Task 5a

Our eyes perceive electromagnetic radiation with a wavelength from 380 to 740 nanometers, or with a frequency from 405 to 790 terahertz. This area is called visible light or just light. electromagnetic radiation in visible light is converted into colors in the brains of humans and animals. The table below shows where the different colors of visible light lie in the spectrum. Wikipedia.

In the file uke_02_oppg_5.py, create the function wavelength_to_color with a parameter that represents the wavelength value in nanometers (nm). The function should return which color in visible light the wavelength corresponds to. If you get a value that is exactly on the border between two colors, you must choose the color with the shortest wavelength of the two. If you get a value that is outside the visible light range, the function must return None.

Test your code by adding these lines to the bottom of the file:

Task 5b

In the file uke_02_oppg_5.py you must create a function frequency_to_color which has the same behavior as the function in task a, but which instead has a parameter representing a wave frequency in terahertz (THz). The function should return which color in visible light the wave frequency represents.

In this function, you must first convert from terahertz to nanometers, and then reuse the function you wrote in exercise a to find the color.

The formula to convert from Hz to meters is

where ? is wavelength in meters, f is frequency in Hz, and c = 3x108 m/s is speed of light.

Test your code by adding these lines to the bottom of the file:

Exercise 5c

In this task, you will use the functions from 5a and 5b to process different inputs from users. In the file uke_02_oppg_5.py, write a function frequency_or_wavelength_to_color without parameters that asks the user for a unit, either nanometer ('nm') or terahertz ('THz'), and then a value (a number). The function should print out which color in visible light that unit and value belong to. If you get a unit that is not nm or THz, your program should inform the user that the unit must be either nm or THz, and the program should end execution (see examples below for exact wording). If the user enters a wavelength or frequency that is outside the spectrum, a message must also be given about this (see examples).

Make a call to this function at the end of the file, so that when you run the uke_02_oppg_5.py file only this is printed to the screen (remove any calls to print you have used to test previous functions in a and b).

Example runs:

frequency_or_wavelength_to_color()

Exercise 6

In the file uke_02_oppg_6.py, create a function is_even_positive_int that has a parameter x. The function should return True if x is a positive even number of type int, and False otherwise.

Test your code by adding these lines to the bottom of the file:

Hint

• To check whether a variable x is of type int, use type(x) == int.

• Use the % operator to determine whether a number is even or odd.

Task 7

In the file uke_02_oppg_7.py, write a function is_legal_triangle that has three parameters. Let the function return True if the three parameters represent positive numbers that can represent the length of the sides of a triangle, and False if such a triangle cannot exist. Note from the triangle inequality that the sum of any choice of two sides is always greater than the third side.

The function should work if the arguments are either float or int; if an argument has a different type, the function must return False, and not crash.

Test your code by adding these lines to the bottom of the file:

Hint

• Start by checking that the arguments have the correct type. For each parameter, check if the type is int or float, and if not return False.

• For each side length, check that the sum of the other two side lengths is greater. If you find a side length that is equal to or longer than the sum of the other two, return False.

• Finally, you just return True

Task 8a

A hyperrectangle is a rectangle whose sides are horizontal and vertical (not rotated). We can represent a hyperrectangle with two coordinates (x_0, y_0)(x 0,y 0 ) and (x_1, y_1)(x 1,y 1) representing two diagonally opposite corners. In this task, you must determine whether a point is located within such a rectangle or not.

In the file uke_02_oppg_8.py, write a function point_in_rectangle that has six parameters x0, y0, x1, y1, x2, y2, where the first four parameters represent a hyperrectangle, and the last two represent an arbitrary point. Let the method return True if the point is inside the rectangle, and False if not. If the point is located exactly on the line, we consider it to be within.

You should not import any libraries to solve this task.

Test your code:

Hint

• Recalculate the rectangle so that you know what is right and left, top and bottom. For example, the minimum of x0 and x1 will be the left-hand side, while the maximum will be the right-hand side. Correspondingly for top and bottom with the y-axis.

• Check that the point is both between the left and right sides, and also between top and bottom.

task 8b

In the file uke_02_oppg_8.py, write a function rectangles_overlap that has eight parameters x0, y0, x1, y1, x2, y2, x4, y4, where the first four parameters represent one hyperrectangle, and the last four represent another. Let the method return True if the rectangles overlap, and False if not. We say that the rectangles overlap even if they only share a single point.

You should not import any libraries to solve this task.

Test your code:

Hint

Recalculate both rectangles so you know what is right and left, top and bottom.

If the right side of one rectangle is to the left of the left side of the other, the answer is false (corresponding logic with top and bottom). Remember to check both directions.

Illustration of the test cases above:

Task 8c

In the file uke_02_oppg_8.py, write a function circle_overlaps_rectangle that has seven parameters x0, y0, x1, y1, x2, y2, r2, where the first four parameters represent a hyperrectangle, and the last three represent a circle centered at (x_2, y_2 )(x 2,y 2) with radius r_2r 2

. Let the method return True if the circle overlaps the rectangle, and False if not. If the circle and the rectangle only share a single point, they are still considered to be overlapping.

You should not import any libraries to solve this task.

Test your code:

Hint

• If the center of the circle is inside the rectangle, the answer is True. Use the function you wrote in 8a to check this. You can call the function, you don't need to rewrite the logic. For example: if point_in_rectangle(...):.

• Convert the points from x0 and x1 to left edge (minimum of x0 and x1) and right edge (maximum). Similarly, convert points y0 and y1 to top and bottom.

• “Expand” the rectangle by the radius of the circle in all directions. If the center of the circle is outside this extended rectangle (use the function from 8a again), there is guaranteed to be no overlap.

• In the remaining cases, the center of the circle is in the frame around the rectangle (see figure above).

If the circle's x-coordinate is between the x-coordinates of the original rectangle, there is an overlap.

Correspondingly for the y-axis.

If the center of the circle does not satisfy either of the two points above, it is located in one of the corners. If the center of the circle has a greater distance than r_2r2

to all corners of the original rectangle, there is no overlap (e.g. as in the figure above). To find the distance end, use the function from lab1.

Illustration of the test cases stated above (one circle per test case):

Read this week's course notes.

Read the first part of Chapter 2 (up to -while loop statements-) in Automate the Boring Stuff with Python.

Task 1

In this task, you will create a function find_longest_words with three parameters in the file uke_02_oppg_1.py. The function prints the longest words. If several words have the longest length, the program must print all the words.

For example, a call to find_longest_words(-Game-, -Action-, -Champion-) will print

while a call to find_longest_words(-apple-, -carrot-, -pineapple-) will print

and a call to find_longest_words(-Four-, -Five-, -Nine-) will print

Task 2

The rule for calculating whether a year is a leap year or not is as follows:

Usually, a year divisible by 4 is a leap year (for example, 1996 was a leap year);

except for years that are also divisible by 100 (for example, 1900 is not a leap year);

but if the year that is divisible by 100 is also divisible by 400, then it is still a leap year (for example, 2000 is a leap year).

n the file uke_02_oppg_2.py create a function is_leap_year(year) which takes in a year number and returns True if it is a leap year, and False if not.

Test your code by adding these lines to the bottom of the file:

Hint

Use the modulus operator (%) to determine whether an integer is divisible by another. Check course notes about the operator %.

PS: If everything is as it should be, the program will print

Testing is_leap_year... OK

Task 3

It is usually said that one human year corresponds to 7 dog years. This does not take into account that dogs become adults when they are about 2 years old. Therefore, it may be better to count both the first 2 human years as 10.5 dog years each, and after that count each human year as 4 dog years.

In the file uke_02_oppg_3.py, create the function human_to_dog_years with one parameter representing the number of human years. Let the function return how many dog years this corresponds to.

Test your code by adding these lines to the bottom of the file:

PS: If an assert fails, it might be a good idea to see what the result actually was. Add a print statement that displays the result of human_to_dog_years before the asserts

Task 4

Here we will do almost the same thing as in task 1 again, but with a small difference. In the file uke_02_oppg_4.py, you must create a function find_first_longest_word which takes in 3 strings as parameters and prints out the longest word. But if several words have the longest length, the program should only print the first word that has the longest length.

Example run:

Task 5a

Our eyes perceive electromagnetic radiation with a wavelength from 380 to 740 nanometers, or with a frequency from 405 to 790 terahertz. This area is called visible light or just light. electromagnetic radiation in visible light is converted into colors in the brains of humans and animals. The table below shows where the different colors of visible light lie in the spectrum. Wikipedia.

In the file uke_02_oppg_5.py, create the function wavelength_to_color with a parameter that represents the wavelength value in nanometers (nm). The function should return which color in visible light the wavelength corresponds to. If you get a value that is exactly on the border between two colors, you must choose the color with the shortest wavelength of the two. If you get a value that is outside the visible light range, the function must return None.

Test your code by adding these lines to the bottom of the file:

Task 5b

In the file uke_02_oppg_5.py you must create a function frequency_to_color which has the same behavior as the function in task a, but which instead has a parameter representing a wave frequency in terahertz (THz). The function should return which color in visible light the wave frequency represents.

In this function, you must first convert from terahertz to nanometers, and then reuse the function you wrote in exercise a to find the color.

The formula to convert from Hz to meters is

where ? is wavelength in meters, f is frequency in Hz, and c = 3x108 m/s is speed of light.

Test your code by adding these lines to the bottom of the file:

Exercise 5c

In this task, you will use the functions from 5a and 5b to process different inputs from users. In the file uke_02_oppg_5.py, write a function frequency_or_wavelength_to_color without parameters that asks the user for a unit, either nanometer ('nm') or terahertz ('THz'), and then a value (a number). The function should print out which color in visible light that unit and value belong to. If you get a unit that is not nm or THz, your program should inform the user that the unit must be either nm or THz, and the program should end execution (see examples below for exact wording). If the user enters a wavelength or frequency that is outside the spectrum, a message must also be given about this (see examples).

Make a call to this function at the end of the file, so that when you run the uke_02_oppg_5.py file only this is printed to the screen (remove any calls to print you have used to test previous functions in a and b).

Example runs:

frequency_or_wavelength_to_color()

Exercise 6

In the file uke_02_oppg_6.py, create a function is_even_positive_int that has a parameter x. The function should return True if x is a positive even number of type int, and False otherwise.

Test your code by adding these lines to the bottom of the file:

Hint

• To check whether a variable x is of type int, use type(x) == int.

• Use the % operator to determine whether a number is even or odd.

Task 7

In the file uke_02_oppg_7.py, write a function is_legal_triangle that has three parameters. Let the function return True if the three parameters represent positive numbers that can represent the length of the sides of a triangle, and False if such a triangle cannot exist. Note from the triangle inequality that the sum of any choice of two sides is always greater than the third side.

The function should work if the arguments are either float or int; if an argument has a different type, the function must return False, and not crash.

Test your code by adding these lines to the bottom of the file:

Hint

• Start by checking that the arguments have the correct type. For each parameter, check if the type is int or float, and if not return False.

• For each side length, check that the sum of the other two side lengths is greater. If you find a side length that is equal to or longer than the sum of the other two, return False.

• Finally, you just return True

Task 8a

A hyperrectangle is a rectangle whose sides are horizontal and vertical (not rotated). We can represent a hyperrectangle with two coordinates (x_0, y_0)(x 0,y 0 ) and (x_1, y_1)(x 1,y 1) representing two diagonally opposite corners. In this task, you must determine whether a point is located within such a rectangle or not.

In the file uke_02_oppg_8.py, write a function point_in_rectangle that has six parameters x0, y0, x1, y1, x2, y2, where the first four parameters represent a hyperrectangle, and the last two represent an arbitrary point. Let the method return True if the point is inside the rectangle, and False if not. If the point is located exactly on the line, we consider it to be within.

You should not import any libraries to solve this task.

Test your code:

Hint

• Recalculate the rectangle so that you know what is right and left, top and bottom. For example, the minimum of x0 and x1 will be the left-hand side, while the maximum will be the right-hand side. Correspondingly for top and bottom with the y-axis.

• Check that the point is both between the left and right sides, and also between top and bottom.

task 8b

In the file uke_02_oppg_8.py, write a function rectangles_overlap that has eight parameters x0, y0, x1, y1, x2, y2, x4, y4, where the first four parameters represent one hyperrectangle, and the last four represent another. Let the method return True if the rectangles overlap, and False if not. We say that the rectangles overlap even if they only share a single point.

You should not import any libraries to solve this task.

Test your code:

Hint

Recalculate both rectangles so you know what is right and left, top and bottom.

If the right side of one rectangle is to the left of the left side of the other, the answer is false (corresponding logic with top and bottom). Remember to check both directions.

Illustration of the test cases above:

Task 8c

In the file uke_02_oppg_8.py, write a function circle_overlaps_rectangle that has seven parameters x0, y0, x1, y1, x2, y2, r2, where the first four parameters represent a hyperrectangle, and the last three represent a circle centered at (x_2, y_2 )(x 2,y 2) with radius r_2r 2

. Let the method return True if the circle overlaps the rectangle, and False if not. If the circle and the rectangle only share a single point, they are still considered to be overlapping.

You should not import any libraries to solve this task.

Test your code:

Hint

• If the center of the circle is inside the rectangle, the answer is True. Use the function you wrote in 8a to check this. You can call the function, you don't need to rewrite the logic. For example: if point_in_rectangle(...):.

• Convert the points from x0 and x1 to left edge (minimum of x0 and x1) and right edge (maximum). Similarly, convert points y0 and y1 to top and bottom.

• “Expand” the rectangle by the radius of the circle in all directions. If the center of the circle is outside this extended rectangle (use the function from 8a again), there is guaranteed to be no overlap.

• In the remaining cases, the center of the circle is in the frame around the rectangle (see figure above).

If the circle's x-coordinate is between the x-coordinates of the original rectangle, there is an overlap.

Correspondingly for the y-axis.

If the center of the circle does not satisfy either of the two points above, it is located in one of the corners. If the center of the circle has a greater distance than r_2r2

to all corners of the original rectangle, there is no overlap (e.g. as in the figure above). To find the distance end, use the function from lab1.

Illustration of the test cases stated above (one circle per test case):

There are activities in question that need to be addressed ,, would u able to assistDon’t need to make a big , just address these activities questions ,Please can you tell me how much it would be to assist with this MSc Psychology and Islamic Counselling assignment:Write a Research Paper of 3000 to 3500 words on How the understanding of Quran and Hadith...Assessment task 3: Take Home ExaminationAssessment SummaryTask type: Take home exam/ written assignmentTask length: 2000 words (+/-10%)Weighting: 40%Due Date/Time: Multiple due datesA penalty of 10% per...Assignment Details :N8002-Professional Reflection in Nursing-G...PurposeSynthesize knowledge from the literature to develop strategies that improve a particular issue in your specialty practice.Task detailsStudents...How much for paraphrasing of the attached and additional conclusion?Page 1 Kaplan Business School Assessment Outline 2024 T1Assessment 3 InformationSubject Code: ACCM4400Subject Name: Auditing and AssuranceAssessment Title: Case StudyAssessment Type: Individual Written...Undergraduate Modular Scheme Open Book ExaminationModule Name Managing DataModule Code BMA4003-20 Level 4Submission Deadline Exam paper release date and time: Sunday 9th June 2024 at 23:59Submission cut-off...**Show All Questions**