| Bytes | Lang | Time | Link |
|---|---|---|---|
| 210 | C# | 230725T013827Z | 138 Aspe |
| 106 | Scala | 230725T011432Z | 138 Aspe |
| 005 | Thunno 2 B | 230625T142503Z | The Thon |
| 055 | C clang | 220820T142213Z | c-- |
| 050 | Python | 220906T020210Z | 97.100.9 |
| 059 | Prolog SWI | 220902T125708Z | Jo King |
| 031 | Matlab | 150724T121534Z | Luis Men |
| 007 | Jelly | 220829T145200Z | Baby_Boy |
| 021 | Burlesque | 220823T212744Z | DeathInc |
| 022 | J | 220823T200748Z | Conor O& |
| 076 | Dart | 220820T124327Z | user1142 |
| 146 | brev | 220820T081535Z | Sandra |
| 045 | PARI/GP | 220820T042426Z | alephalp |
| 007 | Jelly | 220820T034023Z | naffetS |
| 013 | Japt | 170211T174517Z | ETHprodu |
| 016 | Jelly | 161220T180639Z | Erik the |
| 015 | 05AB1E | 161220T173740Z | Magic Oc |
| 062 | Knight | 220803T221712Z | naffetS |
| 007 | Vyxal s | 220801T210536Z | naffetS |
| 025 | Dyalog APL | 220801T065533Z | Vadim Tu |
| 079 | PHP 7 | 200807T130813Z | RFSnake |
| 065 | Python 3 | 150724T133925Z | Tim Pede |
| 027 | Ruby | 200804T234820Z | Aupajo |
| 059 | PowerShell | 200725T125516Z | mazzy |
| 084 | Zig 0.6.0 | 200721T113608Z | pfg |
| 052 | Zsh | 190908T020124Z | GammaFun |
| 065 | Python 2 | 190907T223459Z | Chas Bro |
| 065 | SmileBASIC | 170211T193236Z | 12Me21 |
| 023 | J | 170924T232112Z | FrownyFr |
| 010 | Husk | 170924T154940Z | ბიმო |
| 008 | Jelly | 170924T142125Z | Erik the |
| 176 | PHP | 170924T140730Z | cb0 |
| 069 | Clojure | 170919T103242Z | NikoNyrh |
| 011 | Japt | 170919T085434Z | Shaggy |
| 075 | JavaScript ES6 | 170211T174652Z | ETHprodu |
| 080 | Factor | 160422T143827Z | cat |
| 030 | ><> | 160420T163252Z | Aaron |
| 060 | Mathcad | 160420T160714Z | Stuart B |
| 118 | TSQL | 160420T124034Z | t-clause |
| 041 | Perl 5 | 150724T205536Z | hobbs |
| 127 | R | 150724T020532Z | MickyT |
| 030 | Perl | 150724T094932Z | manatwor |
| 062 | C | 150723T231401Z | r3mainer |
| 012 | Q | 150724T141847Z | scottste |
| 046 | Ruby | 150724T160733Z | histocra |
| 111 | JavaScript ES6 | 150724T151845Z | NinjaBea |
| 081 | Julia | 150723T225124Z | Alex A. |
| 100 | Java | 150724T132151Z | Geobits |
| 075 | Mathematica | 150724T131613Z | jcai |
| 106 | C# 106 Bytes | 150724T094431Z | Alex Car |
| nan | 150724T065606Z | Cristian |
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.+
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;}
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;}
Python, 50 bytes
f=lambda s:s and f(s[:-1])+[int(.5+sum(s)/len(s))]
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.
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).
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
Burlesque, 21 bytes
)?^qavpa0.5?+)avqL[\m
)?^ # 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:)
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++)]);
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)])
PARI/GP, 52 bytes
s->Strchr(Vec(intformal(Ser(Vecsmall(s))/(1-x)))\/1)
This one is longer, but I can't resist to do this:
Vecsmall(s): Converts the string to a vector of ASCII codes.Ser(...): Converts the vector to a formal power series..../(1-x): Divides the result by \$1-x\$. This is equivalent to taking the cumsum.intformal(...): Takes the integral with respect to \$x\$. This is equivalent to dividing then-th term withn.Vec(...): Converts the result back to a vector....\/1: Rounds the result.Strchr(...)Converts the result back to a string.
Jelly, 7 bytes
OÆmƤ+.Ọ
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
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
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)))
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
Kernel#putcprints a single character (e.g.putc "Hello"outputsH). Passing an object to it prints the character whose code is the least-significant byte of the object. Soputc 72outputsH.Kernel#getsreads stdin and returns a string.String#sumreturns a checksum. The result is the sum of the binary value of each byte in the string modulo2**16 - 1. Since ASCII bytes comfortably fit in this space, this just returns the byte total.- Because the byte total is an integer, we can’t do floating point calculations (in Ruby
10 / 3is3whereas10.to_f / 3is3.33…). Here’s whereNumeric#quocomes in –10.quo(3)returns the Rational(10/3). Regexp#~matches a regular expression against the contents of$_(the last string read bygets). It returns index of the match.- The Regexp
/$/matches the end of the string, so~/$/means “print the index of the end of the string” – in other words, the string length. This gives us the final is a short-hand for the length of the string read bygets. - This gives us the Rational (byte total/string length). Now we need to round it into an integer.
+0.5casts the Rational to a float.putcfloors the float into an integer.
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));}}
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))}
In arithmetic mode, #c gets the code of the first character of $c. The parameter expansion ${(#) } prints the character associated with the code.
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.)\
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
| "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"
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.
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;
- The first line reads from stdin and puts the characters on the stack
- The second will remove the EOL char, take the size of the stack and put it in the
®ister - The third line will add numbers on the stack while there are two or more of them
- The fourth line will divide the resulting number by the register's value, then add 1/2, output the value as a character and stop. When faced with a float value when displaying a char, ><> will floor it, which is why we added 1/2
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"
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);}
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
- the "i"$ cast to convert a string (list of characters) to a list of integers
- the avgs function, which computes the running average of a list as a list of floats
- 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)}
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
