Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.
Code Golf

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

Required fields*

Mass of elements

There are more than 100 elements in the modern periodic table. You challenge is to output the rounded mass of an element with respect to the input given.

Input

  • Input will contain an abbreviation of an element.
  • Input can be taken from any one of the following:

    • stdin
    • command-line arguments
    • function arguments
  • Input will contain a one or two letter abbreviation of an element. The first character is always an uppercase letter and the second character, if any, will be a lowercase letter.

Output

  • Output the mass of the element entered
  • Output must be in the stdout or closest equivalent.
  • Output may contain a trailing newline character

Elements and their masses

Here is the list of abbreviation of elements and their masses:

Sl.No Elements Abbreviation Mass 
1 Hydrogen H 1 
2 Helium He 4 
3 Lithium Li 7 
4 Beryllium Be 9 
5 Boron B 11 
6 Carbon C 12 
7 Nitrogen N 14 
8 Oxygen O 16 
9 Fluorine F 19 
10 Neon Ne 20 
11 Sodium Na 23 
12 Magnesium Mg 24 
13 Aluminium Al 27 
14 Silicon Si 28 
15 Phosphorus P 31 
16 Sulfur S 32 
17 Chlorine Cl 35 
18 Argon Ar 40 
19 Potassium K 39 
20 Calcium Ca 40 
21 Scandium Sc 45 
22 Titanium Ti 48 
23 Vanadium V 51 
24 Chromium Cr 52 
25 Manganese Mn 55 
26 Iron Fe 56 
27 Cobalt Co 59 
28 Nickel Ni 59 
29 Copper Cu 64 
30 Zinc Zn 65 
31 Gallium Ga 70 
32 Germanium Ge 73 
33 Arsenic As 75 
34 Selenium Se 79 
35 Bromine Br 80 
36 Krypton Kr 84 
37 Rubidium Rb 85 
38 Strontium Sr 88 
39 Yttrium Y 89 
40 Zirconium Zr 91 
41 Niobium Nb 93 
42 Molybdenum Mo 96 
43 Technetium Tc 98 
44 Ruthenium Ru 101 
45 Rhodium Rh 103 
46 Palladium Pd 106 
47 Silver Ag 108 
48 Cadmium Cd 112 
49 Indium In 115 
50 Tin Sn 119 
51 Antimony Sb 122 
52 Tellurium Te 128 
53 Iodine I 127 
54 Xenon Xe 131 
55 Cesium Cs 133 
56 Barium Ba 137 
57 Lanthanum La 139 
58 Cerium Ce 140 
59 Praseodymium Pr 141 
60 Neodymium Nd 144 
61 Promethium Pm 145 
62 Samarium Sm 150 
63 Europium Eu 152 
64 Gadolinium Gd 157 
65 Terbium Tb 159 
66 Dysprosium Dy 163 
67 Holmium Ho 165 
68 Erbium Er 167 
69 Thulium Tm 169 
70 Ytterbium Yb 173 
71 Lutetium Lu 175 
72 Hafnium Hf 178 
73 Tantalum Ta 181 
74 Tungsten W 184 
75 Rhenium Re 186 
76 Osmium Os 190 
77 Iridium Ir 192 
78 Platinum Pt 195 
79 Gold Au 197 
80 Mercury Hg 201 
81 Thallium Tl 204 
82 Lead Pb 207 
83 Bismuth Bi 209 
84 Polonium Po 209 
85 Astatine At 210 
86 Radon Rn 222 
87 Francium Fr 223 
88 Radium Ra 226 
89 Actinium Ac 227 
90 Thorium Th 232 
91 Protactinium Pa 231 
92 Uranium U 238 
93 Neptunium Np 237 
94 Plutonium Pu 244 
95 Americium Am 243 
96 Curium Cm 247 
97 Berkelium Bk 247 
98 Californium Cf 251 
99 Einsteinium Es 252 
100 Fermium Fm 257 

Yes. Your code only needs to deal with elements having atomic number 0-100 as seen in the table above.

Rules

Test Cases

O --> 16
Sn --> 119
H --> 1
Fm --> 257

Scoring

This is , so the shortest submission (in bytes) wins.

Answer*

Draft saved
Draft discarded
Cancel
1
  • \$\begingroup\$ Why the negative look-ahead? Just make at least on digit mandatory (I mean \d*\d+, then you can be sure "H1" will not be picked when "He4" is needed. \$\endgroup\$ Commented Jun 26, 2015 at 10:32

AltStyle によって変換されたページ (->オリジナル) /