Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.forth > #44194
| From | "WJ" <w_a_x_man@yahoo.com> |
|---|---|
| Newsgroups | comp.lang.forth |
| Subject | Re: Parsing exercise in Forth |
| Date | 2015-12-11 17:12 +0000 |
| Organization | A noiseless patient Spider |
| Message-ID | <n4f04t$b66$1@dont-email.me> (permalink) |
| References | <14a2b0f2-1137-44c5-85ca-e9d52bd7ccac@googlegroups.com> |
krishna.myneni wrote:
> PROBLEM: Parse a string specifying the electron configuration
> of an atom into a array of the number of electrons within each
> subshell. Validate the string fields.
>
> Example 1: The electron configuration for the lowest energy
> state of the neutral boron atom may be specified by the
> following string:
>
> s" 1s(2) 2s(2) 2p(1)"
>
> meaning there are two electrons in the "1s" (sub)shell, two
> electrons in the "2s" subshell, and one electron in the "2p"
> subshell. Therefore there are three array entries:
>
> Array Index Entry
> ------------------
> 0 2
> 1 2
> 2 1
>
> Storage of the electrons numbers into the array depend on the
> mapping between subshell and array index, as follows:
>
> Index Subshell
> 0 "1s"
> 1 "2s"
> 2 "2p"
> 3 "3s"
> 4 "3p"
> 5 "3d"
> 6 "4s"
> 7 "4p"
> 8 "4d"
> 9 "4f"
>
> It's not necessary to go beyond these ten subshells, at this
> point. Any subshell specifiers not in the above list should
> return an error, indicating an invalid entry.
>
> Configuration string properties:
>
> 1) The order of entry of the subshells in the string shouldn't
> matter. Any subshells that are not specified in the string
> imply that the number of electrons in that subshell is zero,
> and the array entries should be zeroed (usually by
> initialization, prior to parsing the configuration string).
>
> 2) Subshells with a different identifier, "spdf", in the
> second character have a maximum number of electrons, given by
>
> s 2
> p 6
> d 10
> f 14
>
> Therefore, a subshell entry such as "1s(3)" or "2p(7)" are
> invalid entries, and should return an error. A negative number
> of electrons is also not permitted, but zero is a valid count,
> e.g. "2p(0)", although unnecessary per 1).
>
> 3) Whitespace between the subshell fields is not required,
> so a string such as s" 1s(1)2p(1)" is valid.
>
> Example 2: s" 1s(2)2s(1)3p(1)" valid entry which results in
> the following array elements:
>
> Index Element
> ---------------
> 0 2
> 1 1
> 2 0
> 3 0
> 4 1
> (subsequent elements are all zero)
>
> Example 3: s" 1s(2)2s(2)2d(1)" ** invalid entry, no subshell "2d"
>
> Example 4: s" 1s(2) 2s(2) 3s(4)" ** invalid entry, s subshell
> can't have 4 electrons.
Ruby:
@subshells = %w(1s 2s 2p 3s 3p 3d 4s 4p 4d 4f)
@limits = Hash['s', 2, 'p', 6, 'd', 10, 'f', 14]
def parse_config string
items = string.gsub(/\s+/,"").scan( %r{([^()]+)\((.*?)\)}).
map{|k,v| [k, v.to_i]}
result = [0] * @subshells.size
items.each{|k,v|
fail "Invalid subshell: #{ k }" unless @subshells.include? k
fail "Too many electrons in #{ k }" if v > @limits[k[1]]
result[ @subshells.index(k) ] = v }
result
end
parse_config "1s(2)2s(1)3p(1)"
==>[2, 1, 0, 0, 1, 0, 0, 0, 0, 0]
parse_config " 1s (2) 2 s ( 1 )3 p(1) "
==>[2, 1, 0, 0, 1, 0, 0, 0, 0, 0]
parse_config "1s(2)2s(2)2d(1)"
RuntimeError: Invalid subshell: 2d
parse_config " 1s(2) 2s(2) 3s(4)"
RuntimeError: Too many electrons in 3s
Back to comp.lang.forth | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Parsing exercise in Forth krishna.myneni@ccreweb.org - 2015-12-10 21:08 -0800
Re: Parsing exercise in Forth Raimond Dragomir <raimond.dragomir@gmail.com> - 2015-12-11 00:09 -0800
Re: Parsing exercise in Forth krishna.myneni@ccreweb.org - 2015-12-11 08:59 -0800
Re: Parsing exercise in Forth krishna.myneni@ccreweb.org - 2015-12-11 09:02 -0800
Re: Parsing exercise in Forth Gerard Sontag <gerardsontag@gmail.com> - 2015-12-11 02:28 -0800
Re: Parsing exercise in Forth krishna.myneni@ccreweb.org - 2015-12-11 09:09 -0800
Re: Parsing exercise in Forth Pablo Hugo Reda <pabloreda@gmail.com> - 2015-12-11 06:02 -0800
Re: Parsing exercise in Forth krishna.myneni@ccreweb.org - 2015-12-11 09:12 -0800
Re: Parsing exercise in Forth "WJ" <w_a_x_man@yahoo.com> - 2015-12-11 17:12 +0000
Re: Parsing exercise in Forth krishna.myneni@ccreweb.org - 2015-12-11 09:24 -0800
Re: Parsing exercise in Forth "WJ" <w_a_x_man@yahoo.com> - 2015-12-12 17:41 +0000
Re: Parsing exercise in Forth John Misciagno <jfmisciagno@gmail.com> - 2015-12-11 15:56 -0800
Re: Parsing exercise in Forth krishna.myneni@ccreweb.org - 2015-12-11 20:21 -0800
Re: Parsing exercise in Forth John Misciagno <jfmisciagno@gmail.com> - 2015-12-11 22:21 -0800
Re: Parsing exercise in Forth Julian Fondren <julian.fondren@gmail.com> - 2015-12-11 19:21 -0800
Re: Parsing exercise in Forth krishna.myneni@ccreweb.org - 2015-12-11 20:35 -0800
Re: Parsing exercise in Forth "WJ" <w_a_x_man@yahoo.com> - 2015-12-12 20:08 +0000
Re: Parsing exercise in Forth Julian Fondren <julian.fondren@gmail.com> - 2015-12-12 17:32 -0800
Re: Parsing exercise in Forth Albert van der Horst <albert@spenarnc.xs4all.nl> - 2015-12-13 11:21 +0000
Re: Parsing exercise in Forth Julian Fondren <julian.fondren@gmail.com> - 2015-12-13 05:14 -0800
Re: Parsing exercise in Forth Albert van der Horst <albert@spenarnc.xs4all.nl> - 2015-12-13 17:33 +0000
Re: Parsing exercise in Forth humptydumpty <ouatubi@gmail.com> - 2015-12-12 00:14 -0800
Re: Parsing exercise in Forth humptydumpty <ouatubi@gmail.com> - 2015-12-12 03:48 -0800
Re: Parsing exercise in Forth krishna.myneni@ccreweb.org - 2015-12-12 14:52 -0800
Re: Parsing exercise in Forth humptydumpty <ouatubi@gmail.com> - 2015-12-13 01:59 -0800
Re: Parsing exercise in Forth franck.bensusan@gmail.com - 2015-12-12 02:53 -0800
Re: Parsing exercise in Forth krishna.myneni@ccreweb.org - 2015-12-12 14:58 -0800
Re: Parsing exercise in Forth Doug Hoffman <glidedog@gmail.com> - 2015-12-12 10:33 -0500
Re: Parsing exercise in Forth krishna.myneni@ccreweb.org - 2015-12-12 14:47 -0800
Re: Parsing exercise in Forth "WJ" <w_a_x_man@yahoo.com> - 2015-12-12 23:13 +0000
Re: Parsing exercise in Forth humptydumpty <ouatubi@gmail.com> - 2015-12-13 10:28 -0800
Re: Parsing exercise in Forth Gerry Jackson <gerry@jackson9000.fsnet.co.uk> - 2015-12-17 17:33 +0000
Re: Parsing exercise in Forth krishna.myneni@ccreweb.org - 2015-12-23 05:07 -0800
Re: Parsing exercise in Forth jg <josefg@gmail.com> - 2015-12-29 15:22 -0800
csiph-web