svgroz

Untitled

Mar 23rd, 2016
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.64 KB | None | 0 0
  1. class DisplayOrder:
  2.     def __init__(self, number, description):
  3.         self.number = number
  4.         self.description = description
  5.  
  6. def switch1(listToSwitch, source, target):
  7.     temp1 = listToSwitch[source]
  8.     temp2 = listToSwitch[target]
  9.     listToSwitch[source] = temp2
  10.     listToSwitch[target] = temp1
  11.  
  12. def sort(listToSort, switch):
  13.     res = listToSort
  14.     for i in range(0, len(res)):
  15.         if (res[i].description == switch and i + 1 < len(res) and res[i].number == res[i + 1].number):
  16.             switch1(res, i, i + 1)
  17.             break
  18.         elif (i > 0 and res[i].number == res[i - 1].number):
  19.             switch1(res, i, i - 1)
  20.             break
  21.     return res
  22.  
  23. def smartSort(listToSort, switch):
  24.     sortedList = sort(listToSort, switch)
  25.     for i in range(0, len(sortedList) - 1):
  26.         delta = (sortedList[i].number + 1) - sortedList[i + 1].number
  27.         sortedList[i + 1].number = sortedList[i + 1].number + delta
  28.     return sortedList
  29.  
  30. def stupidSort(listToSort, switch):
  31.     sortedList = sort(listToSort, switch)
  32.     for i in range(0, len(sortedList)):
  33.         if (sortedList[i].description != switch):
  34.             sortedList[i].number = i + 1   
  35.     return sortedList
  36.  
  37. foo = [
  38.     DisplayOrder(1, "a"),
  39.     DisplayOrder(1, "b"),
  40.     DisplayOrder(3, "c"),
  41.     DisplayOrder(4, "d"),
  42.     DisplayOrder(5, "e"),
  43.     DisplayOrder(6, "f"),
  44.     DisplayOrder(7, "g"),
  45.     DisplayOrder(8, "i"),
  46. ]
  47.  
  48. bar = [
  49.     DisplayOrder(1, "a"),
  50.     DisplayOrder(1, "b"),
  51.     DisplayOrder(3, "c"),
  52.     DisplayOrder(4, "d"),
  53.     DisplayOrder(5, "e"),
  54.     DisplayOrder(6, "f"),
  55.     DisplayOrder(7, "g"),
  56.     DisplayOrder(8, "i"),
  57. ]
  58.  
  59. print("smartSort")
  60. for f in smartSort(foo, "b"):
  61.     print(f.number, f.description)
  62. print("stupidSort")
  63. for f in stupidSort(foo, "b"):
  64.     print(f.number, f.description)
Advertisement
Add Comment
Please, Sign In to add comment