g | x | w | all
Bytes Lang Time Link
210C#230725T013827Z138 Aspe
106Scala230725T011432Z138 Aspe
005Thunno 2 B230625T142503ZThe Thon
055C clang220820T142213Zc--
050Python220906T020210Z97.100.9
059Prolog SWI220902T125708ZJo King
031Matlab150724T121534ZLuis Men
007Jelly220829T145200ZBaby_Boy
021Burlesque220823T212744ZDeathInc
022J220823T200748ZConor O&
076Dart220820T124327Zuser1142
146brev220820T081535ZSandra
045PARI/GP220820T042426Zalephalp
007Jelly220820T034023ZnaffetS
013Japt170211T174517ZETHprodu
016Jelly161220T180639ZErik the
01505AB1E161220T173740ZMagic Oc
062Knight220803T221712ZnaffetS
007Vyxal s220801T210536ZnaffetS
025Dyalog APL220801T065533ZVadim Tu
079PHP 7200807T130813ZRFSnake
065Python 3150724T133925ZTim Pede
027Ruby200804T234820ZAupajo
059PowerShell200725T125516Zmazzy
084Zig 0.6.0200721T113608Zpfg
052Zsh190908T020124ZGammaFun
065Python 2190907T223459ZChas Bro
065SmileBASIC170211T193236Z12Me21
023J170924T232112ZFrownyFr
010Husk170924T154940Zბიმო
008Jelly170924T142125ZErik the
176PHP170924T140730Zcb0
069Clojure170919T103242ZNikoNyrh
011Japt170919T085434ZShaggy
075JavaScript ES6170211T174652ZETHprodu
080Factor160422T143827Zcat
030><>160420T163252ZAaron
060Mathcad160420T160714ZStuart B
118TSQL160420T124034Zt-clause
041Perl 5150724T205536Zhobbs
127R150724T020532ZMickyT
030Perl150724T094932Zmanatwor
062C150723T231401Zr3mainer
012Q150724T141847Zscottste
046Ruby150724T160733Zhistocra
111JavaScript ES6150724T151845ZNinjaBea
081Julia150723T225124ZAlex A.
100Java150724T132151ZGeobits
075Mathematica150724T131613Zjcai
106C# 106 Bytes150724T094431ZAlex Car
nan150724T065606ZCristian

C#, 210 bytes

Saved so many bytes thanks to the comment of @ceilingcat


Golfed version. Try it online!

static string F(string s){int j=s.Length,i=j;var v=new int[j];var t=.0;var r="";for(;i-->0;)v[i]=Convert.ToInt32(s[i]);for(;++i<j;)r+=((char)Math.Min(Math.Max((int)Math.Round((t+=v[i])/-~i),32),126));return r;}

Ungolfed version. Run it on dotnetfiddle!

using System;
using System.Text;

public class Program
{
    public static string ConvertString(string s)
    {
        int[] vecsmall = new int[s.Length];
        for(int j = 0; j < s.Length; j++)
        {
            vecsmall[j] = Convert.ToInt32(s[j]);
        }
        
        double t = 0.0;
        int i = 1;
        StringBuilder res = new StringBuilder();

        foreach(int c in vecsmall)
        {
            t += c;
            int ascii = (int)Math.Round(t / i);
            ascii = Math.Max(ascii, 32); // ensure within printable ASCII range
            ascii = Math.Min(ascii, 126); // ensure within printable ASCII range
            
            res.Append((char)ascii);
            i += 1;
        }
        
        return res.ToString();
    }

    public static void Main(string[] args)
    {
        string[] strings = new string[] {"Hello!", "test", "42", "StackExchange"};

        foreach(string s in strings)
        {
            Console.WriteLine($"{s} -> {ConvertString(s)}");
        }
    }
}

Scala, 106 bytes

Port of @alephalpha's PARI/GP answer in Scala.


Golfed version. Attempt This Online!

s=>{var(t,i)=(0.0,1);s.map(c=>{t+=c;val a=math.round(t/i).toInt.max(32).min(126);i+=1;a.toChar}).mkString}

Ungolfed version. Attempt This Online!

object Main {
  def f(s: String): String = {
    val vecsmall: Array[Int] = s.map(_.toInt).toArray
    var t = 0.0
    var i = 1
    var res = new StringBuilder

    for (c <- vecsmall) {
      t += c
      val ascii = Math.round(t / i).toInt.max(32).min(126) // ensure within printable ASCII range
      res += ascii.toChar
      i += 1
    }

    res.toString()
  }

  def main(args: Array[String]): Unit = {
    val strings = List("Hello!", "test", "42", "StackExchange")
    strings.foreach(s => println(s"$s -> ${f(s)}"))
  }
}

Thunno 2 B, 5 bytes

ƒ€m.+

Attempt This Online!

Explanation

ƒ€m.+  # Implicit input
       # Convert to ordinals
ƒ      # Prefixes of ordinals
 €m    # Mean of each
   .+  # Add 0.5 to each
       # Convert to string
       # Implicit output

C (clang), 55 bytes

n;t;main(c){for(;read(0,&c,1);putchar(t/++n))t+=c+n%2;}

Try it online!

How it works

n;t;           // t and n are initialized to 0
read(0,&c,1)   // Reads a character from stdin into c,
               // returns the number of characters read, 0 if EOF.
t+=c+n%2       // t is the accumulator, n%2 is used to round up.
putchar(t/++n) // increment n and use the new value to calculate the average.

C (clang), 45 bytes

Same idea, but as a function which modifies the string in-place.

n;t;f(*s){for(n=t=0;*s;*s++=t/++n)t+=*s+n%2;}

Try it online!

Python, 50 bytes

f=lambda s:s and f(s[:-1])+[int(.5+sum(s)/len(s))]

Attempt This Online!

Takes in an list of UTF-8 codepoints, return a list of UTF-8 codepoints.

Prolog (SWI), 59 bytes

N+X+[A|R]:-L=X+A,T is round(L/N),put(T),N+1+L+R.
+A:-1+0+A.

Try it online!

Takes a list of codepoints and outputs to STDOUT.

A little longer, but using only one predicate:

Prolog (SWI), 63 bytes

+A:-scanl(plus,A,0,Z),nth0(N,Z,E),N>0,T is round(E/N),\+put(T).

Try it online!

Matlab, 43 31 bytes

Thanks to @beaker for 9 bytes off!

Using an anonymous function:

@(s)[cumsum(+s)./find(s)+.5 '']

Examples:

>> @(s)[cumsum(+s)./find(s)+.5 '']
ans =
  function_handle with value:
    @(s)[cumsum(+s)./(find(s))+.5 '']
>> f=ans;
>> f('Hello!')
ans =
    'HW^adY'
>> f('test')
ans =
    'tmop'
>> f('42')
ans =
    '43'
>> f('StackExchange')
ans =
    'Sdccd_ccccddd'

Jelly, 7 bytes

OÄ÷T+.Ọ
OÄ÷T+.Ọ  ~Main link
O        ~Cast to number
 Ä       ~Cumulative sum; add the values
  ÷T     ~Divide by indices
    +.   ~Add .5
      Ọ  ~Cast to characters

Attempt This Online!

Burlesque, 21 bytes

)?^qavpa0.5?+)avqL[\m

Try it online!

)?^   # To codepoint
qavpa # Average on prefixes
0.5?+ # Add 0.5 to each
)av   # Floor each
qL[\m # Back to char

Should be 17 chars, but R_ (round) rounds 0.5 down, not up.

J, 22 bytes

(0.5<.@++/%#)\&.(3&u:)

Try it online!

Explanation

(0.5<.@++/%#)\&.(3&u:)
              &.(    )  do this, the left verb then the inverse of this:
                 3&u:     characters -> ordinates
(           )\          over the prefixes of the input:
        +/%#            take the average (sum divide length)
 0.5   +                add one-half
    <.@                 then floor   

Dart, 76 bytes

t(a,[c=1,s=0])=>String.fromCharCodes([...a.runes.map((i)=>(s+=i+.5)~/c++)]);

Try it online!

brev, 146 bytes

(as-list reverse(c pair-fold-right(fn(cons(integer->char(inexact->exact(floor(+(/(apply + x)(length x))0.5))))y))'())reverse(c map char->integer))

124 with banker's rounding:

(as-list reverse(c pair-fold-right(fn(cons(integer->char(round(/(apply + x)(length x))))y))'())reverse(c map char->integer))

Which is still a lot. It's clear that a huge painpoint in brev is the type converting (chars to integers, strings to lists etc).

PARI/GP, 45 bytes

s->t=i=0;Strchr([(t+=c)\/i++|c<-Vecsmall(s)])

Attempt This Online!


PARI/GP, 52 bytes

s->Strchr(Vec(intformal(Ser(Vecsmall(s))/(1-x)))\/1)

Attempt This Online!

This one is longer, but I can't resist to do this:

Jelly, 7 bytes

OÆmƤ+.Ọ

Try it online!

OÆmƤ+.Ọ
O       - Get a list of character codes from the input string
   Ƥ    - Over prefixes:
 Æm     -  Get the mean
    +.  - To each, add 0.5
      Ọ - Convert from character codes to charcters. Decimals are rounded down.

Japt, 13 bytes

£T±Xc)/°Y r d

Test it online!

How it works

£   T± Xc)/° Y r d
mXY{T+=Xc)/++Y r d}
                     // Implicit: U = input string, T = 0
mXY{              }  // Replace each char X and index Y in the string by this function:
    T+=Xc            //   Add X.charCodeAt() to T.
         )/++Y       //   Take T / (Y + 1).
               r d   //   Round, and convert to a character.
                     // Implicit: output result of last expression

Jelly, 16 bytes

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

Try it online!

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

05AB1E, 15 bytes

.pvyDSÇOsg/îç}J

Try it online!

Knight, 62 bytes

;;=sP=i 0W<=i+1i+1Ls;=aGs=t 0i;Wa;=t+tAa=aGa 1LaO+A/+t/i 2i"\"

Try it online!

Vyxal s, 7 bytes

CKvṁ.+C

Try it Online!

.+ should be , but there's a bug.

Dyalog APL, 25 bytes

{⎕UCS⌊.5+(+\C)÷⍳⍴C←⎕UCS⍵}

PHP 7, 79 bytes

As I don't have enough reputation to comment on cb0's answer to provide him hints to improve his answer (mainly using the char index method in a string and while to loop through a string). But I still bothered to post it due to the large reduction in byte count.

<?php $i=$b=0;while($a=ord($argv[1][$i++]??'')){$b+=$a;echo chr(round($b/$i));}

I didn't find a way though to use the <?= shortcut and avoid undefined errors without using command line flags (this is why i used $i=$b=0;). This answer does not work in PHP 5 due to the ?? syntax.

Python 3, 65 bytes

n=t=0
for c in input():n+=1;t+=ord(c);print(end=chr(int(.5+t/n)))

Try it online!

If I use round() instead of int(.5+ etc., it saves one character but is technically not in compliance with the challenge: Python's round() rounds halves to the nearest even integer, not upwards. However, it works correctly on all sample inputs.

Ruby, 27 bytes

putc gets.sum.quo(~/$/)+0.5

Solution produced through some collaborative golfing at work.

How this works

PowerShell, 59 bytes

-join($args|%{[char][int][Math]::Floor(($s+=+$_)/++$i+.5)})

Try it online!

Zig 0.6.0, 84 bytes

fn a(b:[]u8)void{var t:u64=0;for(b)|c,i|{t+=c;b[i]=@intCast(u8,(t+(i+1)/2)/(i+1));}}

Try it Online

Formatted:

fn a(b: []u8) void {
    var t: u64 = 0;
    for (b) |c, i| {
        t += c;
        b[i] = @intCast(u8, (t + (i+1)/2) / (i+1));
    }
}

Zsh, 52 bytes

for c (${(s::)1})printf ${(#)$((.5+(n+=0.+#c)/++i))}

Try it online!

In arithmetic mode, #c gets the code of the first character of $c. The parameter expansion ${(#) } prints the character associated with the code.

Python 2, 65 bytes

f=lambda s:s and f(s[:-1])+chr(int(sum(map(ord,s))*1./len(s)+.5))

Try it online!

SmileBASIC, 65 bytes

LINPUT S$FOR I=1TO LEN(S$)A=A-A/I+ASC(S$[I-1])/I?CHR$(A+.5);
NEXT

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 

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"

Jelly, 8 bytes

OÆmƤ+.ḞỌ

Try it online!

PHP, 176 byte

<?=(implode('',array_reduce(str_split($argv[1]),function($c,$k){array_push($c[1],chr(floor(((ord($k)+$c[0])/(count($c[1])+1))+0.5)));return[ord($k)+$c[0],$c[1]];},[0,[]])[1]));

Example:

>php cg.php Hello!
HW^adY
>php cg.php test  
tmop
>php cg.php 42
43

The biggest solution so far, but based on php it can't get much shorter I think. 2 bytes could be saved by removing the newlines.

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.

Japt, 11 bytes

c@V±X /°T r

Try it

JavaScript (ES6), 75 bytes

let f =
s=>s.replace(/./g,x=>String.fromCharCode((t+=x.charCodeAt())/++i+.5),i=t=0)
<input oninput="O.value=f(this.value)" value="Hello!"><br>
<input id=O value="HW^adY" disabled>

I can't believe there's no JS answer with this technique yet...

Factor, 80 bytes

[ cum-sum [ dup zero? 1 0 ? + / ] map-index [ .5 + floor >fixnum ] map >string ]

><>, 30 bytes

i:0(?v
v &l~<
\+l2(?
\&,12,+o;

You can try it on the online interpreter but then you need to use the following version, because the online interpreter pads the code box to a rectangle and applies ? to spaces.

i:0(?v
v &l~<
\+l2(   ?
\&,12,+o;

Mathcad, 60 "bytes"

enter image description here

Mathcad is mathematical application based on 2D worksheets comprised of "regions" each of which can be text, a mathematical expression, program, plot or scripted component.

A mathematical or programming instruction is picked from a palette toolbar or entered using a keyboard shortcut. For golfing purposes, an operation ("byte") is taken to be the number of keyboard operations necessary to create a name or expression (for example, to set the variable a to 3, we would write a:=3. The definition operator := is a single keypress ":", as are a and 3 giving a total of 3 "bytes". The programming for operator requires typing ctl-shft-# (or a single click on the programming toolbar) so again is equivalent to 1 byte.

In Mathcad the user enters programming language commands using keyboard shortcuts (or picking them from the Programming Toolbar) rather than writing them in text. For example, typing ctl-] creates a while-loop operator that has two "placeholders" for entering the condition and a single line of the body, respectively. Typing = at the end of a Mathcad expressions causes Mathcad to evaluate the expression.

(Count bytes) By looking at it from a user input perspective and equating one Mathcad input operation (keyboard usually, mouse-click on toolbar if no kbd shortcut) to a character and interpreting this as a byte. csort = 5 bytes as it's typed char-by-char as are other variable/function names. The for operator is a special construct that occupies 11 characters (including 3 blank "placeholders" and 3 spaces) but is entered by ctl-shft-#, hence = 1 byte (similar to tokens in some languages). Typing ' (quote) creates balanced parentheses (usually) so counts as 1 byte. Indexing v = 3 bytes (type v[k).

TSQL, 118 bytes

DECLARE @ varchar(400) = 'StackExchange'

SELECT
top(len(@))char(avg(ascii(stuff(@,1,number,''))+.5)over(order by number))FROM
master..spt_values
WHERE'P'=type

Returning characters vertical

S
d
c
c
d
_
c
c
c
c
d
d
d

Perl 5, 41 bytes

say map{$s+=ord;chr($s/++$c+.5)}pop=~/./g

run as

$ perl -E 'say map{$s+=ord;chr($s/++$c+.5)}pop=~/./g' StackExchange
Sdccd_ccccddd

R, 135 127 Bytes

This got long real quick and I really got it wrong the first time:) Need to read the questions properly.

cat(sapply(substring(a<-scan(,''),1,1:nchar(a)),function(x)rawToChar(as.raw(round(mean(as.integer(charToRaw(x)))+.5)))),sep='')

Test Run

cat(sapply(substring(a<-scan(,''),1,1:nchar(a)),function(x)rawToChar(as.raw(round(mean(as.integer(charToRaw(x)))+.5)))),sep='')
1: Hello!
2: 
Read 1 item
HW^adY

Perl: 31 30 characters

(29 characters code + 1 character command line option.)

s!.!chr.5+($s+=ord$&)/++$c!ge

Sample run:

bash-4.3$ perl -pe 's!.!chr.5+($s+=ord$&)/++$c!ge' <<< 'StackExchange'
Sdccd_ccccddd

C, 62 bytes

c;t;main(n){for(;(c=getchar())>0;n++)putchar(((t+=c)+n/2)/n);}

(ideone link)

The results are slightly different from the OP's examples, but only because this code rounds 0.5 down instead of up. Not any more!

Q, 15 12 bytes

12 bytes as an expression

"c"$avgs"i"$

q)"c"$avgs"i"$"Hello!"
"HW^adY"
q)"c"$avgs"i"$"test"
"tmop"
q)"c"$avgs"i"$"42"
"43"
q)"c"$avgs"i"$"StackExchange"
"Sdccd_ccccddd"

or 15 bytes as a function

{"c"$avgs"i"$x}

q){"c"$avgs"i"$x} "Hello!"
"HW^adY"
q){"c"$avgs"i"$x} "test"
"tmop"
q){"c"$avgs"i"$x} "42"
"43"
q){"c"$avgs"i"$x} "StackExchange"
"Sdccd_ccccddd"

takes advantage of

  1. the "i"$ cast to convert a string (list of characters) to a list of integers
  2. the avgs function, which computes the running average of a list as a list of floats
  3. the "c"$ cast to convert a list of floats to a list of characters, and which automatically rounds each float to the nearest integer before doing so [i.e. ("c"$99.5) = ("c"$100) and ("c"$99.4) = ("c"$99) ]

Ruby, 46

s=0.0
$<.bytes{|b|s+=b;$><<'%c'%(0.5+s/$.+=1)}

ideone.

With apologies to w0lf, my answer ended up different enough that it seemed worth posting.

$<.bytes iterates over each byte in stdin, so we print the rolling average in each loop. '%c' converts a float to a character by rounding down and taking the ASCII, so all we have to do is add 0.5 to make it round properly. $. is a magic variable that starts off initialized to 0--it's supposed to store the line count, but since here we want byte count we just increment it manually.

JavaScript ES6, 111 bytes

w=>w.replace(/./g,(_,i)=>String.fromCharCode([for(f of w.slice(0,++i))f.charCodeAt()].reduce((a,b)=>a+b)/i+.5))

This is annoyingly long thanks in part to JavaScript's long String.fromCharCode and charCodeAt functions. The stack snippet contains ungolfed, commented, testable code.

f=function(w){
  return w.replace(/./g,function(e,i){
    return String.fromCharCode(w.slice(0,++i).split('').map(function(f){
      return f.charCodeAt()
    }).reduce(function(a,b){
      // Adds all numbers in the array
      return a+b
      // String.fromCharCode automatically floors numbers, so we add .5 to round up
    })/i+.5)
  })
}

run=function(){document.getElementById('output').innerHTML=f(document.getElementById('input').value)};document.getElementById('run').onclick=run;run()
<input type="text" id="input" value="Hello!" /><button id="run">Run</button><br />
<pre id="output"></pre>

Julia, 85 81 bytes

s->(i=[int(c)for c=s];print(join([char(iround(mean(i[1:j])))for j=1:length(i)])))

This creates an unnamed function that accepts a string and creates a vector of its ASCII code points. Means are taken for each sequential group, rounded to integers, converted to characters, joined into a string, and printed to STDOUT.

Java, 100

Much like many other answers here, I'm summing and averaging in a loop. Just here to represent Java :)

void f(char[]z){float s=0;for(int i=0;i<z.length;System.out.print((char)Math.round(s/++i)))s+=z[i];}

My original code is a 97, but it only returns the modified char[] rather than printing it:

char[]g(char[]z){float s=0;for(int i=0;i<z.length;z[i]=(char)Math.round(s/++i))s+=z[i];return z;}

Now, it's just long enough for scrollbars to appear for me, so here's a version with some line breaks, just because:

void f(char[]z){
    float s=0;
    for(int i=0;
            i<z.length;
            System.out.print((char)Math.round(s/++i)))
        s+=z[i];
}

Mathematica, 75 bytes

FromCharacterCode@Floor[.5+Accumulate@#/Range@Length@#]&@ToCharacterCode@#&

C# 189 135 134 106 Bytes

var x=s.Select((t,i)=>Math.Round(s.Select(a=>(int)a).Take(i+1).Average())).Aggregate("",(m,c)=>m+(char)c);

Can be seen here

First time golfer

Ruby 59 61

->w{s=c=0.0;w.chars.map{|l|s+=l.ord;(s/c+=1).round.chr}*''}

Test: http://ideone.com/dT7orT