Malleoz

Untitled

Apr 17th, 2018
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.55 KB | None | 0 0
  1. # Part 1
  2. # Determine if a number is the same forwards and backwards.
  3. def IsPalindrome(number):
  4.     input = str(number)
  5.     inputarray = []
  6.     reverseinputarray = []
  7.     for x in range (0, len(input)):
  8.         inputarray.append(input[x:x+1])
  9.     for x in range(len(input),0,-1):
  10.         reverseinputarray.append(inputarray[x-1])
  11.     if reverseinputarray == inputarray:
  12.         return True
  13.     return False
  14.  
  15. # Part 2
  16. # Determine if a number has 3 or fewer divisors: at most itself, 1, and some other number.
  17. def IsNearPrime(number):
  18.     divisors = 0
  19.     for x in range(1, number+1):
  20.         if number%x == 0:
  21.             divisors+=1
  22.     if divisors>=3:
  23.         return False
  24.     return True
  25.  
  26. # Part 3
  27. # Determine if the digits in the number either continuously count up, continuously count down, or count up and then down.
  28. def NiceNumber(number):
  29.     # This section determines if it is an up number.
  30.     input = str(number)
  31.     maxDigit = -1
  32.     inputarray = []
  33.     for x in range (0, len(input)):
  34.         inputarray.append(input[x:x+1])
  35.     for x in range(0,len(input)):
  36.         if int(inputarray[x]) > maxDigit:
  37.             maxDigit = int(inputarray[x])
  38.             if x == len(input)-1:
  39.                 return True
  40.  
  41.     # This section determines if it is a down number.
  42.     minDigit = 10
  43.     inputarray = []
  44.     for x in range (0, len(input)):
  45.         inputarray.append(input[x:x+1])
  46.     for x in range (0, len(input)):
  47.         if int(inputarray[x]) < minDigit:
  48.             minDigit = int(inputarray[x])
  49.             if x == len(input)-1:
  50.                 return True
  51.  
  52.     # This section determines if it is an updown number.
  53.     maxDigit = -1
  54.     for x in range(0, len(input)):
  55.         if int(inputarray[x]) > maxDigit:
  56.             maxDigit = int(inputarray[x])
  57.         else:
  58.             for y in range(x, len(input)):
  59.                 minDigit = maxDigit
  60.                 if int(inputarray[y]) < minDigit:
  61.                     minDigit = int(inputarray[y])
  62.                     if y == len(input)-1:
  63.                         return True
  64.             break
  65.     return False
  66.  
  67. # Part 4
  68. # Put it all together!
  69. print("Enter a number. Type STOP to end.")
  70. number = input()
  71. numberArray = []
  72. while number != "STOP":
  73.     number = int(number)
  74.     numberArray.append(number)
  75.     number = input()
  76. currentScore = 0
  77. score = []
  78. origNumber = {}
  79. for x in range(0, len(numberArray)):
  80.     currentScore = 0
  81.     currentScore+=int(numberArray[x])
  82.     if IsPalindrome(numberArray[x]):
  83.         currentScore*=2
  84.     if IsNearPrime(numberArray[x]):
  85.         currentScore*=2
  86.     if NiceNumber(numberArray[x]):
  87.         currentScore*=3
  88.     score.append(currentScore)
  89.  
  90. # 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.
  91. for x in range(0, len(score)):
  92.     origNumber[score[x]] = numberArray[x]
  93.  
  94. # Sorts the score array.
  95. for x in range(1, len(score)):
  96.     currentScore = score[x]
  97.     currentIndex = x
  98.     while currentIndex>0 and score[currentIndex-1] > currentScore:
  99.         score[currentIndex]=score[currentIndex-1]
  100.         currentIndex-=1
  101.     score[currentIndex] = currentScore
  102.  
  103. # Combines arrays together such that the number is in the index before its score.
  104. for x in range(0, len(score)+1,2):
  105.     print("Inserting",origNumber[score[x]], "in front of", score[x])
  106.     score.insert(x,origNumber[score[x]])
  107. print(score)
  108.  
  109. for x in range(0, len(score), 2):
  110.     print(str(score[x]) + "," + str(score[x+1]))
Advertisement
Add Comment
Please, Sign In to add comment