コード例 #1
0
    def test_rand(self):
        from random import randint as R, sample as S
        from math import log2, ceil

        def solution(a, n):
            n -= 1
            l = 2**int(log2(ceil((n + 1) / len(a))))
            r = (n - (l - 1) * len(a)) // l
            return a[r]

        names = [
            "Sheldon", "Leonard", "Penny", "Rajesh", "Howard",
            "Daisuke Aramaki", "Motoko Kusanagi", "Batou", "Togusa",
            "Ishikawa", "Saito", "Pazu", "Borma", "Azuma", "Yano", "Proto"
        ]

        for _ in range(100):
            a = S(names, R(1, 16))
            n = R(1, 1000)
            self.assertEqual(who_is_next(a[:], n), solution(a, n))

        for _ in range(100):
            a = S(names, R(1, 16))
            n = R(10**24, 10**30)
            self.assertEqual(who_is_next(a[:], n), solution(a, n))
コード例 #2
0
ファイル: Dynamic.py プロジェクト: medo94my/game-dev
 def randomize(self, num_of_six, color_of_six, letter_of_26):
     self.color = ['GREEN', 'BLUE', 'RED', 'YELLOW', 'PURPLE', 'BROWN']
     self.letter = [
         'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P', 'A', 'S', 'D',
         'F', 'G', 'H', 'J', 'K', 'L', 'Z', 'X', 'C', 'V', 'B', 'N', 'M'
     ]
     self.smap = num_of_six + color_of_six + letter_of_26
     self.num_of_six = num_of_six
     self.color_of_six = color_of_six
     self.letter_of_26 = letter_of_26
     self.Answer = S(
         S(range(1, 9), num_of_six) + S(self.color, color_of_six) +
         S(self.letter, letter_of_26), self.smap)
     print(self.Answer)
     for i in self.Answer:
         self.fors += str(i)
コード例 #3
0
def test_should_find_missing_number_for_random_performance_tests():
    base = list(range(1, 1000001))
    S(base)
    for _ in range(20):
        n = R(1, 1000000)
        a = base[:]
        a.remove(n)
        assert find_missing_number(a) == n
コード例 #4
0
def generate_arrays_from_file(images, labels, ids_list, shuffle=True):
    x = np.zeros((32, 49, 49, 3))
    y = np.zeros((32, 2))
    batch_id = 0
    while 1:
        if shuffle:
            S(ids_list)
        x = np.zeros((32, 49, 49, 3))
        y = np.zeros((32, 2))
        for id_list in ids_list:
            for i in range(len(images[id_list])):
                x[i%32, ...] = images[id_list][i]
                y[i%32, ...] = labels[id_list][i]
                if i%32 == 0:
                    yield (x, y)
                    x = np.zeros((32, 49, 49, 3))
                    y = np.zeros((32, 2))
コード例 #5
0
def generate_arrays_from_file(images, labels, batch_size=32, shuffle=True):
    x = np.zeros((batch_size, 49, 49, 3))
    y = np.zeros((batch_size, 5))
    batch_id = 0
    if shuffle:
        c = list(zip(images, labels))
        S(c)
        images, labels = zip(*c)
    while 1:
        for i in range(0, len(images)):
            x[batch_id, ...] = images[i]
            y[batch_id, ...] = keras.utils.to_categorical(labels[i], num_classes=5)
            if batch_id == (batch_size - 1):
                yield (x, y)
                batch_id = 0
            else:
                batch_id += 1
コード例 #6
0
def generate_arrays_from_file(images, labels, batch_size=32, shuffle=True):
    x = np.zeros((batch_size, 64, 64,1))
    y = np.zeros((batch_size, 5))
    batch_id = 0
    idx = np.arange(0, len(images))
    if shuffle:
        #c = list(zip(images, labels))
        S(idx)
        #images, labels = zip(*c)
    while 1:
        for i in idx:
            x[batch_id, ...] = images[i].reshape(64, 64,1)
            y[batch_id, ...] = keras.utils.to_categorical(labels[i], num_classes=5)
            if batch_id == (batch_size - 1):
                yield (x, y)
                batch_id = 0
            else:
                batch_id += 1
コード例 #7
0
 def it_1():
     for x in S(range(32, 127), 95):
         self.assertEqual(get_ascii(chr(x)), x)
コード例 #8
0
ファイル: MM_A01_002.py プロジェクト: medo94my/game-dev
    def checked(self,
                guess_director):  # ==========================================
        def restart(self):
            self.Counter == 0
            print("counter reseted")

        print(self.Answer)

        number = ['1', '2', '3', '4', '5', '6', '7', '8', '9']

        def guess_num():

            Valid_counter = 0  # This variable detects if the input is valid or not
            guess = guess_director

            for i in range(len(guess)):

                if guess[
                        i] in number:  # If the 4 digits were valid it will get through
                    Valid_counter += 1

                # This detects if there is any duplicate values
            if guess.count(guess[i]) != 1:
                print("Dont enter Duplicates")
                Valid_counter = 14
                return -1

            if Valid_counter == 6 and len(guess) == 6:
                return guess

            else:
                print("Enter four numbers! From 1 to 8")
                return -1

        checker = guess_num()

        if checker != -1:
            self.num_correct = 0
            self.cright = 0  # right position
            self.cwrong = 0  # wrong position

            for x in range(6):
                for y in range(
                        6):  # This checks every guess with the final answer

                    if checker[x] == str(self.Answer[y]):
                        self.num_correct += 1
                        if x == y:
                            self.cright += 1
                        else:
                            self.cwrong += 1

            print("       Number of correct digits " + str(self.num_correct))
            print("       Right position " + str(self.cright))
            print("       Wrong position " + str(self.cwrong))

            self.Counter += 1

            # The function below appends every step into the Guesses_Dic dictionary to compare among each other.
            self.Value_Analyzer.append(
                [int(self.num_correct),
                 int(self.cright)])

            if self.cright == 6:
                print("Congratulations!!! You won!!")
                self.Answer = S(range(1, 10), 6)
コード例 #9
0
ファイル: MM_A01_002.py プロジェクト: medo94my/game-dev
 def randomize(self):
     self.Answer = S(range(1, 10), 6)
     for i in self.Answer:
         self.fors += str(i)
コード例 #10
0
ファイル: Dynamic.py プロジェクト: medo94my/game-dev
    def check(self,
              guess_director):  # ==========================================
        def restart(self):
            self.Counter == 0
            print("counter reseted")

        print(self.Answer)

        number = [
            '1', '2', '3', '4', '5', '6', '7', '8', '9', 'Q', 'W', 'E', 'R',
            'T', 'Y', 'U', 'I', 'O', 'P', 'A', 'S', 'D', 'F', 'G', 'H', 'J',
            'K', 'L', 'Z', 'X', 'C', 'V', 'B', 'N', 'M', 'GREEN', 'BLUE',
            'RED', 'YELLOW', 'PURPLE', 'BROWN'
        ]

        def guess_num():

            Valid_counter = 0  # This variable detects if the input is valid or not
            guess = guess_director
            print(type(guess))
            for i in range(len(guess)):

                if guess[
                        i] in number:  # If the 4 digits were valid it will get through
                    Valid_counter += 1
                    print(Valid_counter)
                # This detects if there is any duplicate values
            if guess.count(guess[i]) != 1:
                print("Dont enter Duplicates")
                Valid_counter = 14
                return -1

            if Valid_counter == len(self.Answer) and len(guess) == len(
                    self.Answer):
                return guess

            else:
                print("Enter four numbers! From 1 to 8")
                return -1

        checker = guess_num()

        if checker != -1:
            self.num_correct = 0
            self.cright = 0  # right position
            self.cwrong = 0  # wrong position

            for x in range(len(self.Answer)):
                for y in range(
                        len(self.Answer)
                ):  # This checks every guess with the final answer

                    if checker[x] == str(self.Answer[y]):
                        print('Answer: ' + str(self.Answer[y]))
                        self.num_correct += 1
                        if x == y:
                            self.cright += 1
                        else:
                            self.cwrong += 1

            print("       Number of correct digits " + str(self.num_correct))
            print("       Right position " + str(self.cright))
            print("       Wrong position " + str(self.cwrong))

            self.Counter += 1

            # The function below appends every step into the Guesses_Dic dictionary to compare among each other.
            self.Value_Analyzer.append(
                [int(self.num_correct),
                 int(self.cright)])

            # self.data['result'] = []
            # self.data['result'].append(
            #     {'correct': int(num_correct), 'Right': int(
            #         self.cright), 'Wrong': int(cwrong), 'score': self.Score})
            # self.data['result'] += self.data['result']

            if self.cright == len(self.Answer):
                print("Congratulations!!! You won!!")
                # self.Answer = S(range(1, 9), 4)
                self.Answer = S(
                    S(range(1, 9), self.num_of_six) +
                    S(self.color, self.color_of_six) +
                    S(self.letter, self.letter_of_26), self.smap)
コード例 #11
0
def test_should_find_missing_number_for_random_normal_tests():
    for _ in range(100):
        a = list(range(1, R(1, 200)))
        S(a)
        n = a.pop() if a else 1
        assert find_missing_number(a) == n