def do_hidden_sets_pass(self,area_iter,zeros): n_eliminated = 0 cell_locs = {} ndxs = [ndx for ndx in area_iter] for val in zeros: val_locs = [] for ndx in ndxs: if val in self.possibles[ndx]: val_locs.append(ndx) val_key = tuple(sorted(val_locs)) if val_key not in cell_locs: cell_locs[val_key] = [] cell_locs[val_key].append(val) for val_key in cell_locs: if len(val_key) == len(cell_locs[val_key]): logging.debug(" Found a hidden set: cells %s have only occurs of %s"%\ (seq_string(val_key),seq_string(cell_locs[val_key]))) # Remove possibles not in the set for ndx in val_key: for p in self.possibles[ndx]: if p not in cell_locs[val_key]: self.possibles[ndx].remove(p) logging.debug(" Hidden set eliminated possible %d from cell %d,%d"%\ (p,ndx[0],ndx[1])) n_eliminated += 1 return n_eliminated
def print_report(n): factorials = [digit_factorials[i] for i in digits(n)] digit_strs = ["%d!"%(i) for i in digits(n)] print("%d: %s == %s"% ( n, seq_string(digit_strs,separator=" + "), seq_string(factorials,separator=" + ") ) )
def print_spiral(spiral): first_row = max([t[0][0] for t in spiral]) last_row = min([t[0][0] for t in spiral]) width = max([len(str(n)) for n in [t[1] for t in spiral]]) format_str = "%%%ds" % (width) first_col = min([t[0][1] for t in spiral]) last_col = max([t[0][1] for t in spiral]) n_in_row = last_col - first_col + 1 r_ndx = first_row while r_ndx >= last_row: nums = [format_str % (" ")] * n_in_row pts = [t for t in spiral if t[0][0] == r_ndx] pts.sort() for pt in pts: j = pt[0][1] + n_in_row // 2 + n_in_row % 2 - 1 nums[j] = format_str % (pt[1]) print(utils.seq_string(nums)) r_ndx -= 1
def main(): max_len = int(sys.argv[1]) exponent = int(sys.argv[2]) s = set_generator(max_len) total = 0 for t in s: combo = tuple(sorted([i for i in t if i != 0])) v = value(combo, exponent) if v < 2: continue vt = value_tuple(v) if vt == combo: total += v print("%d (%s) from %s" % (v, seq_string(vt), seq_string(combo))) print("Total of all is %d" % (total))
def analyze_text(text, cycle_len): divided = divide_text(text, cycle_len) decrypted = [] for i in range(cycle_len): print(" %2d: %s" % (i, seq_string(divided[i]))) table = compute_freq_table(divided[i]) print(" %s" % (seq_string(table))) poss_space = table[0][1] ^ ord(' ') possible = chr(poss_space) if possible not in string.printable: possible = "**NON PRINTABLE**" print(" Possible: %d : %s" % (poss_space, possible)) decrypted.append([c ^ poss_space for c in divided[i]]) print_text(decrypted)
def main(): max_size = int(sys.argv[1]) seen = compute_sets(max_size) seen.sort() print("%d distinct sets:"%(len(seen))) for t in seen: print(" %s"%(utils.seq_string(t,separator=",")))
def do_recip_calc(r): answer = [] remain = 10 in_nonzero = False done = False seen = [] while not done: d = remain // r logging.debug("remain = %d d = %d in_nonzero = %s. seen: %s" % (remain, d, in_nonzero, str(seen))) if d != 0 and not in_nonzero: in_nonzero = True remain -= d * r remain *= 10 if in_nonzero: try: if remain in seen: done = True if d != 0: if d not in answer: answer.append(d) answer.insert(answer.index(d), '(') answer.append(')') else: seen.append(remain) except Exception as e: print("Died with answer [%s]" % (seq_string(answer))) print(str(e)) sys.exit(-1) if not done: answer.append(d) return "0." + seq_string(answer, separator="")
def main(): max_n = int(sys.argv[1]) primes = [str(p) for p in prime_numbers.primes_less_than(max_n)] len_p = len(primes) primes = filter_impossibles(primes) print("Filter dropped length from %d to %d (%d gone)" % (len_p, len(primes), len_p - len(primes))) p_set = [] while len(primes) > 0: p = primes[0] if check_circle_with_removal(p, primes): p_set += gen_rot_set(p) print("%s is one base (added %d)" % (p, len(gen_rot_set(p)))) print("\n%d circular primes < %d" % (len(p_set), max_n)) p_set = sorted([int(i) for i in p_set]) print(" List: %s" % (seq_string(p_set)))
def do_xwing_pass(self,area_iter_gen,scan_iter_gen): n_eliminated = 0 pair_dict = {} name = area_iter_gen((0,0)).name # Find locked pairs: values that have exactly two possible locaions # in the row/column for i in range(_ROW_LEN): it_dict = {} it = area_iter_gen((i,i)) for ndx in it: for p in self.possibles[ndx]: if p not in it_dict: it_dict[p] = [] it_dict[p].append(ndx) for val in it_dict: if len(it_dict[val]) == 2: if val not in pair_dict: pair_dict[val] = [] pair_dict[val].append((tuple(it_dict[val]),i)) for val in pair_dict: val_dict = {} for ndxs,i in pair_dict[val]: i_pair = (ndxs[0][0],ndxs[1][0]) if ndxs[0][0] == i: i_pair = (ndxs[0][1],ndxs[1][1]) logging.debug(" Locked pair of %d in %s %d: %s: index pair is %s"%\ (val,name,i,seq_string(ndxs),seq_string(i_pair))) if i_pair not in val_dict: val_dict[i_pair] = [] val_dict[i_pair].append((i,ndxs)) for i_pair in val_dict: if len(val_dict[i_pair]) == 2: logging.debug(" Found X-Wing of %d: %s in %s %d and %d"%\ (val,str(i_pair), name,val_dict[i_pair][0][0],val_dict[i_pair][1][0])) useful = False ndx_set = [] for i,ndxs in val_dict[i_pair]: ndx_set.append(ndxs[0]) ndx_set.append(ndxs[1]) for base_ndx in ndx_set[:2]: it = scan_iter_gen(base_ndx) logging.debug(" Scanning %s with base index %s"%(it.name,str(base_ndx))) for ndx in it: if ndx not in ndx_set: if val in self.possibles[ndx]: logging.debug(" X-Wing removing possible %d from cell %d,%d"% \ (val,ndx[0],ndx[1])) self.possibles[ndx].remove(val) useful = True n_eliminated += 1 if useful: logging.debug(" X-Wing was useful") return n_eliminated
def binary_str(n): answer = [] while n > 0: answer.append(n % 2) n //= 2 return seq_string(reversed(answer), separator="")