Is there a way to subtract 3 from every digit of a number?
Avi Gross
avigross at verizon.net
Sun Feb 21 10:34:32 EST 2021
Ah, that is an interesting, Mike, but not an informative answer. My
question is where the specific problem came from. Yes, someone used to R
and coming to Python might work at adjusting to what is different and how to
get things done. I do that all the time as one hobby is learning lots of
languages and assessing them against each other.
So care to share your solution in R which I would assume you could do
easily?
My quick and dirty attempt, of no interest to the python community, using
the R form of integer that has a maximum, and the pipe operator that will
not be in standard R for another iteration, is this:
## R code using pipes to convert an integer
## to another integer by subtracting 3
## from each digit and wrapping around from
## 2 to 9 and so on, meaning modulo 10
## Load libraries to be used
library(dplyr)
## define function to return subtraction by N mod 10
rotdown <- function(dig, by=3) (dig -by) %% 10
start <- 123456789L
## Using pipes that send output between operators
start %>%
as.character %>%
strsplit(split="") %>%
unlist %>%
as.integer %>%
rotdown %>%
as.character %>%
paste(collapse="") %>%
as.integer
When run:
> start %>%
+ as.character %>%
+ strsplit(split="") %>%
+ unlist %>%
+ as.integer %>%
+ rotdown %>%
+ as.character %>%
+ paste(collapse="") %>%
+ as.integer
[1] 890123456
The above is not meant to be efficient and I could do better if I take more
than a few minutes but is straightforward and uses the vectorized approach
so no obvious loops are needed.
-----Original Message-----
From: Python-list <python-list-bounces+avigross=verizon.net at python.org> On
Behalf Of C W
Sent: Sunday, February 21, 2021 9:48 AM
To: Chris Angelico <rosuav at gmail.com>
Cc: Python <python-list at python.org>
Subject: Re: Is there a way to subtract 3 from every digit of a number?
Hey Avi,
I am a long time R user now using Python. So, this is my attempt to master
the language.
The problem for me is that I often have an idea about how things are done in
R, but not sure to what functions are available in Python.
I hope that clears up some confusion.
Cheer!
On Sun, Feb 21, 2021 at 9:44 AM Chris Angelico <rosuav at gmail.com> wrote:
> On Mon, Feb 22, 2021 at 1:39 AM Avi Gross via Python-list
> <python-list at python.org> wrote:
> > But you just moved the goalpost by talking about using a data.frame
> > as
> that
> > (and I assume numpy and pandas) are not very basic Python.
>
> Given that the original post mentioned a pd.Series, I don't know how
> far the goalposts actually moved :)
>
> ChrisA
> --
> https://mail.python.org/mailman/listinfo/python-list
>
--
https://mail.python.org/mailman/listinfo/python-list
More information about the Python-list
mailing list