CS1101 Assignment 06 Programming Tasks
CS1101 Assignment 06 Programming Tasks
To calculate a final grade from assignment, exam, and participation scores, use specific weightings such as 40% for assignments, 50% for exams, and 10% for participation. The final grade is the weighted sum of these scores. You can then map this percentage to a letter grade using predefined score ranges. Here’s an example in Python: ``` def grade_calculator(assignment_score, exam_score, participation_score): final_score = (assignment_score * 0.4) + (exam_score * 0.5) + (participation_score * 0.1) if final_score >= 90: return final_score, 'A' elif final_score >= 80: return final_score, 'B' elif final_score >= 70: return final_score, 'C' elif final_score >= 60: return final_score, 'D' else: return final_score, 'F' ``` This function returns both the numerical final score and its corresponding letter grade .
The algorithm to determine if a number is prime involves checking if it has any divisors other than 1 and itself, up to its square root. To print all prime numbers between two integers, you can loop through the range and apply the primality test to each number. Here's a basic implementation: ``` def is_prime(num): if num < 2: return False for i in range(2, int(num ** 0.5) + 1): if num % i == 0: return False return True start, end = 10, 50 # example values for num in range(min(start, end), max(start, end) + 1): if is_prime(num): print(num) ``` The function is_prime checks a number for primality, and the loop prints primes in the specified range regardless of order .
To create a Python function that determines if three stick lengths can form a triangle, you can use the triangle inequality theorem. This theorem states that for any three lengths to form a triangle, the sum of any two sides must be greater than the third side. Here’s an example function: ``` def can_form_triangle(a, b, c): return (a + b > c) and (a + c > b) and (b + c > a) ``` The function takes three arguments representing the lengths of the sticks and returns True if the conditions of the triangle inequality are met; otherwise, it returns False.
To implement a function that calculates the n-th Fibonacci number, you can use a recursive or iterative approach. For demonstration purposes, here's how you can define it using iteration: ``` def fibonacci(n): a, b = 0, 1 for _ in range(n): a, b = b, a + b return a for i in range(1, 9): print(f"Fibonacci({i}) = {fibonacci(i)}") ``` This function will correctly calculate and print Fibonacci numbers from n=1 to n=8 .
To define a function in Python that calculates the product of all odd numbers from 1 to n, you can write a function named 'product_of_odds' that iterates through numbers from 1 to n, checks if a number is odd, and multiplies it to a running product. This can be achieved using a loop. For example: ``` def product_of_odds(n): product = 1 for i in range(1, n+1): if i % 2 != 0: product *= i return product ```
To create a function that calculates the square of the tangent of an angle in degrees, you should first convert the angle from degrees to radians, since trigonometric functions in Python typically use radians. You can then use the math library to calculate the tangent and its square, like this: ``` import math def tan_square(t): radians = t * math.pi / 180 tan_value = math.tan(radians) return tan_value ** 2 ```