Another update from ingydotnet^djgoku

This commit is contained in:
Ingy döt Net 2015-11-18 06:14:39 +00:00
parent 91df62d461
commit 948b86eafa
7604 changed files with 108452 additions and 22726 deletions

View file

@ -0,0 +1,43 @@
#include <iostream>
#include <string>
#include <map>
#include <vector>
#include <algorithm>
std::map<char, int> _map;
std::vector<std::string> _result;
size_t longest = 0;
void make_sequence( std::string n ) {
_map.clear();
for( std::string::iterator i = n.begin(); i != n.end(); i++ )
_map.insert( std::make_pair( *i, _map[*i]++ ) );
std::string z;
for( std::map<char, int>::reverse_iterator i = _map.rbegin(); i != _map.rend(); i++ ) {
char c = ( *i ).second + 48;
z.append( 1, c );
z.append( 1, i->first );
}
if( longest <= z.length() ) {
longest = z.length();
if( std::find( _result.begin(), _result.end(), z ) == _result.end() ) {
_result.push_back( z );
make_sequence( z );
}
}
}
int main( int argc, char* argv[] ) {
std::vector<std::string> tests;
tests.push_back( "9900" ); tests.push_back( "9090" ); tests.push_back( "9009" );
for( std::vector<std::string>::iterator i = tests.begin(); i != tests.end(); i++ ) {
make_sequence( *i );
std::cout << "[" << *i << "] Iterations: " << _result.size() + 1 << "\n";
for( std::vector<std::string>::iterator j = _result.begin(); j != _result.end(); j++ ) {
std::cout << *j << "\n";
}
std::cout << "\n\n";
}
return 0;
}

View file

@ -0,0 +1,198 @@
class
SELF_REFERENTIAL_SEQUENCE
create
make
feature
make
local
i: INTEGER
length, max: INTEGER_64
do
create seed_value.make
create sequence.make (25)
create permuted_values.make
from
i := 1
until
i > 1000000
loop
length := check_length (i.out)
if length > max then
max := length
seed_value.wipe_out
seed_value.extend (i)
elseif length = max then
seed_value.extend (i)
end
sequence.wipe_out
i := next_ascending (i).to_integer
end
io.put_string ("Maximal length: " + max.out)
io.put_string ("%NSeed Value: %N")
across
seed_value as s
loop
permute (s.item.out, 1)
end
across
permuted_values as p
loop
io.put_string (p.item + "%N")
end
io.put_string ("Sequence:%N")
max := check_length (seed_value [1].out)
across
sequence as s
loop
io.put_string (s.item)
io.new_line
end
end
next_ascending (n: INTEGER_64): STRING
-- Next number with ascending digits after 'n'.
-- Numbers with trailing zeros are treated as ascending numbers.
local
st: STRING
first, should_be, zero: STRING
i: INTEGER
do
create Result.make_empty
create zero.make_empty
st := (n + 1).out
from
until
st.count < 2
loop
first := st.at (1).out
if st [2] ~ '0' then
from
i := 3
until
i > st.count
loop
zero.append ("0")
i := i + 1
end
Result.append (first + first + zero)
st := ""
else
should_be := st.at (2).out
if first > should_be then
should_be := first
end
st.remove_head (2)
st.prepend (should_be)
Result.append (first)
end
end
if st.count > 0 then
Result.append (st [st.count].out)
end
end
feature {NONE}
seed_value: SORTED_TWO_WAY_LIST [INTEGER]
permuted_values: SORTED_TWO_WAY_LIST [STRING]
sequence: ARRAYED_LIST [STRING]
permute (a: STRING; k: INTEGER)
-- All permutations of 'a'.
require
count_positive: a.count > 0
k_valid_index: k > 0
local
t: CHARACTER
b: STRING
found: BOOLEAN
do
across
permuted_values as p
loop
if p.item ~ a then
found := True
end
end
if k = a.count and a [1] /= '0' and not found then
create b.make_empty
b.deep_copy (a)
permuted_values.extend (b)
else
across
k |..| a.count as c
loop
t := a [k]
a [k] := a [c.item]
a [c.item] := t
permute (a, k + 1)
t := a [k]
a [k] := a [c.item]
a [c.item] := t
end
end
end
check_length (i: STRING): INTEGER_64
-- Length of the self referential sequence starting with 'i'.
local
found: BOOLEAN
j: INTEGER
s: STRING
do
create s.make_from_string (i)
from
until
found
loop
sequence.extend (s)
s := next (s)
from
j := sequence.count - 1
until
j < 1
loop
if sequence [j] ~ s then
found := True
end
j := j - 1
end
end
Result := sequence.count
end
next (n: STRING): STRING
-- Next item after 'n' in a self referential sequence.
local
i, count: INTEGER
counter: ARRAY [INTEGER]
do
create counter.make_filled (0, 0, 9)
create Result.make_empty
from
i := 1
until
i > n.count
loop
count := n [i].out.to_integer
counter [count] := counter [count] + 1
i := i + 1
end
from
i := 9
until
i < 0
loop
if counter [i] > 0 then
Result.append (counter [i].out + i.out)
end
i := i - 1
end
end
end