-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #73 from jeremydouglass/R
add Processing.R (R language mode) examples
- Loading branch information
Showing
5 changed files
with
172 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# Ackermann function | ||
# https://rosettacode.org/wiki/Ackermann_function#Processing.R | ||
# Processing 3.4 | ||
# 2009-06-30 ShinTakezou (R) | ||
# 2020-04-17 Jeremy Douglass | ||
# | ||
# The Ackermann function is a classic example of a recursive function, notable | ||
# especially because it is not a primitive recursive function. It grows very | ||
# quickly in value, as does the size of its call tree. | ||
# | ||
# The Ackermann function is usually defined as follows: | ||
# | ||
# ( n+1 if m = 0 | ||
# A(m,n) = ( A(m-1, 1) if m > 0 and n = 0 | ||
# ( A(m-1, A(m, n-1)) if m > 0 and n > 0 | ||
# Its arguments are never negative and it always terminates. | ||
# | ||
# Task: | ||
# | ||
# Write a function which returns the value of A(m,n). | ||
# Arbitrary precision is preferred (since the function grows so quickly), | ||
# but not required. | ||
|
||
setup <- function() { | ||
for (m in 0:3) { | ||
for (n in 0:4) { | ||
stdout$print(paste(ackermann(m, n), " ")) | ||
} | ||
stdout$println("") | ||
} | ||
} | ||
|
||
ackermann <- function(m, n) { | ||
if ( m == 0 ) { | ||
return(n+1) | ||
} else if ( n == 0 ) { | ||
ackermann(m-1, 1) | ||
} else { | ||
ackermann(m-1, ackermann(m, n-1)) | ||
} | ||
} | ||
|
||
# Output: | ||
# | ||
# 1 2 3 4 5 | ||
# 2 3 4 5 6 | ||
# 3 5 7 9 11 | ||
# 5 13 29 61 125 | ||
|
||
# note that Processing.R exceeds its stack depth at ~ n==6 | ||
# and returns null. | ||
# -- see also https://rosettacode.org/wiki/Find_limit_of_recursion#R |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
# NinetyNineBottles | ||
# https://rosettacode.org/wiki/99_Bottles_of_Beer#Processing.R | ||
# Processing 3.4 | ||
# 2020-04-17 Jeremy Douglass | ||
# | ||
# Task: | ||
# | ||
# Display the complete lyrics for the song: | ||
# 99 Bottles of Beer on the Wall. | ||
# | ||
# The lyrics follow this form: | ||
# | ||
# 99 bottles of beer on the wall | ||
# 99 bottles of beer | ||
# Take one down, pass it around | ||
# 98 bottles of beer on the wall | ||
# | ||
# 98 bottles of beer on the wall | ||
# 98 bottles of beer | ||
# Take one down, pass it around | ||
# 97 bottles of beer on the wall | ||
# | ||
# ... and so on, until reaching 0. | ||
# | ||
# Grammatical support for "1 bottle of beer" is optional. | ||
# | ||
# As with any puzzle, try to do it in as creative/concise/comical | ||
# a way as possible (simple, obvious solutions allowed, too). | ||
|
||
|
||
# immediately prints all output to the console | ||
setup <- function() { | ||
stdout$print(bottlesong(99)) | ||
} | ||
|
||
bottlesong <- function(num) { | ||
verses = "" | ||
for(i in num:1){ | ||
verses = paste0(verses, | ||
num," bottles of beer on the wall \n", | ||
num," bottles of beer \n", | ||
"Take one down, pass it around \n", | ||
num-1, " bottles of beer on the wall \n\n", sep=""); | ||
num <- num - 1 | ||
} | ||
return(verses) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# 100 Doors | ||
# http://rosettacode.org/wiki/100_doors#Processing.R | ||
# Processing 3.4 | ||
# 2008-09-16 Gregg Lind (R) | ||
# 2019-12-10 Jeremy Douglass | ||
# | ||
# There are 100 doors in a row that are all initially closed. | ||
# You make 100 passes by the doors. | ||
# | ||
# The first time through, visit every door and toggle the door | ||
# (if the door is closed, open it; if it is open, close it). | ||
# The second time, only visit every 2nd door (door #2, #4, #6, ...), | ||
# and toggle it. | ||
# The third time, visit every 3rd door (door #3, #6, #9, ...), | ||
# etc, until you only visit the 100th door. | ||
# | ||
# Task: | ||
# | ||
# Answer the question: | ||
# What state are the doors in after the last pass? | ||
# Which are open, which are closed? | ||
|
||
setup <- function() { | ||
for(door in doors(100, 100)) { | ||
stdout$print(paste(door, "")) | ||
} | ||
} | ||
|
||
doors <- function(ndoors=100,passes=100) { | ||
doors <- rep(FALSE,ndoors) | ||
for (ii in seq(1,passes)) { | ||
mask <- seq(0,ndoors,ii) | ||
doors[mask] <- !doors[mask] | ||
} | ||
return (which(doors == TRUE)) | ||
} |