Ad

Given up to 3 non-repeating numbers from a range of 1-10, identify any prime numbers where 1 <= prime_numbers <= n where n is the number given by the concatenation of the given numbers put together in the order they were chosen. So given the numbers [1, 10, 8] is 1108.
Return the midmost prime number in the list you generated before.
If there are an even number of prime numbers, get the lower number.

Code
Diff
  • def middle_prime(*args):
      nArg = 3
      minArgValue = 1
      maxArgValue = 10
      argsLen = len(args)
      try:
        if argsLen > nArg:
          raise Exception('Only accepts up to {} params.'.format(nArg))
        elif argsLen < 1:
          raise Exception('At least 1 param is required.')
        else:
          for i in args:
            if not minArgValue <= i <= maxArgValue:
              raise Exception('Only accepts numbers from {} to {}.'.format(minArgValue, maxArgValue))
        num = int(''.join([str(i) for i in args]))
        primes = generate_primes(num)
        if primes:
          mididx = len(primes) // 2 if len(primes) % 2 else len(primes) // 2 - 1
          return primes[mididx]
        return -1
      except Exception as e:
        print(e)
        return -1
    
    def is_prime(number:int, primes = [2,3]):
      if not primes:
        primes = [2, 3]
      for i in primes:
        if not number % i:
          return False
      if not i % 2:
        i += 1
      while i <= (number // i):
        if not number % i:
          return False
        i += 2
      return True
      
    def generate_primes(number):
      primes = [2]
      i = 3
      while i <= number:
        if is_prime(i, primes):
          primes.append(i)
        i += 2
      return primes
    • def middle_prime(*args):
    • maximum = 10
    • try:
    • if len(args) > 3:
    • raise Exception('Only 3 numbers are allowed.')
    • else:
    • for i in args:
    • if not 0 < i <= maximum:
    • raise Exception('Only type numbers from 1 to {}.'.format(maximum))
    • nArg = 3
    • minArgValue = 1
    • maxArgValue = 10
    • argsLen = len(args)
    • try:
    • if argsLen > nArg:
    • raise Exception('Only accepts up to {} params.'.format(nArg))
    • elif argsLen < 1:
    • raise Exception('At least 1 param is required.')
    • else:
    • for i in args:
    • if not minArgValue <= i <= maxArgValue:
    • raise Exception('Only accepts numbers from {} to {}.'.format(minArgValue, maxArgValue))
    • num = int(''.join([str(i) for i in args]))
    • primes = generate_primes(num)
    • if primes:
    • mididx = len(primes) // 2 if len(primes) % 2 else len(primes) // 2 - 1
    • return primes[mididx]
    • return -1
    • except Exception as e:
    • print(e)
    • return -1
    • num = int(''.join([str(i) for i in args]))
    • primes = [i for i in range(2, num + 1) if is_prime(i)]
    • if primes:
    • mididx = len(primes) // 2 if len(primes) % 2 else len(primes) // 2 - 1
    • return primes[mididx]
    • return -1
    • except Exception as e:
    • return e
    • def is_prime(int:int):
    • for i in range(2, int):
    • if not int % i:
    • return False
    • return True
    • def is_prime(number:int, primes = [2,3]):
    • if not primes:
    • primes = [2, 3]
    • for i in primes:
    • if not number % i:
    • return False
    • if not i % 2:
    • i += 1
    • while i <= (number // i):
    • if not number % i:
    • return False
    • i += 2
    • return True
    • def generate_primes(number):
    • primes = [2]
    • i = 3
    • while i <= number:
    • if is_prime(i, primes):
    • primes.append(i)
    • i += 2
    • return primes