Question

####################################################################################################### ###################################################################################### ########################################################### ####

#######################################################################################################
######################################################################################
###########################################################
#############################using python###################
###########################################################
#  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

Homework Answers

Answer #1

RQ3:

Code:

def largest_factor(n):
for i in range(n-1,0,-1):
#this loop executes for n-1 to 1
if(n%i==0):
#if the number is divisible by i we return the value of i
return i
print("Largest Factor of 15 is ",largest_factor(15))
print("Largest Factor of 80 is ",largest_factor(80))
#Here we call the function for various values

Output:

Indentation:

RQ4:

Code:

def if_function(condition,true_result,false_result):
if(condition):
#If true then true_result will be returned
return true_result
else:
#if false then false_result will be returned
return false_result
print(if_function(True,2,3))
print(if_function(False,2,3))
print(if_function(3==2,3+2,3-2))
print(if_function(3>2,3+2,3-2))
#Here we call the function for varius inputs

Output:

Indentation:

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Function Example: Write a Python function that receives two integer arguments and writes out their sum...
Function Example: Write a Python function that receives two integer arguments and writes out their sum and their product. Assume no global variables. def writer(n1, n2): sum = n1 + n2 product = n1*n2 print("For the numbers", n1, "and", n2) print("the sum is", sum) print("and the product is", product) ... 1) Create a PYHW2 document that will contain your algorithms in flowchart and pseudocode form along with your screen shots of the running program. 2) Create the algorithm in both...
1.Implement a function which will -       + Accept a number, num, and a list, my_list...
1.Implement a function which will -       + Accept a number, num, and a list, my_list as arguments       + return True if num exists in my_list       + return False otherwise 2. Write Python code to implement the following function - def find_in_list(item, my_list):     """     Look for an item from my_list (of items.)     If the item is found, return the index position of the item;     otherwise, return -1.     """       #add your code here...
TO BE DONE IN PYTHON: Write a function `lowest_integer()` which takes 2 input arguments: 1)a function...
TO BE DONE IN PYTHON: Write a function `lowest_integer()` which takes 2 input arguments: 1)a function `g` representing an increasing function g(x) 2) a number `gmin`, and returns an integer `nmin` such that nmin>0 is the smallest integer that satisfies g(nmin)>gmin. test: def g(n): return 2*n print(lowest_integer(g, 10)) Output: 6
Use python def a function that takes in a list of passwords (as strings), a password...
Use python def a function that takes in a list of passwords (as strings), a password that needs to be checked (as a string), and an integer that represents the number of repetitions. If the given password occurs given number of times, return True, else return False. eg. >>> repeats(["ma48", "ma28", "ma48"], "ma48", 2) True >>> repeats(["ma48", "ma28", "ma48"], "ma48", 3) False >>> repeats(["ma48", "ma28", "ma48", "ma28", "ma48"], "ma38", 2) False >>> repeats(["ma48", "ma28", "ma48", "ma38"], "ma48", 1) False
Answer with Python. A number is cool if the sum of its digits is divisible by...
Answer with Python. A number is cool if the sum of its digits is divisible by 5. Given an integer n, return the nth cool number. def nthCool(n): nthCool(1) = 5 nthCool(2) = 14 nthCool(3) = 19
Implement the following functions in the given code: def random_suit_number(): def get_card_suit_string_from_number(n): def random_value_number(): def get_card_value_string_from_number(n):...
Implement the following functions in the given code: def random_suit_number(): def get_card_suit_string_from_number(n): def random_value_number(): def get_card_value_string_from_number(n): def is_leap_year(year): def get_letter_grade_version_1(x): def get_letter_grade_version_2(x): def get_letter_grade_version_3(x): Pay careful attention to the three different versions of the number-grade-to-letter-grade functions. Their behaviors are subtly different. Use the function descriptions provided in the code to replace the pass keyword with the necessary code. Remember: Parameters contain values that are passed in by the caller. You will need to make use of the parameters that each...
An integer 'n' greater than 1 is prime if its only positive divisor is 1 or...
An integer 'n' greater than 1 is prime if its only positive divisor is 1 or itself. For example, 2, 3, 5, and 7 are prime numbers, but 4, 6, 8, and 9 are not. Write a python program that defines a function isPrime (number) with the following header: def isPrime (number): that checks whether a number is prime or not. Use that function in your main program to count the number of prime numbers that are less than 5000....
IN C++ AS SIMPLE AS POSSIBLE ______ Re-write the given function, printSeriesSquareFifth,  to use a while loop...
IN C++ AS SIMPLE AS POSSIBLE ______ Re-write the given function, printSeriesSquareFifth,  to use a while loop (instead of for). • The function takes a single integer n as a parameter • The function prints a series between 1 and that parameter, and also prints its result • The result is calculated by summing the numbers between 1 and n (inclusive). If a number is divisible by 5, its square gets added to the result instead. • The function does not...
Be able to write Python programming of a factorial of an integer (given input parameters, output...
Be able to write Python programming of a factorial of an integer (given input parameters, output characteristics and expected output behavior, do not use internal python functions for factorials), recall for example that: 5! = 5 ∗ 4 ∗ 3 ∗ 2 ∗ 1 = 120 and to continue asking unless user says to stop NOT USING FACTORIAL FUNCTION
THIS IS IN PYTHON 3.0 Let's call the first function power(a,b). Use the built-in power function...
THIS IS IN PYTHON 3.0 Let's call the first function power(a,b). Use the built-in power function a**b to write a second function called testing(c) that tests if the first function is working or not. I already have the function power(a,b), which is as follows: def power(a, b):     if (b == 0): return 1     elif (int(b % 2) == 0):         return (power(a, int(b / 2)) *                power(a, int(b / 2)))     else:         return (a * power(a, int(a / 2)) *                    power(a, int(b...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT