Skip to content

Commit 8f529ba

Browse files
authored
Update data_get_store
1 parent 2d05283 commit 8f529ba

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

data_get_store

+41
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,29 @@ def CompareTwoElements(x,y,order=0): # 0 means left smaller than right, 1 mea
5656

5757
def JudgeOrderForArray(array,order=0): # order 0 means small to large, 1 means large to small
5858
i = 0
59+
if(len(array)==0):
60+
return 1
5961
last_index = len(array)-1
6062
while(i<last_index and CompareTwoElements(array[i],array[i+1],order)):
6163
i = i+1
6264
return i==last_index
6365

66+
'''
67+
given a array, use following function to judge it is a ordered array or not
68+
if it is ordered, gives its order:
69+
0 represent small to large, 1 means large to small
70+
if it it not ordered, the order is None
71+
'''
72+
def JudgeAndGiveOrderForArray(array):
73+
i=0
74+
while((i<len(array)-1) and array[i]==array[i+1]):
75+
i = i+1
76+
remain_arr=array[i:]
77+
order = (0 if (i==(len(array)-1) or len(array)==0) else (array[i]>array[i+1]))
78+
is_order = JudgeOrderForArray(remain_arr,order)
79+
order_val = int(order) if is_order else None
80+
return bool(is_order),order_val
81+
6482
def OrderedDictReorder(ord_dict,index="key",rev=False):
6583
from collections import OrderedDict
6684
return OrderedDict(sorted(ord_dict.items(), key=lambda t:t[0] if index=="key" else t[1],reverse=rev))
@@ -72,6 +90,26 @@ def WriteNewDictDataToFile(ord_dict,txt_str=""):
7290
datafile.write("%d\t%.4f\n" % (index, ord_dict[index]))
7391
datafile.close()
7492

93+
'''
94+
before call this function, use above functions to reorder the new_data
95+
to a new txt file, and the old txt file has been reordred,
96+
so this function only need to judge the order and decide to merge or add
97+
the new txt data to old txt from which position(start or end)
98+
'''
99+
def AddNewDataToFile(old_data_txt="",add_data_txt="",new_data_txt=""):
100+
from collections import OrderedDict
101+
old_key_list = GetIndexToList(GetDataFromTxt(old_data_txt))
102+
add_key_list = GetIndexToList(GetDataFromTxt(add_data_txt))
103+
old_is_order,old_order_val = JudgeAndGiveOrderForArray(old_key_list)
104+
add_is_order,add_order_val = JudgeAndGiveOrderForArray(add_key_list)
105+
if(old_is_order==False):
106+
return ("Please check the data in %s, it is not ordered!" %s (old_data_txt))
107+
if(add_is_order==False):
108+
return ("Please check the data in %s, it is not ordered!" %s (add_data_txt))
109+
if(old_order_val!=1):
110+
return ("I hope the data in %s order is larger to small for date key!" %s (old_data_txt))
111+
if(add_order_val!=1):
112+
return ("I hope the data in %s order is larger to small for date key!" %s (add_data_txt))
75113

76114
if __name__ == "__main__":
77115
raw_data = GetDataFromTxt("xxx.txt")
@@ -111,3 +149,6 @@ if __name__ == "__main__":
111149

112150
#print(CompareTwoElements(1,1,0))
113151
#print(JudgeOrderForArray([4,3,3,2],1))
152+
153+
print(JudgeAndGiveOrderForArray([4,3,2,1,1,1]))
154+
print(JudgeAndGiveOrderForArray([1]))

0 commit comments

Comments
 (0)