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.
Name:
Anonymous2012-01-15 7:33
Learn with me...
#include <stdio.h>
#include <stdlib.h>
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'))