Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def is_palindrome(number): # Determine if a number is the same forwards and backwards.
- inputArray = []
- for x in range(0, len(number)): # Create an array where every element is one digit.
- inputArray.append(number[x:x + 1])
- reverseInputArray = inputArray[::-1] # Creates a reversed array of the same number.
- if reverseInputArray == inputArray: # When the two arrays are the same, the number is the same backwards and forwards.
- return True
- return False
- def is_near_prime(number): # Determine if a number has 4 or fewer divisors: at most itself, 1, and 2 other numbers.
- divisors = 0 # For each time this function is called, reset number of divisors to 0.
- for x in range(2, int(number) - 1): # x counts up starting from 2 up until the number-1.
- if int(number) % x == 0: # If the number is divisble by x, then add 1 to the number of divisors.
- divisors += 1
- if divisors < 3: # If the number is divisible by less than 3 other numbers, then it is near prime.
- return True
- return False # If that condition is not met, then it is not near prime.
- def nice_number(number): # Determine if the digits in the number either continuously count up, continuously count down, or count up and then down.
- # This section determines if it is an up number.
- maxDigit = -1
- for x in range(0, len(number)): # Goes through every digit in number starting from the leftmost digit.
- if int(number[x:x+1]) >= maxDigit: # If this specific digit is greater than or equal to the previous digit...
- maxDigit = int(number[x:x+1]) # then make that the new greatest digit.
- if x == len(number) - 1: # However, if we've reached the last digit, we've confirmed that all the digits count up.
- return True
- else: # If the new digit is lower than the previous digit, then it is not an up number.
- break
- # This section determines if it is a down number.
- minDigit = 10
- for x in range(0, len(number)): # Goes through every digit in the number starting from the leftmost digit.
- if int(number[x:x+1]) <= minDigit: # If this specific digit is less than or equal to the previous digit...
- minDigit = int(number[x:x+1]) # then make that the new lowest digit.
- if x == len(number) - 1: # However, if we've reached the last digit, we've confirmed that all the digits count down.
- return True
- else: # If the new digit is greater than the previous digit, then it is not an up number.
- break
- # This section determines if it is an "updown" number.
- maxDigit = -1
- for x in range(0, len(number)): # Goes through every digit in the number starting from the leftmost digit.
- if int(number[x:x+1]) >= maxDigit: # If this specific digit is greater than or equal to the previous digit...
- maxDigit = int(number[x:x+1]) # then make that the new greatest digit.
- else: # However, if this specific digit is less than or equal to the previous digit, check to see if the following digits counts down.
- 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.
- for y in range(x, len(number)): # Goes through every digit in the number starting from the greatest digit.
- if int(number[y:y+1]) <= minDigit: # If this specific digit is less than or equal to the previous digit...
- minDigit = int(number[y:y+1]) # then make that the new lowest digit.
- if y == len(number) - 1: # However, if we've reached the last digit, we've confirmed that the digits count up and then down.
- return True
- else: # If the new digit is greater than the previous digit, then it is not an updown number.
- return False
- return False
- runProgram = True # I will use this variable to determine when to quit out of the program.
- while runProgram:
- print("Which part of the program do you need to check?")
- part = int(input(
- "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"))
- if part == 1: # This will check to see if a number is near prime.
- numbersInput = input(
- "\nEnter a list of numbers, separated by commas, you wish to determine if each number is near prime.")
- numbersArray = numbersInput.split(',') # This takes the numbers the user entered and splits each number into a separate entry in a list.
- for x in range(0, len(numbersArray)): # This loop goes through each element in the list.
- if is_near_prime(numbersArray[x]): # Determines if the number is near prime.
- print(numbersArray[x], "is near prime.")
- else:
- print(numbersArray[x], "is not near prime.")
- if part == 2:
- numberInput = input("\nEnter a list of numbers separated by commas.")
- numberArray = numberInput.split(',') # This takes the numbers the user entered and splits each number into a separate entry in a list.
- score = [] # Empty this list if this part is run more than once without quitting out. This list will keep track of all scores.
- origNumber = {} # Empty a dictionary. This dictionary will allow me to reference a number based on its score.
- for x in range(0, len(numberArray)): # This runs through each number in the list.
- 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.
- currentScore += int(numberArray[x]) # Each number's score begins at the number's value.
- if is_palindrome(numberArray[x]): # If the number is a palindrome, double the score.
- currentScore *= 2
- if is_near_prime(numberArray[x]): # If the number is near prime, double the score.
- currentScore *= 2
- if nice_number(numberArray[x]): # If the number is a nice number, triple the score.
- currentScore *= 3
- score.append(currentScore) # Add the total score for this number to the score list.
- origNumber[score[x]] = numberArray[x] # Create a dictionary entry, in which a specific score references its original number.
- for x in range(1, len(score)): # This for loop sorts the list of scores.
- currentScore = score[x]
- currentIndex = x
- while currentIndex > 0 and score[currentIndex - 1] > currentScore:
- score[currentIndex] = score[currentIndex - 1]
- currentIndex -= 1
- score[currentIndex] = currentScore
- positions = input("Enter the positions you want to look at, separated by commas")
- positionsList = positions.split(',') # This takes the numbers the user entered and splits each number into a separate entry in a list.
- for x in range(0, len(positionsList)): # This for loop spits out the number and its corresponding score in a position x.
- print("Position", positionsList[x], "-",
- str(origNumber[score[int(positionsList[x])]]) + " with a score of " + str(
- score[int(positionsList[x])]))
- choice = input("\nWould you like to quit the program now? Type y to quit")
- print()
- if choice == 'y':
- runProgram = False
Advertisement
Add Comment
Please, Sign In to add comment