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

Pages: 1-

fun challenge

Name: Anonymous 2008-05-28 20:15

Write a program that takes an integer as input and writes the integer in English.

ie:

enter integer: 23
twenty-three.

Here's my code in common lisp:

(format t "~r" (read))

Name: Anonymous 2008-05-28 20:26

You're lying, it's not fun.

Name: Anonymous 2008-05-28 20:29

>>2
Exactly. This is a challenge against fun.

Name: Anonymous 2008-05-28 20:40

Preliminary attempt, I'm sure this could be made a lot more efficient.
char *ttt[1000]={ "zero", "one", "two", "three", ... "ten", "eleven", "twelve", ...
 "twenty", "twenty-one", "twenty-two", ... "ninety-nine", "one hundred", ...
 "nine hundred and ninety-nine" };
char *uuu[3] = { "thousand", "million", "billion" };

void eng_int(int n) {
 if(n && n<0) {
  printf("negative ");
  n = -n;
 }
 if(n>=1000000000) {
  printf("%s billion ",ttt[n/1000000000]);
  n-=(n/1000000000)*1000000000;
 }
 if(n>=1000000) {
  printf("%s million ",ttt[n/1000000]);
  n-=(n/1000000)*1000000;
 }
 if(n>=1000) {
  printf("%s thousand ",ttt[n/1000]);
  n-=(n/1000)*1000;
 }
 printf("%s",ttt[n]);
}

Name: Anonymous 2008-05-28 21:03

if(n && n<0)
Facepalm.hs

Name: Anonymous 2008-05-28 21:10

>>5
what's so 'facepalm' about it?

Name: Anonymous 2008-05-28 21:12

>>2,3
Not if you funroll loops

Name: HAX MY PREDICATE MEME FAN 2008-05-28 21:15

>>6
n <= 0

Name: Anonymous 2008-05-28 21:17

>>1
Obviously your homework.
DON'T FUCKING HELP HIM

Name: HAX MY FAN MEME 2008-05-28 21:18

>>8
Wait, I sucked a cock. n<0 is enough.

Name: Anonymous 2008-05-28 21:19

>>10
``hax my fan'' is not a meme.

>>9
Quite boring and pointless homework

Name: Anonymous 2008-05-28 21:23

>>11
I disagree. I happen to have a fan for anus.

Name: fun challenge 2008-05-28 21:38


Write a program that takes a list of lists of alternatives and print each choice combination possible

ie:

enter list: [[1,2,3], [4,5]]
[1,4]
[1,5]
[2,4]
[2,5]
[3,4]
[3,5]

Here's my code in Haskell, the pancreasless doggy:

mapM_ print . foldr (ap . map (:)) [[]] . read =<< getLine

Name: Anonymous 2008-05-28 22:03


english n = if n<0 then "negative " ++ english (-n)
            else if a>0 then k 3 a ++ u
            else g n
  where
  (a,b) = divMod n 1000
  u = if b>0 then if b<100 then ", and " ++ g b else ", " ++ g b else ""

  k i n | a>0 = k (i+3) a ++ if b>0 then ", " ++ g b ++ ' ' : tes i else ""
        | otherwise = if b>0 then g b ++ ' ' : tes i else ""
        where (a,b) = divMod n 1000

g n | n<20 = ones n
    | n<100 = if b0>0 then tens a0 ++ '-' : ones b0 else tens a0
    | b1>0 = ones a1 ++ " hundred and " ++ g b1
    | otherwise = ones a1 ++ " hundred"
    where
    (a0,b0) = divMod n 10
    (a1,b1) = divMod n 100

Name: Anonymous 2008-05-28 22:03

>>9
It'd be profoundly uninteresting homework. There's no real algorithm there, just typing.

Name: Anonymous 2008-05-28 22:05


ones 0 = "zero"
ones 1 = "one"
ones 2 = "two"
ones 3 = "three"
ones 4 = "four"
ones 5 = "five"
ones 6 = "six"
ones 7 = "seven"
ones 8 = "eight"
ones 9 = "nine"
ones 10 = "ten"
ones 11 = "eleven"
ones 12 = "twelve"
ones 13 = "thirteen"
ones 14 = "fourteen"
ones 15 = "fifteen"
ones 16 = "sixteen"
ones 17 = "seventeen"
ones 18 = "eighteen"
ones 19 = "nineteen"

tens 2 = "twenty"
tens 3 = "thirty"
tens 4 = "forty"
tens 5 = "fifty"
tens 6 = "sixty"
tens 7 = "seventy"
tens 8 = "eighty"
tens 9 = "ninety"

tes 3 = "thousand"
tes 6 = "million"
tes 9 = "billion"
tes 12 = "trillion"
tes 15 = "quadrillion"
tes ... and so on. Maybe a nice little function here instead of quintillion and so on.

Name: Anonymous 2008-05-29 3:21

#include <inttypes.h>
#include <limits.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>

int main(){
 char *ones[9] = { "one", "two", "three", "four", "five", "six", "seven",
  "eight", "nine" };
 char *teens[10] = { "ten", "eleven", "twelve", "thirteen", "fourteen",
  "fifteen", "sixteen", "seventeen", "eighteen", "nineteen" };
 char *tens[9] = { "ten", "twenty", "thirty", "forty", "fifty", "sixty",
  "seventy", "eighty", "ninety" };
 char *rest[6] = { "thousand", "million", "billion", "trillion", "quadrillion",
  "quintillion" };
 size_t len;
 printf("enter integer: ");
 char *line = fgetln(stdin, &len);
 line[len] = 0;
 intmax_t num = strtoimax(line, NULL, 0);
 if(!num){
  puts("zero.");
  return 0;
 }
 if(num < 0){
  num *= -1;
  printf("negative ");
 }
 int parts[7];
 int last = 0;
 for(int i = 0; i < 7; ++i){
  parts[i] = num % 1000;
  if(!parts[i] && last == i) ++last;
  num /= 1000;
 }
 for(int i = 6; i >= 0; --i){
  if(parts[i]){
   int n = parts[i];
   int o = n % 10;
   int t = (n / 10) % 10;
   int h = (n / 100) % 10;
   if(h) printf("%s %s%s", ones[h - 1], "hundred", t || o ? " " : "");
   switch(t){
    case 0:
     if(o) printf("%s", ones[o - 1]);
     break;
    case 1:
     printf("%s", teens[o]);
     break;
    default:
     if(o) printf( "%s-%s", tens[t - 1], ones[o - 1]);
     else printf("%s", tens[t - 1]);
   }
   if(i) printf(" %s%s", rest[i - 1], i == last ? "" : " ");
  }
 }
 puts(".");
 return 0;
}

Name: Anonymous 2008-05-29 3:27

>>13
*> mapM_ print . foldr (ap . map (:)) [[]] . read =<< getLine
[[1,2,3],[4,5]]
*** Exception: Prelude.read: no parse


My solution: putStr . unlines . map (show :: [Int]->String) . sequence =<< readLn

Name: Anonymous 2008-05-29 5:27

I seem to remember an elegant recursive solution in TAoCP

Name: Anonymous 2008-05-29 6:12

>>19
Tay-oh See Pee

Name: Anonymous 2008-05-29 7:53

Java:

private String getOnes(int a)(
if (a == 1) {
return "one";
} else if (a == 2) {
return "two";
...
}
private String getTens(int a) {
if (a>19 && a<30) {
return "twenty " + getOnes(a-20);
} else if(a>29 && a<40) {
...
}
private String getHundrets(int a) {
return getOnes(a%100) + " hundret " + getTens(a-a%100);
}

private String getThousands(int a) {
return getOnes(a%1000) + " thousand " + getHundrets(a-a%1000);
}
and so on, and so on.

public static void main(String[] args) {
int a = Integer.parseInt(args[0]);
if (a>0) {
System.out.println(a + " - " getWhateverTheHighestNumberYouHaveManagetToWrite(a);
} else if (a<0) {
System.out.println(a + " - minus " getWhateverTheHighestNumberYouHaveManagetToWrite(-a);
} else if (a == 0) {
System.out.println(a + " - zero");
}

Name: Anonymous 2008-05-29 10:21

>>18
   ___         ___ _
  / _ \ /\  /\/ __(_)
 / /_\// /_/ / /  | |      GHC Interactive, version 6.6, for Haskell 98.
/ /_\\/ __  / /___| |      http://www.haskell.org/ghc/
\____/\/ /_/\____/|_|      Type :? for help.

Loading package base ... linking ... done.
Prelude> :m Monad
Prelude Monad> mapM_ print . foldr (ap . map (:)) [[]] . read =<< getLine
[[1,2,3],[4,5]]
[1,4]
[1,5]
[2,4]
[2,5]
[3,4]
[3,5]
Prelude Monad> Leaving GHCi.

Name: Anonymous 2008-05-29 10:23

Prelude Monad> Leaving GHCi.
<interactive>:1:13: parse error (possibly incorrect indentation)
Prelude Monad>


>:(

Name: Anonymous 2008-05-29 13:26

List-based

import Char
import Control.Arrow
import List

main = interact (unlines . map translate . lines)

translate ('-': num) = case translateAbs num of abs@"zero" -> abs
                                                abs        -> "minus " ++ abs
translate ('+': num) = translateAbs num
translate num        = translateAbs num

translateAbs num
   | length num > (3 * 22) = error "translate: Number too long"
   | otherwise =
        foldl1 ((++) . (++ ", ")) . checkZero . reverse . filter (not . null) . zipWith suffix suffixes
                                  . map translateTrio . splitInTrios $ map digitToInt num
   where suffix s  []  = []
         suffix [] htu = htu
         suffix s  htu = htu ++ " " ++ s

checkZero [] = ["zero"]
checkZero n  = n

splitInTrios =
   takeWhile (not . null) . loop . reverse
   where loop = uncurry (:) . (reverse *** loop) . splitAt 3

translateTrio [u]   = units !! u
translateTrio [t,u] =
   case (t, u) of (0, u) -> translateTrio [u]
                  (1, u) -> teens !! u
                  (t, 0) -> tens !! t
                  (t, u) -> tens !! t ++ "-" ++ translateTrio [u]

translateTrio [h,t,u] =
   case (h, translateTrio [t, u]) of (0, tu) -> tu
                                     (h, []) -> hundreds !! h
                                     (h, tu) -> hundreds !! h ++ " and " ++ tu

units = [[],    "one",    "two",    "three",    "four",     "five",    "six",     "seven",     "eight",    "nine"]
tens  = [[],    "ten",    "twenty", "thirty",   "forty",    "fifty",   "sixty",   "seventy",   "eighty",   "ninety"]
teens = ["ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"]

hundreds = map (++ " hundred") units

suffixes = [[], "thousand"] ++ illions

illions = map (++ "illion") ([    "m",  "b",   "tr",  "quadr",    "quint", "sext", "sept",   "oct",  "non"]
       ++ map (++ "dec")     [[], "un", "duo", "tre", "quattuor", "quin",  "sex",  "septen", "octo", "novem"] ++ ["vingint"])

Name: Anonymous 2008-05-29 15:13

>>24
"minus "
FAIL.

Name: Anonymous 2008-05-29 15:14

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>

static int well_formed(const char *s) {
    return *s && strspn(s, "0123456789") == strlen(s);
}

static const char *Prog = "ausschreib";

static void complain(const char *about, const char *reason) {
    fprintf(stderr, "%s: %s: %s\n", Prog, about, reason);
}

static char *getline(FILE *fp) {
    char *line;
    size_t size, length;
    int c;

    if (!(line = malloc(size = 120))) {
        complain("malloc() failed", strerror(errno));
        return NULL;
    }
    length = 0;
    while ((c = getc(fp)) != EOF && c != '\n') {
        if (length + 1u >= size) {
            char *const tmp = realloc(line, size * 2u);
            if (!tmp) {
                complain("realloc() failed", strerror(errno));
                free(line);
                return NULL;
            }
            line = tmp;
            size *= 2u;
        }
        line[length++] = c;
    }

    if (ferror(fp)) {
        complain("getc()", strerror(errno));
    }
    if (c == EOF && length == 0) {
        free(line);
        return NULL;
    }

    line[length] = '\0';
    return line;
}

static void say(const char *s) {
    static const char *num[][2] = {
        { NULL, NULL },
        { "ein",    "zehn"     },
        { "zwei",   "zwanzig"  },
        { "drei",   "dreissig" },
        { "vier",   "vierzig"  },
        { "fuenf",  "fuenfzig" },
        { "sechs",  "sechzig"  },
        { "sieben", "siebzig"  },
        { "acht",   "achtzig"  },
        { "neun",   "neunzig"  },
    };
    static const char *unit[] = {
        "tausend", "m", "b", "tr", "quadr", "quint", "sext", "sept",
        "okt", "non", "dez", "undez", "duodez", "tredez",
        "quattuordez", "quindez", "sexdez", "septendez", "oktodez",
        "novemdez", "vigint", "unvigint", "duovigint", "trevigint",
        "quattuorvigint", "quinvigint", "sexvigint", "septenvigint",
        "oktovigint", "novemvigint", "trigint", "untrigint",
        "duotrigint", "tretrigint", "quattuortrigint", "quintrigint",
        "sextrigint", "septentrigint", "oktotrigint", "novemtrigint",
        "quadragint", "unquadragint", "duoquadragint", "trequadragint",
        "quattuorquadragint", "quinquadragint", "sexquadragint",
        "septenquadragint", "oktoquadragint", "novemquadragint",
        "quinquagint",
    };

    size_t pos, len;
    int something = 0;
    int eins = 0;

    for (; *s == '0'; ++s)
        ;
    if (!*s) {
        puts("null");
        return;
    }

    len = strlen(s);
    pos = 0;
    switch ((len - pos) % 3) {
        do {
            something = 0;
            eins = 0;

            putchar(' ');

        case 0:
            if (s[pos] != '0') {
                printf("%shundert", !pos && s[pos] == '1' ? "" : num[s[pos] - '0'][0]);
                something = 1;
            }
            ++pos;

        case 2:
            if (s[pos] == '0') {
                ++pos;

        case 1:
                if (s[pos] != '0') {
                    printf("%s", num[s[pos] - '0'][0]);
                    if (s[pos] == '1') {
                        eins = 1;
                    }
                    something = 1;
                }
                ++pos;

            } else {
                if (s[pos] == '1') {
                    switch (s[pos + 1u]) {
                        case '0': fputs("zehn", stdout);     break;
                        case '1': fputs("elf", stdout);      break;
                        case '2': fputs("zwoelf", stdout);   break;
                        case '6': fputs("sechzehn", stdout); break;
                        case '7': fputs("siebzehn", stdout); break;
                        default:
                                  printf("%s%s", num[s[pos + 1u] - '0'][0], num[s[pos] - '0'][1]);
                                  break;
                    }
                } else {
                    if (s[pos + 1u] == '0') {
                        fputs(num[s[pos] - '0'][1], stdout);
                    } else {
                        printf("%sund%s", num[s[pos + 1u] - '0'][0], num[s[pos] - '0'][1]);
                    }
                }
                something = 1;
                pos += 2;
            }

            if (something) {
                if (pos >= len) {
                    if (eins) {
                        putchar('s');
                    }
                } else {
                    const size_t scale = (len - pos) / 3u;
                    printf(
                            "%s%s%s%s%s",
                            !eins ? ""
                            : scale > 1u ? "e"
                            : scale < 1u ? "s"
                            : "",
                            scale == 1u ? "" : " ",
                            scale / 2u >= sizeof unit / sizeof *unit
                            ? "<??\?>"
                            : unit[scale / 2u],
                            scale == 1u ? ""
                            : scale % 2u ? "illiarde"
                            : "illion",
                            eins ? ""
                            : scale == 1u ? ""
                            : scale % 2u ? "n"
                            : "en"
                          );
                }
            }
        } while (pos < len);
    }

    putchar('\n');
}

int main(int argc, char **argv) {
    int status = 0;

    if (argv[0] && argv[0][0]) {
        Prog = argv[0];
    }

    if (argc > 1) {
        int i;
        for (i = 1; i < argc; ++i) {
            if (!well_formed(argv[i])) {
                complain(argv[i], "malformed number");
                status = EXIT_FAILURE;
                continue;
            }

            say(argv[i]);
        }
    } else {
        char *line;

        while ((line = getline(stdin))) {
            if (!well_formed(line)) {
                complain(line, "malformed number");
                status = EXIT_FAILURE;
                free(line);
                continue;
            }

            say(line);
            free(line);
        }
        if (!feof(stdin)) {
            status = EXIT_FAILURE;
        }
    }

    return status;
}

Name: Anonymous 2008-05-29 16:10

>>25
  i i i i i i i       ooooo    o        ooooooo   ooooo   ooooo
  I I I I I I I      8     8   8           8     8     o  8    8
  I  \ `+' /  I      8         8           8     8        8    8
   \  `-+-'  /       8         8           8      ooooo   8oooo
    `-__|__-'        8         8           8           8  8
        |            8     o   8           8     o     8  8
  ------+------       ooooo    8oooooo  ooo8ooo   ooooo   8

Copyright (c) Bruno Haible, Michael Stoll 1992, 1993
Copyright (c) Bruno Haible, Marcus Daniels 1994-1997
Copyright (c) Bruno Haible, Pierpaolo Bernardi, Sam Steingold 1998
Copyright (c) Bruno Haible, Sam Steingold 1999-2000
Copyright (c) Sam Steingold, Bruno Haible 2001-2006

[1]> (format t "~r" (read))
-1
minus one
NIL
[2]>
Bye.

Name: Anonymous 2008-05-29 19:34

minus one is not a number. it's a binary operator and one operand.

Name: Anonymous 2008-05-29 19:52

>>28
*** - EVAL: utter nonsense
The following restarts are available:
RETRY          :R1      Retry
ABORT          :R2      ABORT

Name: Anonymous 2008-05-31 20:06

merging >>13,18

mapM_ print . sequence =<< readLn

Name: Anonymous 2009-03-06 7:04

The defacement group known   as GForce Pakistan   albeit only for   OS X Are.

Name: Anonymous 2010-12-25 4:29

Name: Anonymous 2011-01-31 21:21

<-- check em dubz

Name: Sgt.Kabukiman袰怲 2012-05-23 14:54

All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy

Name: bampu pantsu 2012-05-29 3:37

bampu pantsu

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