0% found this document useful (0 votes)
23 views2 pages

User-Defined Function: showGrades

The document outlines various Python programming tasks focused on user-defined functions, including calculating factorials, finding areas of geometric shapes, performing arithmetic operations, displaying prime numbers, and finding HCF. It also includes functions for grading students based on scores and manipulating strings by replacing certain characters. Additionally, it describes a function to return indices of list elements divisible by 3.

Uploaded by

adi.chauhan.483
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views2 pages

User-Defined Function: showGrades

The document outlines various Python programming tasks focused on user-defined functions, including calculating factorials, finding areas of geometric shapes, performing arithmetic operations, displaying prime numbers, and finding HCF. It also includes functions for grading students based on scores and manipulating strings by replacing certain characters. Additionally, it describes a function to return indices of list elements divisible by 3.

Uploaded by

adi.chauhan.483
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Programs Based on Functions

1. Write a program in Python to find factorial for the given number using user
defined function
2. Write a Menu driven program to find the area of Circle, Rectangle and
Triangle using function.
3. Writer a Menu Driven program to perform arithmetic operations((+,-,*, /,%)
using functions.
4. Write a program to display the prime no’s in a given range using functions.
5. Write a program using function to find the HCF of numbers present in the
list(solution below)
6. Write a user defined function in Python named showGrades(S) which takes
the dictionary S as an argument.
The dictionary S contains Name : [Phy,Chem,Maths] as key:value pairs .
The function displays the corresponding grade obtained by the students
according to the following grading rules:

7. Write a user defined function in Python named Puzzle(W,N) which takes the
arguement W as an English word and N as an integer and returns the string
where every Nth alphabet of the word W is replaced with an underscore("_")
For example: if W contains the word "TELEVISION" and N is 3, then the
function should return the string "TE_EV_SI_N" . Likewise for the word
"TELEVISION" if N is 4, then the function should return "TEL_VIS_ON"
8. Write a function LIST_IND(L), where L is the list of elements passed as
argument to the function. The function returns another list named indexList that
stores the indices of all Elements of L that are divisible by 3.
For example: If L contains [12,4,0,11,0,51]
The indexList will have - [0,5]

Solutions:
5.

Common questions

Powered by AI

The LIST_IND function iterates over the list of elements, checking the remainder of each element when divided by 3. If the remainder is zero, the current index is recorded in a result list. For improved robustness, the function should include input validation, such as verifying the list is non-empty and contains only numeric elements. To enhance efficiency, especially for larger lists, utilizing generator expressions may minimize memory usage.

To design a menu-driven Python program for arithmetic operations using user-defined functions, you first define individual functions for each operation: addition, subtraction, multiplication, division, and modulus. Then, create a main function that will display the menu to the user, prompting them to select an operation and input numbers. Based on the user's choice, the program calls the appropriate function and displays the result. Implementing error handling for division by zero and ensuring inputs are valid are crucial for robustness.

Incorporate higher-order functions, enabling design flexibility by accepting function arguments. Define a general 'operate' function that takes a function parameter (the operation) and numbers to apply it to. Define arithmetic operations (addition, subtraction, multiplication, etc.) as separate functions. The 'operate' function can then invoke these as needed, achieving flexibility by allowing any user-defined operation. This design pattern facilitates extensibility, letting users define custom operations without modifying existing code.

The showGrades function relies on a set of grading rules to determine student grades based on their scores in Physics, Chemistry, and Maths, stored in a dictionary. The function likely computes an average or sums the scores to categorize the student within predetermined thresholds for grades (e.g., A, B, C). It iterates over the dictionary, calculating the necessary result for each student, and outputs the corresponding grade by comparing against the specified criteria.

Python's list comprehensions and slicing capabilities can be efficiently employed to solve selective identification problems like in LIST_IND. For instance, list comprehensions allow succinct iteration with conditional checks for divisibility by 3, collecting indices of elements meeting the criteria. Such approaches reduce boilerplate and leverage Python's powerful, concise iteration constructs, improving both performance and readability by eliminating explicit loops and conditionals.

Using Python functions to find prime numbers involves defining a helper function that checks whether a number is prime by testing divisibility up to its square root. In the main function, iterate over each number in the specified range and use the helper function to determine if it is prime. If a number is prime, add it to a result list. Finally, display the list of prime numbers to the user. Optimizations can include skipping even numbers after checking '2' and starting division checks from '3'.

To find the HCF using Python functions, one typical approach is to use the Euclidean algorithm. First, define a function that takes two numbers as input and returns their HCF using iterative or recursive use of the algorithm. Then, to find the HCF of a list of numbers, iterate through the list, maintaining a cumulative HCF, applying the function to each pair of current HCF and the next number in the list. This process continues until the end of the list is reached, returning the final HCF.

To calculate areas using user-defined functions in a menu-driven Python program, define individual functions for each shape: circle, rectangle, and triangle. Each function should accept relevant parameters (radius for circle, length and width for rectangle, base and height for triangle) and return the computed area using the formulae: π*r^2, length*width, and 0.5*base*height, respectively. The main function should present a menu to the user, prompting them to select a shape and input parameters. Based on the choice, call the corresponding function to display the result.

Manage varying input sizes in functions using varargs (`*args`) or keyword arguments (`**kwargs`), enabling functions to accept a flexible number of arguments. Employ default values for keyword arguments to ensure default behaviors when arguments are omitted. For comprehensive argument management, consider also implementing input checks within functions using assertions or type hints to ensure the correctness of inputs, while utilizing structured unpacking to enhance readability and maintainability of the larger, flexible argument sets.

Implement the function by first iterating over the indices of the word string, using either a loop or list comprehension. Replace every Nth character with an underscore, ensuring to handle the zero-based index by using `(index + 1) % N`. Edge cases include when N is greater than the length of the word, in which the function should return the word unchanged. Additional edge cases such as empty strings should be handled by returning an empty string immediately.

You might also like