-5

Alogrithm 1:

Get a list of numbers L1, L2, L3....LN as argument
Assume L1 is the largest, Largest = L1
Take next number Li from the list and do the following
If Largest is less than Li
Largest = Li
If Li is last number from the list then
return Largest and come out
Else repeat same process starting from step 3

Algorithm 2:

Create a function prime_number that does the following
Takes as parameter an integer and
Returns boolean value true if the value is prime or
Returns boolean value false if the value is not prime

So far my code is :

def get_algorithm_result(num_list):    
    largest =num_list[0]        
    for item in range(0,len(num_list)):    
        if largest < num_list[item]:                
            largest = num_list[item]    
    return largest

def prime_number(integer):    
    if integer%2==0:
        return False
    else:
        return True

After executing the code i get

"Test Spec Failed

Your solution failed to pass all the tests" 

where am I going wrong?

shapiro yaacov
  • 2,308
  • 2
  • 26
  • 39
philip
  • 1
  • 1
  • 5
  • 5
    your prime_number function just checks for divisibility by 2??? – astrosyam Jan 06 '16 at 08:56
  • what prime_number do in another function? – ᴀʀᴍᴀɴ Jan 06 '16 at 08:56
  • 1
    Obviously your `prime_number` function does not really check for prime numbers, just for odd numbers. – Klaus D. Jan 06 '16 at 08:56
  • I can't quite understand what you are trying to achieve with those methods. Please elaborate it. – Rohit Rawat Jan 06 '16 at 08:58
  • suppose i change prime_number function by adding :statement ...............if integer%2==0 and 2!=integer: would it work – philip Jan 06 '16 at 08:59
  • If an integer is odd, and not 2, is it prime? No. – MeetTitan Jan 06 '16 at 09:03
  • No...it will not. Prime number is not divisible by **any** number other than 1 and itself. The most naive way would be to check divisibility by all numbers in range `2..(n-1)` – vish4071 Jan 06 '16 at 09:04
  • works fine after adding.Thanks – philip Jan 06 '16 at 09:04
  • @phil a prime is divisible by just 1 and itself..so maybe see the given integer is divisible by other numbers? If so return False....btw you don't really need to check the divisibility with all the other integers till the given integer..but I will let you figure that one out – astrosyam Jan 06 '16 at 09:05
  • Read [this SO post](http://stackoverflow.com/questions/1801391/what-is-the-best-algorithm-for-checking-if-a-number-is-prime) and the [wikipedia entry](https://en.wikipedia.org/wiki/Primality_test) on primality testing. – Reti43 Jan 06 '16 at 09:14
  • @astrosyam i finally figured it out.@ Reti43 thanks for the info also : – philip Jan 06 '16 at 09:53
  • def prime_number(integer): if integer>=1: for i in range(2,integer): if (integer % i) == 0: return False break else: return True – philip Jan 06 '16 at 10:19

3 Answers3

0

Is what you mean with the first one find the largest number? Then you should use max() like

list = [1,2,4,5,3]
print max(list)
>>> 5

This should help with the second one:

def prime_number(n):

    if n > 1:
       for x in range(2,n):
           if (n % x) == 0:
               return False
               break
       else:
           return True

If a number is prime, then the factors are only 1 and itself. If there is any other factor from 2 to the number, it is not prime. n % x finds the remainder when n is divided by x. If x is a factor of n, then n % x has a remainder of 0.

Dave Lin
  • 68
  • 1
  • 1
  • 8
0
def get_algorithm_result(numbers):
  largest = numbers[0]
  for i in numbers:
    if largest < i:
      largest = i
return largest

and

def prime_number(number):
  if number > 1:
    for i in range(2, number):
      if (number % i) == 0:
        return False
      else:
        return True
  else:
    return False
Ezekiel
  • 167
  • 3
  • 11
0
def get_algorithm_result(numb):
  largest = numb[0]
  for Li in numb:
    if largest < Li:
      largest = Li
    if Li == numb[-1]:
      return largest
    else:
      continue

def prime_number(primes):
  if primes > 1:
    for i in range(3, primes):
      if (primes % i) == 0:
        return False
      else:
        return True
  else:
    return False
Bo Persson
  • 90,663
  • 31
  • 146
  • 203