-5
\$\begingroup\$

This question already has an answer here:

Challenge :

Inspired by Alphabet Position Finder

Given an string and one of two possible mathematical operator (+ or -) as input, your task is to return the operation of each alphabet position from the string.

Example :

Input: "Hello World", "+"
=> "Hello World" = [8, 5, 12, 12, 15, 23, 15, 18, 12, 4] (converted to their equivalents)
=> sum alphabet positions = [8 + 5 + 12 + 12 + 15 + 23 + 15 + 18 + 12 + 4]
Output: 124

Note :

  • Ignore non-alphabetical characters
  • You must use 1-indexing (a = 1, b = 2, ..., z = 26)
  • If empty string submitted return a falsy value
  • Operator can be taken as +/-. 1/-1, truthy/falsey, etc.

Test Cases :

"Hello World", "-"             => -108
"I Love golfing", "+"          => 133
"She doesnt love you :(", "+"  => 224
"@#t%489/*-o", "-"             => 5
"", "-"                        => 0
"a", "-"                       => 1
\$\endgroup\$

marked as duplicate by Muhammad Salman, NoOneIsHere, wastl, Sriotchilism O'Zaic code-golf Jun 7 '18 at 16:44

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • \$\begingroup\$ Comments are not for extended discussion; this conversation has been moved to chat. \$\endgroup\$ – Mego Jun 10 '18 at 20:23
3
\$\begingroup\$

Java (JDK 10), 79 bytes

s->o->{int r=0,i=0;for(int c:s)r+=(c&=95)%32*(c>64&c<91?i++<1?1:o:0);return r;}

Try it online!

Original answer before the question was clarified

Java (JDK 10), 46 bytes
s->o->s.chars().reduce(0,(a,b)->a+b%32*(44-o))

Try it online!

\$\endgroup\$
  • \$\begingroup\$ Seems to fail for "to", "-" (expected 5, actual -35). \$\endgroup\$ – Kevin Cruijssen Jun 7 '18 at 14:33
  • 8
    \$\begingroup\$ @KevinCruijssen I give up on this answer. The requirements changed like the wind. This is valid with the very first version of the question, but not with the latest. Also, the test cases were not consistent. So I took the first two test cases and adapted them with the input given and made this answer. \$\endgroup\$ – Olivier Grégoire Jun 7 '18 at 14:36
  • \$\begingroup\$ Seems the challenge is settled now.. I wish we could somehow enforce the use of the Sandbox for ALL challenges.. Anyway, adding .replaceAll("[^A-Za-z]","") takes care of one problem. The other problem is when the operator is - (it starts at the negative index of the first letter, instead of the positive value). I know some possible solutions, but all are pretty long.. \$\endgroup\$ – Kevin Cruijssen Jun 7 '18 at 15:21
  • \$\begingroup\$ @KevinCruijssen I just updated the answer based on the new question. \$\endgroup\$ – Olivier Grégoire Jun 7 '18 at 15:44
2
\$\begingroup\$

Python 2, 70 66 bytes

lambda s,o:eval(o.join(`ord(c)%32`for c in s if c.isalpha())or'0')

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Japt v2.0a0, 12 13 bytes

+1 bytes to handle empty strings :(

r\L ¬®c uHÃrV

Try it

Original 12 byte version which would be valid if an error counts as a falsey value.

f\L ®c uHÃrV

Try it


Explanation

                  :Implicit input of string U and operator string V
r                 :Remove
 \L               : /[^a-z]/gi
    ¬             :Split
     ®            :Map
      c           :  Character code
        u         :  Modulo
         H        :   32
          Ã       :End map
           rV     :Reduce by V
\$\endgroup\$
1
\$\begingroup\$

Japt 2.0 -F0, 19 15 bytes

OvUf\l ®c %HÃqV

Test it online

Explanation:

 OvUf\l ®c %HÃqV
 Ov                 // Japt eval:
   U                //   First input
    f\l             //   Match [A-Za-z] 
        ®    Ã      //   Map; At each item:
         c          //     Char-Code
           %H       //     %32
              qV    //   Join with Second input (operator)
-F0                 // If the first input (string) is empty, output 0
\$\endgroup\$
0
\$\begingroup\$

JavaScript (Node.js), 92 82 67 bytes

t=>o=>t?eval(t.match(/[a-z]/gi).map(x=>parseInt(x,36)-9).join(o)):0

Try it online!

\$\endgroup\$
0
\$\begingroup\$

JavaScript (Node.js), 52 bytes

T=>_=>eval([...T].map(a=>a.charCodeAt()%32).join(_))

Try it online!


Explanation :

T =>                            // first input for lambda function `text` (T)
    _ =>                        // second input, the separator 
        eval(                   // evaluate a string
            [...T]              // conver `T` into a comma separated array of its char  
            .map(a=>            // map over them `a` is representing the char
                a.charCodeAt()  // a.charCodeAt(0) 
                    % 32)       // find remainder of it when divided by 32
                .join(_)        // and join them using the separator
        )                       // end eval

Note :

This completely ignores non-alphabetic words since the challenge seems to change after every two minutes, I chose this. (Can be changed if OP wants me to)


I posted this on the other challenge, just changed .join to .join(o) :

JavaScript (Node.js), 57 bytes

t=>o=>t.match(/[a-z]/gi).map(i=>parseInt(i,36)-9).join(o)

Try it online!


\$\endgroup\$
0
\$\begingroup\$

CJam, 24 bytes

q~el{'`-_0>*}%{},0+':@+~

Try it online!

Could probably be more efficient, had to add 0 to the array to handle the empty string.

\$\endgroup\$
0
\$\begingroup\$

05AB1E, 9 bytes

áÇ32%Iý.E

Semi-port of @JonathanAllan's 05AB1E answer for the linked challenge.
Outputs nothing as falsey values if the input doesn't contain any letters.

No TIO, because .E isn't on TIO yet. Here an alternative version that does .E manually áÇ32%Iý“…¢(“s')J.e: Try it online or verify all test cases.

Explanation:

á        # Only keep letters of the first input-string
         #  i.e. "Hello World" → "HelloWorld"
 Ç       # Get the list of the unicode values of each letter
         #  i.e. "HelloWorld" → [72,101,108,108,111,87,111,114,108,100]
  32%    # modulo-32 each
         #  i.e. [72,101,108,108,111,87,111,114,108,100] → [8,5,12,12,15,23,15,18,12,4]
Iý       # Join it with the operator input
         #  [8,5,12,12,15,23,15,18,12,4] and "-" → "8-5-12-12-15-23-15-18-12-4"
  .E     # Run it as Python-eval
         #  i.e. "8-5-12-12-15-23-15-18-12-4" → -108

More details on the commit of .E, prove it works, and the alternative version here in a similar answer of mine.

\$\endgroup\$
0
\$\begingroup\$

Jelly, 10 bytes

ŒuØAiⱮḟ0jV

Try it online!

Explanation

ŒuØAiⱮḟ0jV  Main link
Œu          Uppercase the input
     Ɱ      For each character
    i       take its index in
  ØA        the uppercase alphabet
      ḟ0    Filter; remove 0s
        j   String-join by the right element
         V  Evaluate

-4 bytes thanks to Mr. Xcoder

\$\endgroup\$
  • \$\begingroup\$ 9 bytes: ŒuØAiⱮjŒV or 8 bytes: ŒuØAiⱮjV if - can be taken as _ instead. \$\endgroup\$ – Mr. Xcoder Jun 7 '18 at 16:34
  • \$\begingroup\$ @Mr.Xcoder oh that's actually shorter? cool thanks :D \$\endgroup\$ – HyperNeutrino Jun 7 '18 at 16:43
  • \$\begingroup\$ You don't need ḟ0, as this outputs 0 for empty strings, doesn't it? \$\endgroup\$ – Mr. Xcoder Jun 7 '18 at 16:58
  • \$\begingroup\$ @Mr.Xcoder non-alphabetical characters in the first position cause issues \$\endgroup\$ – HyperNeutrino Jun 7 '18 at 22:12

Not the answer you're looking for? Browse other questions tagged or ask your own question.