Question

Define a function that creates a list of all the numbers that are factors of the...

Define a function that creates a list of all the numbers that are factors of the user's number. For example, if the function is called factor, factor(36) should return [1, 2, 3, 4, 6, 9, 12, 18, 36]. The numbers in your list should be sorted from least to greatest, and 1 and the original number should be included.

Remember to consider negative numbers as well as 0.

Bonus: Have the program print the factors of the users number in a comma separated string, without a comma after the last number, and without the brackets of a Python list. If the user's number is prime, note it.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is your answer ::

Code ::

def factors(x):

# For Positive Numbers

# return the list of numbers in the range of 1 to x+1

   if x > 0:
       return [i for i in range(1,x+1) if x%i==0]

# For negative Numbers

# return the list of numbers in the range of x to 0

# Here is x is negative iteration starts from -ve to 0 and checks the condition.

# If x = -4 , the loop runs from - 4 to 0

# if x % i == 0 --> return the number

# This loop runs in the range of ( x, 0 ) and finally the values are returned in the form of list.
   elif x < 0 :
       return [i for i in range(x,0) if x%i==0]


x = input ( "Enter :: " )

print('\n')
print(factors(x))
print('\n')

# Note Down if the user input is Prime
x = abs(x) # used abs() function to check prime for negative numbers also
if x == 1 or x == 0:
print ""
else:
tester=2
checker = True
while tester != x:
if x%tester == 0:
print ""
checker = False
break
tester+=1
if checker == True:
print "User input is Prime"

Output for all test cases ::

Add a comment
Know the answer?
Add Answer to:
Define a function that creates a list of all the numbers that are factors of the...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a Python function that creates and returns a list of prime numbers between 2 and...

    Write a Python function that creates and returns a list of prime numbers between 2 and N, inclusive, sorted in increasing order. A prime number is a number that is divisible only by 1 and itself. This function takes in an integer and returns a list of integers.

  • Write a program that takes a list of integer numbers from a user and creates a...

    Write a program that takes a list of integer numbers from a user and creates a new sorted in ascending order list. As output you need to print original list and sorted one. use list and while python

  • PYTHON PROBLEM Define a function generate_kv_strings( dictionary ) that creates and return a list where the...

    PYTHON PROBLEM Define a function generate_kv_strings( dictionary ) that creates and return a list where the element is a string representing a key-value pair in the format "key: value". Your solution must use a list comprehension. Hint: Remember that you need to convert each key and value into a str before concatenating them, in case they are of a different type. Example Usage: states = {'Florida': 'FL', 'Oregon': 'OR', 'California': 'CA', 'New York': 'NY', 'North Carolina': 'NC'} numbers = {'four':...

  • Define a function add_nums_x_pos_given_list_nums (..) which receives a list that is guaranteed to only have numbers...

    Define a function add_nums_x_pos_given_list_nums (..) which receives a list that is guaranteed to only have numbers (or the empty list) and returns the sum of each number multiplied by the position where the number is. (In the case of the empty list the function it will return 0) For example add_nums_x_pos_given_list_nums ([10,20,30]) will return the value 80 which is the result of adding: 0 * 10 (10 is in position 0 in the list) + 1 * 20 + 2...

  • IN PYTHON 3) Number of Words Write a function numWords() which takes in a string can...

    IN PYTHON 3) Number of Words Write a function numWords() which takes in a string can prints the number of words in the string. You can assume that words will only be separated with spaces, commas, and periods. "hello, world" -> 2 "this is cool" -> 3 4) Is Sorted Write a function isSorted() which takes in an list of integers and returns true if the numbers are sorted from smallest to largest.

  • HW7.26. Return a CSV string from a list of numbers The function below takes a single...

    HW7.26. Return a CSV string from a list of numbers The function below takes a single parameter, a list of numbers called number_list. Complete the function to return a string of the provided numbers as a series of comma separate values (CSV). For example, if the function was provided the argument [22, 33, 44], the function should return '22,33,44'. Hint: in order to use the join function you need to first convert the numbers into strings, which you can do...

  • Create a program that will determine the even number(s) from a list of numbers. Your program...

    Create a program that will determine the even number(s) from a list of numbers. Your program should create and call a function FindEven that accepts a list of numbers and returns a list of only the even numbers in sorted order. Note that the original list is given in the starter code. def body(): numberlist = [1, 1000, 5, -3, 2, 16 # Write your code here and notice level # Do NOT include: if __name__ == been provided for...

  • Define a function called get_n_largest(numbers, n) which takes a list of integers and a value n...

    Define a function called get_n_largest(numbers, n) which takes a list of integers and a value n as parameters and returns a NEW list which contains the n largest values in the parameter list. The values in the returned list should be in increasing order. The returned list must always be of length n. If the number of values in the original list is less than n, the value None should be repeated at the end of the returned list to...

  • Define a function named how_many_substr_of_string(...) which receives two parameters, the first parameters is a list with...

    Define a function named how_many_substr_of_string(...) which receives two parameters, the first parameters is a list with strings (name it  listst) and the second parameter is a single string (name it st). The function should return a number, indicating how many of the strings in the list listst are substrings in the string st As an example, the following code fragment: listst = ["a","bc","dd"] st = "abc" res = how_many_substr_of_string(listst,st) print (res) should produce the output: 2 Language Python

  • The prime factorization of a number is the unique list of prime numbers that, when multiplied,...

    The prime factorization of a number is the unique list of prime numbers that, when multiplied, gives the number. For example, the prime factorization of 60 is 2 ∗ 2 ∗ 3 ∗ 5. In this problem you must write code to recursively find and return the prime factorization of the given number. You must print these in ascending sorted order with spaces in between. For example, if your input is: 120 then you should print the following output: 2...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT