«Сложить» буквы в слове

17

Мой папа - учитель на пенсии, и он давал комбинированные тесты по орфографии и математике, где студент записывал слово, а затем «набирал» слово, складывая буквы, где a = 1, b = 2 и т. Д. (например, кошка = 3 + 1 + 20 = 24). Это облегчало оценку тестов, так как он должен был просто проверять правильность «оценок», а не неправильно написанных слов, и имел дополнительное преимущество в тестировании 2 навыков одновременно.

Он нанял моего друга, чтобы он написал программу, которая будет набирать слова для него, чтобы он мог генерировать длинные ключи ответа без ошибок. Эта проблема вдохновлена ​​этой программой.

Требования:

  1. Принять любое слово с прописными и строчными буквами
  2. Вернуть ошибку для любых специальных символов, например пробелов, дефисов, @ ^% # и т. Д.
  3. a = 1, b = 2, ... и A = 1, B = 2, ...
  4. Распечатать счет слова
  5. (Необязательно) проверьте, что слово находится в словаре после оценки, и выведите предупреждение, если это не так.
  6. Нет импорта внешнего письма-> словарь цифр. Вы должны создать это самостоятельно.

Любой язык приемлем. Это похоже на « цифровую корневую битву» , но намного проще.

Zach
источник
2
Это должен быть код гольф?
Питер Тейлор
2
@ Зак Использование code-golfтэга.
Лоуджекер,
2
Твой папа даже удосужился научить правилу «Я до Е, кроме С»?
Натан Меррилл
2
Да, только проверка результатов? Я бы сказал, как кошка aaaaaaaaaaaaaaaaaaaaaaaa. Папа: оценка 24? Это верно!
ericw31415
3
@ ericw31415 Каждая хеш-функция имеет коллизии ;-). Пока что ни один из его учеников не пробовал этот вектор атаки
Зак

Ответы:

8

Golfscript - 23 chars

0\{.31&.(.26%=@64///+}/

Ensure there are no trailing newlines in input (e.g. use echo -n).

Nabb
источник
We have a new winner!
Zach
Generally, external filtering is supposed to be included in character count somehow (Ctrl-f external filtering.), though I assume it's only 2 extra characters to do without.
Jesse Millikan
2
@Jesse: echo -n really doesn't count as external filtering -- in fact the answer you linked suggests it as a valid form for input.
Nabb
10

Brainf*** (100)

>+[>,---------]<[<]>>
[--------------------
---------------------
-------------->]<[[[<
]>+[>]<->]<<]>-.

I do have to admit though, this does not quite comply with all of the requirements. First, it only accepts capital letters, and the word must be ended with a tab. It has undefined behaviour for invalid characters, and does not throw an error. It displays the sum of the letters as an ASCII character. For example, if the word is "HELLO", (8+5+12+12+15 = 52) it will display the character "4", which is the ASCII character for 52. This also means that the program freaks out when the sum is more than 255.

But other than that, it works just fine. Give me a break, my brain can only handle small doses of...well, you know.

Peter Olson
источник
Why end the word with a tab instead of a newline?
Lowjacker
@Lowjacker Because I assumed TAB is more simple than worrying about \n or \r\n or \n\r. And If I used newline, I wouldn't have a nice round number like 100 as the character count.
Peter Olson
What would happen if you got a large dose?
Mateen Ulhaq
8

Python (65 64)

print sum(['',ord(i)-64]['@'<i<'[']for i in raw_input().upper())

This raises an error if the word contains non-letter characters, but not a helpful or informative one. (Edit: tip of the hat to st0le for the indexing trick.)


источник
1
print sum(['',ord(i)-64]['@'<i<'[']for i in raw_input().upper()) shaved a couple of chars.
st0le
Just one char actually. :-\
st0le
Save 4 characters by using input; forces user to put quotes around input strings, but "user-friendly" and "not dangerous" aren't in the spec!
jscs
Well, just change it to Python 3! raw_inputinput
Oleh Prypin
print sum([i,ord(i)-64]['@'<i<'[']for i in raw_input().upper()) another byte shaved
Alexander Nigl
8

Ruby, 43 characters

$_=gets.upcase;p~/[^A-Z]/?_: $_.sum-64*~/$/

The error message this generates isn't exactly helpful, though. Both solutions posted here assume the input has no trailing linebreak, so to test them, use echo -n.

Ruby, 76 characters with dictionary check

l=STDIN.gets;$_=l.upcase;p~/[^A-Z]/?_: $_.sum-64*~/$/;[*$<].index(l)||$><<?W

The warning message consists of the single character "W". The path to the dictionary has to be supplied via ARGV. Example usage:

$ echo -n asd | ruby addletters.rb /usr/share/dict/words
24
W
$ echo -n cat | ruby addletters.rb /usr/share/dict/words
24
Ventero
источник
2
You could chop of 9 characters in the dictionary check version by making the error message an exclamation mark.
Peter Olson
You get a consolation prize for the shortest entry with a dictionary check. Way to go!
Zach
Why was the dictionary check proposed if it doesn't give you any real edge (on the contrary, it just bloated the code)?
Helper Method
5

Python 2.6 (72 Chars) Without dictionary check

print sum(map(" abcdefghijklmnopqrstuvwxyz".index, raw_input().lower()))

Python 2.6 (178 Chars*) With dictionary check

w=raw_input().lower()
print sum(map(" abcdefghijklmnopqrstuvwxyz".index, w))
if w not in open('/usr/share/dict/american-english').read().split():
 print('Word not in dictionary')

*Can be lowered to 156 with a less helpful error message. :-)

Thanks to all commenters for helping improve this.

John
источник
You might want to consider using the sum builtin with a generator expression, rather than a for loop. It would let you trim off a few chars (~17).
@jloy: Thanks for pointing that out.
John
the parentheses on print can be eliminated
st0le
its seems you're using a just once, so use the literal itself..."0abc....z".index(i) will work equivalently.
st0le
The 0 in your scoring array is clever, but it also means that cat0 is accepted without error, which isn't right, I think. Which is too bad, since it would allow you to pass map(a.index,w) to sum instead (substituting the literal for a as st0le suggests).
4

Perl (52) (48)

golfed even more thanks to Timwi

perl -lpe "($w=uc)=~/[^A-Z]/&&die;$w=~s/./$_-=64-ord$&/ge"

chinese perl goth
источник
You're missing the -e flag there.
Lowjacker
1
You should at least include the p and l interpreter flags in your character count. See this discussion on meta.
Ventero
syntax error at -e line 1, near "(=" Execution of -e aborted due to compilation errors. What am I doing wrong?
user unknown
if you're running on unix, change double quotes to single ones
chinese perl goth
@Timwi: 'Accept any word with uppercase and lowercase letters' went to the wrong thread, sorry. I deleted it now. @chinese: Yes, thanks, with single quotes it's ok. As long as I restrict myself to ascii input. :)
user unknown
4

Python (80)

w=raw_input().lower()
s=0
for l in w:s+=range(97,123).index(ord(l))+1
print s

Python v2 (65 but char ` will get accepted)

print sum(map(range(96,123).index,map(ord,raw_input().lower())))

v3 (60 chars, @ will be accepted but not counted, thanks jloy)

print sum(map(range(64,91).index,map(ord,input().upper())))
OneOfOne
источник
HINT: THERE IS A WAY TO REMOVE ONE MORE CHAR FROM YOUR SOLUTIONS. :)
2
@jloy What a helpful 'hint'. ;)
Mateen Ulhaq
4

Scala: 59 chars, 7 of them payload, no dict:

(0/:"Payload".map(c=>if(c.isLetter)(c-'A')%32 else-999))(_+_)
67

No dictionary so far. Negative result means: Negative!

(0/:"Pay!wall?".map(c=>if(c.isLetter)(c-'A')%32 else-999))(_+_)   
-1915

Handles German Umlaute gracefully, by the way:

(0/:"Müllrößchen".map(c=>if(c.isLetter)(c-'A')%32 else-999))(_+_)
155
user unknown
источник
Wow, less chars than the Perl version (and still more readable).
Helper Method
I tried SHiNKiROUs and chinesis-Perl-solution, but they didn't work for me. Saved them as alpha.pl and started them by perl alpha.pl. Do they just handle Ascii? Well - at Perl is such an old beast ... :)
user unknown
Perl and Unicode is a big mess. You probably have to run them as perl -M5.010 alpha.pl or something like that.
Peter Taylor
I heard that I need single quotes instead of double quotes on Linux, and this worked, thanks.
user unknown
4

Java + Google Guava libraries, 347 characters, with dictionary check

Unreadable 1 long string version :-)

import java.io.*;import com.google.common.base.*;import com.google.common.io.*;class C{public static void main(String[]a)throws Exception{int s=0;for(int c:a[0].toUpperCase().toCharArray()){assert(c>64&&c<91);s+=c-64;}String d=Files.toString(new File(a[1]),Charsets.UTF_8);if(StringUtils.containsIgnoreCase(d,a[0]))System.out.println("w");System.out.println(s);}}

Human-readable version (sort of :-))

import java.io.*;

import com.google.common.base.*;
import com.google.common.io.*;

class C {
    public static void main(String[] a) throws Exception {
        int s=0;

        for(int c : a[0].toUpperCase().toCharArray()) {
            System.out.println(c);
            assert(c > 64 && c < 91);
            s += c - 64;
        }

        String d = Files.toString(new File(a[1]), Charsets.UTF_8);

        if (d.contains(a[0])) System.out.println("w");

        System.out.println(s);
    }
}

The dictionary path is now passed in via a[1], for assertions to work you have to use the -eaflag (+3 more chars). As for the dictionary, the dict /usr/share/dict/words (should be available on most *nix systems) has been used.

Helper Method
источник
So far you're the only one with a dictionary check, so +1
Zach
one line? this isn't particularily readable this way, though i guess it saves chars
Nate Koppenhaver
I will add a more readable solution (and also a shorter one using Google Guava to reduce boilerplate code).
Helper Method
You just allow ascii, but use Charset.UTF-8?
user unknown
1
Because the String UTF-8 is shorter than the other charsets :-).
Helper Method
4

Python 3, 95 chars with dictionary

d=input().lower()
print(d in open("d").read()and sum(['',ord(c)-96]['`'<c<'{']for c in d)or'f')

The dictionary has to be in a file called d.

Python 3, 61 without dictionary, but stolen idea

print(sum(['',ord(c)-96]['`'<c<'{']for c in input().lower()))
cemper93
источник
3

Perl (71)

($a)=lc<>;$a=~/[^a-z]/i&&die;$x+=ord$_ for split//,$a;die$x-96*length$a;
Ming-Tang
источник
3

VB.NET, 84 82 73 71

Console.Write(Console.ReadLine.Sum(Function(c)Asc(Char.ToUpper(c))-64))


Edit: With validation is:

Dim r=Console.ReadLine
Console.Write(If(r.All(AddressOf Char.IsLetter),r.Sum(Function(c)Asc(Char.ToUpper(c))-64),"Invalid input."))

129 characters. In which case:

C#, 118

var r=Console.ReadLine();Console.Write(r.All(char.IsLetter)?r.Sum(c=>char.ToUpper(c)-64).ToString():"Invalid input.");
Ry-
источник
1
This doesn't validate the input.
Lowjacker
Oops! Hold on a second...
Ry-
3
I think you should provide complete programs. Your C# solution doesn’t compile; you need to place it in a Main method inside a class declaration, and count all the characters.
Timwi
1
No, because that code doesn't do anything, and it's not fair to disadvantage users of object-oriented languages. This is valid C#/VB.NET anyways.
Ry-
3

Improving slightly on John's answer: Python (90)

s=0
for i in raw_input().lower():
 s+=("abcdefghijklmnopqrstuvwxyz".index(i)+1)
print(s)
Zach
источник
2
adding the dummy char in the string beginning is shorter...parentheses can be removed
st0le
3

Erlang, 104

a()->
a(string:to_lower(io:get_line([])),0).
a([_|[]],S)->
S;
a([C|R],S) when C<${, C>=$`->
a(R,S+C-$`).
xymostech
источник
3

Golfscript - 39 chars

n%~{.96>{96}{64}if-..26>\0<|{0/}*}%{+}*

The error it throws is not exactly the best, but hey, it aborts execution.

Juan
источник
I don't know anything about golfscript, so I'm going to assume this meets the requirements and declare you the winner!
Zach
Whoops, you've been beat! I guess 2 days isn't long enough to wait on a code golf question?
Zach
3

PYTHON 62 68* Characters

print sum(map(chr,range(65,91)).index(c)+1 for c in input().upper())

Requires user to input strings using quotes, and is not safe (input executes code), but, as I said in a comment to another post, "user-friendly" and "not a security risk" ain't in the spec!


* I forgot about print, dammit.

jscs
источник
jloy's answer is still shorter, actually, because of the input/raw_input difference.
jscs
2

Ruby 1.9, 69

w=gets.chop.upcase
w[/[^A-Z]/]&&fail
p w.bytes.inject(0){|s,b|s+b-64}
Lowjacker
источник
Does only handle ascii characters. I thought Ruby is from our century? :)
user unknown
@user unknown: The spec doesn't say it has to. Doing it would be rather complicated...
Lowjacker
2

GolfScript, 50(53)

Gives an error on bad characters, but not a very good one (50 characters):

);[{""123,97>+91,65>+?}/].-1?0<{{26%1+}%{+}*}{@}if

Gives "E" on error instead (53 characters):

);[{""123,97>+91,65>+?}/].-1?0<{{26%1+}%{+}*}{;"E"}if

The alphabet-generating snippet 123,97>+ is stolen from Ventero .

Jesse Millikan
источник
2

J (55)

+/64-~-&32`]@.(<&97)`_:@.(<&65)`_:@.(>&122)"0,I.a.&e."0

This satisfies all the conditions except the dictionary one. As an error condition, it returns "infinity" (the underscore symbol in J) for words that contain anything but letters.

Gregory Higley
источник
2

Haskell (127)

(raises an error on weird characters)
(also: the space between toUpper. and \x is needed otherwise it parses it as(toUpper) .\ (x))

import Char
main=getLine>>=putStrLn.show.sum.(map$(-65+).ord.toUpper. \x->if x`elem`['A'..'Z']++['a'..'z']then x else error"")

Haskell (70)

(does not raise an error, but 45% shorter)

import Char
main=getLine>>=putStrLn.show.sum.(map$(-65+).ord.toUpper)
marinus
источник
2

C++ (111 107)

void main(){int a=0;s8*b=new s8[99];for(cin>>b;*b;)if(isalpha(*b))a+=tolower(*b++)-96;else return;cout<<a;}

The "set up"/etc:

#include <iostream>
#include <cstdio>
#include <cctype>

#ifdef _MSC_VER
    typedef __int8 s8;
#else
    typedef signed char s8;
#endif

"Undefined" behavior (It's more 'bad practice' than 'undefined', but oh well):

  • void main() That says it all.
  • I'm using new without delete.
Mateen Ulhaq
источник
1

JavaScript 1.8, 80 chars

Surprisingly readable!

alert(Array.reduce(prompt().toLowerCase(),function(a,b)a+b.charCodeAt(0)-96,0))
Casey Chu
источник
For use in Chrome I had to convert it a little: alert(prompt().toLowerCase().split("").reduce(function(a,b){return a+b.charCodeAt(0)-96},0)). I still like JavaScript solutions most :)
pimvdb
It doesn't return an error when you do invalid character???
ericw31415
1

APL (34)

+/{⍵∊⍳26:⍵}¨{64-⍨A-32×96<A←⎕UCS⍵}⍞

Gives either the score, or a VALUE ERROR if there are non-alphabetic characters in the input.

Explanation:

  • : read a line of input
  • {...}: function applied to every character of input
  • A←⎕UCS⍵: store the ASCII value of the current character in A
  • A-32×96<A: make character uppercase: from A is subtracted 32 if 96<A (so, if it's uppercase), otherwise 0
  • 64-⍨: subtract 64 from this, giving A=1, B=2 ...
  • ¨: apply this function to every character:
  • ⍵∊⍳26: if the character is between 1 and 26...
  • :⍵: then return ⍵ (and since there's no else clause there will be a VALUE ERROR if it's not between 1 and 26)
  • +/: sum all the values together (and this value is automatically outputted because it's the final result).
marinus
источник
1

JavaScript, 60 bytes

s=>[...s.toUpperCase()].reduce((a,b)=>a+b.charCodeAt()-64,0)

If the program must return an error on invalid inputs, then 80 bytes:

s=>/[^a-z]/i.test(s)?_:[...s.toUpperCase()].reduce((a,b)=>a+b.charCodeAt()-64,0)

If an input is invalid, then the console will say that _ is not defined (there must not already be a variable defined called _).

ericw31415
источник
1

Python 3, 58 55

print(sum(ord(x)%32for x in input()if x.isalpha()or z))

without dictionary or stolen idea but still unhelpful error ;)

thx @Eᴀsᴛᴇʀʟʏ

Test here.

Alexander Nigl
источник
I think you can save a byte by switching to python 2 and doing print<SPACE>sum(ord(......., removing the 2 parentheses around the expression.
Rɪᴋᴇʀ
@EᴀsᴛᴇʀʟʏIʀᴋ that is right but than the input has to be in parenthesis and I don't want to promote python 2 ;)
Alexander Nigl
PYTHON 2 IS LIFE!! and also, I don't think that would require the input to be parenthesized?
Rɪᴋᴇʀ
@EᴀsᴛᴇʀʟʏIʀᴋ sry i meant quoted. input() in python3 is raw_input() in python2
Alexander Nigl
oh, I forgot. Hm.
Rɪᴋᴇʀ
1

C, 98 bytes

 int a(char *s){int b=0;while(*s){if(!isalpha(*s))throw 1;b+=(toupper(*(s++))-64);}printf("%d",b);}
Adam Spindler
источник
1

F# (no validation) 79 57 chars

let a w=w|>Seq.fold(fun a b->a+(int b)-65)0|>printfn"%i"
Smetad Anarkist
источник
1

C# with validation: 108 chars (with 12 for error message):

var s=Console.ReadLine();Console.Write(s.All(Char.IsLetter)?s.Sum(x=>x&'_'-'@').ToString():"Invalid input");

C# without validation: 60 53 chars:

Console.Write(Console.ReadLine().Sum(x=>x&'_'-'@'));
driis
источник
1
In the second one without validation, you can reduce the characters even more by removing the s variable declaration and using Console.ReadLine() inline.
hermiod
1

Perl (42 31)

perl -F -pale '$c+=ord(uc$_)-64for@F;$_=$c'

I hope counting F, p, a and l as 1 character was correct.

memowe
источник
1

JavaScript, 68 Bytes

This can almost certainly be golfed more

w=>[...w.toLowerCase()].map(v=>v.charCodeAt()-96).reduce((a,b)=>a+b)

With dictionary check (Node.js & Unix Descendants only) 195 Bytes

Uses /usr/share/dict/words, and can definitely be shortened (see the warn message)

w=>(require("fs").readFile("/usr/share/dict/words",(e,t)=>!(t+"").split`
`.includes(w=w.toLowerCase())&&console.warn(w+" not found in dict")),[...w].map(v=>v.charCodeAt()-96).reduce((a,b)=>a+b))
MayorMonty
источник
For an error message, you do console.error(), not console.warn().
ericw31415
But the challenge said to warn (5. (Optional) check that the word is in a dictionary after scoring, and print a warning if it is not.) Don't mean to be pedantic, but the challenge specified a warning
MayorMonty
@SpeedyNinja I think it still counts, that isn't really the point of the challenge...
Rɪᴋᴇʀ
@EᴀsᴛᴇʀʟʏIʀᴋ it is 1 character shorter ;)
MayorMonty
@SpeedyNinja You're right, I misread.
ericw31415