2

For example, if I have the number 14731, I found how to convert it to a list of integers and how to find the highest integer. From here, how would I split the list [1,4,7,3,1] to be [1,4,7] and [3,1]?

Matt
  • 129
  • 5
  • 1
    Is `14731` a string or an integer initially? – TerryA Apr 29 '18 at 00:41
  • 1
    Should say, an integer such as `1473731` be split like `[14737]` and `[31]`? Or `[147]`, `[37]`, `[31]`? Or `[147]`, `[3731]`? i.e, how should the duplicate of the maximum integer be handled? Should an int always be split into two lists if there is more than instance of the max int? What if it is two integers but of the same value, like `11` or `99`? Should these be split into two lists like `[9]` and `[9]`? – Walid Mujahid وليد مجاهد Apr 29 '18 at 00:49

1 Answers1

7
x = 14731 
ls = [int(i) for i in str(x)]

maxIndex = ls.index(max(ls))
ls1, ls2 = ls[:maxIndex+1], ls[maxIndex+1:]
print(ls1, ls2)
# [1, 4, 7] [3, 1]

This example uses slice notation to cut the list into two halves, based on the index of the maximum value.

StardustGogeta
  • 3,331
  • 2
  • 18
  • 32