23
\$\begingroup\$

Task

Your task is to produce string that contains average characters of string. First character of result would be average character of first character (which is first character) and second character average of two first characters and so on.

What is average character?

Strings are arrays of bytes. Average character of string can be found by calculating the average of the ASCII values of characters in string and taking corresponding ASCII character.

For example string "Hello!" can be written as byte sequence 72 101 108 108 111 33. Average of ascii values is 533/6 = 88.833... and when it's rounded to nearest integer we get 89 which is ascii code for captial letter Y.

Rules

  • You can assume that input contains only printable ASCII characters
  • Input can be read from stdin or as command line arguments or as function arguments
  • Output must be stdout. If your program is function, you can also return the string you would otherwise print.
  • It must be whole program or function, not snippet
  • Standard loopholes apply
  • Integers are rounded by function floor(x+0.5) or similar function.

How do I win?

This is , so shortest answer (in bytes) in wins.

Examples

  • Hello!HW^adY
  • testtmop
  • 4243
  • StackExchangeSdccd_ccccddd
\$\endgroup\$
  • \$\begingroup\$ Edited question. Now it should be clear: you have to round halves upwards. \$\endgroup\$ – Hannes Karppila Jul 24 '15 at 9:46
  • 1
    \$\begingroup\$ "Input can be read from stdin or as command line arguments": or as function arguments (since you allow functions), right? \$\endgroup\$ – Luis Mendo Jul 24 '15 at 12:17
  • \$\begingroup\$ Of course, edited again. \$\endgroup\$ – Hannes Karppila Jul 24 '15 at 13:36
  • 2
    \$\begingroup\$ Sorry to bother you once again, but do functions actually have to print the output to STDOUT or can they return the desired string? \$\endgroup\$ – Dennis Jul 24 '15 at 14:20
  • \$\begingroup\$ Sorry, forgot to edit that before. Now it should be ok. \$\endgroup\$ – Hannes Karppila Jul 24 '15 at 20:41

36 Answers 36

0
\$\begingroup\$

Jelly, 16 bytes (non-competing)

S÷L+.Ḟ
OµḣLR$Ç€Ọ

Try it online!

Only if there were Average and Round built-ins...

\$\endgroup\$
0
\$\begingroup\$

SmileBASIC, 64 bytes

INPUT S$FOR I=1TO LEN(S$)A=A-A/I+ASC(S$[I-1])/I?CHR$(A+.5);
NEXT
\$\endgroup\$
0
\$\begingroup\$

Clojure, 69 bytes

#(for[c(map /(reductions +(map int %))(rest(range)))](char(+ c 0.5)))

Returns a sequence of characters, arguably a string-like construct. Would need an #(apply str(for[...]...)) to convert it into a string.

\$\endgroup\$
0
\$\begingroup\$

Jelly, 8 bytes

OÆmƤ+.ḞỌ

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Husk, 10 bytes

zȯci/Nt∫mc

Try it online!

                                                    | "tuna"
         mc  -- map each character to ASCII value   | [116,117,110,97]
       t∫    -- prefix sums & drop leading 0        | [116,233,343,440]
z(   )N      -- zip the list with [1..] using       | 
    /        --   divide                            | [116/1,233/2,343/3,440/4] == [116.0,116.5,114.̅3,110.0]
   i         --   round                             | [116,117,114,110]
  c          --   convert to character              | "turn"
\$\endgroup\$
0
\$\begingroup\$

J, 23 bytes

(0.5<.@++/%#)&.(a.&i.)\

Try it online!

How it works

                      \  on prefixes
               (   i.)   index of the first occurence
               (  &  )   in
               (a.   )   the character set
            x&.y         apply y, then x, then the inverse of y,                            
(0.5        )              that is the element of a. with a given index
(       +/  )            sum
(          #)            number of elements
(         % )            division
(   <.@+    )            add, then floor 
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.