Task
Build a calculator, that takes any string, from a file, stdin or whatever, and adds up all the values of the chars.
Example
Input
Hello World!
Output
1085
Rules
The calculator needs to accept just ASCII encoding.
The shortest code wins.
Notes
Regarding to the comment of m.buettner, I need to say, I didn't thought of the multibyte part.
So I leave it as a bonus thing aswell.
The calculator should be run as written, so no need to modify before compiling or interpreting.
Bonus
Thanks to Synthetica, here is one more bonus,
The program that has the lowest output when you use its code as its input
(削除) wins (削除ここまで)gets a star.
I don't want to modify it completly.
If you write it additional to output the (right) value in UTF-8 you get a star.
The code that executes fastest on my Laptop (Lenovo Yoga 13 Intel Core i5 3317U 1.7Ghz, 8GB RAM, 128GB SSD, Intel HD 4000, Windows 8) gets a star.
Web codes will run first under IE11 with chakra and then in FireFox 29.0.1 with SpiderMonkey
Linux code will run on a Raspberry Pi with Raspbian.
The teststring is this:
q/%8hnp>T%y?'wNb\},9krW &D9']K$n;l.3O+tE*$*._B^s!@k\&Cl:EO1zo8sVxEvBxCock_I+2o6 yeX*0Xq:tS^f)!!7=!tk9K<6#/E`ks(D'$z$6円Ac+MT&[s[]_Y(`<g%"w%cW'`c&q)D0ドル#C$QGf>?A$iawvc,}`9!('`c&q)D0ドル#C$QGf>?A$iawvc,}`9!(
Have fun coding :)
Bonusscoring
I plan to do the scoring at this Saturday so the 07.06.14, all answers after that date won't get bonus points ;)
You can download the code I gonna use for testing here feel free to fork and improve it :)
Little update because of the bonus, my laptop is partially broken so I will do it probably next weekend, I am really sorry for that :(
87 Answers 87
GolfScript, 4 characters
{+}*
Simply uses the fold operator (*
) to add up all the characters.
If it has to work with the empty string, 9 chars:
{{+}*}0if
Thanks to @PeterTaylor for providing an alternative 6-char version that works with empty string:
0\{+}/
-
\$\begingroup\$ Does not work on the empty string. \$\endgroup\$Howard– Howard2014年06月05日 14:32:23 +00:00Commented Jun 5, 2014 at 14:32
-
\$\begingroup\$ @Howard Good point; edited. \$\endgroup\$Doorknob– Doorknob2014年06月05日 14:35:23 +00:00Commented Jun 5, 2014 at 14:35
-
\$\begingroup\$
0\{+}/
supports empty string \$\endgroup\$Peter Taylor– Peter Taylor2014年06月05日 14:40:50 +00:00Commented Jun 5, 2014 at 14:40 -
1\$\begingroup\$ @Doorknob sorry for the stupid question, how to I input data? I use golfscript.apphb.com \$\endgroup\$Knerd– Knerd2014年06月05日 15:21:38 +00:00Commented Jun 5, 2014 at 15:21
-
2\$\begingroup\$ @immibid A cyclops with a weirdly shaped eye. :-P (or, in GolfScript, the "swap-and-add-each" face!) \$\endgroup\$Doorknob– Doorknob2014年06月06日 11:49:46 +00:00Commented Jun 6, 2014 at 11:49
APL (8)
+/⎕UCS⍞
Explanation:
+/
sum of⎕UCS
unicode values of⍞
character input
-
\$\begingroup\$ What would be the result for
Hello World!
? \$\endgroup\$Knerd– Knerd2014年06月05日 14:30:04 +00:00Commented Jun 5, 2014 at 14:30 -
\$\begingroup\$ @Knerd:
1085
. It wouldn't be correct if it gave another output. It sums the values of the Unicode codepoints of the characters. \$\endgroup\$marinus– marinus2014年06月05日 14:43:20 +00:00Commented Jun 5, 2014 at 14:43 -
\$\begingroup\$ ok, I didn't got what the
⍞
means ;) \$\endgroup\$Knerd– Knerd2014年06月05日 14:44:23 +00:00Commented Jun 5, 2014 at 14:44 -
1\$\begingroup\$ @knerd:
⍞
means read a line from the keyboard \$\endgroup\$marinus– marinus2014年06月05日 14:49:56 +00:00Commented Jun 5, 2014 at 14:49 -
\$\begingroup\$ Do you know an APL interpreter that is free? \$\endgroup\$Knerd– Knerd2014年06月05日 17:05:07 +00:00Commented Jun 5, 2014 at 17:05
Haskell 36
main=interact$show.sum.map fromEnum
-
\$\begingroup\$ From where does it read the text? \$\endgroup\$Knerd– Knerd2014年06月05日 14:47:34 +00:00Commented Jun 5, 2014 at 14:47
-
\$\begingroup\$ stdin.
$ printf "Hello World!" | ./charsum
\$\endgroup\$gxtaillon– gxtaillon2014年06月05日 14:50:51 +00:00Commented Jun 5, 2014 at 14:50 -
\$\begingroup\$ ok, I couldn't get it run on my Windows machine, I gonna try it on the rpi when I am at home \$\endgroup\$Knerd– Knerd2014年06月05日 14:54:47 +00:00Commented Jun 5, 2014 at 14:54
-
\$\begingroup\$ When I execute your code, I just get the string "Hello World!" as Output. This is my commandline:
ECHO "Hello World! | ghci charsum.hs
\$\endgroup\$Knerd– Knerd2014年06月05日 16:25:06 +00:00Commented Jun 5, 2014 at 16:25 -
1\$\begingroup\$ use
interact
andshow
instead ofgetContents>>=print
:main=interact$show.sum.map fromEnum
\$\endgroup\$Flonk– Flonk2014年06月05日 17:59:43 +00:00Commented Jun 5, 2014 at 17:59
Shell+GNU tools, 29 bytes
echo `od -An -tuC`|tr \ +|bc
Takes input from stdin:
$ printf "%s" 'Hello World!' | ./addchars.sh
1085
$
Own score: 2385
c, 52 bytes
c;main(p){while(~(p=getchar()))c+=p;printf("%d",c);}
Compile with (some warnings produced):
gcc addchars.c -o addchars
Takes input from stdin:
$ printf "%s" 'Hello World!' | ./addchars
1085 $
Own score: 4354
-
\$\begingroup\$ This is great answer. CodeBlocks with GNU compiler always complains if variables have no type e.g int c, main(int p). So I think these should be included in your answer. \$\endgroup\$bacchusbeale– bacchusbeale2014年06月06日 08:02:34 +00:00Commented Jun 6, 2014 at 8:02
-
\$\begingroup\$ @bacchusbeale I added a note about compilation warnings, but I think this is generally par-for-the-course when golfing in c. As long as the code compiles and runs as expected, then warnings can be ignored. See codegolf.stackexchange.com/a/2230/11259 and codegolf.stackexchange.com/a/2204/11259. Of course production code is a different matter entirely. \$\endgroup\$Digital Trauma– Digital Trauma2014年06月06日 16:38:41 +00:00Commented Jun 6, 2014 at 16:38
-
\$\begingroup\$ @DigitalTrauma are all those spaces actually necessary? Can the Shell not ignore the whitespace and use the - to mark new parameters? \$\endgroup\$Ashwin Gupta– Ashwin Gupta2016年01月04日 01:46:54 +00:00Commented Jan 4, 2016 at 1:46
-
\$\begingroup\$ @AshwinGupta Are you talking about the
od
command?od -AntuC
does not do the same asod -An -tuC
. \$\endgroup\$Digital Trauma– Digital Trauma2016年01月04日 15:43:00 +00:00Commented Jan 4, 2016 at 15:43 -
\$\begingroup\$ @DigitalTrauma yeah I was. I meant couldn't you do
od-An-tuC
orod -An-tuC
\$\endgroup\$Ashwin Gupta– Ashwin Gupta2016年01月04日 23:22:15 +00:00Commented Jan 4, 2016 at 23:22
Javascript (ES6) 51
alert([...prompt(x=0)].map(y=>x+=y.charCodeAt())|x)
-
\$\begingroup\$ @nderscore Can you explain what the
...
before theprompt
does? Is this a new ES6 thing or is it pre-ES6? \$\endgroup\$Eliseo D'Annunzio– Eliseo D'Annunzio2014年06月20日 00:41:02 +00:00Commented Jun 20, 2014 at 0:41 -
1\$\begingroup\$ @WallyWest It's called a spread operator and it's part of the ES6 draft. \$\endgroup\$nderscore– nderscore2014年06月20日 14:12:59 +00:00Commented Jun 20, 2014 at 14:12
-
\$\begingroup\$ @nderscore So If I'm understanding the spread operator syntax, your use of
[...prompt(x=0)]
has taken the prompt with a default value of 0 (which goes later to be used in the sum), and applies that input as an array of characters...? Which technically would be the same asprompt(x=0).split("")
, right? \$\endgroup\$Eliseo D'Annunzio– Eliseo D'Annunzio2014年06月25日 11:24:27 +00:00Commented Jun 25, 2014 at 11:24 -
1\$\begingroup\$ @WallyWest
prompt(x=0)
means "set x to 0, callprompt
with the value of setting x to 0", which is to say, 0. It would be equivalent to write(x=0,prompt(x))
\$\endgroup\$Cyoce– Cyoce2016年01月06日 06:27:59 +00:00Commented Jan 6, 2016 at 6:27
8086 Assembly (16-bit) - (削除) 47 (削除ここまで) 41 bytes
The contents of the test.com
file are:
98 01 c3 b4 01 cd 21 3c 0d 75 f5 89 c7 c6 05 24
89 d8 b1 0a 4f 31 d2 f7 f1 80 ca 30 88 15 09 c0
75 f2 89 fa b4 09 cd 21 c3
Actual work is done in the first 11 bytes; I need the rest to print the result in decimal notation.
Source code (give as input to the DOS debug.com
assembler):
a
; input the string; count the sum
cbw
add bx, ax
mov ah, 1
int 21
cmp al, d
jne 100
; Prepare for output: stuff an end-of-line marker
mov di, ax
mov [di], byte 24
mov ax, bx
mov cl, a
; 114
; Divide by 10; write digits to buffer
dec di
xor dx, dx
div cx
or dl, 30
mov [di], dl
or ax, ax
jne 114
; Print the string
mov dx, di
mov ah, 9
int 21
ret
rcx 29
n test.com
w
q
Some notes on the code:
- Only handles one line (up to end-of-line character 13); hangs if no end-of-line
- Only 7-bit characters are supported (results are incorrect otherwise)
- Outputs 0 for empty input
- Cannot handle output greater than 64K
- Instruction at address 0x10d overwrites itself (pure coincidence)
- Have to use DOS emulators like DosBox to assemble and run this program
-
\$\begingroup\$ How can you understand that? o.O \$\endgroup\$Knerd– Knerd2014年06月19日 08:04:38 +00:00Commented Jun 19, 2014 at 8:04
-
\$\begingroup\$ Love the brutality of just writing the output right into your code :) BTW, You can -2 bytes in the "Prepare for output" section by replacing
mov di, ax
withxchg ax, di
and alsomov ax, bx
withxchg ax, bx
. Nice work! \$\endgroup\$640KB– 640KB2020年01月20日 16:08:40 +00:00Commented Jan 20, 2020 at 16:08 -
\$\begingroup\$ You could also use
si
instead ofdi
for the output buffer pointer since on all mainstream versions of DOSsi
is initialized to100h
by default saving one my byte by removing thexchg ax, di
. \$\endgroup\$640KB– 640KB2020年01月20日 16:22:02 +00:00Commented Jan 20, 2020 at 16:22 -
\$\begingroup\$ OR (last one I promise) you could use
bx
for your pointer and movemov cl, 0ah
andxchg ax, bx
abovemov byte ptr [bx], '$'
and it'll use the0x10d
address as you had it before. Same 38 byte size as above though. \$\endgroup\$640KB– 640KB2020年01月20日 16:59:46 +00:00Commented Jan 20, 2020 at 16:59
CJam, 3 bytes (sum 260)
q1b
You can try it online.
Thanks jimmy23013 for helping chop off 2 characters :)
Explanation:
q read the input into a string
1b convert from base 1, treating each character as its numeric value
-
1\$\begingroup\$
q1b
is shorter. \$\endgroup\$jimmy23013– jimmy230132016年01月04日 03:24:35 +00:00Commented Jan 4, 2016 at 3:24
Ruby, (削除) 13 (削除ここまで) 12 bytes
p~9+gets.sum
sum
is a built-in function that sums the characters of a string. Subtracts 10 to account for the newline at the end of gets
's return value.
(Edited 4 years later to change x-10
to ~9+x
... the value of ~9
is -10
, but it lets us remove the space between p
and its argument, saving a byte.)
-
\$\begingroup\$ I am not familiar with Ruby at all, could you explain your code please? \$\endgroup\$Knerd– Knerd2014年06月05日 14:30:42 +00:00Commented Jun 5, 2014 at 14:30
-
1\$\begingroup\$ gets is a function that reads a string from standard in until a newline is read, it returns a String. String#sum adds the values of each character, which returns a Fixnum. Fixnum#- is just subtraction.
p
is a method for outputting the debug value of something on a line. \$\endgroup\$Kyle Smith– Kyle Smith2014年06月05日 14:35:27 +00:00Commented Jun 5, 2014 at 14:35
Python 3 - 28 bytes
print(sum(map(ord,input())))
Example run:
$ ./sum_string.py <<< 'Hello World!'
1085
Gets input from stdin, map
s the ord
function to it to get the ASCII value of each character, sum
s it and print
s.
-
\$\begingroup\$ Ninja'd, I had the exact same idea. +1 for that. \$\endgroup\$seequ– seequ2014年06月05日 15:16:15 +00:00Commented Jun 5, 2014 at 15:16
-
\$\begingroup\$ @TheRare So did I, even though mine was longer, because I used Python 2.7. I'm getting rusty ;) \$\endgroup\$ɐɔıʇǝɥʇuʎs– ɐɔıʇǝɥʇuʎs2014年06月05日 15:17:21 +00:00Commented Jun 5, 2014 at 15:17
-
\$\begingroup\$ @Synthetica I always use Python 2.7, on which the answer would have been
print sum(map(ord,raw_input()))
\$\endgroup\$seequ– seequ2014年06月05日 15:18:44 +00:00Commented Jun 5, 2014 at 15:18 -
1\$\begingroup\$ @TheRare Which was my exact answer ;) \$\endgroup\$ɐɔıʇǝɥʇuʎs– ɐɔıʇǝɥʇuʎs2014年06月05日 15:19:42 +00:00Commented Jun 5, 2014 at 15:19
-
\$\begingroup\$ Nitpicking here, but you can make it perform better by changing
map(ord,input())
toinput().encode()
. Bytes objects can still be summed, and it stays the same length. \$\endgroup\$cjfaure– cjfaure2014年06月06日 14:30:11 +00:00Commented Jun 6, 2014 at 14:30
Befunge98, 6 bytes, sum: 445
2j@.~+
Any interpreter should be fine. I use CCBI.
Use as follows:
printf 'Hello World!' | ccbi calc.fg
Works for multibyte chars and empty strings.
Explanation
2j
- jump over the next two instructions (@
and.
- see below)~
- put the next char on the stack+
- add the code value of the new char to the current sum. The instruction pointer wraps to the beginning and the cycle repeats- when
~
encounters an EOF it inverses the direction of the pointer and the two "hidden" instructions are executed: .
- print the sum@
- exit
Python, 24 bytes
This is shorter than any Python solution so far: an unnamed anonymous function, which takes the string as an argument, and returns the sum.
lambda x:sum(x.encode())
First, x.encode()
transforms it into a bytes
object. Then, sum
adds the char-code values. As this is a lambda function, the value is implicity returned.
Additionally, one could have lambda x:sum(map(ord,x))
for the same byte count.
PowerShell - 27
[char[]]$args[0]|measure -s
Example
> SumChars.ps1 'Hello World!'
Count : 12
Average :
Sum : 1085
Maximum :
Minimum :
Property :
-
\$\begingroup\$ 26 if you use
[char[]]"$args"|measure -s
as long as there is only one $arg entry. \$\endgroup\$TessellatingHeckler– TessellatingHeckler2019年01月05日 08:34:29 +00:00Commented Jan 5, 2019 at 8:34
Julia - (削除) 11 (削除ここまで) 7 characters, resultant sum = (削除) 943 (削除ここまで) 536
Since the question allows the input to come from whatever source you want, I choose an existing variable. Assume that A
contains the string we wish to evaluate.
sum(A)1
As it turns out, you can sum the string directly, and it will evaluate... however, due to the way that summing of chars is handled, if there is an odd number of characters in the string, it will output a character, rather than an integer of any sort. As such, we force it to cast to int by multiplying by 1.
Old version:
sum(A.data)
Will output in a hexadecimal notation (if the sum is less than 256, it'll be 0x??
, otherwise it'll be 8 byte as 0x????????
). If used in code where the result is used, it will operate just like any other number (it's just how Julia displays unsigned ints).
To see the value of the result in decimal, enclose the above in int()
, as in int(sum(A.data))
.
For anybody who doesn't know Julia, you assign to A
exactly the same way you do other assignments to variables. So, A="Hello World!"
or A="sum(n.data)"
. In the case where you need to put in "
or '
characters, there are multiple options, the easiest of which (because it avoids need for knowledge of the nuances of Julia string literals) is A=readline()
, followed by simply typing in the string into STDIN (won't handle newlines, though). The escape sequence for newline is, as usual, \n
, but I don't believe you can use that with readline().
-
\$\begingroup\$ +1 for the god damn clever solution ^^ Could you post, how to assign the test value to the variable
n
? I don't know Julia at all ;) \$\endgroup\$Knerd– Knerd2014年06月06日 07:09:48 +00:00Commented Jun 6, 2014 at 7:09 -
\$\begingroup\$ @Knerd - I've edited it in. Hope that helps. \$\endgroup\$Glen O– Glen O2014年06月06日 11:12:25 +00:00Commented Jun 6, 2014 at 11:12
-
\$\begingroup\$ Minor change - switched variable from
n
toA
to reduce the resultant sum from 988 to 943. \$\endgroup\$Glen O– Glen O2014年06月06日 17:22:08 +00:00Commented Jun 6, 2014 at 17:22 -
\$\begingroup\$ OK, much bigger change - I realised that you can sum the string directly, rather than extracting the characters with
.data
; but because they're characters, they produce a character result for an odd number of characters. Multiplication by 1 corrects that. \$\endgroup\$Glen O– Glen O2014年06月06日 17:29:21 +00:00Commented Jun 6, 2014 at 17:29
K5, 2 bytes (function), 5 bytes (program)
Function
+/
Program
+/0:`
Not sure if K5 was created before or after this challenge was posted. Regardless...THIS IS AWESOME!!
In K5, if you perform arithmetic operations on strings, it converts the characters to their ASCII codes. So this just uses the sum operator +/
(actually, it's plus + over).
Matlab/Octave 4 bytes (bonus: 405)
This code is an anonymous function, that does the job, it will take a string, and return the required number.
@sum
-
\$\begingroup\$ I am not sure about the
gs2
answer, but at least with the same approach as the Julia answer, I should still writesum(A)
. I thinksum
alone is not ok (wouldn't even be valid code=). \$\endgroup\$flawr– flawr2016年01月06日 17:33:49 +00:00Commented Jan 6, 2016 at 17:33
Go (59 characters)
func d(s string)(t int){for _,x:=range s{t+=int(x)};return}
Everything in Go is utf8 by default. Codetext in ` delimeters run through itself gives an output of: 5399.
-
\$\begingroup\$ I have to say I'm rather surprised there's no
math.Sum
for use withmap
or similar \$\endgroup\$cat– cat2016年01月02日 14:37:59 +00:00Commented Jan 2, 2016 at 14:37
Javascript ES6, 41 bytes
_=>[..._].map(y=>x+=y.charCodeAt(),x=0)|x
Thanks to @ETHproductions for 2 bytes saved!
-
1\$\begingroup\$ How about
_=>[..._].map(y=>x+=y.charCodeAt(),x=0)|x
? \$\endgroup\$ETHproductions– ETHproductions2016年01月04日 02:57:53 +00:00Commented Jan 4, 2016 at 2:57
SML, (削除) 42 (削除ここまで) 36
Just adding another language.
fun$x=foldl op+0(map ord(explode x))
Converts String to char array, calculates ascii number of each value and calculates the sum of all ascii numbers.
-
1\$\begingroup\$ You can drop some spaces and the leading
;
. 36 bytes: tio.run/##DcpBCoAgEAXQq0zRQomiC7ivdYuWEWgRfB2xKby9uXzwHo/… \$\endgroup\$Laikoni– Laikoni2017年11月24日 21:50:08 +00:00Commented Nov 24, 2017 at 21:50
Gol><>, 4 bytes
iEh+
-
\$\begingroup\$ Is it pronounced like 'Golfish?' \$\endgroup\$cat– cat2016年01月02日 18:01:28 +00:00Commented Jan 2, 2016 at 18:01
-
\$\begingroup\$ @cat Yes, it's golfish. \$\endgroup\$randomra– randomra2016年01月02日 18:08:12 +00:00Commented Jan 2, 2016 at 18:08
-
\$\begingroup\$ @randomra is that "gol•fish" or "golf•ish"? As in a fish with gol, or something kind of like golf? \$\endgroup\$Cyoce– Cyoce2016年01月06日 15:22:46 +00:00Commented Jan 6, 2016 at 15:22
-
\$\begingroup\$ imagine my disappointment when I scroll down \$\endgroup\$Seggan– Seggan2022年10月11日 21:06:16 +00:00Commented Oct 11, 2022 at 21:06
C 32
f(char*s){return*s?*s+f(s+1):0;}
-
\$\begingroup\$
main(int argc,char **argv){return(argc?main(0,&(argv[1])):(**argv?**argv+main(0,argv)+((*argv)++?0:0):0));}
(107 chars) though it ignores the first character for some reason. Also, POSIX exit codes are only 8 bits; in bash,echo $?
. \$\endgroup\$user21677– user216772014年06月05日 19:27:27 +00:00Commented Jun 5, 2014 at 19:27 -
\$\begingroup\$ the rules were a little bit broad so i didn't use main. i'll work on something shorter maybe \$\endgroup\$bebe– bebe2014年06月05日 19:51:48 +00:00Commented Jun 5, 2014 at 19:51
-
\$\begingroup\$ @bebe I changed the rules a bit, to make it clear what is needed ;) \$\endgroup\$Knerd– Knerd2014年06月06日 07:11:36 +00:00Commented Jun 6, 2014 at 7:11
D (function: 60)
Definitely not in it to win it.
Assuming it doesn't need to be a complete program
int c(string i){int s;foreach(e;i){s+=cast(int)e;}return s;}
Called like so
void main ()
{
import std.stdio;
auto hw = "Hello World!";
writefln("%s = %d", hw, c(hw));
}
Output:
Hello World! = 1085
D (program: 133)
Does not count line breaks.
void main(){import std.algorithm,std.stdio;stdin.byLine.map!((a){int s;foreach(e;a){s+=cast(int)e;}return s;}).reduce!"a+b".writeln;}
With more whitespace and longer variable names for readability
void main () {
import std.algorithm, std.stdio;
stdin.byLine
.map!((line) {
int sum;
foreach (ch; line) {
sum += cast(int)ch;
}
return sum;
})
.reduce!"a+b"
.writeln;
}
To support line breaks in the input, I could either use byLine(KeepTerminator.yes)
— the correct way, for 20 characters — or append a '\n'
to my line — which breaks single-line input and may give the wrong sum on Windows because of CRLF, for 18 characters.
-
\$\begingroup\$ +1 for posting even if you know, that you won't win \$\endgroup\$Knerd– Knerd2014年06月05日 18:09:43 +00:00Commented Jun 5, 2014 at 18:09
JavaScript (ES6) 54 (削除) 58 (削除ここまで)
(削除)
alert([].reduce.call(prompt(),(v,c)=>v+c.charCodeAt(0),0))
(削除ここまで) 54 bytes thanks to nderscore:
alert([...prompt()].reduce((v,c)=>v+c.charCodeAt(),0))
-
\$\begingroup\$ Works good, I tried it by now in es6fiddle.net \$\endgroup\$Knerd– Knerd2014年06月05日 17:20:06 +00:00Commented Jun 5, 2014 at 17:20
-
\$\begingroup\$ You could just use Firefox ;) \$\endgroup\$core1024– core10242014年06月05日 17:30:14 +00:00Commented Jun 5, 2014 at 17:30
-
1\$\begingroup\$ I was at work so :D \$\endgroup\$Knerd– Knerd2014年06月05日 17:40:14 +00:00Commented Jun 5, 2014 at 17:40
-
1\$\begingroup\$ 54:
alert([...prompt()].reduce((v,c)=>v+c.charCodeAt(),0))
\$\endgroup\$nderscore– nderscore2014年06月06日 00:37:20 +00:00Commented Jun 6, 2014 at 0:37 -
1\$\begingroup\$ Got it down to 51 now :)
alert([...prompt(x=0)].map(y=>x+=y.charCodeAt())|x)
\$\endgroup\$nderscore– nderscore2014年06月06日 16:05:30 +00:00Commented Jun 6, 2014 at 16:05
Delphi ((削除) 87 (削除ここまで) 83)
function x(s:string):int64;var c:char;begin x:=0;for c in s do x:=result+ord(c)end;
Ungolfed
function x(s:string):int64;
var
c:char;
begin
x:=0;
for c in s do
x:=result+ord(c)
end;
Loops through S
adding the ord
value of the char to the result. where x==result
Edits:
Saved 4 characters by switching to int64 and changing the adding to the sum.
-
\$\begingroup\$ Do you have a free version of Delphi (insert you version here) availiable? \$\endgroup\$Knerd– Knerd2014年06月06日 14:54:44 +00:00Commented Jun 6, 2014 at 14:54
-
\$\begingroup\$ Hm.. Not really sorry. But I can explain un-golfed what happens where and do some testcases if you want. Free pascal has more or less the same syntax so you could do that. \$\endgroup\$Teun Pronk– Teun Pronk2014年06月06日 14:57:00 +00:00Commented Jun 6, 2014 at 14:57
-
\$\begingroup\$ Ok, I gonna check that out. \$\endgroup\$Knerd– Knerd2014年06月06日 15:02:21 +00:00Commented Jun 6, 2014 at 15:02
k (8 chars)
+/6h0ドル:0
Q translation
sum `int$read0 0
Bonus value:
k)+/6h0ドル:0
+/6h0ドル:0
438i
J (7)
So close, yet so far... Oh well, I guess 7 is decent enough, since this answer also accepts empty strings. (I'm basing my usage of a variable as input on the phrase from a file, stdin or whatever
)
+/a.i.b
Explanation:
a.
┌┬┐├┼┤└┴┘│─ !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~��������������������������������������������������������������������������������������������������������������������������������
a.
contains all ASCII chars.
'people' i. 'pow'
0 2 6
x i. y
is similar to python's [x.index(i) for i in y]
.
a. i. 'Hello World!'
72 101 108 108 111 32 87 111 114 108 100 33
Therefor, a. i. y
converts y
to an array of its ASCII values
+/1 2 3 4 5 6
21
+/
is like sum
: +/1 2 3 4 5 6
means 1+2+3+4+5+6
+/ a. i. 'Hello World!'
1085
The whole thing in action
For the bonus:
b=:'+/a.i.b'
+/a.i.b
482
Not bad, I guess.
b=:'0\{+}/'
+/a.i.b
478
Well, darn.
A=:'+/a.i.A'
+/a.i.A
449
Thanks @algorithmshark
A=:'+/3 u:A'
+/3 u:A
413
Thanks @marinus
-
\$\begingroup\$ +1 for the great explanation. One little question, where can I best execute J? \$\endgroup\$Knerd– Knerd2014年06月05日 15:07:54 +00:00Commented Jun 5, 2014 at 15:07
-
1\$\begingroup\$ @Knerd From the makers (jsoftware.com) I guess, I don't know any online interpreters. (Fun fact: they have an official console for Android.) jsoftware.com/download/j801 \$\endgroup\$ɐɔıʇǝɥʇuʎs– ɐɔıʇǝɥʇuʎs2014年06月05日 15:10:10 +00:00Commented Jun 5, 2014 at 15:10
-
\$\begingroup\$ @Synthectica That is cool :D Now I need an Android smartphone :P \$\endgroup\$Knerd– Knerd2014年06月05日 15:11:01 +00:00Commented Jun 5, 2014 at 15:11
-
\$\begingroup\$ Renaming
b
toA
results in a score of 449. \$\endgroup\$algorithmshark– algorithmshark2014年06月05日 16:20:01 +00:00Commented Jun 5, 2014 at 16:20 -
\$\begingroup\$ @algorithmshark Oh, right! I'll claim that star for now ;) \$\endgroup\$ɐɔıʇǝɥʇuʎs– ɐɔıʇǝɥʇuʎs2014年06月05日 16:42:17 +00:00Commented Jun 5, 2014 at 16:42
R, (削除) 35 characters (sum of 3086) (削除ここまで) 26 bytes (sum of 2305)
sum(utf8ToInt(readline()))
readline()
is one character longer than scan(,"")
but scan
split the input on spaces by default.
Usage:
> sum(utf8ToInt(readline()))
Hello World!
[1] 1085
> sum(utf8ToInt(readline()))
sum(utf8ToInt(readline()))
[1] 2305
> sum(utf8ToInt(readline()))
q/%8hnp>T%y?'wNb\},9krW &D9']K$n;l.3O+tE*$*._B^s!@k\&Cl:EO1zo8sVxEvBxCock_I+2o6 yeX*0Xq:tS^f)!!7=!tk9K<6#/E`ks(D'$z$6円Ac+MT&[s[]_Y(`<g%"w%cW'`c&q)D0ドル#C$QGf>?A$iawvc,}`9!('`c&q)D0ドル#C$QGf>?A$iawvc,}`9!(
[1] 14835
Hello World!
using two different languages for ASCII values on my computer. \$\endgroup\$