예제 #1
0
파일: sort.py 프로젝트: kidbytes/pdsa
def partition(inOutList, start, end):
    
    if (end - start == 1):
        return start
    
    r = util.get_random_number(start, end)
    inOutList[start], inOutList[r] = inOutList[r], inOutList[start]
#     print inOutList[start:end]
    
    pivot = inOutList[start]
    i = start + 1
    j = end - 1
    
    while (True):
        # Stop at first item > pivot
        while(i < end and inOutList[i] < pivot):
            i += 1
    
        # Stop at first item < pivot
        while( j > start and inOutList[j] > pivot):
            j -= 1
                    
        if (i < j):
            inOutList[i], inOutList[j] = inOutList[j], inOutList[i]
            i += 1
            j -= 1
        else:
            break
        
    inOutList[start], inOutList[j] = inOutList[j], inOutList[start]
#     print "Pivot=%d, %s" % (r, str(inOutList[start:end]))
    return j
예제 #2
0
 def __simulate_one_week__(self):
     """
     Makes the program being simulated to advance in one week.
     This method will search all active members and apply the rules of the program.
     """
     self.__set_week__(self.current_week + 1)
     members = self.db_controller.get_members()
     filtered = filter(lambda x: (len(x['recruited']) < 10) & x['active'],
                       members)
     members = list(filtered)
     if not members:
         self.db_controller.end_program()
         self.continue_simulation = False
         return
     for member_doc in members:
         member = Member(member_doc)
         # 3.b of the document actually says "If the number is greater than the member's probability..."
         # I'm going to use "less than", to make sense. The bigger the probability, the more likely the statement to be true
         if get_random_number() < member.get_recruit_probability():
             candidate = Investor(self.db_controller.get_random_investor())
             self.logger.log(
                 'Member {} will try to recruit investor {}'.format(
                     member.id, candidate.id))
             # Same problem with 3.c of the document
             if get_random_number() < candidate.get_accept_probability():
                 self.logger.log(
                     'Investor {} will join the program invited by member {}'
                     .format(candidate.id, member.id))
                 member.money += 100
                 self.db_controller.add_member(candidate.id, member.id,
                                               self.current_week)
             else:
                 self.logger.log(
                     'Investor {} will not join the program'.format(
                         candidate.id))
         else:
             self.logger.log(
                 'Member {} will not recruit a new investor'.format(
                     member.id))
         lasted_weeks = self.current_week - member.week_joined
         if (member.money < 500) & (lasted_weeks >= member.get_max_weeks()):
             self.logger.log(
                 'Member {} did not recover the investment in {} weeks and will be eliminated from the program with MM${}.'
                 .format(member.id, lasted_weeks, member.money))
             self.db_controller.eliminate_member(member.id,
                                                 self.current_week)
예제 #3
0
파일: sort.py 프로젝트: kidbytes/pdsa
def dutch_quick_sort(inOutList, start, end):
    if (end - start <= 1):
        return
    
    r = util.get_random_number(start, end)
    inOutList[start], inOutList[r] = inOutList[r], inOutList[start]
    
    lt = start
    i = start + 1
    gt = end - 1    
    pivot = inOutList[lt]
    while (i <= gt):
        if (inOutList[i] > pivot):
            inOutList[i], inOutList[gt] = inOutList[gt], inOutList[i]
            gt -= 1
        elif (inOutList[i] == pivot):
            i += 1
        else:
            inOutList[i], inOutList[lt] = inOutList[lt], inOutList[i]
            lt += 1
            i += 1
            
    dutch_quick_sort(inOutList, start, lt)
    dutch_quick_sort(inOutList, gt+1, end)
예제 #4
0
 def test_get_random_number(self):
     result = get_random_number()
     self.assertTrue(result < 1)
     self.assertTrue(result > 0)