Guest User

Untitled

a guest
Apr 22nd, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.27 KB | None | 0 0
  1. ###########################################
  2. # Hofit Marzouk
  3. # 205636848
  4. # 8817003
  5. # Exercise 2
  6. ##########################################
  7.  
  8. ##############################################################
  9. # function name: Get Distances
  10. # The Input: a list of numbers with no jokers
  11. # The output: a list of distances
  12. # The Function operation: calculates the
  13. #distance between every two following numbers in the list.
  14. ##############################################################
  15. def GetDistances (pureNumberList):
  16. l = len(pureNumberList)
  17. distanceList = []
  18. for i in range(1,l):
  19. #indexes in python start from 0
  20. distanceList.append( abs( pureNumberList[i-1] - pureNumberList[i] ) )
  21. return distanceList
  22.  
  23. ###############################################################
  24. # function name: Is Distance One
  25. # The Input: a list of distances
  26. # The output: True or False
  27. # The Function operation: a boolean function that checking if the
  28. #cards' values increase by 1 at a time
  29. ################################################################
  30. def IsDistanceOne (distanceList):
  31. flag = True
  32. #checking if the cards' values increase by 1 at a time
  33. for i in range (1,len(distanceList)):
  34. if distanceList[i] != 1:
  35. flag = False
  36. return flag
  37.  
  38. ###############################################################
  39. # function name: Is Same Kind
  40. # The Input: a list of kinds with no jokers
  41. # The output: True or False
  42. # The Function operation: a boolean function that checks if all
  43. #the cards are from the same kind
  44. ################################################################
  45. def IsSameKind(pureKindList):
  46. #checking if all the cards are from the same kind
  47. kind = pureKindList[0]
  48. if pureKindList.count(kind) == len(pureKindList):
  49. return True
  50. return False
  51.  
  52. ###############################################################
  53. # function name: Jokers In The End
  54. # The Input: number of jokers and a list
  55. # The output: list
  56. # The Function operation: puts the jokers in the end when needed
  57. ################################################################
  58.  
  59. def JokersInTheEnd (jokers,list):
  60. while jokers > 0 :
  61. list.append(0)
  62. jokers = jokers - 1
  63. return list
  64.  
  65.  
  66. #so we define a function that taking a list and sorting it by the rules
  67. #11 12 13 2 0
  68. #-2 -1 0 2
  69. #d = 11
  70. #
  71. #2 11 12 13
  72. #
  73. #11 12 0 0 2
  74. #
  75. #2 11 12
  76.  
  77. ###############################################################
  78. # function name: Sort Yaniv Style
  79. # The Input: a list of numbers
  80. # The output: a list of numbers
  81. # The Function operation: sort the list by Yaniv rules
  82. ################################################################
  83. def SortYanivStyle (numberList):
  84. tempNumberList = numberList
  85. for i in tempNumberList:
  86. for j in tempNumberList:
  87. if abs ( i - j) >=9:
  88. for num in tempNumberList:
  89. if 10<=num<=13:
  90. #we transfer this
  91. #2 11 12 13 =>
  92. #into this
  93. #2 0 -1 -2
  94. num = num -13
  95. #into this
  96. #-2 -1 0 2
  97. tempNumberList.sort()
  98. return tempNumberList
  99.  
  100. list=[]
  101. list.sort()
  102.  
  103. #getting cards until -1 is given or until we have 5 cards
  104. card = input()
  105. list.append(card)
  106. while card != -1 and len(list) != 5: #simple while loop:
  107. list.append(card)
  108. card = input()
  109.  
  110. #finding the cards' kind
  111. kindList=list
  112. for i in kindList:
  113. if i != 0:
  114. i = 4 - i%4
  115.  
  116. #finding the cards' numbers
  117. numberList=list
  118. for i in numberList:
  119. if i != 0:
  120. i = 1 + int(i-i%4)/4
  121.  
  122. #defining lists with no jokers
  123. pureNumberList = numberList
  124. pureKindList = kindList
  125. while pureNumberList[0] == 0:
  126. pureNumberList.remove(0)
  127.  
  128.  
  129. #checking if all cards have the same number
  130. number = numberList[0]
  131. error1 = True
  132. if numberList.count(number) == len(numberList):
  133. error1 = False
  134.  
  135. #checking how many jokers there are between the cards
  136. jokers=0
  137. for i in list:
  138. if i == 0:
  139. jokers = jokers + 1
  140.  
  141. #defining a list of distances between every two following numbers in the number list
  142. distanceList = GetDistances (pureNumberList)
  143. error2 = True
  144. indexList = []
  145. tempList = distanceList
  146. #second condition for good series
  147. if len(numberList)>=3:
  148. #checking if all the cards are from the same kind
  149. if IsSameKind(pureKindList):
  150. #case of no necessary jokers
  151. if IsDistanceOne(pureNumberList) == True:
  152. error2 = False
  153. #case of one joker
  154. elif jokers == 1:
  155. #if there is one distance in the length 2 and the rest are 1, this is good series
  156. if distance in distanceList == 2 and distance.count(distance) == 1:
  157. #getting the index of the distances
  158. index = distanceList.index (distance)
  159. distanceList.remove ( distance )
  160. indexList.append(index)
  161. #case of two jokers
  162. elif jokers == 2:
  163. #if there are 2 jokers and one space of 3 and the rest are 1,
  164. #or 2 spaces of two and the rest are one, it's a good series.
  165. if distance in distanceList == 2:
  166. if 1 == distanceList.count(distance) :
  167.  
  168. #getting the indexes of the distances
  169. index = distanceList.index (distance)
  170. tempList[index1] = trash
  171. distanceList.remove ( distance )
  172. indexList.append(index)
  173. elif 2 == distanceList.count(distance):
  174. index = distanceList.index (distance)
  175. tempList[index1] = trash
  176. distanceList.remove ( distance )
  177. indexList.append(index)
  178. #if it exists
  179. index2 = tempList.index (distance)
  180. distanceList.remove ( distance )
  181. indexList.append(index2)
  182. elif distance in distanceList == 3 and distanceList.count(distance) == 1:
  183. #getting the index of the distance
  184. index = distanceList.index (distance)
  185. distanceList.remove ( distance )
  186.  
  187.  
  188.  
  189. #sorting the series by the rule Ace rules
  190. pureNumberList = SortYanivStyle (pureNumberList)
  191.  
  192. #this part should be at the beginning
  193. for i in pureNumberList:
  194. #now we replace the non positive numbers into their real values
  195. if -3 <= i <= 0 in pureNumberList:
  196. i = i + 13
  197. #11 12 13 2
  198.  
  199. #solve the distances problem
  200. #putting 0's in their places to create good series
  201. if IsDistanceOne( distanceList ):
  202. error2 = False
  203. for i in range(len(indexList)):
  204. pureNumberList.insert(indexList.index[i], 0)
  205.  
  206.  
  207.  
  208. jokers = jokers - len(indexList)
  209.  
  210. #putting the jockers in the end
  211. numberList = JokersInTheEnd (jokers,pureNumberList)
  212.  
  213. if error1 == True and error2 == True:
  214. print ('ERROR')
  215. else:
  216. print(' '.join(str(number) for number in numberList))
  217.  
  218.  
  219.  
  220.  
  221.  
  222.  
  223.  
  224.  
  225.  
  226.  
  227.  
  228.  
  229.  
  230.  
  231.  
  232.  
  233.  
  234.  
  235.  
  236.  
  237. ###########################################
  238. # Hofit Marzouk
  239. # 205636848
  240. # 8817003
  241. # Exercise 2
  242. ##########################################
  243.  
  244. ##############################################################
  245. # function name: Get Distances
  246. # The Input: a list of numbers with no jokers
  247. # The output: a list of distances
  248. # The Function operation: calculates the
  249. #distance between every two following numbers in the list.
  250. ##############################################################
  251. def GetDistances (pureNumberList):
  252. l = len(pureNumberList)
  253. distanceList = []
  254. for i in range(1,l):
  255. #indexes in python start from 0
  256. distanceList.append( abs( pureNumberList[i-1] - pureNumberList[i] ) )
  257. return distanceList
  258.  
  259. ###############################################################
  260. # function name: Is Distance One
  261. # The Input: a list of distances
  262. # The output: True or False
  263. # The Function operation: a boolean function that checking if the
  264. #cards' values increase by 1 at a time
  265. ################################################################
  266. def IsDistanceOne (distanceList):
  267. flag = True
  268. #checking if the cards' values increase by 1 at a time
  269. for i in range (1,len(distanceList)):
  270. if distanceList[i] != 1:
  271. flag = False
  272. return flag
  273.  
  274. ###############################################################
  275. # function name: Is Same Kind
  276. # The Input: a list of kinds with no jokers
  277. # The output: True or False
  278. # The Function operation: a boolean function that checks if all
  279. #the cards are from the same kind
  280. ################################################################
  281. def IsSameKind(pureKindList):
  282. #checking if all the cards are from the same kind
  283. kind = pureKindList[0]
  284. if pureKindList.count(kind) == len(pureKindList):
  285. return True
  286. return False
  287.  
  288. ###############################################################
  289. # function name: Jokers In The End
  290. # The Input: number of jokers and a list
  291. # The output: list
  292. # The Function operation: puts the jokers in the end when needed
  293. ################################################################
  294.  
  295. def JokersInTheEnd (jokers,list):
  296. while jokers > 0 :
  297. list.append(0)
  298. jokers = jokers - 1
  299. return list
  300.  
  301.  
  302. #so we define a function that taking a list and sorting it by the rules
  303. #11 12 13 2 0
  304. #-2 -1 0 2
  305. #d = 11
  306. #
  307. #2 11 12 13
  308. #
  309. #11 12 0 0 2
  310. #
  311. #2 11 12
  312.  
  313. ###############################################################
  314. # function name: Sort Yaniv Style
  315. # The Input: a list of numbers
  316. # The output: a list of numbers
  317. # The Function operation: sort the list by Yaniv rules
  318. ################################################################
  319. def SortYanivStyle (numberList):
  320. tempNumberList = numberList
  321. for i in tempNumberList:
  322. for j in tempNumberList:
  323. if abs ( i - j) >=9:
  324. for num in tempNumberList:
  325. if 10<=num<=13:
  326. #we transfer this
  327. #2 11 12 13 =>
  328. #into this
  329. #2 0 -1 -2
  330. num = num -13
  331. #into this
  332. #-2 -1 0 2
  333. tempNumberList.sort()
  334. return tempNumberList
  335.  
  336. list=[]
  337. list.sort()
  338.  
  339. #getting cards until -1 is given or until we have 5 cards
  340. card = input()
  341. list.append(card)
  342. while card != -1 and len(list) != 5: #simple while loop:
  343. list.append(card)
  344. card = input()
  345.  
  346. #finding the cards' kind
  347. kindList=list
  348. for i in kindList:
  349. if i != 0:
  350. i = 4 - i%4
  351.  
  352. #finding the cards' numbers
  353. numberList=list
  354. for i in numberList:
  355. if i != 0:
  356. i = 1 + int(i-i%4)/4
  357.  
  358. #defining lists with no jokers
  359. pureNumberList = numberList
  360. pureKindList = kindList
  361. while pureNumberList[0] == 0:
  362. pureNumberList.remove(0)
  363.  
  364.  
  365. #checking if all cards have the same number
  366. number = numberList[0]
  367. error1 = True
  368. if numberList.count(number) == len(numberList):
  369. error1 = False
  370.  
  371. #checking how many jokers there are between the cards
  372. jokers=0
  373. for i in list:
  374. if i == 0:
  375. jokers = jokers + 1
  376.  
  377. #defining a list of distances between every two following numbers in the number list
  378. distanceList = GetDistances (pureNumberList)
  379. error2 = True
  380. indexList = []
  381. tempList = distanceList
  382. #second condition for good series
  383. if len(numberList)>=3:
  384. #checking if all the cards are from the same kind
  385. if IsSameKind(pureKindList):
  386. #case of no necessary jokers
  387. if IsDistanceOne(pureNumberList) == True:
  388. error2 = False
  389. #case of one joker
  390. elif jokers == 1:
  391. #if there is one distance in the length 2 and the rest are 1, this is good series
  392. if distance in distanceList == 2 and distance.count(distance) == 1:
  393. #getting the index of the distances
  394. index = distanceList.index (distance)
  395. distanceList.remove ( distance )
  396. indexList.append(index)
  397. #case of two jokers
  398. elif jokers == 2:
  399. #if there are 2 jokers and one space of 3 and the rest are 1,
  400. #or 2 spaces of two and the rest are one, it's a good series.
  401. if distance in distanceList == 2:
  402. if 1 == distanceList.count(distance) :
  403.  
  404. #getting the indexes of the distances
  405. index = distanceList.index (distance)
  406. tempList[index1] = trash
  407. distanceList.remove ( distance )
  408. indexList.append(index)
  409. elif 2 == distanceList.count(distance):
  410. index = distanceList.index (distance)
  411. tempList[index1] = trash
  412. distanceList.remove ( distance )
  413. indexList.append(index)
  414. #if it exists
  415. index2 = tempList.index (distance)
  416. distanceList.remove ( distance )
  417. indexList.append(index2)
  418. elif distance in distanceList == 3 and distanceList.count(distance) == 1:
  419. #getting the index of the distance
  420. index = distanceList.index (distance)
  421. distanceList.remove ( distance )
  422.  
  423.  
  424.  
  425. #sorting the series by the rule Ace rules
  426. pureNumberList = SortYanivStyle (pureNumberList)
  427.  
  428. #this part should be at the beginning
  429. for i in pureNumberList:
  430. #now we replace the non positive numbers into their real values
  431. if -3 <= i <= 0 in pureNumberList:
  432. i = i + 13
  433. #11 12 13 2
  434.  
  435. #solve the distances problem
  436. #putting 0's in their places to create good series
  437. if IsDistanceOne( distanceList ):
  438. error2 = False
  439. for i in range(len(indexList)):
  440. pureNumberList.insert(indexList.index[i], 0)
  441.  
  442.  
  443.  
  444. jokers = jokers - len(indexList)
  445.  
  446. #putting the jockers in the end
  447. numberList = JokersInTheEnd (jokers,pureNumberList)
  448.  
  449. if error1 == True and error2 == True:
  450. print ('ERROR')
  451. else:
  452. print(' '.join(str(number) for number in numberList))
Add Comment
Please, Sign In to add comment