daily pastebin goal
25%
SHARE
TWEET

Untitled

a guest Oct 17th, 2018 88 Never
Upgrade to PRO!
ENDING IN00days00hours00mins00secs
  1. import random
  2. import math
  3.  
  4.  
  5. def roundup(x):
  6.     return int(math.floor(x / 10.0)) * 10
  7.  
  8.  
  9. def get_key(element):
  10.     element = str(element).split(";")
  11.     return str(element[1]) + ";" + str(element[2]) + ";" + str(element[7]) + ";" + str(
  12.         element[13]) + ";" + str(element[14])
  13.  
  14.  
  15. def get_key_nosplit(element):
  16.     return str(element[1]) + ";" + str(element[2]) + ";" + str(element[7]) + ";" + str(
  17.         element[13]) + ";" + str(element[14])
  18.  
  19.  
  20. def get_intervals(data_local, categories_local):
  21.     intervals_local = {}
  22.  
  23.     for category in categories_local:
  24.         nums = []
  25.         for idx_local in range(0, len(data_local)):
  26.             row_local = data_local[idx_local]
  27.             if category == get_key(row_local).split(";")[1]:
  28.                 nums.append(int(str(row_local).split(";")[3]))
  29.         print("Interval for category: " + str(category) + " " + str(min(nums)) + " " + str(max(nums)))
  30.         intervals_local[category] = [min(nums), max(nums)]
  31.     return intervals_local
  32.  
  33.  
  34. def get_sale(dataset_local, key_local, intervals_local):
  35.     sales_local = {}
  36.     sales_local["elektronika_high"] = [0, 0, 0, 0, 0, 5, 5, 10, 10, 10, 10]
  37.     sales_local["elektronika_medium"] = [0, 0, 0, 0, 10, 10, 15, 15, 15, 15, 20, 20]
  38.     sales_local["elektronika_low"] = [0, 0, 0, 0, 20, 20, 25, 25, 25, 25, 30]
  39.     sales_local["oblecenie_high"] = [0, 0, 0, 0, 10, 15, 20, 25, 30]
  40.     sales_local["oblecenie_medium"] = [0, 0, 0, 0, 30, 35, 40, 45, 50]
  41.     sales_local["oblecenie_low"] = [0, 0, 0, 0, 50, 55, 60, 65, 70]
  42.     sales_local["kancelarske_potreby_high"] = [0, 0, 0, 0, 5, 5, 5, 10]
  43.     sales_local["kancelarske_potreby_medium"] = [0, 0, 0, 0, 10, 10, 10, 15]
  44.     sales_local["kancelarske_potreby_low"] = [0, 0, 0, 0, 15, 15, 15, 20]
  45.     count = 0
  46.     total = 0
  47.     for row_local in dataset_local:
  48.         row_local = row_local.split(";")
  49.         if get_key_nosplit(row_local) == key_local:
  50.             count += 1
  51.             #            print(row_local[3])
  52.             total = total + int(row_local[3])
  53.     category = key_local.split(";")[1]
  54.     interval_local = intervals_local[category]
  55.     range_local = int(interval_local[1]) - int(interval_local[0])
  56.     step = range_local / 3.0
  57.     # print("Category: " + str(category) + " step: " + str(step))
  58.     if count > 0:
  59.         average = total / count
  60.         #        print("Average: " + str(average))
  61.         if average >= interval_local[1] - step:
  62.             category += "_high"
  63.             return random.choice(sales_local[category])
  64.         elif average <= interval_local[0] + step:
  65.             category += "_low"
  66.             return random.choice(sales_local[category])
  67.         else:
  68.             category += "_medium"
  69.             return random.choice(sales_local[category])
  70.  
  71.  
  72. file = open('Data2.csv', newline='', encoding="utf-8")
  73. data = list(file.readlines())
  74. keys = []
  75.  
  76. for row in data:
  77.     keys.append(get_key(row))
  78.  
  79. del keys[0]
  80. keys = list(set(keys))
  81. print(len(keys))
  82. categories = []
  83. for key in keys:
  84.     categories.append(key.split(";")[1])
  85. categories = set(list(categories))
  86. intervals = get_intervals(data, categories)
  87. print(intervals)
  88. sales = {}
  89. data_edited = [data[0]]
  90. del data[0]
  91. for key in keys:
  92.     sales[key] = get_sale(data, key, intervals)
  93.  
  94. for idx in range(0, len(data)):
  95.     row = data[idx]
  96.     sale = sales[get_key(row)]
  97.     row = str(row).split(";")
  98.     row[4] = str(sale)
  99.     data_edited.append(row)
  100.  
  101. data_edited[0] = data_edited[0].split(";")
  102.  
  103. with open("data_new.csv", "w+", encoding="utf-8", newline='') as f:
  104.     for row in data_edited:
  105.         row = ";".join(row)
  106.         f.write(str(row))
  107.     f.close()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top