The challenge: Develop a program that takes in the notation of a hydrocarbon in standard form, and outputs a correct ball-and-stick graphical representation of that hydrocarbon.
The Deadline: 2010-08-30 20:00
Your implementation will be judged based on the following guidelines:
1. Accuracy
2. Consistency
3. Run time
4. Clarity of output
5. Code readability
6. Overall functionality
>>9
Not so much.
1) First you have to see if the electrons balance out, which also indicates how the covalent or ionic bonds work. Try starting with larger atoms and connect the atoms with the smallest number of available electrons first, and keep going from there.
2) Then you use the number of atoms and their masses and radii to determine bonding angles and relative distances.
3) The modeling is as easy as your language permits from that point onward.
There is probably more detail that can be gone into but that is the kind of thing a person who understands chemistry will implement.
I never passed a molecular chemistry course with anywhere near good marks.
if [ -z $(type -t seq) ]; then
seq()
{
local i
i=1
while [ $i -lt $1 ]; do
echo $i
let i++
done
echo $1
}
fi
flat_model()
{
local i
echo -n " "
for i in `seq $1`; do echo -n " H"; done
echo -en "\n "
for i in `seq $1`; do echo -n " |"; done
echo -en "\nH"
for i in `seq $1`; do echo -n "--C"; done
echo -en "--H\n "
for i in `seq $1`; do echo -n " |"; done
echo -en "\n "
for i in `seq $1`; do echo -n " H"; done
echo
}
display_flat()
{
REGEX="^C([0-9]+)H([0-9]+)$"
if [[ $1 =~ $REGEX ]]; then
CARBON=${BASH_REMATCH[1]}
HYDROGEN=${BASH_REMATCH[2]}
let EXPECTED_HYDROGEN=$CARBON*2+2
if [[ $EXPECTED_HYDROGEN != $HYDROGEN ]]; then
echo -e "Unexpected number of hydrogen atoms: \033[1m$1\033[0m" &2>1
return
fi
flat_model $CARBON
else
echo -e "Input is not a hydrocarbon: \033[1m$1\033[0m" &2>1
return
fi
}
if [ $# -lt 1 ]; then
echo "Usage: $0 C2H6" &2>1
exit 1
fi
while [ 1 ]; do
display_flat $1
shift
[ -z $1 ] && exit
done
There isn't enough information in the ``standard form'' (I'm assuming you mean the straightforward chemical formula; define what you mean if that isn't it) to compose the ball-and-stick representation. Unless you want something that will show you all possible structural isomers, in which case fuck you.
Alkanes are easy. Alkenes might be quite easy I cant remember. Its been 11 years since I took chemistry classes.
Name:
Anonymous2010-08-09 14:03
Alkanes are incredibly easy. Its the other crap that sucks.
Name:
Anonymous2010-08-09 14:06
>>14
You know, some hydrocarbons have thousands of isomers. Why would you even assume that's what OP ment. Did you really think he would be willing to take the time to check THAT many molecular representations?
Name:
Anonymous2010-08-09 14:38
What about Alkynes, Arenes and Buckminsterfullerene?
for gene in `echo $1 | sed 's/[CGAT]/&\n/g'`; do
let j=$i%3
echo " ${DNA[$j]} $gene"
let i++
done
else
echo -e "Usage: \033[1m$0 CCGATTCA\033[0m" &2>1
fi
Name:
Anonymous2010-08-09 22:10
This code solves the problem of finding connected multigraphs without self-edges with C vertices of degree 4 and H vertices of degree 1. No pruning or further checking is done.
Returned are the edges of the C vertices. Adding the H edges and visualising the graph is left as an exercise for the reader.
(define %bind
(%rel (i j n a b)
((i j n a b)
(%=:= n 0) !
(%= b empty))
((i j n a b)
(%>= i C) !
%fail)
((i j n a b)
(%or (%is 4 (hash-ref a i 0)) (%>= j C)) !
(%let (i* j*)
(%and
(%is i* (+ i 1))
(%is j* (+ i* 1))
(%bind i* j* n a b))))
((i j n a b)
(%is #t (< (hash-ref a j 0) 4))
(%let (n* a-i a-ij b*)
(%and
(%is n* (- n 1))
(%is a-i (hash-update a i add1 0))
(%is a-ij (hash-update a-i j add1 0))
(%bind i j n* a-ij b*)
(%= b (cons (list i j) b*)))))
((i j n a b)
(%let (j*)
(%and
(%is j* (+ j 1))
(%bind i j* n a b))))))
(define %connect
(%rel (i j a b)
((i j a b)
(%>= i C) !
(%bind 0 1 (- C-bonds (- C 1)) a b))
((i j a b)
(%>= j i) !
%fail)
((i j a b)
(%let (j*)
(%and
(%is j* (+ j 1))
(%connect i j* a b))))
((i j a b)
(%is #t (< (hash-ref a j 0) 4))
(%let (i* a-i a-ij b*)
(%and
(%is a-i (hash-update a i add1 0))
(%is a-ij (hash-update a j add1 0))
(%is i* (+ i 1))
(%connect i* 0 a-ij b*)
(%= b (cons (list j i) b*)))))))
(displayln (cdar (%which (b) (%connect 1 0 (make-immutable-hash empty) b))))
(let loop ()
(displayln "Press enter for another result")
(let ((ln (read-line)))
(when (not (eof-object? ln))
(displayln (cdar (%more)))
(loop))))))
>>31 Xarn would have used a proper em dash. >>32
You're an idiot if you dismiss this project because you don't know chemistry. Not to mention that alkanes, alkenes, alkynes, and cycloalkanes are high school chemistry.
>>34
You're an idiot if you dismiss people for not knowing chemistry. It's not compulsory in the country I was educated in, so I didn't take it.
If the chemistry is as simple as claimed, I'm sure it's not too much to ask that you reproduce the production rules here for brevity. If it is not, I have little interest in reading a book or researching something which is of little practical use to me to participate in what should be a programming challenge. Perhaps this thread should be relocated to /sci/?
>>35
Americans always get so defensive when their ignorance is laid bare. Your secondary education is an international laughing stock, and your own laziness seems to be typical of your countrymen. Maybe the two problems are related.
>>35,39
Unlike your shitty Europeon country, chemistry is required in American schools. You Europeons are so funny when you accuse each other of being Americans and reveal your complete ignorance of anything that happens outside of your ass-backwards wannabe Soviet Union.
>>39
I'm an American and I had to take chemistry all throughout my education system. It was mixed into a generic "Science" all through 2-8, in high school I was required to pass two classes that involved chemistry, and college Freshman prerequisite classes involved passing two semesters of Chemistry and a separate chemistry lab class before you could begin your formal major's courses. The latter-most was probably the only bit excessive part.
In fact, based on what I've read, there aren't many parts of the United States where a certain amount of chemistry isn't a prerequisite. If you're American, what part of the country do you come from and may we assist its education system?
Otherwise, I don't want to know about your country.
>>41,43,45 I'm an American and I don't want to know about your shitty country, because America is the best, also I'm ignorant of other educational systems, also whether chemistry is compulsory or not is a decent measure of how good/communist/European the country is.
>>45
My country has the highest (equal) education index in the world.1 The fact you blindly make assumptions and quote, "don't want to know about your [my] country." because chemistry isn't compulsory is almost laughable. The ignorance of the uneducated knows no bounds.
_________________ References:
1. http://en.wikipedia.org/wiki/Education_Index Retrieved on 11 August, 2010
>>52
No, seriously, I mean I don't want to know about your country. I thought you Euro-guys already believed that Americans didn't care about geography and other countries.
>>52 The fact you blindly make assumptions
You mean like >>39 did when he claimed that American students aren't required to take chemistry, which is incorrect?
Butthurt Americans always getting defensive over the fact that their secondary education is a laughing stock. None of them seem to be able to contribute to the thread, I notice.