Code Golf Asked by Hannes Karppila on November 5, 2021
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.
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
.
floor(x+0.5)
or similar function.This is code-golf, so shortest answer (in bytes) in wins.
Hello!
→ HW^adY
test
→ tmop
42
→ 43
StackExchange
→ Sdccd_ccccddd
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.
Answered by RFSnake on November 5, 2021
putc gets.sum.quo(~/$/)+0.5
Solution produced through some collaborative golfing at work.
Kernel#putc
prints a single character (e.g. putc "Hello"
outputs H
). Passing an object to it prints the character whose code is the least-significant byte of the object. So putc 72
outputs H
.Kernel#gets
reads stdin and returns a string.String#sum
returns a checksum. The result is the sum of the binary value of each byte in the string modulo 2**16 - 1
. Since ASCII bytes comfortably fit in this space, this just returns the byte total.10 / 3
is 3
whereas 10.to_f / 3
is 3.33…
). Here’s where Numeric#quo
comes in – 10.quo(3)
returns the Rational (10/3)
.Regexp#~
matches a regular expression against the contents of $_
(the last string read by gets
). It returns index of the match./$/
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 by gets
.+0.5
casts the Rational to a float. putc
floors the float into an integer.Answered by Aupajo on November 5, 2021
Answered by mazzy on November 5, 2021
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));
}
}
Answered by pfg on November 5, 2021
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.
Answered by GammaFunction on November 5, 2021
Answered by Chas Brown on November 5, 2021
(0.5<.@++/%#)&.(a.&i.)
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
Answered by FrownyFrog on November 5, 2021
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"
Answered by ბიმო on November 5, 2021
Answered by Erik the Outgolfer on November 5, 2021
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.
Answered by cb0 on November 5, 2021
#(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.
Answered by NikoNyrh on November 5, 2021
LINPUT S$FOR I=1TO LEN(S$)A=A-A/I+ASC(S$[I-1])/I?CHR$(A+.5);
NEXT
Answered by 12Me21 on November 5, 2021
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...
Answered by ETHproductions on November 5, 2021
£T±Xc)/°Y r d
£ 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
Answered by ETHproductions on November 5, 2021
Answered by Erik the Outgolfer on November 5, 2021
Answered by Magic Octopus Urn on November 5, 2021
[ cum-sum [ dup zero? 1 0 ? + / ] map-index [ .5 + floor >fixnum ] map >string ]
Answered by cat on November 5, 2021
i:0(?v
v &l~<
+l2(?
&,12,+o;
&
registerYou 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;
Answered by Aaron on November 5, 2021
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).
Answered by Stuart Bruff on November 5, 2021
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
Answered by t-clausen.dk on November 5, 2021
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
Answered by hobbs on November 5, 2021
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.
Answered by histocrat on November 5, 2021
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>
Answered by NinjaBearMonkey on November 5, 2021
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
Answered by scottstein37 on November 5, 2021
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.
Answered by Tim Pederick on November 5, 2021
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];
}
Answered by Geobits on November 5, 2021
FromCharacterCode@Floor[.5+Accumulate@#/Range@Length@#]&@ToCharacterCode@#&
Answered by jcai on November 5, 2021
Using an anonymous function:
f=@(s)char(round(cumsum(+s)./(1:numel(s))))
Examples:
>> f=@(s)char(round(cumsum(+s)./(1:numel(s))))
f =
@(s)char(round(cumsum(+s)./(1:numel(s))))
>> f('Hello!')
ans =
HW^adY
>> f('test')
ans =
tmop
>> f('42')
ans =
43
>> f('StackExchange')
ans =
Sdccd_ccccddd
Answered by Luis Mendo on November 5, 2021
(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
Answered by manatwork on November 5, 2021
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
Answered by Alex Carlsen on November 5, 2021
,[>,<.[->+<]>>++<[>[->+>+<<]>[-<<-[>]>>>[<[>>>-<<<[-]]>>]<<]>>>+<<[-<<+>>]<<<]>[-]>>>>[-<<<<<+>>>>>]<<<<<]
This is my first participation in a code-golf, please be gentle! It does work but brainfuck can't handle floats (not that i know of) so the rounded value is always the bottom one (might fix my algorithm later).
Also, the algorithm averages the values 2 by 2, meaning it could be innacurate in some spots. And I need to fix a bug that is printing a number at the end of the output too.
Answered by RedPanda on November 5, 2021
i=s=0
r=''
for c in input():s+=ord(c);i+=1.;r+=chr(int(s/i+.5))
print r
With each new character, updates the character sum s
and the number of characters i
to compute and append the average character.
Answered by xnor on November 5, 2021
It's not short enough. Since it's python it's way too verbose, you can even read what it does by looking code. But it's working.
a=[.0]+[ord(i)for i in raw_input()]
print"".join([chr(int(.5+(sum(a[:i+1])/i)))for i in range(1,len(a))])
Answered by Hannes Karppila on November 5, 2021
->w{s=c=0.0;w.chars.map{|l|s+=l.ord;(s/c+=1).round.chr}*''}
Test: http://ideone.com/dT7orT
Answered by Cristian Lupascu on November 5, 2021
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
Answered by MickyT on November 5, 2021
smCs+.5csaYCdlYz
Pretty straightforward. Using s+.5
instead of rounding, because for some reason round(0.5, 0)
is 0 in Python.
Answered by orlp on November 5, 2021
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!
Answered by r3mainer on November 5, 2021
`0:_ci_.5+{(+/x)%#x}'.0+1_|(-1_)_ic
Usage:
`0:_ci_.5+{(+/x)%#x}'.0+1_|(-1_)_ic"Hello!"
HW^adY
`0:_ci_.5+{(+/x)%#x}'.0+1_|(-1_)_ic"test"
tmop
`0:_ci_.5+{(+/x)%#x}'.0+1_|(-1_)_ic"42"
43
`0:_ci_.5+{(+/x)%#x}'.0+1_|(-1_)_ic"StackExchange"
Sdccd_ccccddd
_ci
and _ic
convert ascii to chars and vice versa, respectively. {(+/x)%#x}
is a classic K idiom for calculating a mean. Pretty straightforward overall.
Edit: oh, misread the spec. `0: is needed to print the result to stdout. Waiting for clarification on input re. Dennis' question.
Answered by JohnE on November 5, 2021
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.
Answered by Alex A. on November 5, 2021
s=>String.fromCharCode(...[for(i of s)i.charCodeAt()].map((l,i,a)=>Math.round(eval((t=a.slice(0,++i)).join`+`)/t.length)))
Mostly everything is happening in this bit
eval((t=a.slice(0,++i)).join`+`)/t.length)
The rest is looping / character code conversion
Split up:
s=>
String.fromCharCode(... ) // Converts average character code array to string, ... allows it to take an array
[for(i of s)i.charCodeAt()] // Converts string to char code array
.map((l,i,a)=> ) // Loops through each character
Math.round( /t.length) // Rounds sum of previous char codes, divides by position + 1
eval( ) // evals string of char codes seperated with +
( ).join`+` // joins previous char codes with +
t=a.slice(0,++i) // creates an array with all the char codes
If functions aren't allowed:
alert(String.fromCharCode(...[for(i of prompt())i.charCodeAt()].map((l,i,a)=>Math.round(eval((t=a.slice(0,++i)).join`+`)/t.length))))
133 bytes
ES5 Snippet:
function _toConsumableArray(r){if(Array.isArray(r)){for(var e=0,t=Array(r.length);e<r.length;e++)t[e]=r[e];return t}return Array.from(r)}function _taggedTemplateLiteral(r,e){return Object.freeze(Object.defineProperties(r,{raw:{value:Object.freeze(e)}}))}var _templateObject=_taggedTemplateLiteral(["+"],["+"]),f,t=function t(s){return String.fromCharCode.apply(String,_toConsumableArray(function(){var r=[],e=!0,t=!1,a=void 0;try{for(var n,i=s[Symbol.iterator]();!(e=(n=i.next()).done);e=!0){var o=n.value;r.push(o.charCodeAt())}}catch(l){t=!0,a=l}finally{try{!e&&i["return"]&&i["return"]()}finally{if(t)throw a}}return r}().map(function(l,i,a){return Math.round(eval((f=a.slice(0,++i)).join(_templateObject))/f.length)})))};
// Demo
document.getElementById('go').onclick=function(){
document.getElementById('output').innerHTML = t(document.getElementById('input').value)
};
<div style="padding-left:5px;padding-right:5px;"><h2 style="font-family:sans-serif">Average of Words Snippet</h2><div><div style="background-color:#EFEFEF;border-radius:4px;padding:10px;"><input placeholder="Text here..." style="resize:none;border:1px solid #DDD;" id="input"><button id='go'>Run!</button></div><br><div style="background-color:#EFEFEF;border-radius:4px;padding:10px;"><span style="font-family:sans-serif;">Output:</span><br><pre id="output" style="background-color:#DEDEDE;padding:1em;border-radius:2px;overflow-x:auto;"></pre></div></div></div>
Answered by Downgoat on November 5, 2021
Answered by Dennis on November 5, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP