Advertisements

On 19 May 2005 06:56:45 -0700,
"rh0dium" <> wrote:
> Hi All,
> While I know there is a zillion ways to do this.. What is the most
> efficient ( in terms of lines of code ) do simply do this.
> a=1, b=2, c=3 ... z=26

Then, for example
nrFromDg("bc")
gives
55
and
nrFromDg("aaa")
gives
703
and so on for whatever you want to evaluate.

This is efficient in terms of lines of code, but of course the function
is evaluating ord("a") and len(dg) multiple times, so it's not the most
efficient in terms of avoiding redundant calculations. And
nrFromDg("A") gives you -31, so you should really force dg into
lowercase before evaluating it. Oh, and it's pretty hard to read that
lambda expression.

It would probably be better to get in the habit of writing
globals()[x] = y
instead of
locals()[x] = y
You almost never want to do the latter[1]. The only reason it works in
this case is because, at the module level, locals() is globals().

You probably already knew this, but I note it here to help any newbies
avoid future confusion.

Steve

[1] For 99% of use cases. Modifying locals() might be useful if you're
just going to pass it to another function as a dict. But I think I've
seen *maybe* 1 use case for this.

Guest

Hi rh0dium,
Your request gives me the opportunity of showing a more realistic
example of the technique of "self-modification coding".
Although the coding is not as short as that suggested by the guys who
replayed to you, I think that it can be interesting....

# newVars.py
lCod=[]
for n in range(1,27):
.. lCod.append(chr(n+96)+'='+str(n)+'\n')
# other for-loops if you want define additional variables in sequence
(ex. aa,bb,cc etc...)
# write the variable definitions in the file "varDef.py"
fNewV=open('varDef.py','w')
fNewV.writelines(lCod)
fNewV.close()
from varDef import *
# ...
If you open the generated file (varDef.py) you can see all the variable
definitions, which are runned by "from varDef import *"
Bye.

What doesn't work? What did python output when you tried to do it? It
is python 2.4 specific, it requires some changes for 2.3, and more for
earlier versions of python.
> for i, digraph in enumerate(sorted([''.join((x, y)) for x in alpha for
> y in [''] + [z for z in alpha]], key=len)):
> globals()[digraph]=i+1
>
> How do you implement this sucker??

Works just fine for me. Let me know what error you're getting and I'll
help you figure it out.

On 19 May 2005 11:59:00 -0700, rh0dium <> wrote:
> This is great but backwards...
>
> Ok because you all want to know why.. I need to convert Excel columns
> A2 into , [1,0] and I need a simple way to do that..
>
> ( The way this works is A->0 and 2->1 -- Yes they interchange -- So
> B14 == [13,1] )

That's not your original code. You used the contents to modify the locals()
(effectively globals()) dictionary:
>>>> alpha = 'abcdefghijklmnopqrstuvwxyz'
>>>> for i, digraph in enumerate(sorted([''.join((x, y)) for x in alpha \
> for y in [''] + [z for z in alpha]], key=len)):
> ... locals()[digraph] = i + i
> ...
>

Of course you lose the order in that process.
When you do care about order, I suggest that you swap the for clauses
instead of sorting, e. g:
>>> alpha = list("abc")
>>> items = [x + y for x in [""] + alpha for y in alpha]
>>> items == sorted(items, key=len)
True

We weren't really backwards; just gave a full solution to a half-stated
problem.

Bill, you've forgotten the least-lines-of-code requirement

Mine's still a one-liner (chopped up so line breaks don't break it):

z = lambda cp: (int(cp[min([i for \
i in xrange(0, len(cp)) if \
cp.isdigit()]):])-1,
sum(((ord(cp[0:min([i for i in \
xrange(0, len(cp)) if \
cp.isdigit()])][x])-ord('A')+1) \
* (26 ** (len(cp[0:min([i for i in \
xrange(0, len(cp)) if \
cp.isdigit()])])-x-1)) for x in \
xrange(0, len(cp[0:min([i for i in \
xrange(0, len(cp)) if \
cp.isdigit()])]))))-1)

Without the initial test, it has a buglet of return values for "AB"
and similar strings. If searching uppercase twice really bothers you,
you can drop the uppercase.find; then you'll get less informative
error messages if coord2tuple2 is passed single characters that aren't
in uppercase.

Share This Page

Welcome to The Coding Forums!

Welcome to the Coding Forums, the place to chat about anything related to programming and coding languages.

Please join our friendly community by clicking the button below - it only takes a few seconds and is totally free. You'll be able to ask questions about coding or chat with the community and help others.
Sign up now!