Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from math import sqrt
- 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(sqrt(int(number))) + 1): # x counts up starting from 2 up until the square root of the number. This is more efficient than checking for divisors above the sqrt of the number.
- if int(number) % x == 0: # If the number is divisble by x, then add 1 to the number of divisors.
- if x*x == number:
- divisors += 1 # This is the square root of the number, so that only counts as one divisor as it does not have any other divisor that can be multiplied to produce the number.
- else:
- divisors += 2 # In the event where x is not the sqrt of the number, there is always another divisor that can be multiplied to produce the number.
- 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.
- maxDigit = int(number[0:1]) # Sets the current maximum number to the first digit of the number.
- 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.
- if x == len(number) -1: # I can immediately return True if it counts upwards to the very last digit of the number. This considers the number an "upnumber."
- return True
- 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 down from this point forward.
- return True # This therefore confirms that the number is EITHER an updown or a down number. For the sake of this project, I don't need to know which specifically.
- else: # If the new digit is greater than the previous digit, then it is not an updown number.
- 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.
- scoreSorted = sorted(score, key=int)
- 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[scoreSorted[int(positionsList[x])]]) + " with a score of " + str(
- scoreSorted[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