How to generate an array with random values, without using a loop?

后端 未结 24 2178
逝去的感伤
逝去的感伤 2020-12-13 19:23

How can I generate an array in Perl with 100 random values, without using a loop?

I have to avoid all kind of loops, like \"for\", foreach\", while. This is my exerc

24条回答
  •  不知归路
    2020-12-13 20:16

    Since the task seems to be either to get you to use recursion or to learn how to write an easy loop in a not-so-easy form, humbly I submit the following FULLY EXECUTABLE Perl programs:

    Camel:

    #!/usr/bin/perl
                                          ''=~('('.'?'
               .'{'.(                   '`'|'%').("\["^
            '-').('`'|                '!').('`'|',').'"'
     .'\\'.'$'.  ("\`"|              ',').('`'|')').('`'|
    '-').'='.('^'^("\`"|            '/')).('^'^('`'|'.')).
    ('^'^('`'|'.')).';'.(          '!'^'+').('`'|'&').('`'
      |'/').('['^')').'('        .'\\'.'$'.'='.'='.(('^')^(
           '`'|'/')).';'.      '\\'.'$'.'='.'<'.'='.'\\'.'$'
          .('`'|(',')).(     '`'|')').('`'|'-').';'.'+'."\+".
         '\\'.'$'.('=').   ')'.'\\'.'{'.('['^'+').('['^"\.").(
        '['^'(').("\`"|   '(').('{'^'[').'\\'.'@'.'='.','.("\{"^
        '[').('['^')').  ('`'|'!').('`'|'.').('`'|'$').'\\'.'}'.(
        '!'^'+').'\\'.  '$'.'='.'='.('^'^('`'|'/')).';'.('!'^'+')
        .('`'|('&')).(  '`'|'/').('['^')').('{'^'[').'('.'\\'.'@'.
        '='.')'.('{'^'[').'\\'.'{'.('!'^'+').('*'^'#').('['^'+').(
        '['^')').('`'|')').('`'|'.').('['^'/').('{'^'[').'\\'.'"'.(
         '['^')').('`'|'!').('`'|'.').('`'|'$').('{'^'[').'\\'.'$'.
         '='.('{'^'[').('`'|'/').('`'|'&').('{'^'[').'\\'.'$'.("\`"|
          ',').('`'|')').('`'|'-').'='.'\\'.'$'.'_'.'\\'.'\\'.(('`')|
           '.').'\\'.'"'.';'.('!'^'+').('*'^'#').'\\'.'$'.'='.'+'.'+'
            .';'.('!'^'+').('*'^'#').'\\'.'}'.'"'.'}'.')');$:='.' ^((
             '~'));$~='@'|'(';$^=')'^'[';$/='`'|'.';$,='('^"\}";  $\=
              '`'|'!';$:=')'^'}';$~='*'|'`';$^='+'^'_'; $/="\&"|  '@'
                ;$,='['&'~';$\=','^'|';$:='.'^"\~";$~=  '@'|'('   ;$^
                 =')'^ '[';$/='`'|'.';$,='('^"\}";$\=   '`'|'!'   ;$:
                       =')'^'}';$~='*'|'`';$^=('+')^    '_';$/=   '&'
                       |'@';$,=    '['&'~';$\ ="\,"^     '|';$:   =(
                       ('.'))^     "\~";$~=   ('@')|     '(';$^  =(
                       (')'))^     "\[";$/=   "\`"|       "\.";  (
                       ($,))=      '('^'}';   ($\)         ='`'
                       |"\!";     $:=(')')^   '}';         ($~)
                        ='*'|     "\`";$^=    '+'^         '_';
                        ($/)=     '&'|'@'     ;$,=         '['&
                        '~';     $\=','       ^'|'         ;$:=
                        '.'^     '~'          ;$~=         '@'|
                        '(';      $^=         ')'          ^((
                        '['        ));       $/=           '`'
                        |((         '.'     ));            $,=
                        '('          ^((   '}'              ))
                        ;(             ($\))=               ((
                        ((              '`'))               ))
                        |+             "\!";$:=             ((
                       ')'            ))^+ "\}";            $~
                      =((           '*'))|  '`';           $^=
                     '+'^         "\_";$/=   '&'          |'@'
                   ;($,)=                                ('[')&
                 "\~";$\=                               ','^'|'
    

    Martini:

    #!/usr/bin/perl
                                                      ''=~('(?{'.
                                                      ('`'|'%').(
                                                      '['^"\-").(
                                                      '`'|"\!").(
                                                      '`'|(',')).
                                                      '"\\$'.('`'
                                                        |',').(
                                                        '`'|')'
                                                        ).('`'|
                                                        ('-')).
                                                        ('=').(
                                                       '^'^('`'|
                                                       ('/'))).(
                                                       '^'^('`'|
                                                       ('.'))).(
                                                      '^'^(('`')|
                                                      '.')).';'.(
                                                     '!'^'+').('`'
                                                     |'&').(('`')|
                                                    '/').('['^')').
                                                    '(\\$=='.('^'^(
                                                   '`'|'/')).';\\$='
                                                  .'<=\\$'.('`'|',').
                                                 ('`'|')').('`'|"\-").
                                                ';++\\$=)\\{'.('['^'+')
                                               .('['^'.').('['^'(').('`'
                                              |'(').('{'^'[').'\\@'.('`'|
                                              '!').('['^')').('['^"\)").(
                                              '`'|'!').('['^'"').','.('{'
                                              ^'[').('['^')').('`'|'!').(
                                              '`'|'.').('`'|"\$").'\\}'.(
    '!'^'+').'\\$'.('`'|')').'='.("\^"^(      '`'|'/')).';'.('!'^('+')).(
     '`'|'&').('`'|'/').('['^')').('{'^       '[').'(\\@'.('`'|'!').('['^
       ')').('['^')').('`'|'!').('['^         '"').')'.('{'^"\[").'\\{'.(
        '!'^'+').('*'^'#').('['^'+')          .('['^')').('`'|')').("\`"|
          '.').('['^'/').('{'^'[')            .'\\"'.('['^')').('`'|'!').
           ('`'|'.').('`'|"\$").(             '{'^'[').'\\$'.('`'|"\)").(
             '{'^'[').('`'|'/')               .('`'|'&').('{'^'[').'\\$'.
              ('`'|',').("\`"|                ')').('`'|'-').'=\\$_\\\\'.
                ('`'|('.')).                  '\\";'.('!'^'+').('*'^'#').
                 '\\$'.('`'                   |')').'++;'.('!'^'+').('*'^
                   "\#").                     '\\}"})');$:='.'^'~';$~='@'
                    |'('                      ;$^=')'^'[';$/='`'|"\.";$,=
                    '('^                      '}';$\='`'|'!';$:=')'^"\}";
                    ($~)                      ='*'|'`';$^='+'^'_';$/='&'|
                    '@';                      $,='['&'~';$\=','^('|');$:=
                    '.'^                      '~';$~='@'|'(';$^=')'^"\[";
                    ($/)                      ='`'|'.';$,='('^'}';$\='`'|
                    '!';                      $:=')'^'}';$~='*'|('`');$^=
                    '+'^                      '_';$/='&'|'@';$,='['&"\~";
                    ($\)                      =','^'|';$:='.'^'~';$~='@'|
                    '(';                      $^=')'^'[';$/='`'|('.');$,=
                    '('^                      '}';$\='`'|'!';$:=')'^"\}";
                    ($~)                      ='*'|'`';$^='+'^'_';$/='&'|
    '@';$,='['&'~';$\=','^'|';$:='.'^'~'      ;$~='@'|'(';$^=')'^"\[";$/=
    '`'|'.';$,='('^'}';$\='`'|'!';$:=')'      ^'}';$~='*'|'`';$^='+'^'_';
    

    For the Holidays, snowflakes with recursion rather than iteration inside:

    #!/usr/bin/perl
               '?'                          =~(
             '('.'?'                      ."\{".(
            '`'   |'%'  ).('['^"\-").(  '`'|   '!'
             ).('`'|',').    '"'.    '\\'.('$').(
             '`'|(',')).(    '`'|    ')').(('`')|
            ((  '-')   )).    +(    '`'   |')'  ).
           (((    '['   ))^+  ((  '/')   )).    '='
          .('^'^   ('`'|'/')) .( '^'^("\`"|   '.')).
         +(     '^'^('`'|'.')).';'.('!'^"\+").     ((
     '\\')).'$'.('`'|'#').('`'|'/').('['^'.').('`'|'.').(
    '['^  '/').'='.  (('^')^(    '`'|'/')  ).(';').(  '!'^
    '+'    ).('['^    '(').(      ('[')^    "\.").(    '`'
    |'"'  ).(('{')^  ('[')).(    '['^'+')  .('['^'.'  ).+(
     '['^'(').('`'|'(').'_'.('['^')').('`'|'!').('`'|'.')
         .(     '`'|'$').('{'^'[').'\\'."\{".(     ((
          '!'))^   '+').('{'^ (( ('[')))).(   ('{')^
           '['    ).(   '{'^  ((  '[')   )).    (((
            ((  '{')   )))    ^+    '['   ).+(  ((
             '['))^')').(    '`'|    '%').(('[')^
             '/').(('[')^    '.')    .('['^')').(
            '`'   |'.'  ).('{'^"\[").(  '`'|   ')'
             ).('`'|                      "\&").(
               '{'                          ^((
               '['                          )))
             .'\\'.+                      '$'.'#'
            .+(   '`'|  '!').('['^')')  .''.   (((
             '['))^')').(    '`'|    '!').(('[')^
             '"').('_').(    '`'|    '/').(('`')|
            ((  '&')   )).    ((    '_'   )).(  ((
           '['    ))^   ')')  .(  '`'|   '!'    ).(
          ('`')|   '.').('`'| (( ('$')))).(   ('[')^
         ((     '('))).'>'.'\\'.'$'.('`'|',').     +(
     '`'|')').('`'|'-').('`'|')').('['^'/').';'.('!'^'+')
    .''.  ('{'^'[')  .(('{')^    ('[')).(  '{'^'[').  ('{'
    ^((    '['))).    ("\["^      '+').(    '['^'.'    ).(
    '['^  '(').('`'  |"\(").(    '{'^'[')  .'\\'.'@'  .''.
     ('`'|'!').('['^')').('['^')').('`'|'!').('['^('"')).
         ((     '_')).('`'|'/').('`'|'&').'_'.     +(
          ('[')^   ')').('`'| (( ('!')))).(   ('`')|
           '.'    ).(   '`'|  ((  '$')   )).    (((
            ((  '[')   )))    ^+    '('   ).((  ((
             ',')))).('{'    ^'['    ).('['^')').
             ('`'|"\!").(    '`'|    '.').(('`')|
            '$'   ).((  ';')).('!'^'+'  ).+(   '{'
             ^'[').(                      '{'^'['
               ).(                          '{'
               ^((                          '['
             ))).''.                      (('{')^
            '['   ).+(  '['^'+').('['^  '.')   .+(
             '['^('(')).(    '`'|    '(').('_').(
             '['^(')')).(    '`'|    '!').(('`')|
            ((  '.')   )).    +(    '`'   |'$'  ).
           '('    .((   ')')  ).  ';'.   (((    '!'
          ))^'+'   ).'\\'.'}' .( '!'^'+').(   ('!')^
         ((     '+'))).('!'^'+').('['^('(')).(     ((
     '['))^'.').('`'|'"').('{'^'[').('['^'+').('['^')').(
    '`'|  ')').('`'  |"\.").(    '['^'/')  .'_'.('['  ^')'
    ).(    '`'|'%'    ).('`'      |'#').    (('[')^    '.'
    ).+(  '['^')').  ('['^'('    ).("\`"|  ')').('['  ^'-'
     ).('`'|'%').('{'^'[').'\\'.'{'.('!'^'+').('{'^'[').(
         ((     '{'))^'[').('{'^'[').('{'^'[')     .+
          '\\'.+   '$'.("\["^ (( '/'))).'='   .('['^
           '+'    ).(   '`'|  ((  '/')   )).    (((
            ((  '[')   )))    ^+    '+'   ).+(  ((
             '{'))^"\[").    '\\'    .'@'.'_'.';'
             .('!'^'+').(    '*'^    '#').(('[')^
            '+'   ).+(  '['^')').('`'|  ')')   .+(
             '`'|'.'                      ).('['^
               '/'                          ).(
               '{'                          ^((
             '['))).                      ('\\').
            '"'   .''.  ('['^')').('`'  |'!'   ).(
             '`'|('.')).(    '`'|    '$').(('{')^
             '[').('\\').    '$'.    ('`'|"\#").(
            ((  '`')   )|+    ((    '/'   ))).  +(
           '['    ^((   '.')  ))  .''.   (((    '`'
          ))|'.'   ).('['^'/' ). ('{'^'[').   ("\`"|
         ((     '/'))).('`'|'&').('{'^'[').''.     ((
     '\\')).'$'.('`'|',').('`'|')').('`'|'-').('`'|')').(
    '['^  '/').'='.  '\\'.'$'    .(('[')^  '/').'\\'  .''.
    (((    '\\')))    .('`'|      "\.").    ('\\').    '"'
    .';'  .('!'^'+'  ).("\*"^    '#').''.  '\\'.'$'.  ('`'
     |'#').('`'|'/').('['^'.').('`'|'.').('['^'/').('+').
         ((     '+')).';'.('!'^'+').('*'^'#').     +(
          ('[')^   '+').('['^ (( (')')))).(   ('`')|
           ')'    ).(   '`'|  ((  '.')   )).    (((
            ((  '[')   )))    ^+    '/'   ).((  ((
             '_')))).('['    ^')'    ).('`'|'%').
             ('`'|"\#").(    '['^    '.').(('[')^
            ')'   ).+(  '['^'(').('`'|  ')')   .+(
             '['^'-'                      ).('`'|
               '%'                          ).+
               '('                          .((
             '\\')).                      '@'.'_'
            .((   ')')  ).('{'^"\[").(  '`'|   ')'
             ).('`'|'&').    ('{'    ^'[').('(').
             '\\'.'@'.'_'    .')'    .';'.(('!')^
            ((  '+')   )).    ((    (((   '\\'  ))
           )))    .((   '}')  ).  ('!'   ^((    '+'
          ))).+(   '!'^'+').( (( '['))^'+')   .('['^
         ((     '.'))).('['^'(').('`'|'(').'_'     .(
     '['^')').('`'|'!').('`'|'.').('`'|'$').'('.')'.';'.(
    '['^  '+').('['  ^"\)").(    '`'|')')  .('`'|'.'  ).+(
    '['    ^"\/").    "\_".(      ('[')^    "\)").(    '`'
    |'%'  ).(('`')|  ('#')).(    '['^'.')  .('['^')'  ).+(
     '['^'(').('`'|')').('['^'-').('`'|'%').'('.'\\'.'@'.
         +(     '`'|'!').('['^')').('['^')').(     ((
          '`'))|   '!').('['^ (( '"'))).'_'   .('`'|
           '/'    ).(   '`'|  ((  '&')   )).    '_'
            .(  '['^   ')'    ).    (((   '`')  )|
             '!').(('`')|    '.')    .('`'|'$').(
             '['^'(').')'    .';'    .'"'.'}'.')'
            );(   $:)=  '.'^'~';$~='@'  |'('   ;$^
             =(')')^                      '[';#;#
               ;#;                          #;#
    

    In each case, the output is something like this:

    rand 1 of 100=0.625268682212667
    rand 2 of 100=0.30160434879096
    ...
    rand 100 of 100=0.584811321826528
    

    If you want to see the loops or recursion embedded within, you can use perl -MO=Deparse martini.pl or perl -MO=Deparse camel.pl etc.

    Only with Perl, right???

    If you want to generate these lovely things -- check out Acme::Eyedrops

提交回复
热议问题