"R or Python, which one to do?" There are many articles like this in the world. You'll see a controversy that doesn't end on the day you search for "R Python." In general, "R is often used in data analysis specialization and academia." A phrase such as "Python is versatile and often used on the business side" is said.
However, when I read these articles When you actually code, you don't know what's different. After all it seems that you need to try both to know the difference ... With that in mind, I decided to write a simple program in both languages.
By the way, I have been in R for 1 year, and I usually play with data.frame in Tidyverse, so I rarely write control syntax like this one. Python has just finished building the environment, so this article is also for practice. If you have any mistakes or comments that there is a better code, please do not hesitate to contact us.
I will explain the Euclidean algorithm to be implemented this time. (I miss high school mathematics)
Euclidean algorithm is an algorithm for finding the ** greatest common divisor ** of two natural numbers. For that, we use the following properties
$ A and b are natural numbers $ $ Equation: For a = bq + r, gcd (a, b) = gcd (b, r) holds for $
Taking advantage of this property
"Dividing $ a $ by $ b $ to calculate the remainder $ r_1 $" → "Dividing $ b $ by $ r_1 $ to calculate the remainder $ r_2 $" →…
→ "Divided by dividing $ r_ {n-1} $ by $ r_n
#Euclidean algorithm
gcd <- function(a, b){
if (!(a %% 1 == 0 & b %% 1 == 0 & a > 0 & b > 0)) {
cat('Input is not a natural number, so start over')
} else if (a < b) {
w <- a
a <- b
b <- w
while (b != 0) {
r <- a %% b
a <- b
b <- r
}
return(a)
} else {
while (b != 0) {
r <- a %% b
a <- b
b <- r
}
return(a)
}
}
> gcd(50856, 96007)
[1] 163
#Euclidean algorithm
def gcd(a, b):
if not (a % 1 == 0 and b % 1 == 0 and a > 0 and b > 0):
print('Input is not a natural number, so start over')
elif a < b:
w = a
a = b
b = w
while not b == 0:
r = a % b
a = b
b = r
else:
return(a)
else:
while not b == 0:
r = a % b
a = b
b = r
else:
return(a)
>>> gcd(50856, 96007)
163
Let's take a look at the coding characteristics of both. (Note that this is a subjective judgment based on the attributes of the author!)
It's almost the first time I've written Python, I was worried, "This isn't closed, but it works properly ...?" But it works perfectly. Is it the point where the convenience of this area is supported?
R is enclosed in parentheses, and Python is indented to indicate the beginning and end of the process. Using indentation may be better in terms of "processing + easier for the user to see". (I heard a rumor that the corresponding parentheses are colored with the new function of Rstudio, so I'm expecting it)
motion | R | Python |
---|---|---|
Function definition | name <- function(argument){processing} | def name(argument):processing |
Conditional branch 1 | if(Conditional expression){processing} | if Conditional expression:processing |
Conditional branch 2 | else if(Conditional expression){processing} | elif Conditional expression:processing |
repetition | while(Conditional expression){processing} | while Conditional expression:processing |
This is about the same. Forcibly speaking, the difference between else if and el if.
motion | R | Python |
---|---|---|
Integer quotient | %/% | // |
Surplus | %% | % |
Logical AND | & | and |
Logical sum | | | or |
denial | ! | not |
In R, operators are consistently given symbols, while In Python, the alphabet is used for the part related to conditional branching.
While it is imagined that R logical operators are often used in filter processing,
Is Python supposed to be used exclusively for conditional branching?
(ʻIf not ~ is naturally easy to read, but
filter (a == 1 and b <= 3 and ~) `seems to be long and difficult to read)
The above is a rough comparison, but I think that the advantages and disadvantages of both languages have become apparent. Personally, I was surprised at how easy it is to write Python control syntax.
This time, I compared the basis of the language called control syntax (= the part where the idea is strongly expressed). If I can afford it, I would like to compare data frame processing and so on.
I felt that there was a lot of waste in the code, so I rewrote it while referring to various things.
R
;
(what you are doing is the same)gcd <- function(a, b){
if (!(a %% 1 == 0 & b %% 1 == 0 & a > 0 & b > 0)) {
cat('Input is not a natural number, so start over')
} else {
if(a < b){
tmp <- a; a <- b; b <- tmp
}
while(b != 0){
r <- a %% b; a <- b; b <- r
}
return(a)
}
}
Python
def gcd(a, b):
if not (a % 1 == 0 and b % 1 == 0 and a > 0 and b > 0):
print('Input is not a natural number, so start over')
else:
if a < b:
a, b = b, a
while b != 0:
a, b = b, a % b
else:
return b
I was told about the recursive function in the comments, so I implemented it.
As a caveat, if you repeat until b == 0
, the argument violates the condition of a natural number, so you need to rewrite it so that it repeats until one time before (ʻa% b == 0`). is there.
def gcd(a,b):
if not (a % 1 == 0 and b % 1 == 0 and a > 0 and b > 0):
print('Input is not a natural number, so start over')
else:
if a < b:
a, b = b, a
if not a % b == 0:
return gcd(b, a % b)
else:
return b
There is also a function called Recall
for calling recursive functions
gcd <- function(a, b){
if (!(a %% 1 == 0 & b %% 1 == 0 & a > 0 & b > 0)) {
cat('Input is not a natural number, so start over')
} else {
if(a < b){
tmp <- a; a <- b; b <- tmp
}
if(a %% b != 0){
return(Recall(b, a %% b) #Or gcd(b, a %% b)
} else{
return(b)
}
}
}
Recommended Posts