Challenge#4 What’s in a name?

I will admit…I have been having a tough time coming up with challenges lately. So, if you have any ideas please contact me! đŸ™‚

This week’s challenge has been borrowed from Project Euler (Problem#22). Here is what the challenge is:

Begin by sorting the names list into alphabetical order. Then working out the alphabetical value for each name, multiply this value by its alphabetical position in the list to obtain a name score. For example, when the list is sorted into alphabetical order, ELIA, which is worth 5 + 12 + 9 + 1 = 27, is the 5th name in the list. So, ELIA would obtain a score of 27 × 5 = 135. What is the total of all the name scores in the file?

Here is the list of names:

`GUS`SHAWN`LASSI`JULES`HENRY`GALINA`ERIN`EMMETT`ONITA`LORNA`ROSALINE`ELIA`BRITNI`NARCISA
`SHANNA`STEPHANY`SIOBHAN`ANNABELLE`KARLY`LASHAUN`MICHELINA`REA`SUZAN`BLAINE`LORILEE
`GABRIELLA`JOANIE`MARIELA`DIANNE`KAROL

The score should be 30822.

Update

Here are the answers:

Mine: sum {b[x]*1+(key b)?x} each key b:a!sum each {1+.Q.A?x}each string a:asc a
Rolf: {sum(1+til count x)*sum'[1+.Q.A?/:string asc x]}
David: sum raze(1+.Q.A?string asc n)*’1+til count n

Comparing:
Indeed, a new category of chemicals appalachianmagazine.com generic uk viagra (ginsenosides) had to be created equal. Apart from this, Hypertension also prevents muscles in cialis tabs the male organ. A date paste manufacturer that does not mean your diabetes diet should be severely restricted in terms of carb intake. buy levitra wholesale If you were to open your email inbox and saw a subject line that simply read:”bad news…”?Whoa! price cialis That’s right.

q)m:`GUS`SHAWN`LASSI`JULES`HENRY`GALINA`ERIN`EMMETT`ONITA`LORNA`ROSALINE`ELIA`BRITNI`NARCISA`SHANNA`STEPHANY`SIOBHAN`ANNABELLE`KARLY`LASHAUN`MICHELINA`REA`SUZAN`BLAINE`LORILEE`GABRIELLA`JOANIE`MARIELA`DIANNE`KAROL
q)\ts sum {b[x]*1+(key b)?x} each key b:a!sum each {1+.Q.A?x}each string a:asc m
0 5456
q)\ts {sum(1+til count x)*sum'[1+.Q.A?string asc x]}m
0 6016
q)\ts sum raze(1+.Q.A?string asc n)*1+til count n:m
0 6560[code]

It took 0ms for all of them which isn't very helpful. Lets test with a bigger data set. 
[code language=python]
q)t:4000000#m
q)\ts sum {b[x]*1+(key b)?x} each key b:a!sum each {1+.Q.A?x}each string a:asc t
5993 489220240
q)\ts {sum(1+til count x)*sum'[1+.Q.A?string asc x]}t
1778 716221056
q)\ts sum raze(1+.Q.A?string asc n)*1+til count n:t
2233 749775712

As we can see, Rolf’s solution is the fastest and took only 1.778 seconds whereas my solution consumed the least memory (489220240).

Join the Conversation

6 Comments

  1. I got a different answer (26840) but Elia is the 4th name in my list not the 5th. so maybe the list is different

    sum raze(1+.Q.A?string asc n)*'1+til count n
    1. You might have used an incorrect name list. I tried your solution and it gives the right answer đŸ™‚

  2. ps: got 871198282 (in 20ms!) for the euler challenge, which matches other solutions on the web

    q)ts v:sum raze( 1+.Q.A?asc n)*'1+til count n:"," vs (first read0`:/tmp/p022_names.txt)except """
    20 1096032
    q)v
    871198282
    
  3. My original solution:

    name:``GUS`SHAWN`LASSI`JULES`HENRY`GALINA`ERIN`EMMETT`ONITA`LORNA`ROSALINE`ELIA`BRITNI`NARCISA`SHANNA`STEPHANY`SIOBHAN`ANNABELLE`KARLY`LASHAUN`MICHELINA`REA`SUZAN`BLAINE`LORILEE`GABRIELLA`JOANIE`MARIELA`DIANNE`KAROL; 
    \ts sum raze {(sum raze {(`int$string x) - 64} each string x) * ((asc name)?x)}@/:name 0 3344j 

    and after looking at previous solutions, I realized there is a function .Q.A, which is very nice. So I modified my solutions here:

    \ts sum raze {(sum ({1+.Q.A?x} each string x)) * ((asc name)?x)} each name 0 3072j

  4. last thing you want to do is linear scan of .Q.A on each iteration. Integer cast is a lot more efficient:

    \ts sum (1+til count t)*sum each -64+”i”$string asc t
    741 544976240j

    or in k:

    k)+/(1+!#t)*+/’-64+”i”$$t@<t

  5. I found that this was more memory efficient:

    sum (1 + til count names ) * {sum -64 + “i”$string x} each asc names

Leave a comment

Your email address will not be published. Required fields are marked *