Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ###########################################
- # Hofit Marzouk
- # 205636848
- # 8817003
- # Exercise 2
- ##########################################
- ##############################################################
- # function name: Get Distances
- # The Input: a list of numbers with no jokers
- # The output: a list of distances
- # The Function operation: calculates the
- #distance between every two following numbers in the list.
- ##############################################################
- def GetDistances (pureNumberList):
- l = len(pureNumberList)
- distanceList = []
- for i in range(1,l):
- #indexes in python start from 0
- distanceList.append( abs( pureNumberList[i-1] - pureNumberList[i] ) )
- return distanceList
- ###############################################################
- # function name: Is Distance One
- # The Input: a list of distances
- # The output: True or False
- # The Function operation: a boolean function that checking if the
- #cards' values increase by 1 at a time
- ################################################################
- def IsDistanceOne (distanceList):
- flag = True
- #checking if the cards' values increase by 1 at a time
- for i in range (1,len(distanceList)):
- if distanceList[i] != 1:
- flag = False
- return flag
- ###############################################################
- # function name: Is Same Kind
- # The Input: a list of kinds with no jokers
- # The output: True or False
- # The Function operation: a boolean function that checks if all
- #the cards are from the same kind
- ################################################################
- def IsSameKind(pureKindList):
- #checking if all the cards are from the same kind
- kind = pureKindList[0]
- if pureKindList.count(kind) == len(pureKindList):
- return True
- return False
- ###############################################################
- # function name: Jokers In The End
- # The Input: number of jokers and a list
- # The output: list
- # The Function operation: puts the jokers in the end when needed
- ################################################################
- def JokersInTheEnd (jokers,list):
- while jokers > 0 :
- list.append(0)
- jokers = jokers - 1
- return list
- #so we define a function that taking a list and sorting it by the rules
- #11 12 13 2 0
- #-2 -1 0 2
- #d = 11
- #
- #2 11 12 13
- #
- #11 12 0 0 2
- #
- #2 11 12
- ###############################################################
- # function name: Sort Yaniv Style
- # The Input: a list of numbers
- # The output: a list of numbers
- # The Function operation: sort the list by Yaniv rules
- ################################################################
- def SortYanivStyle (numberList):
- tempNumberList = numberList
- for i in tempNumberList:
- for j in tempNumberList:
- if abs ( i - j) >=9:
- for num in tempNumberList:
- if 10<=num<=13:
- #we transfer this
- #2 11 12 13 =>
- #into this
- #2 0 -1 -2
- num = num -13
- #into this
- #-2 -1 0 2
- tempNumberList.sort()
- return tempNumberList
- list=[]
- list.sort()
- #getting cards until -1 is given or until we have 5 cards
- card = input()
- list.append(card)
- while card != -1 and len(list) != 5: #simple while loop:
- list.append(card)
- card = input()
- #finding the cards' kind
- kindList=list
- for i in kindList:
- if i != 0:
- i = 4 - i%4
- #finding the cards' numbers
- numberList=list
- for i in numberList:
- if i != 0:
- i = 1 + int(i-i%4)/4
- #defining lists with no jokers
- pureNumberList = numberList
- pureKindList = kindList
- while pureNumberList[0] == 0:
- pureNumberList.remove(0)
- #checking if all cards have the same number
- number = numberList[0]
- error1 = True
- if numberList.count(number) == len(numberList):
- error1 = False
- #checking how many jokers there are between the cards
- jokers=0
- for i in list:
- if i == 0:
- jokers = jokers + 1
- #defining a list of distances between every two following numbers in the number list
- distanceList = GetDistances (pureNumberList)
- error2 = True
- indexList = []
- tempList = distanceList
- #second condition for good series
- if len(numberList)>=3:
- #checking if all the cards are from the same kind
- if IsSameKind(pureKindList):
- #case of no necessary jokers
- if IsDistanceOne(pureNumberList) == True:
- error2 = False
- #case of one joker
- elif jokers == 1:
- #if there is one distance in the length 2 and the rest are 1, this is good series
- if distance in distanceList == 2 and distance.count(distance) == 1:
- #getting the index of the distances
- index = distanceList.index (distance)
- distanceList.remove ( distance )
- indexList.append(index)
- #case of two jokers
- elif jokers == 2:
- #if there are 2 jokers and one space of 3 and the rest are 1,
- #or 2 spaces of two and the rest are one, it's a good series.
- if distance in distanceList == 2:
- if 1 == distanceList.count(distance) :
- #getting the indexes of the distances
- index = distanceList.index (distance)
- tempList[index1] = trash
- distanceList.remove ( distance )
- indexList.append(index)
- elif 2 == distanceList.count(distance):
- index = distanceList.index (distance)
- tempList[index1] = trash
- distanceList.remove ( distance )
- indexList.append(index)
- #if it exists
- index2 = tempList.index (distance)
- distanceList.remove ( distance )
- indexList.append(index2)
- elif distance in distanceList == 3 and distanceList.count(distance) == 1:
- #getting the index of the distance
- index = distanceList.index (distance)
- distanceList.remove ( distance )
- #sorting the series by the rule Ace rules
- pureNumberList = SortYanivStyle (pureNumberList)
- #this part should be at the beginning
- for i in pureNumberList:
- #now we replace the non positive numbers into their real values
- if -3 <= i <= 0 in pureNumberList:
- i = i + 13
- #11 12 13 2
- #solve the distances problem
- #putting 0's in their places to create good series
- if IsDistanceOne( distanceList ):
- error2 = False
- for i in range(len(indexList)):
- pureNumberList.insert(indexList.index[i], 0)
- jokers = jokers - len(indexList)
- #putting the jockers in the end
- numberList = JokersInTheEnd (jokers,pureNumberList)
- if error1 == True and error2 == True:
- print ('ERROR')
- else:
- print(' '.join(str(number) for number in numberList))
- ###########################################
- # Hofit Marzouk
- # 205636848
- # 8817003
- # Exercise 2
- ##########################################
- ##############################################################
- # function name: Get Distances
- # The Input: a list of numbers with no jokers
- # The output: a list of distances
- # The Function operation: calculates the
- #distance between every two following numbers in the list.
- ##############################################################
- def GetDistances (pureNumberList):
- l = len(pureNumberList)
- distanceList = []
- for i in range(1,l):
- #indexes in python start from 0
- distanceList.append( abs( pureNumberList[i-1] - pureNumberList[i] ) )
- return distanceList
- ###############################################################
- # function name: Is Distance One
- # The Input: a list of distances
- # The output: True or False
- # The Function operation: a boolean function that checking if the
- #cards' values increase by 1 at a time
- ################################################################
- def IsDistanceOne (distanceList):
- flag = True
- #checking if the cards' values increase by 1 at a time
- for i in range (1,len(distanceList)):
- if distanceList[i] != 1:
- flag = False
- return flag
- ###############################################################
- # function name: Is Same Kind
- # The Input: a list of kinds with no jokers
- # The output: True or False
- # The Function operation: a boolean function that checks if all
- #the cards are from the same kind
- ################################################################
- def IsSameKind(pureKindList):
- #checking if all the cards are from the same kind
- kind = pureKindList[0]
- if pureKindList.count(kind) == len(pureKindList):
- return True
- return False
- ###############################################################
- # function name: Jokers In The End
- # The Input: number of jokers and a list
- # The output: list
- # The Function operation: puts the jokers in the end when needed
- ################################################################
- def JokersInTheEnd (jokers,list):
- while jokers > 0 :
- list.append(0)
- jokers = jokers - 1
- return list
- #so we define a function that taking a list and sorting it by the rules
- #11 12 13 2 0
- #-2 -1 0 2
- #d = 11
- #
- #2 11 12 13
- #
- #11 12 0 0 2
- #
- #2 11 12
- ###############################################################
- # function name: Sort Yaniv Style
- # The Input: a list of numbers
- # The output: a list of numbers
- # The Function operation: sort the list by Yaniv rules
- ################################################################
- def SortYanivStyle (numberList):
- tempNumberList = numberList
- for i in tempNumberList:
- for j in tempNumberList:
- if abs ( i - j) >=9:
- for num in tempNumberList:
- if 10<=num<=13:
- #we transfer this
- #2 11 12 13 =>
- #into this
- #2 0 -1 -2
- num = num -13
- #into this
- #-2 -1 0 2
- tempNumberList.sort()
- return tempNumberList
- list=[]
- list.sort()
- #getting cards until -1 is given or until we have 5 cards
- card = input()
- list.append(card)
- while card != -1 and len(list) != 5: #simple while loop:
- list.append(card)
- card = input()
- #finding the cards' kind
- kindList=list
- for i in kindList:
- if i != 0:
- i = 4 - i%4
- #finding the cards' numbers
- numberList=list
- for i in numberList:
- if i != 0:
- i = 1 + int(i-i%4)/4
- #defining lists with no jokers
- pureNumberList = numberList
- pureKindList = kindList
- while pureNumberList[0] == 0:
- pureNumberList.remove(0)
- #checking if all cards have the same number
- number = numberList[0]
- error1 = True
- if numberList.count(number) == len(numberList):
- error1 = False
- #checking how many jokers there are between the cards
- jokers=0
- for i in list:
- if i == 0:
- jokers = jokers + 1
- #defining a list of distances between every two following numbers in the number list
- distanceList = GetDistances (pureNumberList)
- error2 = True
- indexList = []
- tempList = distanceList
- #second condition for good series
- if len(numberList)>=3:
- #checking if all the cards are from the same kind
- if IsSameKind(pureKindList):
- #case of no necessary jokers
- if IsDistanceOne(pureNumberList) == True:
- error2 = False
- #case of one joker
- elif jokers == 1:
- #if there is one distance in the length 2 and the rest are 1, this is good series
- if distance in distanceList == 2 and distance.count(distance) == 1:
- #getting the index of the distances
- index = distanceList.index (distance)
- distanceList.remove ( distance )
- indexList.append(index)
- #case of two jokers
- elif jokers == 2:
- #if there are 2 jokers and one space of 3 and the rest are 1,
- #or 2 spaces of two and the rest are one, it's a good series.
- if distance in distanceList == 2:
- if 1 == distanceList.count(distance) :
- #getting the indexes of the distances
- index = distanceList.index (distance)
- tempList[index1] = trash
- distanceList.remove ( distance )
- indexList.append(index)
- elif 2 == distanceList.count(distance):
- index = distanceList.index (distance)
- tempList[index1] = trash
- distanceList.remove ( distance )
- indexList.append(index)
- #if it exists
- index2 = tempList.index (distance)
- distanceList.remove ( distance )
- indexList.append(index2)
- elif distance in distanceList == 3 and distanceList.count(distance) == 1:
- #getting the index of the distance
- index = distanceList.index (distance)
- distanceList.remove ( distance )
- #sorting the series by the rule Ace rules
- pureNumberList = SortYanivStyle (pureNumberList)
- #this part should be at the beginning
- for i in pureNumberList:
- #now we replace the non positive numbers into their real values
- if -3 <= i <= 0 in pureNumberList:
- i = i + 13
- #11 12 13 2
- #solve the distances problem
- #putting 0's in their places to create good series
- if IsDistanceOne( distanceList ):
- error2 = False
- for i in range(len(indexList)):
- pureNumberList.insert(indexList.index[i], 0)
- jokers = jokers - len(indexList)
- #putting the jockers in the end
- numberList = JokersInTheEnd (jokers,pureNumberList)
- if error1 == True and error2 == True:
- print ('ERROR')
- else:
- print(' '.join(str(number) for number in numberList))
Add Comment
Please, Sign In to add comment