I hesitate a bit to post this, but... on the Italian Python NG, somebody
was asking whether there was any way to convert an integer number x into
a string which represents it in an arbitrary base N (given a sequence
with a len of N that gives the digits to use) as "a single expression".
I haven't found a really general way, much less a clear one, but, the
best I have so far is...:
def number_in_base( x, N, digits, maxlen=99):
return '-'[x>=0:] + (
(x and ''.join([digits[k%N] for i in range(maxlen)
for k in [abs(x)//N**i] if k>0])[::-1]
) or digits[0])
Besides the lack of clarity, the obvious defect of this approach is that
darned 'maxlen' parameter -- but then, since I can have only a 'for',
not a 'while', in a list comprehension or generator expression, and I
don't think recursion qualifies as 'a single expression'...:-(
Anyway, improvements and suggestions welcome, thanks!
Alex
Jul 18 '05
21 2156
Andrea Griffini wrote: roman = lambda x: "".join(["".join(map(lam bda c: "IVXLCDM"[int(c) +2*N],"/0/00/000/01/1/10/100/1000/02".split("/")[x//10**N % 10])) for N in (3, 2, 1, 0)])
This rocks!! I'm still trying to understand how it works though :)
--Irmen
On Mon, 01 Nov 2004 11:06:24 +0100, Irmen de Jong
<irmen@-NOSPAM-REMOVETHIS-xs4all.nl> wrote: Andrea Griffini wrote:
roman = lambda x: "".join(["".join(map(lam bda c: "IVXLCDM"[int(c) +2*N],"/0/00/000/01/1/10/100/1000/02".split("/")[x//10**N % 10])) for N in (3, 2, 1, 0)])
This rocks!! I'm still trying to understand how it works though :)
The idea is to use the pattern
"", "0", "00", "000", "01", "1", "10", "100", "1000", "02"
where every character listed above is an offest in
"IVX", "XLC", "CDM", "M??"
For example applying the offsets to the first group you get:
"", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"
i.e. the roman numbers for 0..9; and applying it to the second
"", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC",
i.e. 0, 10, 20, ... 90.
I just used "split" and string indexing instead of regular
lists because the code becomes smaller (and more readable
than a mess of quotes and commas).
The rest is a loop on the thousands, the hundreds, the tens
and the units of the number and the joins needed to glue
all the pieces into a resulting string.
Andrea This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Boštjan Jerko |
last post by:
Hello !
I need to know if the result of math formula is nan (Not a number).
How can I do that?
Thanks,
B.
|
by: Gianluca_Venezia |
last post by:
Talking about high number of record seem ridiculus, if this number is
about 88.000
but
when I open a form, linked via ODBC to a MYSQL table, the open and the
use of that form is slow, and very slow if the form has combo
controls.
I have a order form, and a combo control for custemer codes.
Order table counts 88.000 records, customer tables has 1.500 records.
|
by: gahagan |
last post by:
If you encounter this error, most likely one of the controls you're
trying to cut/paste is a combo box with a longer-than-average 'Row
Source' value. That, apparently, is the problem. Shorter Row Source
values don't seem to be a problem. (I don't have time to figure out at
what length the error triggers.)
Workaround: Remove the Row Source (you can cut/paste the text somewhere
else), then try cutting/pasting the control(s) again. Then...
|
by: Fao |
last post by:
Hello, I am having some problems with inheritance. The compiler does
not not return any error messages, but when I execute the program, it
only allows me to enter the number, but nothing else happend. I think
the problem may be in my input function or in the main function.
If anyone out there can help me it woul be greatly appreciated.
Here is the code:
#include <iostream>
|
by: Nevyn |
last post by:
How do I do that? Turning the layout from the customerID-number "10205" into looking like "10 205"? I've just incresed it from four digits to five, and it looks like this: lsvItem.SubItems(1) = Right("0000" & CStr(rs!customerID), 5), but how do I affect the layout?
| |
by: zensunni |
last post by:
Here's what my code looks like:
=======================================
Set RS = Server.CreateObject("ADODB.Recordset")
RS.Open "Table", objConn, 1, 3, 2
If Len(Request.Form("AreaID1")) > 0 Then
RS("AreaID1") = Request.Form("AreaID1")
End If
If Len(Request.Form("AreaID2")) > 0 Then
|
by: mktselvan |
last post by:
Hi,
Existing running oracle application 11i (11.5.8)
Database version is 8.1.7.4
There is any command / way to know the number of concurrent users for this application.
select SESSIONS_MAX,
SESSIONS_WARNING,
|
by: ravi |
last post by:
Hi all,
I written and compiled a c++ program using g++ no errors or
warning are reported.
But when I run it , reporting an error : ERROR: Wrong magic number.
What is the reason for this error?
anybody had an idea ?
|
by: ajmastrean |
last post by:
I cannot get any (hex) number in the "0x80"-"0x89" range (inclusive)
to write properly to a file. Any number in this range magically
transforms itself into "0x3F". For instance, debugging shows that
"0x83" = UInt16 "131" and that converts to Char (curly) "f". Any
information would be helpful.
String hexNum = { "79", "80", "89", "90" };
System.IO.StreamWriter streamWriter = new System.IO.StreamWriter(@"C:
\test.dbf", true,...
|
by: nickyazura |
last post by:
hello,
i would like to know how to do numbering format for "0000" where each time it will generate 0001,0002,0003 and so on untill 9999.
counter = "0000"
counter = counter + 1
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |