Пример #1
0
def naive_prod(nums):
	'''
	Initial naive O(n^2) algorith
	'''
	if len(nums) < 2:
		raise ValueError('Too short')

	products = [0 for i in nums]
	for i in range(len(nums)):
		product = 1
		for j in range(len(nums)):
			if j != i:
				product = product * nums[j]
		products[i] = product
	return products


def gen_rand_arr(largest_array, largest_int, smallest_int):
	return [[random.randint(-5,10) for _ in range(random.randrange(2,10))]]

print(f_timer.time_funcs([prod_all_except_curr, prod_all_except_curr_v2, naive_prod], gen_rand_arr, [10000,100,-50], 1000000))

# print(prod_all_except_curr_v2([1,2,3,4,5]))
# print(prod_all_except_curr_v2([1,7,3,4]))
# print(prod_all_except_curr_v2([1,7,3,4,0]))
# print(prod_all_except_curr_v2([1,7,3,-4,-3]))
# print(prod_all_except_curr_v2([-3,4,1,2,5]))



            solutions[i][j] = up + left

    return solutions[n][len(cs) - 1]


def makeCoinProblem(maxGoal, maxNumCoins, maxCoin):
    goal = random.randint(0, maxGoal)

    num_coins = random.randint(2, maxNumCoins)
    # Generate random number of coins which does not exceed number
    # of available spots

    realNumCoins = random.randint(1, min(maxCoin - 1, num_coins))

    coins = random.sample(range(1, maxCoin), realNumCoins)

    return [goal, coins]


makeCoinProblem(10, 2, 4)
problemInput = [30, 10, 6]
print(
    f_timer.time_funcs([getWays, getWays2], makeCoinProblem, problemInput,
                       500))
print('done')

# print(ways_for_change(4, [1,2,3]))
# ways_for_change(10, [2,5,3,6])
# ways_for_change(4, [1,2,3])
Пример #3
0

def gen_rand_str(maxLong, maxSub):
    len_long = random.randint(0, maxLong)
    len_sub = random.randint(0, maxSub)

    str_long = ''.join(random.choices(string.ascii_lowercase, k=len_long))
    str_sub = ''.join(random.choices(string.ascii_lowercase, k=len_sub))

    return (str_long, str_sub)


def gen_bad_str(maxLong, maxSub):
    return ['ab' * maxLong, 'a' * maxSub]


print(
    f_timer.time_funcs([has_anagram2, has_anagram3, has_anagram3_2],
                       gen_rand_str, [1000, 11], 10000))

# s1 = "vmdnmfaxkqbhxvvrcgwtjvnumpsrfkofnwbnmjbelkxmwefdkweerdppxvepbxqsqqoczleuoemizwgznrlpzzaeylxizuhgkkslmlfnrspbgoodqricguajnagngvylahrduquyrcdhpptkyyotyjyfaplifutjgncfjwgyfnmztjurbikhqduvjurkbqrhgdtgxurkkfgglduifllccyewlofsaeeetmdoivuliortwkjxrrmbwbfodnfttvgmdkitkhpvqrhrhrqodlguacxzuiybkmxddikzwdvprtxtwkabsdaixjpiwskkepklqxdicqtddvtimufrmyoygh"
# print(len(s1))
# s12 = s1[338:342]
# print(s12)
# s2 = "yogh"

# print(has_anagram1(s12,s2))
# print(has_anagram2(s12,s2,))
# print(has_anagram3(s12,s2,))
# print(has_anagram4(s12,s2,))
Пример #4
0
            val = int(board[top_i+k][top_j+l])
            if not missing[val -1]:
              return False
            else: missing[val - 1] = False
        if any(missing):
          print('missing in box ', i,j)
          return False
    else: return True
  except Error as err:
    print('found .', err)
    return False

# a = [['.', '.', '.', '.', '.', '.', '5', '1', '.'], ['.', '.', '.', '.', '.', '.', '.', '.', '.'], ['.', '3', '.', '.', '.', '.', '.', '.', '.'], ['.', '.', '.', '.', '.', '.', '.', '.', '.'], ['.', '.', '.', '.', '.', '.', '.', '.', '.'], ['.', '.', '.', '.', '.', '.', '.', '.', '.'], ['.', '.', '.', '.', '3', '.', '.', '.', '.'], ['.', '.', '.', '.', '.', '.', '.', '.', '.'], ['.', '.', '.', '.', '.', '.', '.', '.', '.']]


# print(find_min_possible_loc(c))
# printSodoku(b)
# print(int('.'))
# print(check_valid_solution(b))
# print(sudoku_solve(a))
# print(sudoku_solve2(a))
# print(sudoku_solve3(a))
# print(sudoku_solve4(a))
# print(b)
# sudoku_solve3(a)



print(f_timer.time_funcs([sudoku_solve, sudoku_solve4], gen_sodoku, [0], 800))

Пример #5
0
            i = 1
            while i < len(max_3) and (m > max_3[i]):
                i += 1
            max_3.insert(i, m)
            max_3.pop(0)
        if (m < min_2[0]):
            i = 1
            while i < len(min_2) and (m < min_2[i]):
                i += 1
            min_2.insert(i, m)
            min_2.pop(0)

    return max(max_3[2] * max_3[1] * max_3[0], min_2[1] * min_2[0] * max_3[2])


def makeRandArr(largest_array, largest_int, smallest_int):
    return [[
        random.randint(smallest_int, largest_int)
        for _ in range(random.randint(3, largest_array))
    ]]


test_params = [50000, 10, -10]
print(
    f_timer.time_funcs([max_3_prod1, max_3_prod2, max_3_prod3], makeRandArr,
                       test_params, 10000))

# Output with
# test_params = [600,5000,1000,-1000]
# [42.93114352226257, 11.546314239501953, 3.6002357006073]
    return [random_arr, target_sum]


def gauss_problem(maxLen, avgSum, stdDev, hardness):
    arr_len = random.randint(4, maxLen)
    target_sum = round(random.gauss(avgSum, stdDev))
    random_arr = sorted(
        [round(random.gauss(0, avgSum / hardness)) for _ in range(arr_len)])
    return [random_arr, target_sum]


a = [
    -3687, -3683, -3665, -3584, -3373, -3335, -3274, -3221, -3189, -3010,
    -2879, -2744, -2668, -2649, -2592, -2570, -2457, -2450, -2401, -2128,
    -2092, -2048, -1982, -1973, -1721, -1664, -1619, -1601, -1109, -1033, -960,
    -948, -928, -908, -902, -818, -788, -782, -673, -665, -638, -569, -296,
    -241, -219, -111, 193, 260, 453, 520, 611, 675, 809, 822, 985, 1163, 1211,
    1285, 1331, 1394, 1480, 1559, 1594, 1697, 1734, 1787, 1965, 1969, 2017,
    2057, 2063, 2074, 2263, 2270, 2331, 2343, 2515, 2523, 2589, 2591, 2634,
    2643, 2703, 2867, 2876, 2973, 2997, 3396, 3461, 3485, 3682, 3722, 3730
]

target = 10597

algorithms = [find_array_quadruplet2, find_array_quadruplet3]
# print(sorted([[1,2],[0,2],[3,5],[12,5],[0,0],[22,0]]))
# print(f_timer.time_funcs(algorithms, problem_builder, [100, 30000], 1000, True))
# print(f_timer.time_funcs(algorithms, problem_builder, [200, 30000], 1000, True))
# print(f_timer.time_funcs(algorithms, problem_builder, [400, 30000], 100, True))
print(f_timer.time_funcs(algorithms, problem_builder, [1600, 300], 100, False))
Пример #7
0
            j = i + 1
            while j <= endOfWord:
                newArr.append(arr[j])
                j += 1
            endOfWord = i - 1
            newArr.append(' ')

    j = 0
    while j <= endOfWord:
        newArr.append(arr[j])
        j += 1
    return newArr


def genRandomSentence(maxNumWords, maxWordLength):
    numWords = random.randint(0, maxNumWords)
    output = []
    for i in range(numWords):
        wordLength = random.randint(0, maxWordLength)
        for _ in range(wordLength):
            output.append(random.choice(string.ascii_letters))
        output.append(' ')
    if (numWords > 0):
        output.pop()
    return [output]


print(
    f_timer.time_funcs([reverseWords, reverseWords2], genRandomSentence,
                       [100, 100], 1000))
Пример #8
0
    for j in range(i+1):
      left = 0
      down = 0
      if i > 0:
        left = previous_col[j]
      if j > 0: 
        down = curr_col[j-1]
      curr_col[j] = left + down

    previous_col = curr_col
  
  return curr_col[n-1]



print(f_timer.time_funcs([num_of_paths_to_dest, num_of_paths_to_dest2, num_of_paths_to_dest3], 
  lambda n:[random.randint(0,n)], [10], 100))

# print(num_of_paths_to_dest3(6))
# print(num_of_paths_to_dest(6))


'''
This function iteratively constructs an array. At the end of 
iteration, each location should contain the number of ways that 
the car could reach that location. 

Because the car can only move east or north, it cannot go back
on itself and the only two ways to get to a particular point 
is from the point south or west of it. Thus the sum of the 
ways to get to the point to the west and south are the number
of ways to get to the current point.