4
a = []
for i in range(3):
    a.append(input())
j = 0
for i in a:
   if i % 10 != 7:
       j = min(a)
print j

I need an algorithm which finds the smallest positive number in list, which decimal representation does not end with the number 7. It is guaranteed that the list has at least one positive element, which decimal representation does not end with the number 7. I tried this, but condition doesn't work. For example: it says that 7 is smallest in [9,8,7].

1
  • Have you learned floating point numbers already? Should these be taken into account? Also do note that min gives you the smallest number, not the smallest positive one. Commented Jun 7, 2014 at 14:20

2 Answers 2

5

You are always testing for the minimum number in a, albeit as many times as there are unfiltered numbers in a. Don't add numbers that end in 7 to a in the first place; you probably want to filter on positive numbers too:

a = []
for i in range(3):
    value = input()
    if i % 10 != 7 and i >= 0:
        a.append(value)

print min(a)

Alternatively, filter out values in a generator expression:

a = []
for i in range(3):
    a.append(input())

print min(i for i in a if i % 10 != 7 and i >= 0)
Sign up to request clarification or add additional context in comments.

1 Comment

Maybe indicate exactly how his code could be fixed - min(I,j) - instead of - min(a) - however yes, your solution is better.
0

EDIT: Ok I misreaded your code, you were using remainder which i think is better. Although it could work using just plain divisions like this.

if ((i / 10) - int(i / 10)) * 10 != 7:

And also, if you aren't using Python 3, you might need to use this for the above to work:

from __future__ import division

Or casting to floats with float(), but that makes it too messy.

1 Comment

From the code snippet he posted i think it's safe to assume he is not using python 3.x ;-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.