***** IN PYTHON ***** NEAR PRIME (definition): We call a number near prime if it
ID: 3719693 • Letter: #
Question
***** IN PYTHON *****
NEAR PRIME (definition): We call a number near prime if it is divisible by 1, itself and at most 2 other numbers. To illustrate: 49 is near prime as only 1,7 and 49 divide it.
"NICE": We call a number an up number if the digits of the number increase (or stay the same) as we read the number. To illustrate: 123 is an up number Similarly, in a down number the digits of the number decrease (or stay the same). To illustrate 321 is a down number. Finally combining the two we define an updown number. In an updown number the digits initially increase (or stay the same) and then they decrease (or stay the same). Once they have started to decrease they can not increase again. To illustrate 123431 is an updown number, 4789089 is not an updown number. We consider a number “nice” if its either an up, a down or an updown number.
You will be given a list of numbers. Each number is assigned a score. Its score is determined as follows:
A number’s base score is the value of the number itself. If the number is a palindrome it scores is doubled. If the number is a near prime its score is doubled. If the number is a nice number its score is tripled.
Consider how to determine the score of a number 89. It is not a palindrome and but it is nice and near prime:
Base score: 89
Score after palindrome check: 89 (remains same as not a palindrome) Score after near prime check: 188 (doubled as 89 is near prime) Score after nice check: 534 (tripled as 89 is nice)
Consider the number 101: It is a palindrome, it is prime but it is not a nice number: Base score: 101 Score after palindrome check: 202 (doubled as it’s a palindrome) Score after near prime check: 404 (doubled as 101 a near prime) Score after nice check: 404 (not tripled as 101 is not nice)
Given a list of numbers entered by the user output the list of numbers in sorted order (ascending) of their scores. Also include the score achieved with each number separated by a comma after the number. For example if the user enters 89 101 You have to output 101,404 89, 534
***** Does anyone have a working code for this? I need it as a reference for an exam. Thanks. *****
Explanation / Answer
This code has been tested on python3. You just need to call the solve method with a list of input numbers as an argument.
import math
def getDivisors(n) :
# Note that this loop runs till square root
i = 1
div = 0
while i <= math.sqrt(n):
if (n % i == 0) :
# If divisors are equal, print only one
if (n / i == i) :
div+=1
else :
# Otherwise print both
div+=2
i = i + 1
return div
def isUp(n):
l = -2**31+1
for c in str(n):
if int(c) <l:
return False
l = int(c)
return True
def isUpDown(n):
check = isUp(int(str(n)[::-1])) or isUp(n)
if check:
return True
l = -2**31+1
for i in range(len(str(n))):
s = str(n)
if int(s[i])<l:
k = i
break
l = int(s[i])
return isUp(str(n)[:k]) and isUp(str(n)[k:][::-1])
def ispal(n):
return str(n)==str(n)[::-1]
def gen_score(n):
score = n
if ispal(n):
score=score*2
if getDivisors(n)<=4:
score=score*2
if isUpDown(n):
score=score*3
return score
def solve(numbers):
store = {}
for n in numbers:
store[gen_score(n)]=n
keys = sorted(list(store.keys()))
for k in keys:
print('{},{}'.format(store[k], k) , end=' ')
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.