12
\$\begingroup\$

Identify each letter of the English alphabet with the number denoting its position in the alphabet, that is,

a = 1, b = 2, c = 3, ..., z = 26

(no distinction between lower and upper case letters is made). Every other character is identified with 0.

The "sum" of a word is the sum of its characters, for example: the sum of e-mail is 40 = 5 + 0 + 13 + 1 + 9 + 12, since e = 5, - = 0, m = 13, a = 1, i = 9, l = 12.

The Challenge

Write a program or a function that takes as input an integer n in the range 10-100 and returns a word of the English language whose sum is equal to n

This is codegolf, standard loopholes are forbidden.

ADDENDUM:

\$\endgroup\$
26
  • 8
    \$\begingroup\$ Welcome to code golf! This appears to almost be a good challenge, you just have one significant problem: you haven't defined the English language. I would personally recommend saying the submissions are allowed to take a lexicon of English words as a free argument. For the future, please consider using our sandbox to work through such problems before posting. \$\endgroup\$ Mar 18, 2020 at 15:58
  • 6
    \$\begingroup\$ Essentially, you need to somehow define what is a valid English word. @FryAmTheEggman suggested adding a list of possible English words as an input. \$\endgroup\$ Mar 18, 2020 at 16:05
  • 7
    \$\begingroup\$ @Nik Challenges on this site are required to be objective, with posters being able to validate and score their solutions as they post. We've found that asking voters to vote based on a certain judgment is ineffective and does not make for good quality control. \$\endgroup\$
    – xnor
    Mar 18, 2020 at 16:13
  • 9
    \$\begingroup\$ As an alternative to making the dictionary be an input, I think a good way to go is to specify your own dictionary by linking to a downloadable file (that code is not allowed to access) in the question. Then it becomes a compression-style challenge about finding a good set of words to use for each length. \$\endgroup\$
    – xnor
    Mar 18, 2020 at 16:16
  • 4
    \$\begingroup\$ Due to some confusion, I think you should clearly edit in that only those words in the file count as English words. Thank you for making the suggested changes, so far. \$\endgroup\$ Mar 18, 2020 at 16:58

5 Answers 5

5
\$\begingroup\$

Python 3.8 (pre-release), 262 235 bytes

lambda x:f'JKLmGoPlheAhVegytMtosOdtthfvtWypvttgYsRWspytps    e  eegli ra eehnkPooeoroyyauiuruotoituooup    b   aggd  p ai    mnrwiwpnwsnyotrosturwutr{"":8}d{"":14}gnlz   nht ntkpstmsltty'[(k:=x-10)//46*23+k%23::46].strip()+k//23%2*'er'

Try it online!

Uses words from the list that appear with and without a trailing er, cutting the number of required words almost in half.

\$\endgroup\$
4
\$\begingroup\$

05AB1E, 187 51 48 bytes

žĆ•o‹§d∍(Ì•3в¾7:65Å1«.¥žy+èãJε„'ÿ.V}ʒAsSk>OQ}Réθ

Try it online. (Pretty slow unfortunately, so no test suite, but here is a list of all the words used.)

Explanation:

  •o‹§d∍(Ì•           # Push compressed integer 13897672729830113
    3в                # Convert it to base-3 as list: [2,1,1,1,1,1,1,1,1,1,1,2,0,1,1,2,1,1,1,2,1,2,1,1,1,1,1,1,1,1,1,1,1,2]
      ¾7:             # Replace the 0 with a 7
         65Å1         # Push a list of 65 1s
             «        # And append it to the list
              .¥      # Undelta the list (with leading 0): [0,2,3,4,5,6,7,8,9,10,11,12,14,21,22,...]
                žy+   # Add 128 to each: [128,130,131,...]
žĆ                 è  # Index each into the 05AB1E codepage builtin
ã                     # Get the cartesian product with itself, to create all possible pairs
 J                    # Join each pair together
  ε     }             # Map over each pair:
   „'ÿ               '#  Push string "'ÿ", where the `ÿ` is automatically filled with the current pair we're mapping over
      .V              #  And eval it as 05AB1E code, pushing the dictionary word
ʒ       }             # Filter the list of words by:
 A                    #  Push the alphabet
  s                   #  Swap to take the current word we're filtering over
   S                  #  Convert it to a list of characters
    k                 #  Get the index of each letter inside the alphabet
     >                #  Increase it by 1 to make the 0-based indices 1-based
      O               #  Sum them all together
       Q              #  And check if it's equal to the (implicit) input-integer
R                     # Then reverse the list of remaining words (work-around for input n=11,
                      # which otherwise would result in "fda", which is not in OP's list)
 é                    # Then (stable) sort all words based on length, from shortest to longest
  θ                   # And only leave the last one
                      # (after which it is output implicitly as result)

Unfortunately, not all words that can be found in the 05AB1E word list can be found in the provided list as well.

See this 05AB1E tip of mine (sections How to use the dictionary?, How to compress large integers?, and How to compress integer lists?), to understand how the dictionary words and compressed integer list works.


Original 187 bytes answer:

“„é…ªƒ…°»ØÎ‰éãŠêˆÜаí©ÓŽÄÍÝ‚†–ìŠÑÞã™Ä‡ÉˆÁ³²áäÁÑÆ©…¥Ê†…íÆ±Ñœ›Ç²èœÊ‚ì€€Šµ€„±½†íª¯ÅÝ‚îäÔÈÃ޺줃‚ž¨€º¢êˆ¨ïëïÆÐ¼ˆÌ²Ï……ƒÔ…ÙÒ„±ÞŒÂä†ÔÚ´´½é‚¤»œË´¥º™Žâ¤Ñ¸‚É„¾ÈÕˆî†Û‚äŠÁ‹‚Œæ‹Ì†ÏƒÓ‡È…ωɃà‚ë„¶™£“#sè

Try it online or verify all test cases.

I've chosen random words which are both in the English dictionary provided, as well as in 05AB1E word list.

Explanation:

“...“     # Dictionary string containing all words, space-separated
     #    # Split it on spaces
      s   # Swap to get the (implicit) input
       è  # And used it to index into the list (0-based and with wrap-around,
          # so 91-100 wrap around to the first few words)
          # (after which the result is output implicitly)

Here is just the dictionary string part of the program.

See this 05AB1E tip of mine (section How to use the dictionary?), to understand how the dictionary string works.

\$\endgroup\$
4
\$\begingroup\$

JavaScript (ES6),  217  205 bytes

An idea similar to the one used by ovs.

Most words are used twice: once with the -ty suffix (which is worth \$45\$ points) and once without it.

n=>"JK"[n-10]||["Hwy",,"Yor",,"Yot"][n-56]||"KaMMaOPQCanSTAtMiAmiCatDuBawBetGilBolPieBitBooCooEmpToFooBunCotDotKitBoxBusButCutJotJaunGusGutYesUmpJutSixMusUnsNut".match(/.[a-z]*/g)[(n-12)%45]+(n>55?'ty':'')

Try it online!


JavaScript (Node.js),  325 ... 240  235 bytes

Words of \$3\$ to \$5\$ letters ending in either "d" or "y", with the penultimate letter deduced from the other ones.

n=>(w="EAGCABCABACBCCHFABAAAAdBADFaFGAAAKBCASCDBFEYLGNHHArOPSPPTOYRSDoCoDoCuFoEyYaLoYoNoSpSiPoOoRoToAttCurIzAtyButCurDizSuDruExpYesMurFurYumMusMizBuz".match(/.[a-z]*/g)[n-10])+(B=Buffer)([B(w+(c=n>27?'y':'d')).map(c=>n+=96-c)&&n+64])+c

Try it online!

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

Perl 5, 361 bytes

say''.(memGunzip(decode_base64('H4sIAINkcl4CAx2PC3LDIBBDL/Qu5Q+JneJAgbVZTl/RGWA0WkloQ2BdDs6Niy+JPRIi50llqZyRRTcUQsWJX1Znc943wXnpDFLBLlIjdqLza9RGckaiF4pTneYyVvpD76w2CGKOp/FNDx+7eYs77sk5P24KdbLmaVSytNmcYp3Uhb1Tpa/S7pPY0nAF32x2T9BVTv6P6ek+pilyuWrlohYvG4M8jcdE1YsG1oZa5kftpuaaE7dmCpBvs6Lk/Rz66LFh7CrjLG7qGsOkc2pa9GVFOFq3mVHV/Po3aefhfyb5uBZsAQAA'))=~/\w+/g)[-10+pop]

Try it online!

Ungolfed program.pl:

#!/usr/bin/perl
use v5.10;
use MIME::Base64 'decode_base64';
use Compress::Zlib 'memGunzip';

# base64-encoded gzip'ed string of 91 words:
my $data=<<'';
H4sIAINkcl4CAx2PC3LDIBBDL/Qu5Q+JneJAgbVZTl/RGWA0WkloQ2BdDs6Niy+JPRIi50
llqZyRRTcUQsWJX1Znc943wXnpDFLBLlIjdqLza9RGckaiF4pTneYyVvpD76w2CGKOp/FN
Dx+7eYs77sk5P24KdbLmaVSytNmcYp3Uhb1Tpa/S7pPY0nAF32x2T9BVTv6P6ek+pilyuW
rlohYvG4M8jcdE1YsG1oZa5kftpuaaE7dmCpBvs6Lk/Rz66LFh7CrjLG7qGsOkc2pa9GVF
OFq3mVHV/Po3aefhfyb5uBZsAQAA

say ''.(memGunzip(decode_base64($data))=~/\w+/g)[-10+pop]

Run:

for n in {10..100};do echo -n "n=$n "; perl program.pl $n; done
.
.
.
n=95 potsy
n=96 furzy
n=97 luxus
n=98 musty
n=99 mizzy
n=100 buzzy

Improvement: I could have search for words in words.txt that gave shorter gzip'ed $data. Probably shortest AND most similar word from one n to the next.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ You can shave off some bytes: Try it online! \$\endgroup\$
    – Xcali
    Mar 18, 2020 at 21:52
0
\$\begingroup\$

C (gcc), 574 bytes

char*s[]={"ee","afd","ic","m","n","o","dl","el","ii","s","as","il","3rd","er","es","y","ln","by","cy","gv","ey","fy","fz","or","um","ot","lx","yl","ym","yn","yo","zo","xr","yr","ys","ty","bys","xw","xx","buz","ety","hwt","now","yew","guz","hvy","hwy","kyu","yor","yum","yot","you","puy","yow","yox","yoy","suz","swy","doxy","cozy","xxv","cuvy","xxx","eyry","yawy","xyz","yoky","youp","prys","fuzz","yoyo","huzz","yows","yowt","typw","burys","muzz","yutu","xyst","curvy","dizzy","wuzu","druxy","ayuyu","yesty","potsy","furzy","yummy","yours","yourt","buzzy"};f(n){n=s[n-10];}

Wordlist generated using my program run as program < words.txt.

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ I think you can ditch the char at the beginning. \$\endgroup\$
    – ceilingcat
    Jul 4, 2020 at 4:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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