View the step-by-step solution to:

This there any different way to coding these? And also for def palindrome(number): is there different way to code?

This there any different way to coding these?

And also for def palindrome(number): is there different way to code?



Part 4.

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 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 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



def palindrome(number):

   reverse=0

   temp=number

   while(number>0):

       digit=number%10

       reverse=reverse*10+digit

       number=number//10

   if(temp==reverse):

       return "YES"

   else:

     return "NO"

 

def nearPrime(number):

   counter=0

   if(number>1):

       for i in range(2,number):

           if(number%i)==0:

               counter=counter+1

       if(counter<=2):

           return "YES"

       else:

           return "NO"

   else:

       return "NO" 


def determineNo(number):

 upNumber=False

 downNumber=False

 upDownNumber=False

 digit=[int(x) for x in str(number)]

 for i in range(0,len(digit)-1):

     if(digit[i]<=digit[i+1]):

         upNumber=True

     else:

         upNumber=False

         break

 for j in range(0,len(digit)-1):

     if(digit[j]>=digit[j+1]):

         downNumber=True

     else:

         downNumber=False

         break

 for k in range(0,len(digit)-1):

     if(digit[k]<=digit[k+1]):

         upDownNumber=True

     else:

         upDownNumber=False

         break

 for k in range(0,len(digit)-1):

     if(digit[k]>=digit[k+1]):

         upDownNumber=True

     else:

         upDownNumber=False

         break

 if(upNumber==True):

     return "YES"

 elif(downNumber==True):

     return "YES"

 elif(upDownNumber==True):

     return "YES"

 else:

     return "NO"


inp = input("Please enter the list of numbers: ")

lst = [int(x) for x in inp.split()]

# another list for storing scores

score = list(lst)

for i in range(0,len(lst)):

 # double the score if number is palindrome

 if(palindrome(lst[i])=="YES"):

     score[i]=score[i]*2

 # double the score if number is near prime

 if(nearPrime(lst[i])=="YES"):

     score[i]=score[i]*2

 # triple the score if number is nice

 if(determineNo(lst[i])=="YES"):

     score[i] =score[i]*3

# sort both list together on basis of scores of numbers

from operator import itemgetter

l1 , l2 = [list(x) for x in zip(*sorted(zip(score, lst), key=itemgetter(0)))]

for i in range(0, len(l1)):

   print(l2[i], ",", l1[i]) # print the final result

Top Answer

Different way to code palindrome: def palindrome... View the full answer

Sign up to view the full answer

Why Join Course Hero?

Course Hero has all the homework and study help you need to succeed! We’ve got course-specific notes, study guides, and practice tests along with expert tutors.

-

Educational Resources
  • -

    Study Documents

    Find the best study resources around, tagged to your specific courses. Share your own to gain free Course Hero access.

    Browse Documents
  • -

    Question & Answers

    Get one-on-one homework help from our expert tutors—available online 24/7. Ask your own questions or browse existing Q&A threads. Satisfaction guaranteed!

    Ask a Question
Ask a homework question - tutors are online