Print strings from "a" to "zzzzz" without using any loop or conditional statements. Don't just write all 1000 permutations out by hand. The output should look like this: a
b
c
...
aa
ab
ac
...
zzzzx
zzzzy
zzzzz
inb4 lipthfags and dead dogs using some obscure functionality of their obscure languages.
lol its over nien tousand so randum xD lolol u so cool u call ppl fags xD
This is not /g/. Leave /prog/ and never come back.
Name:
12011-05-02 20:14
Don't just write all 1000 permutations out by hand.
Please ignore "1000", that shouldn't be there.
Don't just write all permutations out by hand.
Name:
Anonymous2011-05-02 20:16
>>2
I only named it 9002 because the last one was named 9001. I wanted to keep it chronological order, despite obviously not being the actual 9002nd challenge.
This should print "aaaaa" -> "zzzzz", I was going to alter it to do "a" -> "zzzzz" after testing it, but I can't really test it as its been compiling for about 10 minutes now.
#include <iostream>
template<char a, char b, char c, char d, char e>
struct permutator
{
static void print()
{
std::cout << a << b << c << d << e << std::endl;
permutator<a, b, c, d, e+1>::print();
}
};
template<char a, char b, char c, char d>
struct permutator<a, b, c, d, 'z'>
{
static void print()
{
std::cout << a << b << c << d << 'z' << std::endl;
permutator<a, b, c, d+1, 'a'>::print();
}
};
template<char a, char b, char c>
struct permutator<a, b, c, 'z', 'z'>
{
static void print()
{
std::cout << a << b << c << "zz" << std::endl;
permutator<a, b, c+1, 'a', 'a'>::print();
}
};
([X~] (['a'..'z'], ['a'..'z', ''] xx 4))>>.fmt('%5s').sort.uniq>>.say;
Name:
Anonymous2011-05-03 1:22
Pretty sure this uses absolutely no loops or conditionals... But it requires a huge stack to run without crashing. I haven't run it to the end so I'm not even sure if a stack size of 100,000,000 is big enough.
#pragma comment(linker, "/stack:100000000")
#include <iostream>
#include <cstdlib>
template<typename T>
struct true_define
{
typedef T (*is_true)(T);
};
template<typename T>
T is_true1(T x)
{
return x;
}
template<typename T>
T is_true2(T x)
{
return 1;
}
template<typename T, int I>
struct check_bits
{
static T check(T x, T cur, typename true_define<T>::is_true tests[2])
{
cur = tests[cur]((x >> I) & 1);
return check_bits<T, I-1>::check(x, cur, tests);
}
};
template<typename T>
struct check_bits<T, 0>
{
static T check(T x, T cur, typename true_define<T>::is_true tests[2])
{
cur = tests[cur](x & 1);
return cur;
}
};
>>21
By connecting SIGFPE to an exit function and dividing some dummy volatile value with ('z' - c0) + ('z' - c1) + ('z' - c2) + ('z' - c3) + ('z' - c4), you could make it terminate at the right time.
void recursive_increment(char* p)
{
incrementer inc(p+4, 5);
inc.if_(p[4] / 'z');
print_string(p);
__asm {
pop eax;
pop eax;
pop eax;
pop eax;
pop eax;
pop eax;
pop eax;
pop eax;
}
recursive_increment(p);
}
>>23
I have to admit that you might be correct here. I could have also used a recursion with main() (or rather a separate function) instead of the goto here, though, which, by definition, is not a loop, but gets unstable on my machine and quits after ~37000 loops, so I'd rather use the goto.
Anyways, both most likely create a JMP in asm, anyway. I'm not even sure if this is possible without, since we're basically talking about a looping/repeating behaviour (PRINT(X) Y TIMES)...
>>22
>By connecting SIGFPE to an exit function
If you tell me how, I'll do it :3
Breaking out by causing a div by zero is working but getting a runtime error isn't so nice.
(defun integer->base-n-list (i n &optional append-on)
(if (zerop i) (or append-on (list 0))
(multiple-value-bind (div mod) (truncate i n)
(integer->base-n-list div n (cons mod append-on)))))
(defun base-n-list->integer (list n)
(reduce #'(lambda (i next) (+ (* n i) next)) list :initial-value 0))
;; this would be fast if most CL compilers didn't suck at
;; numbered ecases (unless you make a macro that properly writes a jumptable using an array or hashtable)
(defun integer->letter (i)
(meta
`(ecase i
,@(loop for x from 0 to 25 collect
`(,x ,(code-char (+ x (char-code #\a))))))))
;; so I write this instead
(defun integer->letter (i)
(if (<= 0 i 25)
(code-char (+ (char-code #\a) i))
(error "Character out of range")))
(defun letter->integer (char)
(let ((n (- (char-code char) (char-code #\a))))
(if (<= 0 n 25) n (error "Character out of range"))))
(defun all-letter-permutations (&key (start "a") (end "zzzzz"))
(let ((start-n (base26a->integer start))
(end-n (base26a->integer end)))
(loop for i from start-n to end-n collect (integer->base26a i))))
;; this will obviously generate a huge function and waste lots of memory
;; like: (progn (format t "~A~&" "a") ... (format t "~A~&" "a"))
(meta
`(progn ,@(mapcar #'(lambda (s) `(format t "~A~&" ,s))
(all-letter-permutations))))
;; so let's make something cleaner which
;; just does (princ *hugestringwithallpermutations*)
(meta
`(princ
,(with-output-to-string (stream)
(dolist (x (all-letter-permutations))
(princ x stream)
(terpri stream)))))
Half the code here is redundant, but it does the job.
It generates the output at compile time then generates the code which prints it.
I'm not writing a C and x86 assembly version this time around, even though it wouldn't be hard at all.
>>22 >>23 >>26 >>30
Ok, thanks for all your tips but I did it somewhat different now. Hers's the new code, with cheap ASM, function pointing and actual spaces instead of null characters: #include <stdio.h>
...yes, this compiles for me. I'm using TCC/Win32 for compiling, so ASM is in GAS/AT&T format. The function pointing didn't work like this for me in GCC, though.
I also removed the return statement, since this might be interpreted as a jump/loop/whatever. Only calls now, no JMPs.
void display_word(unsigned long long word, int n)
{
char *buffer = malloc (n+1); // Allocates enough space for word in buffer
buffer[n] = '\0'; // Make string end with \0
while (n)
{
buffer[--n] = word % 26 + 'A';
word /= 26;
}
printf ("%s\n", buffer); // Displays the word
free (buffer);
}
int main ()
{
int length = 1;
unsigned long long word = 0;
unsigned long long limit = 26;
while (1)
{
display_word (word++, length);
if (word == limit)
{
word = 0;
length++;
limit *= 26;
}
}
}
>>43
No, because without a conditional statement in your recursive function it's an infinite loop.
Name:
Anonymous2012-01-15 9:52
Here's the idiomatic prolog version with loops and conditionals.
straz(0,[]).
straz(N,[A|T]) :- code_type(A,lower), 0'a =< A, A =< 0'z, succ(Nm1,N), straz(Nm1,T).
progchl :- findall([], (between(1,5,L), straz(L,Z), format("~s~n",[Z])), _J).
Name:
Anonymous2012-01-15 10:05
A variation that with length/2 instead of countdown recursion.
straz([]).
straz([A|T]) :- code_type(A,lower), 0'a =< A, A =< 0'z, straz(T).
progchl :- findall([], (between(1,5,L), length(S, L), straz(S), format("~s~n",[S])), _J).
The ultimate problem with challenges like this is you'll probably end up with conditionals somewhere anyway. The compiled machine code, or the definition for printf, etc. -- there's no clear way to specify what exactly is allowed in which languages.
Name:
Anonymous2012-01-15 18:54
Is tihs even possible?
Name:
Anonymous2012-01-15 18:55
Conditionals in perl:
grep
&&
||
while
if
unless
// #regexes
Loops in perl:
for
while
..
map
x
Name:
Anonymous2012-01-15 18:58
>>61
Yeah with lookup tables or array initialization, or something that is a conditional but doesn't smell like it.
A purely recursive solution requires a decision in it to terminate so have fun with that.. well except the challenge never asked for termination.
Name:
Anonymous2012-01-15 19:01
He didn't say anything about putting a try and catch exception for the recursion
def fn(fs, i, w):
p = lambda l: (lambda s: w(l + s))
f = fs[i]
i -= 1
f(fs, i, p('a'))
f(fs, i, p('b'))
f(fs, i, p('c'))
f(fs, i, p('d'))
f(fs, i, p('e'))
f(fs, i, p('f'))
f(fs, i, p('g'))
f(fs, i, p('h'))
f(fs, i, p('i'))
f(fs, i, p('j'))
f(fs, i, p('k'))
f(fs, i, p('l'))
f(fs, i, p('m'))
f(fs, i, p('n'))
f(fs, i, p('o'))
f(fs, i, p('p'))
f(fs, i, p('q'))
f(fs, i, p('r'))
f(fs, i, p('s'))
f(fs, i, p('t'))
f(fs, i, p('u'))
f(fs, i, p('v'))
f(fs, i, p('w'))
f(fs, i, p('x'))
f(fs, i, p('y'))
f(fs, i, p('z'))