Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Part 1
- # Determine if a number is the same forwards and backwards.
- def IsPalindrome(number):
- input = str(number)
- inputarray = []
- reverseinputarray = []
- for x in range (0, len(input)):
- inputarray.append(input[x:x+1])
- for x in range(len(input),0,-1):
- reverseinputarray.append(inputarray[x-1])
- if reverseinputarray == inputarray:
- return True
- return False
- # Part 2
- # Determine if a number has 3 or fewer divisors: at most itself, 1, and some other number.
- def IsNearPrime(number):
- divisors = 0
- for x in range(1, number+1):
- if number%x == 0:
- divisors+=1
- if divisors>=3:
- return False
- return True
- # Part 3
- # Determine if the digits in the number either continuously count up, continuously count down, or count up and then down.
- def NiceNumber(number):
- # This section determines if it is an up number.
- input = str(number)
- maxDigit = -1
- inputarray = []
- for x in range (0, len(input)):
- inputarray.append(input[x:x+1])
- for x in range(0,len(input)):
- if int(inputarray[x]) > maxDigit:
- maxDigit = int(inputarray[x])
- if x == len(input)-1:
- return True
- # This section determines if it is a down number.
- minDigit = 10
- inputarray = []
- for x in range (0, len(input)):
- inputarray.append(input[x:x+1])
- for x in range (0, len(input)):
- if int(inputarray[x]) < minDigit:
- minDigit = int(inputarray[x])
- if x == len(input)-1:
- return True
- # This section determines if it is an updown number.
- maxDigit = -1
- for x in range(0, len(input)):
- if int(inputarray[x]) > maxDigit:
- maxDigit = int(inputarray[x])
- else:
- for y in range(x, len(input)):
- minDigit = maxDigit
- if int(inputarray[y]) < minDigit:
- minDigit = int(inputarray[y])
- if y == len(input)-1:
- return True
- break
- return False
- # Part 4
- # Put it all together!
- print("Enter a number. Type STOP to end.")
- number = input()
- numberArray = []
- while number != "STOP":
- number = int(number)
- numberArray.append(number)
- number = input()
- currentScore = 0
- score = []
- origNumber = {}
- for x in range(0, len(numberArray)):
- currentScore = 0
- currentScore+=int(numberArray[x])
- if IsPalindrome(numberArray[x]):
- currentScore*=2
- if IsNearPrime(numberArray[x]):
- currentScore*=2
- if NiceNumber(numberArray[x]):
- currentScore*=3
- score.append(currentScore)
- # This for loop creates entries in a dictionary that pair each score to its original number. This way, I can sort the scores, and then re-insert the original numbers by keeping track of them through the dictionary.
- for x in range(0, len(score)):
- origNumber[score[x]] = numberArray[x]
- # Sorts the score array.
- for x in range(1, len(score)):
- currentScore = score[x]
- currentIndex = x
- while currentIndex>0 and score[currentIndex-1] > currentScore:
- score[currentIndex]=score[currentIndex-1]
- currentIndex-=1
- score[currentIndex] = currentScore
- # Combines arrays together such that the number is in the index before its score.
- for x in range(0, len(score)+1,2):
- print("Inserting",origNumber[score[x]], "in front of", score[x])
- score.insert(x,origNumber[score[x]])
- print(score)
- for x in range(0, len(score), 2):
- print(str(score[x]) + "," + str(score[x+1]))
Advertisement
Add Comment
Please, Sign In to add comment