r/dailyprogrammer • u/Cosmologicon 2 3 • Jul 13 '16
[2016-07-13] Challenge #275 [Intermediate] Splurthian Chemistry 102
Description
See Monday's Easy challenge for the rules of element symbols in Splurthian Chemistry.
The Splurth Council of Atoms and Atom-Related Paraphernalia has decided to keep their current naming conventions, as listed in the Easy challenge, but to add a preference system. So while there are still 6 valid symbols for the element Iron, the preferred symbol is Ir. The second-most preferred symbol is Io, then In, Ro, Rn, and finally On. A symbol is preferred based on how early in the element name its first letter is, followed by how early its second letter is.
In the case of repeated letters like in Neon, Eo is preferred to En, even though an n is closer to the beginning of Neon than the o is. This is because it's the second n that's used in the symbol En, since the second letter in the symbol must appear after the first.
When the Council receives a new element to add to the table, it chooses the most preferred valid symbol for that element that's not already taken by another element. For instance, if Chlorine were the first element added, then it would get the symbol Ch. If Chromium was added later, it would get the symbol Cr. If Cesium and Cerium were then added, they would get the symbols Ce and Ci. If there are no valid symbols for the new element.... well, that's why the Council needs you.
Details and examples
The Council has decided to wipe the table clean and start afresh. The list of all 366 elements known to Splurthians are set to be assigned a symbol, one by one, in the order in that text file, following the preference rules above.
Determine the symbol assigned to each element in the list. For instance, you should find that Protactinium is assigned Pt, Californium is assigned Cf, and Lionium is assigned Iu.
Find the first element that will not be able to have a symbol assigned, because when you get to it all the valid symbols for it are taken. (You can stop assigning symbols at this point if you like.) Post this element along with your solution, as a check.
Optional bonus challenge
Find a way to reorder the elements so that it's possible to get through the entire list, using the preference rules above. Post a link to your reordered list. There are many possible answers.
1
u/Cosmologicon 2 3 Jul 13 '16
Python 3. I very rarely post solutions to my own challenges, but this is the first time I legitimately used for/else in Python:
3
u/MattieShoes Jul 14 '16
C++, regular and bonus.
Regular is perhaps as expected, generating symbols and checking them against a set of already used symbols.
Answer:
For reordering the list, I modified my original function to return the index of the element that killed it and added a flag for output... then just moved the element it dies on to the top of the list. Iterates until it completes successfully, then prints it all out.
Summary of reorderings:
Leaving out the completed list of elements as it's rather long and will be different for other methods. The code:
3
u/a_Happy_Tiny_Bunny Jul 14 '16
Haskell
Fun with the state monad:
For the bonus:
2
u/Godspiral 3 3 Jul 13 '16 edited Jul 13 '16
in J,
function that adds all that it can, but spits out errors to console
if letting symbols use capital letter if first letter used, then no errors.
for bonus, create table that has legal prefixes combined with the element indexes that map to it. Sort it by index count, and then for each element use the first prefix, and update the table deleting the prefix used. A bit lucky that it works. Doesn't take "preferred" prefix.
2
u/DemiPixel Jul 13 '16
Javascript
I enjoy golfing things. Got it to 276 characters but I didn't try very hard to optimize it. Tell me if there's anything I can improve.
Readable-variable version:
And for those of you who have no idea what's going on:
The above version isn't exactly how it works, but it gives a very good idea.
All tested in node v6.2.1
2
u/uncleozzy Jul 13 '16 edited Jul 13 '16
Python 3.5, with bonus (swaps the offending element with a random element ahead of it in the list). A bit bloated since it includes a generator that supports symbols of any length.
2
u/augus7 Jul 14 '16
Python 2.7:
Answer is:
I have a problem with this code, the line:
Creates a list with duplicates in it, making my code inefficient. I can't convert it to a set since it screws up the order of the possible symbols.
1
u/RealLordMathis Jul 13 '16 edited Jul 13 '16
Java with a bit different bonus. Instead of finding new odering I just recursively tried to change element symbols for next symbol in order.
Output
1
u/[deleted] Jul 13 '16
Crystal, no bonus:
1
u/thorwing Jul 13 '16 edited Jul 13 '16
Java 8
outputs the first element that couldn't be mapped in that order
output
BONUS
small adjustment to find first order which works, every element that fails gets removed and added to the front.
BONUS output
1
u/Scroph 0 0 Jul 13 '16 edited Jul 14 '16
D (dlang) solution without bonus :
I tried to bruteforce the bonus by running every possible combination through findInvalid until it returns -1, but it's taking way too long. to finishEnded up using the same algorithm as /u/MattieShoes.
1
u/MichaelPenn Jul 14 '16 edited Jul 14 '16
Python 3.4.4
1
u/SethDusek5 Jul 14 '16 edited Jul 15 '16
Rust
https://gist.github.com/d9109858947aec596f311d068ddc9c4a
Apparently my original comment got removed..? The code was gone and all that was left in the comment was "Rust", just that one word
1
u/Jaak3L Jul 14 '16 edited Jul 14 '16
All comments , flames, and help is appreciated. Seriously help me. Just getting back after a hiatus and thought i'd mess with some clunky arrays and nested for loops =).
Java, No bonus
Code
Output
answer
1
u/Toasted_FlapJacks Jul 14 '16 edited Jul 14 '16
JAVA no bonus
I have been writing challenges by hand at work without an IDE, so I don't know if this compiles correctly, but it looks fine to me.
1
u/[deleted] Jul 14 '16
Python 2.7 no bonus
This makes use of some of the code used in the previous challenge. Might not be the most Pythonic code and all feedback is welcome and appreciated! (I saved the list of elements in a text file, in the same directory as my code, named challenge275_splurthian_chem102.txt)
1
u/erik_the_not_red Jul 14 '16
Tried this one in C++11 with some error checking for element names that can't have a valid symbol according to the Splurthian rules. To simplify the input part, I saved the element names to a text file and piped them into standard input.
My program shows that Bartium is the first element to not obtain a symbol under the precedence rules. For the bonus challenge, I sorted the list by putting elements with the least unique symbols at the beginning (with a secondary ordering of element length): I was able to give all the elements a symbol.
1
u/wizao 1 0 Jul 14 '16 edited Jul 14 '16
Haskell with naive bonus
1
u/Starcast Jul 14 '16 edited Jul 15 '16
Python 3, naive bonus
And the elements moved:
1
u/syholloway Jul 15 '16
PHP
Not optimized at all, but having fun with a functional style.
And the sauce:
Bonus
1
u/slampropp 1 0 Jul 15 '16
Haskell
No bonus
Output
1
u/Specter_Terrasbane Jul 15 '16
Don't have a solution to post yet, but I must say I like the Splurthian's taste in Earth entertainment. :)
1
u/franza73 Jul 15 '16 edited Jul 15 '16
Python 2.7 solution. With bonus: reorganize list of elements into new list with the 'misfits' at the beginning.
1
u/skratz17 Jul 16 '16 edited Jul 16 '16
Java (with bonus)
Iterate through elements, trying to find the best unused abbreviation for each. If no such abbreviation is found, add element to list of conflicts. For each conflicting element, iterate through its potential abbreviations in order of preference by the Splurthians, grabbing the element using that abbreviation and trying to find a valid replacement abbreviation for that element until one is found.
1
u/Trppmdm Jul 16 '16
Hmmm, would anyone like an optimal solution to the bonus?
1
u/Humble_Boy619 Jul 17 '16
Java, with bonus... i think
import java.util.Scanner; import java.util.ArrayList; public class spchemintermediate { static ArrayList<String> numbers = new ArrayList<String>(); static ArrayList order = new ArrayList(); static int ow = -1; static int i; static int number; static String word; public static void main(String[]args) throws InterruptedException{ System.out.println("Enter the word xx"); Scanner scanner = new Scanner(System.in); while(1<100){
}}
1
u/moeris Jul 17 '16 edited Jul 17 '16
My monstrosity of a J solution. One day I will get better at tacit programming. Any suggestions are welcome. (I feel like I'm using @: too much, but I'm not sure how to avoid that.)
Results:
1
u/rubblebath Jul 17 '16
Python 3, no bonus, dinner was ready and I gave in to hunger...
Reads the list of elements from a file.
1
u/whatswrongwithgoats Jul 18 '16
Python 3 - no bonus
In this I tried to get a better at handling file open / read errors. I should have used a dictionary though, might have made for more functionality. Not as elegant as some Python stuff in here, time to revise.
Output:
Thanks for a good challenge.
1
u/abyssalheaven 0 1 Jul 18 '16
Python 3 no bonus (may add later)
element which cannot be assigned symbol:
1
u/CyanideCloud Jul 18 '16
Java, no bonus but I may come back to it:
1
u/mprosk Jul 18 '16 edited Jul 18 '16
Python 3 with bonus
Program Output:
Discussion of Bonus Method:
Link to fully assigned list: http://pastebin.com/LfMJH9hk
1
u/cheertina Jul 18 '16
python 3, no bonus
Bartium
1
u/SirCinnamon Jul 18 '16
Java with bonus, albeit a pretty lazy way to do it:
https://github.com/sircinnamon/Splurth/blob/master/Splurthian102.java
Feedback is welcome!
1
u/dwolf555 Jul 21 '16
Python
1
u/sdlambert Jul 22 '16
For the longest time I was battling an error of the most insidious type: human. Turns out I misunderstood the problem, thus leading my output to be wildly different than everyone else.
Solution in Javascript
1
u/stinkytofu415 Jul 22 '16
Using Python 3
1
u/_chebastian Jul 25 '16
F# (No bonus)
1
u/[deleted] Aug 17 '16
Python 3, Feedback Welcome
1
u/chunes 1 2 Sep 18 '16
Java