Malleoz

CMPSC121 Final Project

Apr 28th, 2018
337
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.53 KB | None | 0 0
  1. def is_palindrome(number):  # Determine if a number is the same forwards and backwards.
  2.     inputArray = []
  3.     for x in range(0, len(number)):  # Create an array where every element is one digit.
  4.         inputArray.append(number[x:x + 1])
  5.     reverseInputArray = inputArray[::-1]  # Creates a reversed array of the same number.
  6.     if reverseInputArray == inputArray:  # When the two arrays are the same, the number is the same backwards and forwards.
  7.         return True
  8.     return False
  9.  
  10.  
  11. def is_near_prime(number):  # Determine if a number has 4 or fewer divisors: at most itself, 1, and 2 other numbers.
  12.     divisors = 0  # For each time this function is called, reset number of divisors to 0.
  13.     for x in range(2, int(number) - 1):  # x counts up starting from 2 up until the number-1.
  14.         if int(number) % x == 0:  # If the number is divisble by x, then add 1 to the number of divisors.
  15.             divisors += 1
  16.     if divisors < 3:  # If the number is divisible by less than 3 other numbers, then it is near prime.
  17.         return True
  18.     return False  # If that condition is not met, then it is not near prime.
  19.  
  20.  
  21. def nice_number(number):  # Determine if the digits in the number either continuously count up, continuously count down, or count up and then down.
  22.     # This section determines if it is an up number.
  23.     maxDigit = -1
  24.     for x in range(0, len(number)):  # Goes through every digit in number starting from the leftmost digit.
  25.         if int(number[x:x+1]) >= maxDigit:  # If this specific digit is greater than or equal to the previous digit...
  26.             maxDigit = int(number[x:x+1])   # then make that the new greatest digit.
  27.             if x == len(number) - 1:        # However, if we've reached the last digit, we've confirmed that all the digits count up.
  28.                 return True
  29.         else:  # If the new digit is lower than the previous digit, then it is not an up number.
  30.             break
  31.  
  32.     # This section determines if it is a down number.
  33.     minDigit = 10
  34.     for x in range(0, len(number)):  # Goes through every digit in the number starting from the leftmost digit.
  35.         if int(number[x:x+1]) <= minDigit:  # If this specific digit is less than or equal to the previous digit...
  36.             minDigit = int(number[x:x+1])   # then make that the new lowest digit.
  37.             if x == len(number) - 1:        # However, if we've reached the last digit, we've confirmed that all the digits count down.
  38.                 return True
  39.         else:  # If the new digit is greater than the previous digit, then it is not an up number.
  40.             break
  41.  
  42.     # This section determines if it is an "updown" number.
  43.     maxDigit = -1
  44.     for x in range(0, len(number)):  # Goes through every digit in the number starting from the leftmost digit.
  45.         if int(number[x:x+1]) >= maxDigit:  # If this specific digit is greater than or equal to the previous digit...
  46.             maxDigit = int(number[x:x+1])   # then make that the new greatest digit.
  47.         else:  # However, if this specific digit is less than or equal to the previous digit, check to see if the following digits counts down.
  48.             minDigit = maxDigit  # This sets the minDigit in order to make sure that the next digit after this point is less than or equal to the previous (aka greatest) digit.
  49.             for y in range(x, len(number)):  # Goes through every digit in the number starting from the greatest digit.
  50.                 if int(number[y:y+1]) <= minDigit:  # If this specific digit is less than or equal to the previous digit...
  51.                     minDigit = int(number[y:y+1])   # then make that the new lowest digit.
  52.                     if y == len(number) - 1:        # However, if we've reached the last digit, we've confirmed that the digits count up and then down.
  53.                         return True
  54.                 else:  # If the new digit is greater than the previous digit, then it is not an updown number.
  55.                     return False
  56.     return False
  57.  
  58.  
  59. runProgram = True  # I will use this variable to determine when to quit out of the program.
  60. while runProgram:
  61.     print("Which part of the program do you need to check?")
  62.     part = int(input(
  63.         "1 - Determine if a number is near prime\n2 - Sort a large list and ask for number and score at a list of certain positions"))
  64.  
  65.     if part == 1:  # This will check to see if a number is near prime.
  66.         numbersInput = input(
  67.             "\nEnter a list of numbers, separated by commas, you wish to determine if each number is near prime.")
  68.         numbersArray = numbersInput.split(',')  # This takes the numbers the user entered and splits each number into a separate entry in a list.
  69.         for x in range(0, len(numbersArray)):  # This loop goes through each element in the list.
  70.             if is_near_prime(numbersArray[x]):  # Determines if the number is near prime.
  71.                 print(numbersArray[x], "is near prime.")
  72.             else:
  73.                 print(numbersArray[x], "is not near prime.")
  74.  
  75.     if part == 2:
  76.         numberInput = input("\nEnter a list of numbers separated by commas.")
  77.         numberArray = numberInput.split(',')  # This takes the numbers the user entered and splits each number into a separate entry in a list.
  78.         score = []  # Empty this list if this part is run more than once without quitting out. This list will keep track of all scores.
  79.         origNumber = {}  # Empty a dictionary. This dictionary will allow me to reference a number based on its score.
  80.         for x in range(0, len(numberArray)):  # This runs through each number in the list.
  81.             currentScore = 0  # Reset this variable to 0 if this part is run more than once without quitting out. This variable will keep track of the calculated score for the current number.
  82.             currentScore += int(numberArray[x])  # Each number's score begins at the number's value.
  83.             if is_palindrome(numberArray[x]):  # If the number is a palindrome, double the score.
  84.                 currentScore *= 2
  85.             if is_near_prime(numberArray[x]):  # If the number is near prime, double the score.
  86.                 currentScore *= 2
  87.             if nice_number(numberArray[x]):  # If the number is a nice number, triple the score.
  88.                 currentScore *= 3
  89.             score.append(currentScore)  # Add the total score for this number to the score list.
  90.             origNumber[score[x]] = numberArray[x]  # Create a dictionary entry, in which a specific score references its original number.
  91.  
  92.         for x in range(1, len(score)):  # This for loop sorts the list of scores.
  93.             currentScore = score[x]
  94.             currentIndex = x
  95.             while currentIndex > 0 and score[currentIndex - 1] > currentScore:
  96.                 score[currentIndex] = score[currentIndex - 1]
  97.                 currentIndex -= 1
  98.             score[currentIndex] = currentScore
  99.  
  100.         positions = input("Enter the positions you want to look at, separated by commas")
  101.         positionsList = positions.split(',')  # This takes the numbers the user entered and splits each number into a separate entry in a list.
  102.         for x in range(0, len(positionsList)):  # This for loop spits out the number and its corresponding score in a position x.
  103.             print("Position", positionsList[x], "-",
  104.                   str(origNumber[score[int(positionsList[x])]]) + " with a score of " + str(
  105.                       score[int(positionsList[x])]))
  106.  
  107.     choice = input("\nWould you like to quit the program now? Type y to quit")
  108.     print()
  109.     if choice == 'y':
  110.         runProgram = False
Advertisement
Add Comment
Please, Sign In to add comment