Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

PLEASE ONLY DO QUESTION 2 IN PYTHON in grading homework 5 I noticed a variety of solutions to question 1 0 ( print all numbers

PLEASE ONLY DO QUESTION 2 IN PYTHON in grading homework 5 I noticed a variety of solutions to question 10(print all numbers between 1000
and 3000, inclusive, where each digit is even). I appreciate questions like this for it allows me to see a
range of thinking on how to solve a problem. Here are two solutions that were given:
print(*[n for n in range(1000,3001) if all([(int(c)%2==0) for c in str(n)])], sep=',')
for i in range(1000,3001):
is_even = true
for j in str(i):
if int(j)%2==1:
is_even = false
if is_even:
numbers.append(i)
print(numbers)
The second solution takes more coding but essentially does the same thing. Part of writing software
(or doing analysis, at least) is identifying patterns of what needs to be done. Making a problem
generic can sometimes help in this regard (find numbers between x and y, etc.). Looking at a
problem in a different way (thinking outside of the box) can provide a unique solution as well. One
thing to always remember is that just because something is a number doesn't mean it has to be
treated as a number. Think of phone numbers or social security numbers. While they contain only
digits, one almost never performs mathematical operations against them so they could just as easily
be strings, and probably should be.
The two solutions above are more efficient than examining every number between 1000(or x) and
3000(or y), but they could be more efficient yet. Think about what problem we're trying to solve. The
first solution is probably a good one, but is there a better one? If the 10's position contains an odd
number, does it matter what the digit is in the one's position? No, so we should move to the next 10's
position value. The same holds true for 100's and 1000's positions.
Q1[10 pts]. Determine how to time programs in Python. Run 3 tests of each of the first two solutions
given above and average the results. Come up with a solution that is more efficient (faster). Run 3
CIS 298
2
tests on it and record the average. Supply your faster algorithm and the average result of all three
algorithms.
Q2[50]. Deep dive into logically cutting corners in a solution through analysis. Find a solution to this
problem that executes in under 1 second (can it be even faster?) without hardcoding the
answer. Displaying values takes a long time so remove most output statements once youre getting
close. I'll spare you the long version of the question. Simply do this: find the price of four items who's
sum and who's product both equal 7.11. So, a+b+c+d == a*b*c*d ==7.11. There may be more than
one answer. I originally solved this problem 42 years ago (in C). My initial algorithm ran for over 12
hours. My deep dive analysis produced one that solved it in about 1 second. Supply your final
algorithm and how long it takes to solve the problem (using the average of three runs).

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 Processing

Authors: David Kroenke

11th Edition

0132302675, 9780132302678

More Books

Students also viewed these Databases questions