Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

/prog/ Challenge #9002

Name: Anonymous 2011-05-02 20:12

The task:

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: Anonymous 2012-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;
    }
  }
}


Do I win?

Name: Anonymous 2012-01-15 7:51

>>41
while (n)
while (1)
if (word == limit)

No.

Name: Anonymous 2012-01-15 8:06

>>42 Yeah I just read the exercise, i can't do it other way. Then it seems we're down to recursion.

Name: Anonymous 2012-01-15 8:16

Check my dubs.

Name: Anonymous 2012-01-15 8:17

Name: Anonymous 2012-01-15 8:51

>>43
No, because without a conditional statement in your recursive function it's an infinite loop.

Name: Anonymous 2012-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: Anonymous 2012-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).

Name: Anonymous 2012-01-15 10:24

>>39

perl lyf

also stupid thread

Name: Anonymous 2012-01-15 15:34

>>39

wowo

Name: Anonymous 2012-01-15 16:56

#include <stdio.h>

#define X s[j--] = c % 26 + 'a'; c /= 26;
#define I i = s[0] = s[1] = s[2] = s[3] = s[4] = s[5] = 0;
#define P10 c = i++; j = 0; X; puts(s);
#define P20 c = i++; j = 1; X; X; puts(s);
#define P30 c = i++; j = 2; X; X; X; puts(s);
#define P40 c = i++; j = 3; X; X; X; X; puts(s);
#define P50 c = i++; j = 4; X; X; X; X; X; puts(s);

#define P11 P10 P10 P10 P10 P10 P10 P10 P10 P10 P10 P10 P10 P10 P10 P10 P10 P10 P10 P10 P10 P10 P10 P10 P10 P10 P10
#define P21 P20 P20 P20 P20 P20 P20 P20 P20 P20 P20 P20 P20 P20 P20 P20 P20 P20 P20 P20 P20 P20 P20 P20 P20 P20 P20
#define P22 P21 P21 P21 P21 P21 P21 P21 P21 P21 P21 P21 P21 P21 P21 P21 P21 P21 P21 P21 P21 P21 P21 P21 P21 P21 P21
#define P31 P30 P30 P30 P30 P30 P30 P30 P30 P30 P30 P30 P30 P30 P30 P30 P30 P30 P30 P30 P30 P30 P30 P30 P30 P30 P30
#define P32 P31 P31 P31 P31 P31 P31 P31 P31 P31 P31 P31 P31 P31 P31 P31 P31 P31 P31 P31 P31 P31 P31 P31 P31 P31 P31
#define P33 P32 P32 P32 P32 P32 P32 P32 P32 P32 P32 P32 P32 P32 P32 P32 P32 P32 P32 P32 P32 P32 P32 P32 P32 P32 P32
#define P41 P40 P40 P40 P40 P40 P40 P40 P40 P40 P40 P40 P40 P40 P40 P40 P40 P40 P40 P40 P40 P40 P40 P40 P40 P40 P40
#define P42 P41 P41 P41 P41 P41 P41 P41 P41 P41 P41 P41 P41 P41 P41 P41 P41 P41 P41 P41 P41 P41 P41 P41 P41 P41 P41
#define P43 P42 P42 P42 P42 P42 P42 P42 P42 P42 P42 P42 P42 P42 P42 P42 P42 P42 P42 P42 P42 P42 P42 P42 P42 P42 P42
#define P44 P43 P43 P43 P43 P43 P43 P43 P43 P43 P43 P43 P43 P43 P43 P43 P43 P43 P43 P43 P43 P43 P43 P43 P43 P43 P43
#define P51 P50 P50 P50 P50 P50 P50 P50 P50 P50 P50 P50 P50 P50 P50 P50 P50 P50 P50 P50 P50 P50 P50 P50 P50 P50 P50
#define P52 P51 P51 P51 P51 P51 P51 P51 P51 P51 P51 P51 P51 P51 P51 P51 P51 P51 P51 P51 P51 P51 P51 P51 P51 P51 P51
#define P53 P52 P52 P52 P52 P52 P52 P52 P52 P52 P52 P52 P52 P52 P52 P52 P52 P52 P52 P52 P52 P52 P52 P52 P52 P52 P52
#define P54 P53 P53 P53 P53 P53 P53 P53 P53 P53 P53 P53 P53 P53 P53 P53 P53 P53 P53 P53 P53 P53 P53 P53 P53 P53 P53
#define P55 P54 P54 P54 P54 P54 P54 P54 P54 P54 P54 P54 P54 P54 P54 P54 P54 P54 P54 P54 P54 P54 P54 P54 P54 P54 P54

int main() {
  int i, j, c; char s[6];
  I; P11; I; P22; I; P33; I; P44; I; P55;
  return 0;
}


Works, theoretically. gcc runs out of memory; I imagine most other compilers do, too.

Name: Anonymous 2012-01-15 17:52



const char charmap[28] = {" abcdefghijklmnopqrstuvwxyz"};

int placeholder[2];


int recurthis(){

placeholder[0]++;

placeholder[1] += placeholder[0] == 28;

placeholder[0] = (placeholder[0]<27) * (placeholder[0] +1);

printf("%c%c\n", charmap[placeholder[1]], charmap[placeholder[0]]);

recurthis();
}


still pretty sketchy though

Name: Anonymous 2012-01-15 18:00

**doing +1 twice ,oops

should halt once it asks for charmap[29] ..?

Name: Anonymous 2012-01-15 18:22

>>39

HELLO FROM RUBY

puts ('a'..'zzzzz').to_a

BYE LOL

Name: Anonymous 2012-01-15 18:31

Solve the problem by converting to bases. But you run into the problem of the character set is base 27 not base 26.

So here's some hacky perl that doesn't use a lot of memory. For the rest of you, why did you over complicate it?

 my $i = 0;
 my $l = "";
 while ($l ne "zzzzz") {
     eval {
         $l = convert($i);
         print $l,$/;
     };
     $i++;
 }
 sub convert {
     my ($in) = @_;
     my $mod = $in%27;
     if ($mod == 0) { die "Fuckit"; }
     my $diff = $in - $mod;
     my $new = $diff / 27;
     my $c = ((' ','a'..'z')[$mod]);
     return ($new==0)?$c:convert($new).$c;
 }

Name: Anonymous 2012-01-15 18:32

>>54
This.

{$ time echo "puts ('a'..'zzzzz').to_a" | ruby > /dev/null

real    0m8.908s
user    0m8.169s
sys     0m0.730s}

No. Forget what I said.

Name: Anonymous 2012-01-15 18:36

Hey guys, map is a loop.

Name: Anonymous 2012-01-15 18:37

>>55
well that's fine if you ignore the requirements of no loops or conditionals

Name: Anonymous 2012-01-15 18:40

>>55
>if ($mod == 0
>no conditionals

Really faggot?

Name: Anonymous 2012-01-15 18:43

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: Anonymous 2012-01-15 18:54

Is tihs even possible?

Name: Anonymous 2012-01-15 18:55

Conditionals in perl:
grep
&&
||
while
if
unless
// #regexes

Loops in perl:
for
while
..
map
x

Name: Anonymous 2012-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: Anonymous 2012-01-15 19:01

He didn't say anything about putting a try and catch exception for the recursion

Name: Anonymous 2012-01-15 19:20

>>63

Yeah that was the part I couldn't solve. You can't end the recursive solution without a condition. I'm looking forward to see a solution.

Name: Anonymous 2012-01-15 19:30

Recursion not counting as a loop is just stupid.

Name: Anonymous 2012-01-15 19:34

>>66
Yeah, I agree.

Name: Anonymous 2012-01-15 20:21

You can trivially end the recursion by dividing by zero or some such, for example, and exiting in the signal handler.

Name: Anonymous 2014-04-06 0:42

69 GET

Name: Anonymous 2014-04-06 6:14

>>8
Aaaaand... Haskell is king once again.

Name: Anonymous 2014-04-07 7:32

Since when is /prog/ full of idiots?

Here, have a trivial and fast solution in Python{2,3} (same algorithm can be easily implemented in most other good languages):


from __future__ import print_function

def f1(fs, i, w):
    w('a')
    w('b')
    w('c')
    w('d')
    w('e')
    w('f')
    w('g')
    w('h')
    w('i')
    w('j')
    w('k')
    w('l')
    w('m')
    w('n')
    w('o')
    w('p')
    w('q')
    w('r')
    w('s')
    w('t')
    w('u')
    w('v')
    w('w')
    w('x')
    w('y')
    w('z')

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'))

def main():
    fs = [None, f1, fn, fn, fn]
    f1(fs, 0, print)
    fn(fs, 1, print)
    fn(fs, 2, print)
    fn(fs, 3, print)
    fn(fs, 4, print)

if __name__ == '__main__':
    main()

Name: Anonymous 2014-04-07 7:34

>>71
Accidental sage, sorry.

Name: Anonymous 2014-04-07 8:15

>>71
Nice conditional, ``accidental'' pedophile.

Name: Anonymous 2014-04-07 8:25

>Shitty Project Euler-relaed problems

This whole thread

Name: Anonymous 2014-04-07 11:24

>>16
what language is this?

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List