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.