Python, R Programming, Data Structurs, Algorithms , Date: 01/22/23
<aside> 💡
Project Description: This lab report presents solutions to a series of algorithmic tasks using Python and R, two distinct programming languages. The tasks involve basic list operations and summation calculations, comparing the readability, writability, and overall usability of each language. The project focuses on three specific tasks, each implemented in both Python and R, followed by a summary comparison.
</aside>
Python:
def printall(list):
for sublist in list:
for element in sublist:
print(element)
printall([[1, 2], [3, 4, 5], [6, 7, 8, 9]])
R:
printall <- function(lists) {
lapply(lists, function(x) {
lapply(x, function(y) {
print(y)
})
})
}
# To call: printall(list(list(1,2), list(3,4,5), list(6,7,8,9)))
kalkul(n)Python:
def kalkul(n):
somme = 0
for i in range(1, n + 1):
somme += (i / (i + 1) + (n + 1) / (n + 2))
return somme
print(kalkul(4))
R:
kalkul <- function(n) {
somme <- 0
for (i in 1:n) {
somme <- somme + (i / (i + 1) + (n + 1) / (n + 2))
}
print(somme)
}
dsum(n)Python:
def dsum(n):
somme = 0
for i in range(1, n + 1):
for j in range(1, n + 1):
somme += 3 * i
print("The sum is", somme)
dsum(2)
R:
dsum <- function(n) {
somme <- 0
for (i in 1:n) {
for (j in 1:n) {
somme <- somme + 3 * i
}
}
print(somme)
}
Summary Comparison: After completing the tasks, it is evident that Python offers superior readability and writability compared to R. Python's syntax is more straightforward, making it easier to implement tasks like list iteration and summation. In contrast, R requires more explicit declarations, which can make the code less intuitive to write and read. However, R's strength lies in its specialized tools for data analysis, particularly with its robust visualization capabilities in RStudio. Overall, Python's general-purpose nature makes it a more accessible language for a wide range of programming tasks.