Skip to content

Instantly share code, notes, and snippets.

@pedrogrande
Created March 12, 2017 06:18
Show Gist options
  • Save pedrogrande/05a2d39635dd73fbc4a7ecb5a563f060 to your computer and use it in GitHub Desktop.
Save pedrogrande/05a2d39635dd73fbc4a7ecb5a563f060 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
L = [9,1,1,2,2,2,2,2,3,4,4,5,6,6,6,4,4,4,4,4,7,9,4,9]
length_of_array = len(L)
print("Length of array: %d" % length_of_array)
# temp_con {}
con = {}
array_of_con = []
i = 0
k = 0
while (i < (length_of_array - 1)):
first_item = L[i]
second_item = L[i+1]
con[k] = con.get(k, 1)
if first_item == second_item:
con[k] = con.get(k) + 1
else:
k = k + 1
array_of_con.append(first_item)
i = i + 1
print "==========="
# print hash of consecutive number groups
print "Value of con dictionary: "
print(con)
print "==========="
# consecutive number groups by count of consecutive numbers
con_values = con.values()
print "con_values: "
print(con_values)
print "==========="
# find max value in con_values
max_value = max(con_values)
print("Highest number in con_values")
print(max(con_values))
# find number of occurences of max value in con_values
num_occurances = [i for i, w in enumerate(con_values) if w == max_value]
print("Index/indices in con_values where highest number occurs")
print(num_occurances)
print("array_of_con: ")
print(array_of_con)
# make new array of just values in consecutive groups of numbers
new_array = []
o = 0
while o < len(num_occurances):
new_array.append(array_of_con[num_occurances[o]])
o = o + 1
print(new_array)
lowest_value = min(new_array)
print("Lowest value of consecutive numbers")
print(lowest_value)
# if first and last number in L are the same, find number of consecutive numbers in the first and last places in array_of_con
length_of_con_values = len(con_values)
if L[0] == L[len(L) - 1]:
consecutive_numbers_of_first_and_last = con_values[0] + con_values[length_of_con_values - 1]
print("Number of consecutive occurences of first and last numbers in L")
print(consecutive_numbers_of_first_and_last)
else:
print('First and last numbers in L are not the same')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment