Data update
This commit is contained in:
parent
4bb20c9b71
commit
cbaf4c4b64
12390 changed files with 318560 additions and 27248 deletions
35
Task/Pancake-numbers/COBOL/pancake-numbers.cob
Normal file
35
Task/Pancake-numbers/COBOL/pancake-numbers.cob
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
IDENTIFICATION DIVISION.
|
||||
PROGRAM-ID. Pancake.
|
||||
|
||||
DATA DIVISION.
|
||||
WORKING-STORAGE SECTION.
|
||||
01 I PIC 9 VALUE 0.
|
||||
01 J PIC 9 VALUE 0.
|
||||
01 N PIC 99 VALUE 0.
|
||||
01 C PIC 99 VALUE 0.
|
||||
01 P PIC 99 VALUE 0.
|
||||
01 GAP PIC 99 VALUE 2.
|
||||
01 SUMA PIC 99 VALUE 2.
|
||||
01 ADJ PIC S99 VALUE -1.
|
||||
|
||||
PROCEDURE DIVISION.
|
||||
PERFORM VARYING I FROM 0 BY 1 UNTIL I > 3
|
||||
PERFORM VARYING J FROM 1 BY 1 UNTIL J > 5
|
||||
COMPUTE N = (I * 5) + J
|
||||
ADD 1 TO C
|
||||
PERFORM PANCAKE-CALCULATION
|
||||
DISPLAY "P(" N ") = " P
|
||||
END-PERFORM
|
||||
END-PERFORM
|
||||
STOP RUN.
|
||||
|
||||
PANCAKE-CALCULATION.
|
||||
MOVE 2 TO GAP
|
||||
MOVE 2 TO SUMA
|
||||
MOVE -1 TO ADJ
|
||||
PERFORM UNTIL SUMA >= N
|
||||
ADD 1 TO ADJ
|
||||
COMPUTE GAP = (GAP * 2) - 1
|
||||
ADD GAP TO SUMA
|
||||
END-PERFORM
|
||||
COMPUTE P = N + ADJ.
|
||||
|
|
@ -7,19 +7,42 @@ import (
|
|||
"time"
|
||||
)
|
||||
|
||||
type assoc map[string]int
|
||||
const MAX_N = 10
|
||||
|
||||
// Converts a string of the form "[1 2]" into a slice of ints: {1, 2}
|
||||
func asSlice(s string) []int {
|
||||
split := strings.Split(s[1:len(s)-1], " ")
|
||||
le := len(split)
|
||||
res := make([]int, le)
|
||||
for i := 0; i < le; i++ {
|
||||
res[i], _ = strconv.Atoi(split[i])
|
||||
}
|
||||
return res
|
||||
// store sequences in 2 int64s, 5 bits per number
|
||||
// (12 in each int64, ignoring the top 4 bits)
|
||||
// this will support length 24 or less
|
||||
type Sequence struct {
|
||||
num1 int64
|
||||
num2 int64
|
||||
}
|
||||
|
||||
// get the number at position n in the sequence
|
||||
func (s *Sequence) get(n int64) int64 {
|
||||
if n > 12 {
|
||||
n = (n - 12) * 5
|
||||
return (s.num2 & (31 << n)) >> n
|
||||
} else {
|
||||
n *= 5
|
||||
return (s.num1 & (31 << n)) >> n
|
||||
}
|
||||
}
|
||||
|
||||
// set value at position n in the sequence
|
||||
func (s *Sequence) set(n int64, value int64) {
|
||||
if n > 12 {
|
||||
n = (n - 12) * 5
|
||||
var bits int64 = 31 << n
|
||||
s.num2 = (s.num2 | bits) ^ bits | (value << n)
|
||||
} else {
|
||||
n *= 5
|
||||
var bits int64 = 31 << n
|
||||
s.num1 = (s.num1 | bits) ^ bits | (value << n)
|
||||
}
|
||||
}
|
||||
|
||||
type assoc map[Sequence]int
|
||||
|
||||
// Merges two assocs into one. If the same key is present in both assocs
|
||||
// its value will be the one in the second assoc.
|
||||
func merge(m1, m2 assoc) assoc {
|
||||
|
|
@ -35,11 +58,11 @@ func merge(m1, m2 assoc) assoc {
|
|||
|
||||
// Finds the maximum value in 'dict' and returns the first key
|
||||
// it finds (iteration order is undefined) with that value.
|
||||
func findMax(dict assoc) string {
|
||||
func findMax(dict assoc) Sequence {
|
||||
max := -1
|
||||
maxKey := ""
|
||||
var maxKey Sequence
|
||||
for k, v := range dict {
|
||||
if v > max {
|
||||
if v > max {
|
||||
max = v
|
||||
maxKey = k
|
||||
}
|
||||
|
|
@ -47,32 +70,32 @@ func findMax(dict assoc) string {
|
|||
return maxKey
|
||||
}
|
||||
|
||||
// Creates a new slice of ints by reversing an existing one.
|
||||
func reverse(s []int) []int {
|
||||
le := len(s)
|
||||
rev := make([]int, le)
|
||||
for i := 0; i < le; i++ {
|
||||
rev[i] = s[le-1-i]
|
||||
// reverse order up to pos
|
||||
func reorder(stack Sequence, pos int64) Sequence {
|
||||
for i := int64(0); i < pos / 2; i++ {
|
||||
j := pos - i - 1
|
||||
a := stack.get(i)
|
||||
b := stack.get(j)
|
||||
stack.set(i, b)
|
||||
stack.set(j, a)
|
||||
}
|
||||
return rev
|
||||
return stack
|
||||
}
|
||||
|
||||
func pancake(n int) (string, int) {
|
||||
func pancake(n int64) (Sequence, int) {
|
||||
numStacks := 1
|
||||
gs := make([]int, n)
|
||||
for i := 0; i < n; i++ {
|
||||
gs[i] = i + 1
|
||||
|
||||
goalStack := Sequence{}
|
||||
for i := int64(0); i < n; i++ {
|
||||
goalStack.set(i, i+1)
|
||||
}
|
||||
goalStack := fmt.Sprintf("%v", gs)
|
||||
stacks := assoc{goalStack: 0}
|
||||
newStacks := assoc{goalStack: 0}
|
||||
for i := 1; i <= 1000; i++ {
|
||||
for i := 1; ; i++ {
|
||||
nextStacks := assoc{}
|
||||
for key := range newStacks {
|
||||
arr := asSlice(key)
|
||||
for pos := 2; pos <= n; pos++ {
|
||||
t := append(reverse(arr[0:pos]), arr[pos:len(arr)]...)
|
||||
newStack := fmt.Sprintf("%v", t)
|
||||
for stack := range newStacks {
|
||||
for pos := int64(2); pos <= n; pos++ {
|
||||
newStack := reorder(stack, pos)
|
||||
if _, ok := stacks[newStack]; !ok {
|
||||
nextStacks[newStack] = i
|
||||
}
|
||||
|
|
@ -86,15 +109,19 @@ func pancake(n int) (string, int) {
|
|||
}
|
||||
numStacks = perms
|
||||
}
|
||||
return "", 0
|
||||
return Sequence{}, 0
|
||||
}
|
||||
|
||||
func main() {
|
||||
start := time.Now()
|
||||
fmt.Println("The maximum number of flips to sort a given number of elements is:")
|
||||
for i := 1; i <= 10; i++ {
|
||||
for i := int64(1); i <= MAX_N; i++ {
|
||||
example, steps := pancake(i)
|
||||
fmt.Printf("pancake(%2d) = %-2d example: %s\n", i, steps, example)
|
||||
example_strs := make([]string, i)
|
||||
for j := int64(0); j < i; j++ {
|
||||
example_strs[j] = strconv.Itoa(int(example.get(j)))
|
||||
}
|
||||
fmt.Printf("pancake(%2d) = %-2d example: %s\n", i, steps, strings.Join(example_strs, " "))
|
||||
}
|
||||
fmt.Printf("\nTook %s\n", time.Since(start))
|
||||
}
|
||||
|
|
|
|||
61
Task/Pancake-numbers/JavaScript/pancake-numbers.js
Normal file
61
Task/Pancake-numbers/JavaScript/pancake-numbers.js
Normal file
|
|
@ -0,0 +1,61 @@
|
|||
function flipStack(stack, index) {
|
||||
const newStack = [...stack];
|
||||
let start = 0;
|
||||
let end = index - 1;
|
||||
while (start < end) {
|
||||
const temp = newStack[start];
|
||||
newStack[start] = newStack[end];
|
||||
newStack[end] = temp;
|
||||
start++;
|
||||
end--;
|
||||
}
|
||||
return newStack;
|
||||
}
|
||||
function pancake(number) {
|
||||
|
||||
const initialStack = Array.from({ length: number }, (_, i) => i + 1);
|
||||
|
||||
const stackFlips = {};
|
||||
const initialKey = initialStack.join(',');
|
||||
stackFlips[initialKey] = 0;
|
||||
|
||||
const queue = [initialStack];
|
||||
while (queue.length > 0) {
|
||||
const stack = queue.shift();
|
||||
const key = stack.join(',');
|
||||
|
||||
const flips = stackFlips[key] + 1;
|
||||
|
||||
for (let i = 2; i <= number; ++i) {
|
||||
const flipped = flipStack(stack, i);
|
||||
const flippedKey = flipped.join(',');
|
||||
|
||||
if (stackFlips[flippedKey] === undefined) {
|
||||
stackFlips[flippedKey] = flips;
|
||||
queue.push(flipped);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let maxFlips = -1;
|
||||
let worstStackKey = null;
|
||||
for (const key in stackFlips) {
|
||||
if (stackFlips[key] > maxFlips) {
|
||||
maxFlips = stackFlips[key];
|
||||
worstStackKey = key;
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
stack: worstStackKey.split(',').map(Number),
|
||||
flips: maxFlips
|
||||
};
|
||||
}
|
||||
|
||||
for (let n = 1; n <= 8; ++n) {
|
||||
const result = pancake(n);
|
||||
|
||||
const flipsStr = String(result.flips).padStart(2, ' ');
|
||||
|
||||
console.log(`pancake(${n}) = ${flipsStr}. Example [${result.stack.join(', ')}]`);
|
||||
}
|
||||
102
Task/Pancake-numbers/V-(Vlang)/pancake-numbers.v
Normal file
102
Task/Pancake-numbers/V-(Vlang)/pancake-numbers.v
Normal file
|
|
@ -0,0 +1,102 @@
|
|||
struct PancakeSolver {
|
||||
nir int
|
||||
mut:
|
||||
fact []int
|
||||
visited []bool
|
||||
dist []int
|
||||
perm_string []string
|
||||
queue []string
|
||||
}
|
||||
|
||||
fn flip_string(s string, k int) string {
|
||||
mut res := ""
|
||||
for ial := k - 1; ial >= 0; ial-- {
|
||||
res += s[ial].ascii_str()
|
||||
}
|
||||
if k < s.len { res += s[k..] }
|
||||
return res
|
||||
}
|
||||
|
||||
fn (ps PancakeSolver) permutation_rank(s string) int {
|
||||
mut rank := 0
|
||||
for ial := 0; ial < ps.nir - 1; ial++ {
|
||||
mut cnt := 0
|
||||
for j := ial + 1; j < ps.nir; j++ {
|
||||
if s[j] < s[ial] { cnt++ }
|
||||
}
|
||||
rank += cnt * ps.fact[ps.nir - ial - 1]
|
||||
}
|
||||
return rank
|
||||
}
|
||||
|
||||
fn new_pancake_solver(pir int) PancakeSolver {
|
||||
mut vact := []int{len: pir + 1}
|
||||
vact[0] = 1
|
||||
for ial := 1; ial <= pir; ial++ {
|
||||
vact[ial] = vact[ial - 1] * ial
|
||||
}
|
||||
total_states := vact[pir]
|
||||
return PancakeSolver{
|
||||
nir: pir
|
||||
fact: vact
|
||||
visited: []bool{len: total_states}
|
||||
dist: []int{len: total_states}
|
||||
perm_string: []string{len: total_states}
|
||||
queue: []string{len: total_states}
|
||||
}
|
||||
}
|
||||
|
||||
fn (mut ps PancakeSolver) pancake_number() {
|
||||
mut max_dist, mut head, mut tail := 0, 0, 1
|
||||
mut max_perm, mut start := "", ""
|
||||
mut ordered_parts := []string{}
|
||||
for ial := 1; ial <= ps.nir; ial++ {
|
||||
start += ial.str()
|
||||
}
|
||||
start_rank := ps.permutation_rank(start)
|
||||
ps.visited[start_rank] = true
|
||||
ps.dist[start_rank] = 0
|
||||
ps.perm_string[start_rank] = start
|
||||
ps.queue[0] = start
|
||||
for head < tail {
|
||||
pir := ps.queue[head]
|
||||
head++
|
||||
current_rank := ps.permutation_rank(pir)
|
||||
for ial := 2; ial <= ps.nir; ial++ {
|
||||
qir := flip_string(pir, ial)
|
||||
rir := ps.permutation_rank(qir)
|
||||
if !ps.visited[rir] {
|
||||
ps.visited[rir] = true
|
||||
ps.dist[rir] = ps.dist[current_rank] + 1
|
||||
ps.perm_string[rir] = qir
|
||||
ps.queue[tail] = qir
|
||||
tail++
|
||||
}
|
||||
}
|
||||
}
|
||||
for idx, val in ps.visited {
|
||||
if val && ps.dist[idx] > max_dist {
|
||||
max_dist = ps.dist[idx]
|
||||
max_perm = ps.perm_string[idx]
|
||||
}
|
||||
}
|
||||
if max_perm.len == 0 {
|
||||
for ial := 1; ial <= ps.nir; ial++ {
|
||||
max_perm += ial.str()
|
||||
}
|
||||
}
|
||||
max_perm_formatted := max_perm.split("").join("; ")
|
||||
for ial := 1; ial <= ps.nir; ial++ {
|
||||
ordered_parts << ial.str()
|
||||
}
|
||||
ordered := ordered_parts.join("; ")
|
||||
print("Maximum number of flips to sort ${ps.nir} ")
|
||||
println("elements is ${max_dist}. e.g [${max_perm_formatted}] -> [${ordered}]")
|
||||
}
|
||||
|
||||
fn main() {
|
||||
for nal := 1; nal <= 9; nal++ {
|
||||
mut solver := new_pancake_solver(nal)
|
||||
solver.pancake_number()
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue