Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

import sys One of the things in the sys module is a list named sys.argv , sys tem arg ument v alues. This list

import sys

One of the things in the sys module is a list named sys.argv, "system argument values". This list will contain all of the items on the command line, starting with the name of the script. If the length of sys.argv is greater than one, there are some (one or more) additional arguments on the command line.

Rewrite main to run the test cases if there is one command-line argument, "-test". Otherwise, ask the user for an input value and call the factors with the numerical value from the user.

def main(): # check for command-line arguments if len(sys.argv) > 1 and sys.argv[1] == '-test': # hard-coded test cases factors(12) factors(60) # otherwise, ask the user for input else: # code to prompt the user for input # perform input validation # call the factors function

The input validation is checking to make sure that the number the user entered is between 1 and 1,000, inclusive. If the number is out of bounds, report an error and end the program.

The $ represents the command-line prompt. Here is a example run using the hard-coded testing values.

$ factors.py -test Factors of 12: 2 is a factor of 12. 3 is a factor of 12. Factors of 82: 2 is a factor of 82.

Here are two examples asking the user for input. One using acceptable input:

$ factors.py Enter an integer between 1 and 1,000: 12 Factors of 12: 2 is a factor of 12. 3 is a factor of 12.

And one showing what happens when input validation fails:

$ factors.py Enter an integer between 1 and 1,000: 1002 Value out of range.

The input validation can (should) occur within the main function. You don't need to alter your working factors function in incorporate input validation. At this point, namely, the minimal version, you may assume that the user input is a numeric value. Remember that the input function always returns a str value.

Standard Level

For the standard level, you have two tasks:

  • update the factors function so that it prints out the prime factorization of a positive integer between 1 and 1,000 that is the parameter.
  • update the input validation to recognize non-numeric input and print out an error message, rather than crashing. After the error message, the program can simply end. The Challenge work includes prompting repetitively for input.

Note, input validation is performed before running the code that calculates the factors.

Here is a sample run. The user input is shown in blue bold text.

Enter an integer between 1 and 1,000: 12 The factors of 12: 2 2 3

Notice that the output format has been updated so that all of the factor information appears on one line.

The Algorithm

The algorithm that you will implement will repeatedly check for multiples of the given primes.

Here's the basic algorithm:

  • Store the input (parameter) value.
  • Work through the list of primes in order. We'll work with only one prime at a time. This will be called "current prime".
  • As long as the current prime evenly divides the input value,
    • Output the current prime.
    • Set the input value to its quotient when divided by current prime. Hint: Use integer division here.
  • When current prime no longer evenly divides the input value, go to the next prime number in the list.
  • Repeat until value is 1.
  • If you reach the end of the list of primes and the input value does not equal one, then that remaining input value is prime. Output that value.

Let's look at a couple of concrete examples:

Enter a integer between 1 and 1,000: 264 The factors of 264: 2 2 2 3 11

Here is the algorithm applied to 264:

  • We start by prompting the user for an input value. In this example, they enter 264.
  • We can start the output with " The factors of 264:".
  • Take the value 264 and divide by the current prime number, 2, the first one. It goes in evenly so 2 is factor. We output the factor 2 and update the value to be the quotient, 132.
  • Take the value 132 and divide by the current prime, 2. It goes in evenly. Output 2 and update value to the the quotient, 66.
  • Take the value 66 and divide by the current prime, 2. It goes in evenly. Output 2 and update value to 33.
  • Take the value 33 and divide by the current prime, 2. It doesn't go in evenly. Go on to the next prime.
  • Take the value 33 and divide by the current prime, 3. It goes in evenly. Output 3 and update value to 11.
  • Take the value 11 and divide by the current prime, 3. It doesn't go in evenly. Go on to the next prime.
  • Take the value 11 and divide by the current prime, 5. It doesn't go in evenly. Go on to the next prime.
  • Take the value 11 and divide by the current prime, 7. It doesn't go in evenly. Go on to the next prime.
  • Take the value 11 and divide by the current prime, 11. It goes in evenly. Output 11 and update value to 1.
  • Since value is 1, end the processing.

Enter a integer between 1 and 1,000: 246 The factors of 246: 2 3 41

Here is the algorithm applied to 246:

  • We start by prompting the user for an input value. In this example, they enter 246.
  • We can start the output with " The factors of 246:".
  • Take the value 246 and divide by the current prime, 2. It goes in evenly. Output 2 and update value to 123.
  • Take the value 123 and divide by the current prime, 2. It doesn't go in evenly. Go on to the next prime.
  • Take the value 123 and divide by the current prime, 3. It goes in evenly. Output 3 and update value to 41.
  • Take the value 41 and divide by the current prime, 3. It doesn't go in evenly. Go on to the next prime.
  • Take the value 41 and divide by the current prime, 5. It doesn't go in evenly. Go on to the next prime.
  • Take the value 41 and divide by the current prime, 7. It doesn't go in evenly. Go on to the next prime.
  • Take the value 41 and divide by the current prime, 11. It doesn't go in evenly. Go on to the next prime.
  • Take the value 41 and divide by the current prime, 13. It doesn't go in evenly. Go on to the next prime.
  • Take the value 41 and divide by the current prime, 17. It doesn't go in evenly. Go on to the next prime.
  • Take the value 41 and divide by the current prime, 19. It doesn't go in evenly. Go on to the next prime.
  • Take the value 41 and divide by the current prime, 23. It doesn't go in evenly. Go on to the next prime.
  • Take the value 41 and divide by the current prime, 29. It doesn't go in evenly. Go on to the next prime.
  • Take the value 41 and divide by the current prime, 31. It doesn't go in evenly. Go on to the next prime.
  • There are no more primes in the list. Output value 41, since it is not equal to 1. End the processing.

Make sure that you end the output line with a newline character. For aesthetic reasons, you could add a second newline character after printing the factors. Notice that this algorithms will entails a loops within another loop.

Now that the function is printing a prime factoring of the input value, make sure you update the expected value for your test cases.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Machine Performance Modeling Methodologies And Evaluation Strategies Lncs 257

Authors: Francesca Cesarini ,Silvio Salza

1st Edition

3540179429, 978-3540179429

More Books

Students also viewed these Databases questions