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

Python Functions for Math and Strings

The document contains five programming questions requiring the creation of functions. The first question involves summing numbers divisible by a given integer within a range. The subsequent questions focus on evaluating inequalities, replacing vowels in strings, calculating factorials recursively, and computing Hamming distance between two strings.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views2 pages

Python Functions for Math and Strings

The document contains five programming questions requiring the creation of functions. The first question involves summing numbers divisible by a given integer within a range. The subsequent questions focus on evaluating inequalities, replacing vowels in strings, calculating factorials recursively, and computing Hamming distance between two strings.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Question1.

Create a function that takes three arguments a, b, c and returns the sum of the
numbers that are evenly divided by c from the range a, b inclusive.

Examples
evenly_divisible(1, 10, 20) ➞ 0
# No number between 1 and 10 can be evenly divided by 20.

evenly_divisible(1, 10, 2) ➞ 30
# 2 + 4 + 6 + 8 + 10 = 30

evenly_divisible(1, 10, 3) ➞ 18
# 3 + 6 + 9 = 18

Question2. Create a function that returns True if a given inequality expression is correct and
False otherwise.

Examples
correct_signs("3 < 7 < 11") ➞ True

correct_signs("13 > 44 > 33 > 1") ➞ False

correct_signs("1 < 2 < 6 < 9 > 3") ➞ True

Question3. Create a function that replaces all the vowels in a string with a specified character.

Examples
replace_vowels("the aardvark", "#") ➞ "th# ##rdv#rk"

replace_vowels("minnie mouse", "?") ➞ "m?nn?? m??s?"

replace_vowels("shakespeare", "*") ➞ "sh*k*sp**r*"

Question4. Write a function that calculates the factorial of a number recursively.

Examples
factorial(5) ➞ 120

factorial(3) ➞ 6

factorial(1) ➞ 1

factorial(0) ➞ 1
Question 5

Hamming distance is the number of characters that differ between two strings.

To illustrate:

String1: "abcbba"
String2: "abcbda"

Hamming Distance: 1 - "b" vs. "d" is the only difference.

Create a function that computes the hamming distance between two strings.

Examples
hamming_distance("abcde", "bcdef") ➞ 5

hamming_distance("abcde", "abcde") ➞ 0

hamming_distance("strong", "strung") ➞ 1

Common questions

Powered by AI

Inequality checks within strings require parsing and evaluation of sub-expressions for correctness, unlike direct language-supported expression evaluation that uses predefined operators. When processing inequality strings, logic has to parse each section and evaluate each increment. For instance, parsing '1 < 2 < 6 > 5' involves first evaluating '1 < 2', then '2 < 6', followed by '6 > 5', confirming that each is correct sequentially—not possible as a single string evaluation without splitting it .

To create this function, named `evenly_divisible`, you should iterate through all numbers from `a` to `b` inclusive. For each number, check if it is divisible by `c` by using the modulus operator. If the modulus yields zero, add it to a running total. Finally, return the total. This method ensures you only sum numbers within the range that divide evenly by `c` without any remainder, as demonstrated with examples such as `evenly_divisible(1, 10, 2)` returning 30 because 2, 4, 6, 8, and 10 are all divisible by 2 .

Evaluating string expressions for validity is crucial in fields such as computational logic parsing, automated mathematical assessments in education, and syntax checking in programming languages. It allows for the development of interpreters that can dynamically assess and validate user input for errors before execution, such as in calculators or query languages. This type of string evaluation is essential for building reliable systems that leverage human-readable syntax into machine-interpretable instructions efficiently .

To calculate the Hamming distance, create a function that iterates through corresponding characters of two strings of equal length, using a counter to tally mismatches. The function should return this count. Hamming distance measures error or variation between two sequences, which is critical in fields like information technology and genetics for error detection and categorization tasks. For example, comparing 'strong' and 'strung', which differ at one character, results in a Hamming distance of 1 .

To prevent inefficiency in a factorial function, iterative methods can replace recursion to reduce stack depth and memory use when calculating large values. Optimizing through memoization or tabulation may also help save repeated calculations, enhancing processing scalability for vast inputs. For instance, while recursive methods are readable for small inputs, large computations benefit significantly from incremental calculations, similar to dynamic programming techniques such as precomputing or storing interim results .

The function should iterate over each character of the string and check if it is a vowel. This can be done using a set or list containing vowels. When a vowel is found, it should be replaced with the specified replacement character, constructing a new string. For example, using this strategy, `replace_vowels('minnie mouse', '?')` results in 'm?nn?? m??s?' .

A recursive approach to compute the factorial involves defining a base case where the factorial of 0 is 1. For any other number n, use the recursive formula n! = n * (n-1)! until reaching the base case. This is efficiently implemented by having the function call itself with decremented arguments until n is zero. For example, calculating `factorial(5)` returns 120, as the function will compute 5*4*3*2*1 .

To verify the correctness of inequality expressions like '3 < 7 < 11', you need to assess each pair of numbers with their respective inequality operators. This requires parsing the string and evaluating the inequalities progressively from left to right. If all comparisons return True, the function should return True, otherwise False. For instance, the expression '3 < 7 < 11' should return True as the comparisons 3 < 7 and 7 < 11 are both valid .

When designing a string replacement function, consider the character set to be replaced (e.g., vowels) and case sensitivity, ensuring replacements match both upper and lowercase. Incorporate flexibility for different lists of characters, potentially using regular expressions for dynamic and expansive handling. Efficient string building using lists might save memory. Also, decide if changing multiple character types simultaneously or replacing each consecutively affects the intended outcome. Ultimately, the function's design should be efficient for the given use case .

A recursive factorial function is preferred in scenarios where code readability and simplicity are more crucial than performance constraints. Recursive functions mirror mathematical definitions closely and can be easier to understand in an educational or learning context. However, due to stack memory usage concerns in deep recursions, they're less efficient than iterative methods for higher input values. Recursive functions illustrate concepts of recursion effectively, which can be crucial when teaching or illustrating base-case recursion principles .

You might also like