fork download
  1. // challenge 302 Easy Spelling With Chemistry
  2. // https://w...content-available-to-author-only...t.com/r/dailyprogrammer/comments/5seexn/20170206_challenge_302_easy_spelling_with/
  3.  
  4. /*
  5. Challenge Input:
  6. functions
  7. bacon
  8. poison
  9. sickness
  10. ticklish
  11.  
  12. Challenge Output:
  13. FUNCTiONS (flourine, uranium, nitrogen, carbon, titanium, oxygen, nitrogen, sulfur)
  14. BaCoN (barium, cobalt, nitrogen)
  15. POISON (phosphorus, oxygen, iodine, sulfur, oxygen, nitrogen)
  16. SiCKNeSS (silicon, carbon, potassium, neon, sulfur, sulfur)
  17. TiCKLiSH (titanium, carbon, potassium, lithium, sulfur, hydrogen)
  18.  */
  19. package challenge._302_easy
  20.  
  21. import java.util.*
  22. import java.lang.*
  23.  
  24. data class Element(val atNum: Int, val symbol: String, val name: String, val atWt: Double)
  25. val elements: MutableList<Element> = ArrayList<Element>()
  26. val symMap: MutableMap<String, Element> = HashMap<String, Element>()
  27. fun findSolutions(word: String) : List<List<Element>>? {
  28. if (word.length == 0) return null
  29. if (word.length == 1) {
  30. val elem = symMap[word]
  31. return if (elem != null) listOf(listOf(elem)) else null
  32. }
  33. if (word.length > 1) {
  34. val rlst: MutableList<List<Element>> = ArrayList<List<Element>>()
  35. val elem1 = symMap[word.substring(0..0)]
  36. if (elem1!=null) {
  37. val sol2 = findSolutions(word.substring(1))
  38. if (sol2!=null) {
  39. for (lst in sol2) {
  40. val lst2: List<Element> = listOf(elem1) + lst
  41. rlst.add(lst2)
  42. }
  43. }
  44. }
  45.  
  46. val elem2 = symMap[word.substring(0..1)]
  47. if (elem2 != null) {
  48. if (word.length == 2) {
  49. val lst2: List<Element> = listOf(elem2)
  50. rlst.add(listOf(elem2))
  51. } else {
  52. val sol2 = findSolutions(word.substring(2))
  53. if (sol2 != null) {
  54. for (lst in sol2) {
  55. val lst2: List<Element> = listOf(elem2) + lst
  56. rlst.add(lst2)
  57. }
  58. }
  59. }
  60. }
  61. return if (!rlst.isEmpty()) rlst else null
  62. }
  63. return null
  64. }
  65. fun formatSolution(solution: List<Element>): String {
  66. val sb = StringBuilder()
  67. val name = StringBuilder()
  68. for (element in solution) {
  69. name.append(element.symbol)
  70. }
  71. sb.append(name)
  72. sb.append(" (")
  73. val names: MutableList<String> = ArrayList<String>()
  74. for (element in solution) {
  75. names.add(element.name)
  76. }
  77. sb.append(names.joinToString(separator=" "))
  78. sb.append(")")
  79. return sb.toString()
  80. }
  81.  
  82. fun findMaxWeightInfo(solutions: List<List<Element>>): Pair<Int, Double> {
  83. var maxWeight: Double = 0.0
  84. var maxWeightIndex: Int = -1
  85. for ((index,solution) in solutions.withIndex()) {
  86. var solutionWeight: Double = 0.0
  87. for (element in solution) {
  88. solutionWeight += element.atWt
  89. }
  90. if (solutionWeight > maxWeight) {
  91. maxWeight = solutionWeight
  92. maxWeightIndex = index
  93. }
  94. }
  95. return Pair(maxWeightIndex, maxWeight)
  96. }
  97. fun main(args: Array<String>) {
  98. for (elem in _elementData) {
  99. val spl = elem.split(",")
  100. val elem = Element(spl[0].toInt(), spl[1], spl[2].toLowerCase(), spl[3].toDouble())
  101. elements.add(elem)
  102. symMap[spl[1].toLowerCase()] = elem
  103. }
  104.  
  105. val words = arrayOf("functions", "bacon", "poison", "sickness", "ticklish")
  106. for (word in words) {
  107. val solutions = findSolutions(word)
  108. if (solutions != null) {
  109. val count:Int = solutions.size
  110. val weightInfo = findMaxWeightInfo(solutions)
  111. for ((index,solution) in solutions.withIndex()) {
  112. var text = formatSolution(solution)
  113. if (solutions.size > 1 && weightInfo.first == index) {
  114. text += " *"
  115. }
  116. println(text)
  117. }
  118. }
  119. }
  120. println("* - indicates max atomic weight in group")
  121. }
  122.  
  123. val _elementData = arrayOf(
  124. //"Atomic Number,SI Symbol,Element,Atomic Weight",
  125. "1,H,Hydrogen,1.00794",
  126. "2,He,Helium,4.002602",
  127. "3,Li,Lithium,6.941",
  128. "4,Be,Beryllium,9.012182",
  129. "5,B,Boron,10.811",
  130. "6,C,Carbon,12.0107",
  131. "7,N,Nitrogen,14.0067",
  132. "8,O,Oxygen,15.9994",
  133. "9,F,Fluorine,18.9984032",
  134. "10,Ne,Neon,20.1797",
  135. "11,Na,Sodium,22.98976928",
  136. "12,Mg,Magnesium,24.3050",
  137. "13,Al,Aluminium,26.9815386",
  138. "14,Si,Silicon,28.0855",
  139. "15,P,Phosphorus,30.973762",
  140. "16,S,Sulfur,32.065",
  141. "17,Cl,Chlorine,35.453",
  142. "18,Ar,Argon,39.948",
  143. "19,K,Potassium,39.0983",
  144. "20,Ca,Calcium,40.078",
  145. "21,Sc,Scandium,44.955912",
  146. "22,Ti,Titanium,47.867",
  147. "23,V,Vanadium,50.9415",
  148. "24,Cr,Chromium,51.9961",
  149. "25,Mn,Manganese,54.938045",
  150. "26,Fe,Iron,55.845",
  151. "27,Co,Cobalt,58.933195",
  152. "28,Ni,Nickel,58.6934",
  153. "29,Cu,Copper,63.546",
  154. "30,Zn,Zinc,65.38",
  155. "31,Ga,Gallium,69.723",
  156. "32,Ge,Germanium,72.64",
  157. "33,As,Arsenic,74.92160",
  158. "34,Se,Selenium,78.96",
  159. "35,Br,Bromine,79.904",
  160. "36,Kr,Krypton,83.798",
  161. "37,Rb,Rubidium,85.4678",
  162. "38,Sr,Strontium,87.62",
  163. "39,Y,Yttrium,88.90585",
  164. "40,Zr,Zirconium,91.224",
  165. "41,Nb,Niobium,92.90638",
  166. "42,Mo,Molybdenum,95.96",
  167. "43,Tc,Technetium,98",
  168. "44,Ru,Ruthenium,101.07",
  169. "45,Rh,Rhodium,102.90550",
  170. "46,Pd,Palladium,106.42",
  171. "47,Ag,Silver,107.8682",
  172. "48,Cd,Cadmium,112.411",
  173. "49,In,Indium,114.818",
  174. "50,Sn,Tin,118.710",
  175. "51,Sb,Antimony,121.760",
  176. "52,Te,Tellurium,127.60",
  177. "53,I,Iodine,126.90447",
  178. "54,Xe,Xenon,131.293",
  179. "55,Cs,Cesium,132.9054519",
  180. "56,Ba,Barium,137.327",
  181. "57,La,Lanthanum,138.90547",
  182. "58,Ce,Cerium,140.116",
  183. "59,Pr,Praseodymium,140.90765",
  184. "60,Nd,Neodymium,144.242",
  185. "61,Pm,Promethium,145",
  186. "62,Sm,Samarium,150.36",
  187. "63,Eu,Europium,151.964",
  188. "64,Gd,Gadolinium,157.25",
  189. "65,Tb,Terbium,158.92535",
  190. "66,Dy,Dysprosium,162.500",
  191. "67,Ho,Holmium,164.93032",
  192. "68,Er,Erbium,167.259",
  193. "69,Tm,Thulium,168.93421",
  194. "70,Yb,Ytterbium,173.054",
  195. "71,Lu,Lutetium,174.9668",
  196. "72,Hf,Hafnium,178.49",
  197. "73,Ta,Tantalum,180.94788",
  198. "74,W,Tungsten,183.84",
  199. "75,Re,Rhenium,186.207",
  200. "76,Os,Osmium,190.23",
  201. "77,Ir,Iridium,192.217",
  202. "78,Pt,Platinum,195.084",
  203. "79,Au,Gold,196.966569",
  204. "80,Hg,Mercury,200.59",
  205. "81,Tl,Thallium,204.3833",
  206. "82,Pb,Lead,207.2",
  207. "83,Bi,Bismuth,208.98040",
  208. "84,Po,Polonium,209",
  209. "85,At,Astatine,210",
  210. "86,Rn,Radon,222",
  211. "87,Fr,Francium,223",
  212. "88,Ra,Radium,226",
  213. "89,Ac,Actinium,227",
  214. "90,Th,Thorium,232.03806",
  215. "91,Pa,Protactinium,231.03588",
  216. "92,U,Uranium,238.02891",
  217. "93,Np,Neptunium,237",
  218. "94,Pu,Plutonium,244",
  219. "95,Am,Americium,243",
  220. "96,Cm,Curium,247",
  221. "97,Bk,Berkelium,247",
  222. "98,Cf,Californium,251",
  223. "99,Es,Einsteinium,252",
  224. "100,Fm,Fermium,257",
  225. "101,Md,Mendelevium,258",
  226. "102,No,Nobelium,259",
  227. "103,Lr,Lawrencium,266",
  228. "104,Rf,Rutherfordium,267",
  229. "105,Db,Dubnium,268",
  230. "106,Sg,Seaborgium,269",
  231. "107,Bh,Bohrium,270",
  232. "108,Hs,Hassium,277",
  233. "109,Mt,Meitnerium,278",
  234. "110,Ds,Darmstadtium,281",
  235. "111,Rg,Roentgenium,282",
  236. "112,Cn,Copernicium,285",
  237. "113,Nh,Nihonium,284",
  238. "114,Fl,Flerovium,286",
  239. "115,Mc,Moscovium,290",
  240. "116,Lv,Livermorium,293",
  241. "117,Ts,Tennessine,294",
  242. "118,Og,Oganesson,294"
  243. )
Success #stdin #stdout 0.11s 4382720KB
stdin
Standard input is empty
stdout
FUNCTiONS (fluorine uranium nitrogen carbon titanium oxygen nitrogen sulfur)
BAcON (boron actinium oxygen nitrogen) *
BaCON (barium carbon oxygen nitrogen)
BaCoN (barium cobalt nitrogen)
POISON (phosphorus oxygen iodine sulfur oxygen nitrogen)
PoISON (polonium iodine sulfur oxygen nitrogen) *
SICKNEsS (sulfur iodine carbon potassium nitrogen einsteinium sulfur) *
SICKNeSS (sulfur iodine carbon potassium neon sulfur sulfur)
SiCKNEsS (silicon carbon potassium nitrogen einsteinium sulfur)
SiCKNeSS (silicon carbon potassium neon sulfur sulfur)
TiCKLiSH (titanium carbon potassium lithium sulfur hydrogen)
* - indicates max atomic weight in group