# Check that gofmt run on the list of files does not change them
def CheckGofmt(ui, repo, files, just_warn=False):
- files = [f for f in files if f.startswith('src/') and f.endswith('.go')]
+ files = [f for f in files if (f.startswith('src/') or f.startswith('test/bench/')) and f.endswith('.go')]
if not files:
return
cwd = os.getcwd()
err = commands.postincoming(ui, repo, modheads, True, "tip")
if err:
return err
+ commands.update(ui, repo)
sync_changes(ui, repo)
def sync_note(msg):
var n = flag.Int("n", 15, "depth")
type Node struct {
- item int;
- left, right *Node;
+ item int;
+ left, right *Node;
}
type Arena struct {
- head *Node
+ head *Node;
}
var arena Arena
func (a *Arena) New(item int, left, right *Node) *Node {
if a.head == nil {
- nodes := make([]Node, 3 << uint(*n));
+ nodes := make([]Node, 3<<uint(*n));
for i := 0; i < len(nodes)-1; i++ {
- nodes[i].left = &nodes[i+1];
+ nodes[i].left = &nodes[i+1]
}
a.head = &nodes[0];
}
return n;
}
-func bottomUpTree(item, depth int) *Node {
+func bottomUpTree(item, depth int) *Node {
if depth <= 0 {
return arena.New(item, nil, nil)
}
- return arena.New(item, bottomUpTree(2*item-1, depth-1), bottomUpTree(2*item, depth-1))
+ return arena.New(item, bottomUpTree(2*item-1, depth-1), bottomUpTree(2*item, depth-1));
}
func (n *Node) itemCheck() int {
return n.item + n.left.itemCheck() - n.right.itemCheck();
}
-const minDepth = 4;
+const minDepth = 4
func main() {
flag.Parse();
maxDepth := *n;
- if minDepth + 2 > *n {
+ if minDepth+2 > *n {
maxDepth = minDepth + 2
}
stretchDepth := maxDepth + 1;
longLivedTree := bottomUpTree(0, maxDepth);
- for depth := minDepth; depth <= maxDepth; depth+=2 {
- iterations := 1 << uint(maxDepth - depth + minDepth);
+ for depth := minDepth; depth <= maxDepth; depth += 2 {
+ iterations := 1 << uint(maxDepth-depth+minDepth);
check = 0;
for i := 1; i <= iterations; i++ {
- t := bottomUpTree(i,depth);
+ t := bottomUpTree(i, depth);
check += t.itemCheck();
t.free();
- t = bottomUpTree(-i,depth);
+ t = bottomUpTree(-i, depth);
check += t.itemCheck();
t.free();
}
var n = flag.Int("n", 15, "depth")
type Node struct {
- item int;
- left, right *Node;
+ item int;
+ left, right *Node;
}
-func bottomUpTree(item, depth int) *Node {
+func bottomUpTree(item, depth int) *Node {
if depth <= 0 {
return &Node{item: item}
}
- return &Node{ item, bottomUpTree(2*item-1, depth-1), bottomUpTree(2*item, depth-1) }
+ return &Node{item, bottomUpTree(2*item-1, depth-1), bottomUpTree(2*item, depth-1)};
}
func (n *Node) itemCheck() int {
return n.item + n.left.itemCheck() - n.right.itemCheck();
}
-const minDepth = 4;
+const minDepth = 4
func main() {
flag.Parse();
maxDepth := *n;
- if minDepth + 2 > *n {
+ if minDepth+2 > *n {
maxDepth = minDepth + 2
}
stretchDepth := maxDepth + 1;
longLivedTree := bottomUpTree(0, maxDepth);
- for depth := minDepth; depth <= maxDepth; depth+=2 {
- iterations := 1 << uint(maxDepth - depth + minDepth);
+ for depth := minDepth; depth <= maxDepth; depth += 2 {
+ iterations := 1 << uint(maxDepth-depth+minDepth);
check = 0;
for i := 1; i <= iterations; i++ {
- check += bottomUpTree(i,depth).itemCheck();
- check += bottomUpTree(-i,depth).itemCheck();
+ check += bottomUpTree(i, depth).itemCheck();
+ check += bottomUpTree(-i, depth).itemCheck();
}
fmt.Printf("%d\t trees of depth %d\t check: %d\n", iterations*2, depth, check);
}
func fannkuch(n int) int {
if n < 1 {
- return 0;
+ return 0
}
n1 := n - 1;
count := make([]int, n);
for i := 0; i < n; i++ {
- perm1[i] = i; // initial (trivial) permutation
+ perm1[i] = i // initial (trivial) permutation
}
r := n;
didpr := 0;
flipsMax := 0;
for {
- if didpr < 30 {
+ if didpr < 30 {
for i := 0; i < n; i++ {
- fmt.Printf("%d", 1+perm1[i]);
+ fmt.Printf("%d", 1+perm1[i])
}
fmt.Printf("\n");
didpr++;
}
for ; r != 1; r-- {
- count[r-1] = r;
+ count[r-1] = r
}
if perm1[0] != 0 && perm1[n1] != n1 {
flips := 0;
for i := 1; i < n; i++ { // perm = perm1
- perm[i] = perm1[i];
+ perm[i] = perm1[i]
}
- k := perm1[0]; // cache perm[0] in k
- for { // k!=0 ==> k>0
+ k := perm1[0]; // cache perm[0] in k
+ for { // k!=0 ==> k>0
for i, j := 1, k-1; i < j; i, j = i+1, j-1 {
- perm[i], perm[j] = perm[j], perm[i];
+ perm[i], perm[j] = perm[j], perm[i]
}
flips++;
// Now exchange k (caching perm[0]) and perm[k]... with care!
- j := perm[k]; perm[k] = k; k = j;
+ j := perm[k];
+ perm[k] = k;
+ k = j;
if k == 0 {
break
}
}
if flipsMax < flips {
- flipsMax = flips;
+ flipsMax = flips
}
}
// rotate down perm[0..r] by one
perm0 := perm1[0];
for i := 0; i < r; i++ {
- perm1[i] = perm1[i+1];
+ perm1[i] = perm1[i+1]
}
perm1[r] = perm0;
count[r]--;
if count[r] > 0 {
- break;
+ break
}
}
if r == n {
- return flipsMax;
+ return flipsMax
}
}
- return 0
+ return 0;
}
func main() {
pos := 0;
s2 := make([]byte, len(s)+WIDTH);
copy(s2, s);
- copy(s2[len(s):len(s2)], s);
+ copy(s2[len(s):], s);
for count > 0 {
line := min(WIDTH, count);
out.Write(s2[pos : pos+line]);
var in *bufio.Reader
-func count(data string, n int) map[string] int {
- counts := make(map[string] int);
+func count(data string, n int) map[string]int {
+ counts := make(map[string]int);
top := len(data) - n;
for i := 0; i <= top; i++ {
- s := data[i:i+n];
+ s := data[i : i+n];
if k, ok := counts[s]; ok {
- counts[s] = k+1
+ counts[s] = k + 1
} else {
counts[s] = 1
}
}
- return counts
+ return counts;
}
func countOne(data string, s string) int {
if i, ok := counts[s]; ok {
return i
}
- return 0
+ return 0;
}
type kNucArray []kNuc
-func (kn kNucArray) Len() int { return len(kn) }
-func (kn kNucArray) Swap(i, j int) { kn[i], kn[j] = kn[j], kn[i] }
+func (kn kNucArray) Len() int { return len(kn) }
+func (kn kNucArray) Swap(i, j int) { kn[i], kn[j] = kn[j], kn[i] }
func (kn kNucArray) Less(i, j int) bool {
if kn[i].count == kn[j].count {
return kn[i].name > kn[j].name // sort down
}
- return kn[i].count > kn[j].count
+ return kn[i].count > kn[j].count;
}
-func sortedArray(m map[string] int) kNucArray {
+func sortedArray(m map[string]int) kNucArray {
kn := make(kNucArray, len(m));
i := 0;
for k, v := range m {
return kn;
}
-func print(m map[string] int) {
+func print(m map[string]int) {
a := sortedArray(m);
sum := 0;
for _, kn := range a {
- sum += kn.count;
+ sum += kn.count
}
for _, kn := range a {
- fmt.Printf("%s %.3f\n", kn.name, 100*float64(kn.count)/float64(sum));
+ fmt.Printf("%s %.3f\n", kn.name, 100*float64(kn.count)/float64(sum))
}
}
os.Exit(2);
}
if line[0] == '>' && bytes.Equal(line[0:len(three)], three) {
- break;
+ break
}
}
data, err := io.ReadAll(in);
for i := 0; i < len(data); i++ {
if data[i] != '\n' {
data[j] = data[i] &^ ' '; // upper case
- j++
+ j++;
}
}
str := string(data[0:j]);
interests := []string{"GGT", "GGTA", "GGTATT", "GGTATTTTAATT", "GGTATTTTAATTTATAGT"};
for _, s := range interests {
- fmt.Printf("%d %s\n", countOne(str, s), s);
+ fmt.Printf("%d %s\n", countOne(str, s), s)
}
}
*
* contributed by The Go Authors.
* Based on mandelbrot.c contributed by Greg Buchholz
-*/
+ */
package main
fmt.Fprintf(out, "P4\n%d %d\n", w, h);
for y := 0; y < h; y++ {
- for x := 0; x<w; x++ {
+ for x := 0; x < w; x++ {
Zr, Zi, Tr, Ti := Zero, Zero, Zero, Zero;
Cr := (2*float64(x)/float64(w) - 1.5);
Ci := (2*float64(y)/float64(h) - 1.0);
byte_acc <<= 1;
if Tr+Ti <= Limit*Limit {
- byte_acc |= 0x01;
+ byte_acc |= 0x01
}
bit_num++;
byte_acc = 0;
bit_num = 0;
} else if x == w-1 {
- byte_acc <<= uint(8-w%8);
+ byte_acc <<= uint(8 - w%8);
out.WriteByte(byte_acc);
byte_acc = 0;
bit_num = 0;
if b {
return 1
}
- return 0
+ return 0;
}
/* The board is a 50 cell hexagonal pattern. For . . . . .
* . . . . .
*/
-var board uint64 = 0xFFFC000000000000
+var board uint64 = 0xFFFC000000000000
/* The puzzle pieces must be specified by the path followed
* from one end to the other along 12 hexagonal directions.
*/
const (
- E = iota;
+ E = iota;
ESE;
SE;
S;
PIVOT;
)
-var piece_def = [10][4]int8 {
- [4]int8{ E, E, E, SE},
- [4]int8{ SE, E, NE, E},
- [4]int8{ E, E, SE, SW},
- [4]int8{ E, E, SW, SE},
- [4]int8{ SE, E, NE, S},
- [4]int8{ E, E, SW, E},
- [4]int8{ E, SE, SE, NE},
- [4]int8{ E, SE, SE, W},
- [4]int8{ E, SE, E, E},
- [4]int8{ E, E, E, SW}
+var piece_def = [10][4]int8{
+ [4]int8{E, E, E, SE},
+ [4]int8{SE, E, NE, E},
+ [4]int8{E, E, SE, SW},
+ [4]int8{E, E, SW, SE},
+ [4]int8{SE, E, NE, S},
+ [4]int8{E, E, SW, E},
+ [4]int8{E, SE, SE, NE},
+ [4]int8{E, SE, SE, W},
+ [4]int8{E, SE, E, E},
+ [4]int8{E, E, E, SW},
}
* location to reduce the burden on the solve function.
*/
var (
- pieces[10][50][12] uint64;
- piece_counts[10][50] int;
- next_cell[10][50][12] int8;
+ pieces [10][50][12]uint64;
+ piece_counts [10][50]int;
+ next_cell [10][50][12]int8;
)
/* Returns the direction rotated 60 degrees clockwise */
-func rotate(dir int8) int8 {
- return (dir + 2) % PIVOT;
-}
+func rotate(dir int8) int8 { return (dir + 2) % PIVOT }
/* Returns the direction flipped on the horizontal axis */
-func flip(dir int8) int8 {
- return (PIVOT - dir) % PIVOT;
-}
+func flip(dir int8) int8 { return (PIVOT - dir) % PIVOT }
/* Returns the new cell index from the specified cell in the
func shift(cell, dir int8) int8 {
switch dir {
case E:
- return cell + 1;
+ return cell + 1
case ESE:
if ((cell / 5) % 2) != 0 {
- return cell + 7;
+ return cell + 7
} else {
- return cell + 6;
+ return cell + 6
}
case SE:
if ((cell / 5) % 2) != 0 {
- return cell + 6;
+ return cell + 6
} else {
- return cell + 5;
+ return cell + 5
}
case S:
- return cell + 10;
+ return cell + 10
case SW:
if ((cell / 5) % 2) != 0 {
- return cell + 5;
+ return cell + 5
} else {
- return cell + 4;
+ return cell + 4
}
case WSW:
if ((cell / 5) % 2) != 0 {
- return cell + 4;
+ return cell + 4
} else {
- return cell + 3;
+ return cell + 3
}
case W:
- return cell - 1;
+ return cell - 1
case WNW:
- if ((cell / 5) % 2) != 0{
- return cell - 6;
+ if ((cell / 5) % 2) != 0 {
+ return cell - 6
} else {
- return cell - 7;
+ return cell - 7
}
case NW:
- if ((cell / 5) % 2) != 0{
- return cell - 5;
+ if ((cell / 5) % 2) != 0 {
+ return cell - 5
} else {
- return cell - 6;
+ return cell - 6
}
case N:
- return cell - 10;
+ return cell - 10
case NE:
- if ((cell / 5) % 2) != 0{
- return cell - 4;
+ if ((cell / 5) % 2) != 0 {
+ return cell - 4
} else {
- return cell - 5;
+ return cell - 5
}
case ENE:
- if ((cell / 5) % 2) != 0{
- return cell - 3;
+ if ((cell / 5) % 2) != 0 {
+ return cell - 3
} else {
- return cell - 4;
+ return cell - 4
}
}
return cell;
* location or not.
*/
func out_of_bounds(cell, dir int8) bool {
- switch(dir) {
+ switch dir {
case E:
- return cell % 5 == 4;
+ return cell%5 == 4
case ESE:
i := cell % 10;
return i == 4 || i == 8 || i == 9 || cell >= 45;
case SE:
- return cell % 10 == 9 || cell >= 45;
+ return cell%10 == 9 || cell >= 45
case S:
- return cell >= 40;
+ return cell >= 40
case SW:
- return cell % 10 == 0 || cell >= 45;
+ return cell%10 == 0 || cell >= 45
case WSW:
i := cell % 10;
return i == 0 || i == 1 || i == 5 || cell >= 45;
case W:
- return cell % 5 == 0;
+ return cell%5 == 0
case WNW:
i := cell % 10;
return i == 0 || i == 1 || i == 5 || cell < 5;
case NW:
- return cell % 10 == 0 || cell < 5;
+ return cell%10 == 0 || cell < 5
case N:
- return cell < 10;
+ return cell < 10
case NE:
- return cell % 10 == 9 || cell < 5;
+ return cell%10 == 9 || cell < 5
case ENE:
i := cell % 10;
return i == 4 || i == 8 || i == 9 || cell < 5;
/* Rotate a piece 60 degrees clockwise */
func rotate_piece(piece int) {
for i := 0; i < 4; i++ {
- piece_def[piece][i] = rotate(piece_def[piece][i]);
+ piece_def[piece][i] = rotate(piece_def[piece][i])
}
}
/* Flip a piece along the horizontal axis */
func flip_piece(piece int) {
for i := 0; i < 4; i++ {
- piece_def[piece][i] = flip(piece_def[piece][i]);
+ piece_def[piece][i] = flip(piece_def[piece][i])
}
}
func calc_cell_indices(cell []int8, piece int, index int8) {
cell[0] = index;
for i := 1; i < 5; i++ {
- cell[i] = shift(cell[i-1], piece_def[piece][i-1]);
+ cell[i] = shift(cell[i-1], piece_def[piece][i-1])
}
}
/* Convenience function to quickly calculate if a piece fits on the board */
func cells_fit_on_board(cell []int8, piece int) bool {
return !out_of_bounds(cell[0], piece_def[piece][0]) &&
- !out_of_bounds(cell[1], piece_def[piece][1]) &&
- !out_of_bounds(cell[2], piece_def[piece][2]) &&
- !out_of_bounds(cell[3], piece_def[piece][3]);
+ !out_of_bounds(cell[1], piece_def[piece][1]) &&
+ !out_of_bounds(cell[2], piece_def[piece][2]) &&
+ !out_of_bounds(cell[3], piece_def[piece][3])
}
/* Returns the lowest index of the cells of a piece.
func first_empty_cell(cell []int8, minimum int8) int8 {
first_empty := minimum;
for first_empty == cell[0] || first_empty == cell[1] ||
- first_empty == cell[2] || first_empty == cell[3] ||
- first_empty == cell[4] {
- first_empty++;
+ first_empty == cell[2] || first_empty == cell[3] ||
+ first_empty == cell[4] {
+ first_empty++
}
return first_empty;
}
func bitmask_from_cells(cell []int8) uint64 {
var piece_mask uint64;
for i := 0; i < 5; i++ {
- piece_mask |= 1 << uint(cell[i]);
+ piece_mask |= 1 << uint(cell[i])
}
return piece_mask;
}
*/
func fill_contiguous_space(board []int8, index int8) {
if board[index] == 1 {
- return;
+ return
}
board[index] = 1;
if !out_of_bounds(index, E) {
- fill_contiguous_space(board, shift(index, E));
+ fill_contiguous_space(board, shift(index, E))
}
if !out_of_bounds(index, SE) {
- fill_contiguous_space(board, shift(index, SE));
+ fill_contiguous_space(board, shift(index, SE))
}
if !out_of_bounds(index, SW) {
- fill_contiguous_space(board, shift(index, SW));
+ fill_contiguous_space(board, shift(index, SW))
}
if !out_of_bounds(index, W) {
- fill_contiguous_space(board, shift(index, W));
+ fill_contiguous_space(board, shift(index, W))
}
if !out_of_bounds(index, NW) {
- fill_contiguous_space(board, shift(index, NW));
+ fill_contiguous_space(board, shift(index, NW))
}
if !out_of_bounds(index, NE) {
- fill_contiguous_space(board, shift(index, NE));
+ fill_contiguous_space(board, shift(index, NE))
}
}
temp_board := make([]int8, 50);
var i int;
for i = 0; i < 5; i++ {
- temp_board[cell[i]] = 1;
+ temp_board[cell[i]] = 1
}
i = 49;
for temp_board[i] == 1 {
- i--;
+ i--
}
fill_contiguous_space(temp_board, int8(i));
c := 0;
for i = 0; i < 50; i++ {
if temp_board[i] == 0 {
- c++;
+ c++
}
}
if c == 0 || (c == 5 && piece == 8) || (c == 40 && piece == 8) ||
- (c % 5 == 0 && piece == 0) {
- return false;
+ (c%5 == 0 && piece == 0) {
+ return false
}
return true;
}
}
-
/* Calculate all 32 possible states for a 5-bit row and all rows that will
* create islands that follow any of the 32 possible rows. These pre-
* calculated 5-bit rows will be used to find islands in a partially solved
* board in the solve function.
*/
- const (
- ROW_MASK = 0x1F;
- TRIPLE_MASK = 0x7FFF;
+const (
+ ROW_MASK = 0x1F;
+ TRIPLE_MASK = 0x7FFF;
)
+
var (
- all_rows = [32]int8{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
- 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31};
- bad_even_rows [32][32]int8;
- bad_odd_rows [32][32]int8;
- bad_even_triple [32768]int8;
- bad_odd_triple [32768]int8;
+ all_rows = [32]int8{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
+ 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
+ };
+ bad_even_rows [32][32]int8;
+ bad_odd_rows [32][32]int8;
+ bad_even_triple [32768]int8;
+ bad_odd_triple [32768]int8;
)
func rows_bad(row1, row2 int8, even bool) int8 {
var row2_shift int8;
/* Test for blockages at same index and shifted index */
if even {
- row2_shift = ((row2 << 1) & ROW_MASK) | 0x01;
+ row2_shift = ((row2 << 1) & ROW_MASK) | 0x01
} else {
- row2_shift = (row2 >> 1) | 0x10;
+ row2_shift = (row2 >> 1) | 0x10
}
block := ((row1 ^ row2) & row2) & ((row1 ^ row2_shift) & row2_shift);
/* Test for groups of 0's */
in_zeroes := false;
group_okay := false;
for i := uint8(0); i < 5; i++ {
- if row1 & (1 << i) != 0 {
+ if row1&(1<<i) != 0 {
if in_zeroes {
if !group_okay {
- return 1;
+ return 1
}
in_zeroes = false;
group_okay = false;
}
} else {
if !in_zeroes {
- in_zeroes = true;
+ in_zeroes = true
}
if (block & (1 << i)) == 0 {
- group_okay = true;
+ group_okay = true
}
}
}
if in_zeroes {
- return boolInt(!group_okay);
+ return boolInt(!group_okay)
}
return 0;
}
* row3: 011?? 00110 ????? ?????
*/
return ((row1 == 0x03) && (row2 == 0x0B) && ((row3 & 0x1C) == 0x0C)) ||
- ((row1 == 0x01) && (row2 == 0x05) && (row3 == 0x06)) ||
- ((row1 == 0x19) && (row2 == 0x11)) ||
- ((row1 == 0x15) && (row2 == 0x11));
+ ((row1 == 0x01) && (row2 == 0x05) && (row3 == 0x06)) ||
+ ((row1 == 0x19) && (row2 == 0x11)) ||
+ ((row1 == 0x15) && (row2 == 0x11))
}
/* There are two cases:
* row1: 10011 10101
* row3: ????? ?????
*/
return ((row1 == 0x13) && (row2 == 0x11)) ||
- ((row1 == 0x15) && (row2 == 0x11));
+ ((row1 == 0x15) && (row2 == 0x11));
}
func calc_rows() {
for row3 := 0; row3 < 32; row3++ {
result1 := bad_even_rows[row1][row2];
result2 := bad_odd_rows[row2][row3];
- if result1==0 && result2!=0 && triple_is_okay(row1, row2, row3, true) {
- bad_even_triple[row1+(row2*32)+(row3*1024)] = 0;
+ if result1 == 0 && result2 != 0 && triple_is_okay(row1, row2, row3, true) {
+ bad_even_triple[row1+(row2*32)+(row3*1024)] = 0
} else {
- bad_even_triple[row1+(row2*32)+(row3*1024)] = boolInt(result1!=0 || result2!=0);
+ bad_even_triple[row1+(row2*32)+(row3*1024)] = boolInt(result1 != 0 || result2 != 0)
}
result1 = bad_odd_rows[row1][row2];
result2 = bad_even_rows[row2][row3];
- if result1==0 && result2!=0 && triple_is_okay(row1, row2, row3, false) {
- bad_odd_triple[row1+(row2*32)+(row3*1024)] = 0;
+ if result1 == 0 && result2 != 0 && triple_is_okay(row1, row2, row3, false) {
+ bad_odd_triple[row1+(row2*32)+(row3*1024)] = 0
} else {
- bad_odd_triple[row1+(row2*32)+(row3*1024)] = boolInt(result1!=0 || result2!=0);
+ bad_odd_triple[row1+(row2*32)+(row3*1024)] = boolInt(result1 != 0 || result2 != 0)
}
}
}
}
-
/* Calculate islands while solving the board.
- */
+*/
func boardHasIslands(cell int8) int8 {
/* Too low on board, don't bother checking */
if cell >= 40 {
- return 0;
+ return 0
}
- current_triple := (board >> uint((cell / 5) * 5)) & TRIPLE_MASK;
- if (cell / 5) % 2 != 0 {
- return bad_odd_triple[current_triple];
+ current_triple := (board >> uint((cell/5)*5)) & TRIPLE_MASK;
+ if (cell/5)%2 != 0 {
+ return bad_odd_triple[current_triple]
}
return bad_even_triple[current_triple];
}
* array if a solution is found.
*/
var (
- avail uint16 = 0x03FF;
- sol_nums [10]int8;
- sol_masks [10]uint64;
- solutions [2100][50]int8;
- solution_count = 0;
+ avail uint16 = 0x03FF;
+ sol_nums [10]int8;
+ sol_masks [10]uint64;
+ solutions [2100][50]int8;
+ solution_count = 0;
)
func record_solution() {
- for sol_no := 0; sol_no < 10; sol_no++ {
+ for sol_no := 0; sol_no < 10; sol_no++ {
sol_mask := sol_masks[sol_no];
for index := 0; index < 50; index++ {
- if sol_mask & 1 == 1 {
+ if sol_mask&1 == 1 {
solutions[solution_count][index] = sol_nums[sol_no];
/* Board rotated 180 degrees is a solution too! */
solutions[solution_count+1][49-index] = sol_nums[sol_no];
func solve(depth, cell int8) {
if solution_count >= *max_solutions {
- return;
+ return
}
- for board & (1 << uint(cell)) != 0 {
- cell++;
+ for board&(1<<uint(cell)) != 0 {
+ cell++
}
- for piece := int8(0); piece < 10; piece++ {
+ for piece := int8(0); piece < 10; piece++ {
var piece_no_mask uint16 = 1 << uint(piece);
- if avail & piece_no_mask == 0 {
- continue;
+ if avail&piece_no_mask == 0 {
+ continue
}
avail ^= piece_no_mask;
max_rots := piece_counts[piece][cell];
piece_mask := pieces[piece][cell];
for rotation := 0; rotation < max_rots; rotation++ {
- if board & piece_mask[rotation] == 0 {
+ if board&piece_mask[rotation] == 0 {
sol_nums[depth] = piece;
sol_masks[depth] = piece_mask[rotation];
if depth == 9 {
}
board |= piece_mask[rotation];
if boardHasIslands(next_cell[piece][cell][rotation]) == 0 {
- solve(depth + 1, next_cell[piece][cell][rotation]);
+ solve(depth+1, next_cell[piece][cell][rotation])
}
board ^= piece_mask[rotation];
}
func pretty(b *[50]int8) {
for i := 0; i < 50; i += 10 {
fmt.Printf("%c %c %c %c %c \n %c %c %c %c %c \n", b[i]+'0', b[i+1]+'0',
- b[i+2]+'0', b[i+3]+'0', b[i+4]+'0', b[i+5]+'0', b[i+6]+'0',
- b[i+7]+'0', b[i+8]+'0', b[i+9]+'0');
+ b[i+2]+'0', b[i+3]+'0', b[i+4]+'0', b[i+5]+'0', b[i+6]+'0',
+ b[i+7]+'0', b[i+8]+'0', b[i+9]+'0')
}
fmt.Printf("\n");
}
continue
}
if c < s {
- smallest = candidate;
+ smallest = candidate
}
break;
}
continue
}
if c > s {
- largest = candidate;
+ largest = candidate
}
break;
}
var n = flag.Int("n", 1000, "number of iterations")
type Body struct {
- x, y, z, vx, vy, vz, mass float64
+ x, y, z, vx, vy, vz, mass float64;
}
const (
- solarMass = 4 * math.Pi * math.Pi;
- daysPerYear = 365.24;
+ solarMass = 4 * math.Pi * math.Pi;
+ daysPerYear = 365.24;
)
-func (b *Body) offsetMomentum(px, py, pz float64){
+func (b *Body) offsetMomentum(px, py, pz float64) {
b.vx = -px / solarMass;
b.vy = -py / solarMass;
b.vz = -pz / solarMass;
for i, body := range sys {
e += 0.5 * body.mass *
(body.vx*body.vx + body.vy*body.vy + body.vz*body.vz);
- for j := i+1; j < len(sys); j++ {
+ for j := i + 1; j < len(sys); j++ {
body2 := sys[j];
dx := body.x - body2.x;
dy := body.y - body2.y;
func (sys System) advance(dt float64) {
for i, body := range sys {
- for j := i+1; j < len(sys); j++ {
+ for j := i + 1; j < len(sys); j++ {
body2 := sys[j];
dx := body.x - body2.x;
dy := body.y - body2.y;
}
var (
- jupiter = Body {
+ jupiter = Body{
x: 4.84143144246472090e+00,
y: -1.16032004402742839e+00,
z: -1.03622044471123109e-01,
vz: -6.90460016972063023e-05 * daysPerYear,
mass: 9.54791938424326609e-04 * solarMass,
};
- saturn = Body {
+ saturn = Body{
x: 8.34336671824457987e+00,
y: 4.12479856412430479e+00,
z: -4.03523417114321381e-01,
vz: 2.30417297573763929e-05 * daysPerYear,
mass: 2.85885980666130812e-04 * solarMass,
};
- uranus = Body {
+ uranus = Body{
x: 1.28943695621391310e+01,
y: -1.51111514016986312e+01,
z: -2.23307578892655734e-01,
vz: -2.96589568540237556e-05 * daysPerYear,
mass: 4.36624404335156298e-05 * solarMass,
};
- neptune = Body {
+ neptune = Body{
x: 1.53796971148509165e+01,
y: -2.59193146099879641e+01,
z: 1.79258772950371181e-01,
vz: -9.51592254519715870e-05 * daysPerYear,
mass: 5.15138902046611451e-05 * solarMass,
};
- sun = Body {
- mass: solarMass
- }
+ sun = Body{
+ mass: solarMass,
+ };
)
func main() {
"fmt";
)
-var n = flag.Int("n", 27, "number of digits");
-var silent = flag.Bool("s", false, "don't print result");
+var n = flag.Int("n", 27, "number of digits")
+var silent = flag.Bool("s", false, "don't print result")
var (
- tmp1 *bignum.Integer;
- tmp2 *bignum.Integer;
- numer = bignum.Int(1);
- accum = bignum.Int(0);
- denom = bignum.Int(1);
+ tmp1 *bignum.Integer;
+ tmp2 *bignum.Integer;
+ numer = bignum.Int(1);
+ accum = bignum.Int(0);
+ denom = bignum.Int(1);
)
func extract_digit() int64 {
if numer.Cmp(accum) > 0 {
- return -1;
+ return -1
}
// Compute (numer * 3 + accum) / denom
// ... is normalized, then the two divisions have the same result.
if tmp2.Cmp(denom) >= 0 {
- return -1;
+ return -1
}
return tmp1.Value();
func printf(s string, arg ...) {
if !*silent {
- fmt.Printf(s, arg);
+ fmt.Printf(s, arg)
}
}
d = extract_digit();
}
- printf("%c", d + '0');
+ printf("%c", d+'0');
i++;
- m = i%10;
+ m = i % 10;
if m == 0 {
- printf("\t:%d\n", i);
+ printf("\t:%d\n", i)
}
if i >= *n {
- break;
+ break
}
eliminate_digit(d);
}
if m > 0 {
- printf("%s\t:%d\n", " "[m : 10], *n);
+ printf("%s\t:%d\n", " "[m:10], *n)
}
}
"strings";
)
-var variants = []string {
+var variants = []string{
"agggtaaa|tttaccct",
"[cgt]gggtaaa|tttaccc[acg]",
"a[act]ggtaaa|tttacc[agt]t",
}
type Subst struct {
- pat, repl string
+ pat, repl string;
}
-var substs = [] Subst {
- Subst {"B", "(c|g|t)"},
- Subst {"D", "(a|g|t)"},
- Subst {"H", "(a|c|t)"},
- Subst {"K", "(g|t)"},
- Subst {"M", "(a|c)"},
- Subst {"N", "(a|c|g|t)"},
- Subst {"R", "(a|g)"},
- Subst {"S", "(c|g)"},
- Subst {"V", "(a|c|g)"},
- Subst {"W", "(a|t)"},
- Subst {"Y", "(c|t)"},
+var substs = []Subst{
+ Subst{"B", "(c|g|t)"},
+ Subst{"D", "(a|g|t)"},
+ Subst{"H", "(a|c|t)"},
+ Subst{"K", "(g|t)"},
+ Subst{"M", "(a|c)"},
+ Subst{"N", "(a|c|g|t)"},
+ Subst{"R", "(a|g)"},
+ Subst{"S", "(c|g)"},
+ Subst{"V", "(a|c|g)"},
+ Subst{"W", "(a|t)"},
+ Subst{"Y", "(c|t)"},
}
func countMatches(pat string, bytes []byte) int {
for {
e := re.Execute(bytes);
if len(e) == 0 {
- break;
+ break
}
n++;
- bytes = bytes[e[1]:len(bytes)];
+ bytes = bytes[e[1]:];
}
return n;
}
bytes = regexp.MustCompile("(>[^\n]+)?\n").ReplaceAll(bytes, []byte{});
clen := len(bytes);
for _, s := range variants {
- fmt.Printf("%s %d\n", s, countMatches(s, bytes));
+ fmt.Printf("%s %d\n", s, countMatches(s, bytes))
}
for _, sub := range substs {
- bytes = regexp.MustCompile(sub.pat).ReplaceAll(bytes, strings.Bytes(sub.repl));
+ bytes = regexp.MustCompile(sub.pat).ReplaceAll(bytes, strings.Bytes(sub.repl))
}
fmt.Printf("\n%d\n%d\n%d\n", ilen, clen, len(bytes));
}
line, err := in.ReadSlice('\n');
for err == nil {
os.Stdout.Write(line);
-
+
// Accumulate reversed complement in buf[w:]
nchar := 0;
w := len(buf);
for {
line, err = in.ReadSlice('\n');
if err != nil || line[0] == '>' {
- break;
+ break
}
- line = line[0:len(line)-1];
+ line = line[0 : len(line)-1];
nchar += len(line);
if len(line)+nchar/60+128 >= w {
nbuf := make([]byte, len(buf)*5);
- copy(nbuf[len(nbuf)-len(buf):len(nbuf)], buf);
+ copy(nbuf[len(nbuf)-len(buf):], buf);
w += len(nbuf) - len(buf);
buf = nbuf;
}
buf[w] = complement[line[r]];
}
}
-
+
// Copy down to beginning of buffer, inserting newlines.
// The loop left room for the newlines and 128 bytes of padding.
i := 0;
for j := w; j < len(buf); j += 60 {
- n := copy(buf[i:i+60], buf[j:len(buf)]);
+ n := copy(buf[i:i+60], buf[j:]);
buf[i+n] = '\n';
- i += n+1;
+ i += n + 1;
}
os.Stdout.Write(buf[0:i]);
}
var n = flag.Int("n", 2000, "count")
var nCPU = flag.Int("ncpu", 4, "number of cpus")
-func evalA(i, j int) float64 {
- return 1 / float64(((i + j)*(i + j + 1)/2 + i + 1));
-}
+func evalA(i, j int) float64 { return 1 / float64(((i+j)*(i+j+1)/2 + i + 1)) }
type Vec []float64
for ; i < n; i++ {
v[i] = 0;
for j := 0; j < len(u); j++ {
- v[i] += evalA(i, j)*u[j];
+ v[i] += evalA(i, j) * u[j]
}
}
c <- 1;
for ; i < n; i++ {
v[i] = 0;
for j := 0; j < len(u); j++ {
- v[i] += evalA(j, i)*u[j];
+ v[i] += evalA(j, i) * u[j]
}
}
c <- 1;
x := make(Vec, len(u));
c := make(chan int, *nCPU);
for i := 0; i < *nCPU; i++ {
- go x.Times(i*len(v) / *nCPU, (i+1)*len(v) / *nCPU, u, c);
+ go x.Times(i*len(v) / *nCPU, (i+1)*len(v) / *nCPU, u, c)
}
wait(c);
for i := 0; i < *nCPU; i++ {
- go v.TimesTransp(i*len(v) / *nCPU, (i+1)*len(v) / *nCPU, x, c);
+ go v.TimesTransp(i*len(v) / *nCPU, (i+1)*len(v) / *nCPU, x, c)
}
wait(c);
}
N := *n;
u := make(Vec, N);
for i := 0; i < N; i++ {
- u[i] = 1;
+ u[i] = 1
}
v := make(Vec, N);
for i := 0; i < 10; i++ {
}
var vBv, vv float64;
for i := 0; i < N; i++ {
- vBv += u[i]*v[i];
- vv += v[i]*v[i];
+ vBv += u[i] * v[i];
+ vv += v[i] * v[i];
}
fmt.Printf("%0.9f\n", math.Sqrt(vBv/vv));
}
var n = flag.Int("n", 2000, "count")
-func evalA(i, j int) float64 {
- return 1 / float64(((i + j)*(i + j + 1)/2+ i + 1));
-}
+func evalA(i, j int) float64 { return 1 / float64(((i+j)*(i+j+1)/2 + i + 1)) }
type Vec []float64
for i := 0; i < len(v); i++ {
v[i] = 0;
for j := 0; j < len(u); j++ {
- v[i] += evalA(i, j)*u[j];
+ v[i] += evalA(i, j) * u[j]
}
}
}
for i := 0; i < len(v); i++ {
v[i] = 0;
for j := 0; j < len(u); j++ {
- v[i] += evalA(j, i)*u[j];
+ v[i] += evalA(j, i) * u[j]
}
}
}
N := *n;
u := make(Vec, N);
for i := 0; i < N; i++ {
- u[i] = 1;
+ u[i] = 1
}
v := make(Vec, N);
for i := 0; i < 10; i++ {
}
var vBv, vv float64;
for i := 0; i < N; i++ {
- vBv += u[i]*v[i];
- vv += v[i]*v[i];
+ vBv += u[i] * v[i];
+ vv += v[i] * v[i];
}
fmt.Printf("%0.9f\n", math.Sqrt(vBv/vv));
}
fmt.Printf("%d\n", i);
os.Exit(0);
}
- out <- n - 1
+ out <- n-1;
}
}