Bir kelimedeki harfleri 'ekle'


17

Babam emekli bir öğretmendi ve öğrencinin bir kelimeyi heceleyeceği kombine yazım ve matematik sınavları verirdi ve sonra a = 1, b = 2 vb. Harfleri ekleyerek kelimeyi 'puanlar'. (örneğin, kedi = 3 + 1 + 20 = 24). Bu, sınavların notlandırılmasını kolaylaştırdı, çünkü yanlış yazılmış kelimeler yerine yanlış 'puanları' kontrol etmek zorunda kalacak ve aynı anda 2 beceriyi test etme avantajına sahip olacaktı.

Ona bir kelime yazmak için bir arkadaşımı işe aldı, böylece hatasız uzun cevap anahtarları üretebilirdi. Bu sorun bu programdan ilham alıyor.

Gereksinimler:

  1. Büyük ve küçük harf içeren herhangi bir kelimeyi kabul etme
  2. Boşluklar, kısa çizgiler, @ ^% # vb. Gibi özel karakterler için hata döndürün.
  3. a = 1, b = 2, ... ve A = 1, B = 2, ...
  4. Kelimenin puanını yazdır
  5. (İsteğe bağlı) Puanlamadan sonra kelimenin sözlükte olup olmadığını kontrol edin ve değilse bir uyarı yazdırın.
  6. Harici bir harf-> sayı sözlüğü alınmaz. Siz kendiniz üretmelisiniz.

Herhangi bir dil kabul edilebilir. Bu, ' dijital kök savaşı'na benzer , ancak çok daha basittir.


2
Bu bir kod golf olmalı mı?
Peter Taylor

2
@Zach Etiketi kullanma code-golf.
Lowjacker

2
Babanız "C hariç E öncesi ben" kuralını öğretmeyi bile rahatsız etti mi?
Nathan Merrill

2
Evet, sadece skorlarý kontrol ediyor musun? Ben kediyi büyüdüm aaaaaaaaaaaaaaaaaaaaaaaa. Baba: Puan 24 mü? Doğru!
ericw31415

3
@ ericw31415 Her karma işlevinin çarpışmaları vardır ;-). Şimdiye kadar hiçbir öğrencisi bu saldırı vektörünü denemedi
Zach

Yanıtlar:


8

Golfscript - 23 karakter

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

Girişte sondaki yeni satır olmadığından emin olun (örn. Kullanım echo -n).


Yeni bir kazananımız var!
Zach

Genel olarak, dış filtrelemenin bir şekilde karakter sayısına dahil edilmesi gerekiyordu (Ctrl-f dış filtreleme.)
Jesse Millikan

2
@Jesse: echo -ngerçekten dış filtreleme olarak sayılmaz - aslında bağladığınız cevap bunu giriş için geçerli bir form olarak önerir.
Nabb

10

Brainf *** (100)

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

Ancak itiraf etmeliyim ki, bu tüm gereksinimlere tam olarak uymuyor. İlk olarak, sadece büyük harfleri kabul eder ve kelime bir sekme ile bitmelidir. Geçersiz karakterler için tanımlanmamış bir davranışa sahiptir ve hata atmaz. Harflerin toplamını ASCII karakteri olarak görüntüler. Örneğin, kelime "HELLO" ise, (8 + 5 + 12 + 12 + 15 = 52) 52 için ASCII karakteri olan "4" karakterini görüntüler. Bu aynı zamanda programın toplam 255'ten fazla.

Ama bunun dışında , gayet iyi çalışıyor. Bana bir mola verin, beynim sadece küçük dozlarla başa çıkabilir ...


Sözcüğü neden satırsonu yerine sekmeyle sonlandırıyorsunuz?
Lowjacker

@ Lowjacker Çünkü SEKME'nin endişelenmekten daha basit olduğunu varsayıyorum \nveya \r\nveya \n\r. Ve eğer yeni satır kullanırsam, karakter sayısı olarak 100 gibi güzel bir yuvarlak sayıya sahip olmazdım.
Peter Olson

Büyük bir dozunuz olsaydı ne olurdu?
Mateen Ulhaq

8

Python ( 65 64)

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

Sözcük, harf olmayan karakterler içeriyor ancak yararlı veya bilgilendirici karakterler içermiyorsa bu bir hata oluşturur. Düzenle


1
print sum(['',ord(i)-64]['@'<i<'[']for i in raw_input().upper())birkaç karakter tıraş etti.
st0le

Aslında sadece bir karakter. : - \
st0le

Kullanarak 4 karakter kaydedin input; kullanıcıyı giriş dizelerinin etrafına tırnak işareti koymaya zorlar, ancak "kullanıcı dostu" ve "tehlikeli değil" spesifikasyonlarında değildir!
jscs

Sadece Python 3 olarak değiştirin! raw_inputinput
Oleh Prypin

print sum([i,ord(i)-64]['@'<i<'[']for i in raw_input().upper()) başka bir bayt traş
Alexander Nigl

8

Ruby, 43 karakter

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

Ancak, bu hata mesajı tam olarak yardımcı değildir. Burada yayınlanan her iki çözüm de, girdinin sondaki satır sonu olmadığını varsayar, bu nedenle bunları test etmek için kullanın echo -n.

Ruby, sözlük kontrolü ile 76 karakter

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

Uyarı mesajı "W" tek karakterinden oluşur. Sözlüğün yolu ARGV aracılığıyla sağlanmalıdır. Örnek kullanım:

$ echo -n asd | ruby addletters.rb /usr/share/dict/words
24
W
$ echo -n cat | ruby addletters.rb /usr/share/dict/words
24

2
Hata mesajını bir ünlem işareti yaparak sözlük kontrolü sürümünde 9 karakteri kesebilirsiniz.
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.


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"


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())))

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

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.


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()))

3

Perl (71)

($a)=lc<>;$a=~/[^a-z]/i&&die;$x+=ord$_ for split//,$a;die$x-96*length$a;

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.");

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)

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-$`).

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.


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.


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}

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 .


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.


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)

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.

1

JavaScript 1.8, 80 chars

Surprisingly readable!

alert(Array.reduce(prompt().toLowerCase(),function(a,b)a+b.charCodeAt(0)-96,0))

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).

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 _).


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.


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);}

1

F# (no validation) 79 57 chars

let a w=w|>Seq.fold(fun a b->a+(int b)-65)0|>printfn"%i"

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&'_'-'@'));

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.


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))

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
Sitemizi kullandığınızda şunları okuyup anladığınızı kabul etmiş olursunuz: Çerez Politikası ve Gizlilik Politikası.
Licensed under cc by-sa 3.0 with attribution required.