Question

In: Computer Science

# RQ3 def largest_factor(n): """Return the largest factor of n that is smaller than n. >>>...




#  RQ3
def largest_factor(n):
    """Return the largest factor of n that is smaller than n.

    >>> largest_factor(15) # factors are 1, 3, 5
    5
    >>> largest_factor(80) # factors are 1, 2, 4, 5, 8, 10, 16, 20, 40
    40
    """
    "*** YOUR CODE HERE ***"


         
#  RQ4
#  Write functions c, t, and f such that calling the with_if_statement and
#  calling the with_if_function do different things.
#  In particular, write the functions (c,t,f) so that calling  with_if_statement function returns the integer number 1, 
#  but calling the with_if_function function throws a ZeroDivisionError.

def if_function(condition, true_result, false_result):
    """Return true_result if condition is a true value, and
    false_result otherwise.

    >>> if_function(True, 2, 3)
    2
    >>> if_function(False, 2, 3)
    3
    >>> if_function(3==2, 3+2, 3-2)
    1
    >>> if_function(3>2, 3+2, 3-2)
    5
    """
    if condition:
        return true_result
    else:
        return false_result


def with_if_statement():
    """
    >>> with_if_statement()
    1
    """
    if c():
        return t()
    else:
        return f()

def with_if_function():
    return if_function(c(), t(), f())

def c():
    "*** YOUR CODE HERE ***"

def t():
    "*** YOUR CODE HERE ***"

def f():
    "*** YOUR CODE HERE ***"



Solutions

Expert Solution

RQ3

def largest_factor(n):
    l=[]
    for i in range(1,n):
        if n%i==0:
            l.append(i)
    return l[-1]
print(largest_factor(15))
print(largest_factor(80))


Output

RQ4

def if_function(condition, true_result, false_result):
    """Return true_result if condition is a true value, and
    false_result otherwise.

    >>> if_function(True, 2, 3)
    2
    >>> if_function(False, 2, 3)
    3
    >>> if_function(3==2, 3+2, 3-2)
    1
    >>> if_function(3>2, 3+2, 3-2)
    5
    """
    if condition:
        return true_result
    else:
        return false_result


def with_if_statement():
    """
    >>> with_if_statement()
    1
    """
    if c(): #if c() is false then f() should be executed and should return 1
        return t()
    else:
        return f()

def with_if_function(): #should throw ZeroDivisionError
    return if_function(c(), t(), f())

def c():
    return False

def t():
    return 2/0 #throws ZeroDivisionError

def f():
    return 1
print(with_if_statement())
print(with_if_function())
  


Output


Related Solutions

def annoying_factorial(n): if n == 0 or n == 1: return 1 if n == 2:...
def annoying_factorial(n): if n == 0 or n == 1: return 1 if n == 2: return 2 if n == 3: return 6 if n == 4: return 4 * annoying_factorial(3) if n == 5: return 5 * annoying_factorial(4) if n == 6: return 6 * annoying_factorial(5) else: return n * annoying_factorial(n-1) def annoying_fibonacci(n): if n==0: return 0 if n==1: return 1 if n==2: return 1 if n==3: return 2 if n==4: return annoying_fibonacci(4-1)+annoying_fibonacci(4-2) if n==5: return annoying_fibonacci(5-1)+annoying_fibonacci(5-2) if...
############callbacks ##def function_1( x ) : return x ** 2 ##def function_2( x ) : return...
############callbacks ##def function_1( x ) : return x ** 2 ##def function_2( x ) : return x ** 3 ##def function_3( x ) : return x ** 4 ## ###### create a list of callbacks to each of the functions ######by referencing their names ## ##callbacks = [ function_1 , function_2 , function_3 ] ## ######display a heading and the result of passing a value to each of the ######named functions: ## ##print( '\nNamed Functions:' ) ##for function in callbacks...
python def create_fourier_dataset(x, max_val=7): """ Return the sum of sin(n*x)/n where n = 1,2,3,4,5... max_val Remember,...
python def create_fourier_dataset(x, max_val=7): """ Return the sum of sin(n*x)/n where n = 1,2,3,4,5... max_val Remember, n is a scalar quantity (float/int). x is a NumPy array and you should return an equal length NumPy array :param x: numpy array :param max_val: The maximum value :return: a tuple with two numpy arrays x and the sum """
why is the N region of a PNP transistor smaller than the P region? why wouldn't...
why is the N region of a PNP transistor smaller than the P region? why wouldn't it work if the N region were bigger?
Given a number n, what is the largest gap between successive primes which are less than...
Given a number n, what is the largest gap between successive primes which are less than number n? For example, if n = 12, the prime numbers less than 12 are: [2, 3, 5, 7, 11]. The largest gap between any two prime numbers in this list is 4, so the function would return '4'. >>>print(largestGap(12)) 4 Write Python code to solve this problem, and include the following 3 test cases in your answer. >>>print(largestGap(100)) 8 >>>print(largestGap(1000)) 20 >>>print(largestGap(10000)) 36...
Given a number n, what is the largest gap between successive primes which are less than...
Given a number n, what is the largest gap between successive primes which are less than number n? For example, if n = 12, the prime numbers less than 12 are: [2, 3, 5, 7, 11]. The largest gap between any two prime numbers in this list is 4, so the function would return '4'. >>>print(largestGap(12)) 4 Write Python code to solve this problem, and include the following 3 test cases in your answer. >>>print(largestGap(100)) 8 >>>print(largestGap(1000)) 20 >>>print(largestGap(10000)) 36
def annoying_valley(n): if n == 0: print() elif n == 1: print("*") elif n == 2:...
def annoying_valley(n): if n == 0: print() elif n == 1: print("*") elif n == 2: print("./") print("*") print(".\\") elif n == 3: print("../") print("./") print("*") print(".\\") print("..\\") elif n == 4: print(".../") annoying_valley(3) print("...\\") elif n == 5: print("..../") annoying_valley(4) print("....\\") elif n == 6: print("...../") annoying_valley(5) print(".....\\") else: print("." * (n - 1) + "/") annoying_valley(n - 1) print("." * (n - 1) + "\\") def annoying_int_sequence(n): if n == 0: return [] elif n == 1: return...
def num_to_digit_rec(num, base): """ Return a list of digits for num with given base; Return an...
def num_to_digit_rec(num, base): """ Return a list of digits for num with given base; Return an empty list [] if base < 2 or num <= 0 """ # Write your code here return [] def digit_sum(num, base): """ Return the sum of all digits for a num with given base Your implementation should use num_to_digit_rec() function """ # Write your code here return 0 def digit_str(num, base): """ Given a number and a base, for base between [2, 36]...
If a distribution is skewed right, then the median for this population is smaller than the...
If a distribution is skewed right, then the median for this population is smaller than the median for the sampling distribution of sample means with sample size 80. True or False
The areas of two similar triangles ABC and DEF are 144cm2 and 81cm2, respectively. If the longest side of larger triangle ABC is 36 cm, then the longest side of the smaller triangle DEF is
The areas of two similar triangles ABC and DEF are 144cm2 and 81cm2, respectively. If the longest side of larger triangle ABC is 36 cm, then the longest side of the smaller triangle DEF is (1) 20 cm (2) 26 cm (3) 27 cm (4) 30 cm
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT