Saturday, February 22, 2014

week5

There is really nothing totalk about this week....

Find something useful online about unittest:

unittest.skip(reason)
Unconditionally skip the decorated test. reason should describe why the test is being skipped.

unittest.skipIf(condition, reason)
Skip the decorated test if condition is true.
unittest.skipUnless(condition, reason)
Skip the decorated test unless condition is true.
unittest.expectedFailure()
Mark the test as an expected failure. If the test fails when run, the test is not counted as a failure.
 unittest.SkipTest(reason)
This exception is raised to skip a test.




Maybe it is not clear how to use it. I will show you a homemade example.


import random
import unittest
def #this the function you want to test define youself
      #in oreder for convenience, we test the whether f(x,y)= x/y is right
class Test(unittest.TestCase):

    @unittest.skipIf(b==0, "0 can not divide")
    def test_shuffle(self):
        self.assertEqual(f(1,5), 0.2)
        expectedFailure(f(1,2),3)
       a=random.choice(range(10))
        b=random.choice(self.seq(10))
        self.assertEqual(f(b,a), b/a

Tuesday, February 4, 2014

week4

Since This week there is no topic assigned, maybe I will talk something about assignment 1? I am pround to tell you guys that I have finished.

Thank you those who watching my blogger. In order to return your attention, I decide to write something to make the assignment easier.

If TA see this article, do not worry! I will handle the extent!

Here we go!

1. step(1)(2)(3) The difficulty of this step is "reading GUIController and GUIViewables".

If you are really unconfident about trace the information they contain about TOAHModel, then open the Wing, drag the part that may be relative to TOAHModel. The Wing will automatically show the repeated part in different color for you. I promise it will speed you up.


For GUIController, if you really want or are interested in understanding all of them, reading chapter 16 of your CSC108 book is a good option.

For GUIViewables, I advice you to treat CheeseView simply as class Cheese in TOAHModel.


2.step(4) this step is not hard once you finish the steps above

3. step(5) If you can handle the recursion well, this part should not be a problem for you. But considering there are still some of you feel uncomfortable about recursion, I provide another way of doing it. Still you should be able to get the best seq for solving three_stools type.

Using 'while' to try all the possible.

suppose we want to solve an  8 cheeses four_stools problem, what is the possible of moving seq?

we can move 1 cheese using 4 stools, then moving 6 cheese using 3 stools, then moving 1 cheese using 4 stools.

or we we can move 2 cheese using four_stools, then moving 4 cheese using three_stools, then moving 2 cheese using four_stools.

or we can move 3 cheese using four_stools, then moving 2 cheese using  three_stools, then moving 3 cheese using four_stools.

or we can move 4 cheese using four_stools, then moving 0 cheese using  three_stools, then moving 4 cheese using four_stools.

we have 4 options here.

for each option exist another two four_stools problems.

By keeping trace of these problems, here is my conclusion!:

for every n cheeses four_stools problem, it is equal to solve:
a1,b1,a2,b2,a3,b3.....a(n),b(n)
where a(i) is 1 cheese four_stools problem for all i,
and b(i) is some cheese( can be zero) three_stools problem for all i.

the sum of number of cheeses is n.
By Exhaustive Attack method, list all the possible ways( Notice the sequence have to be symmtric! And in each sub part, they are still symmtric!)

sequence for 1 cheese four_stools is easy, so we are done.

But please notice, this solution is trivial! If you have any idea of recursion, you should not use this. This method is for those who try to get grades but fail to use recursion. Last option only!

I follow two functions that might be useful to you, even you want to use recursion to solve problem.

def M(n):
#smallest number of moves of four_stools problem with n cheeses
    small=(2**n)-1
    if n==1:
        return(small)
    else:
        for i in range(1,n):
            if (2*M(i)+2**(n-i)-1) < (small):
                small=(2*M(i)+2**(n-i)-1)
    return(small)


def H(n):
#next division number
#which is the proper number i s.t make M(n)=M(i)+ 2**(n-i)+M(i) be the smallest

    for i in range(1,n):
        if M(n)==(2*M(i)+2**(n-i)-1):
            return i


4.step(6)
animating the program:
    Here is a trick, since you finished the step(5), anyway you can get the best seq. It is not necessary to generating the animation while generating the seq. You can simply generate the seq first, then use a simple "for" loop to make a model move along the seq.

In this case, all you need to do use a "while" loop to check the time.
for instance,

t1 = time.perf_counter()
t2 = time.perf_counter()
            while (t2 - t1) < delay_btw_moves:
                t2 = time.perf_counter()

and insert "print('TOAHModel')" inside some where.

Okay! Thanks for reading.