Skip to content

Instantly share code, notes, and snippets.

@primaryobjects
Created November 28, 2015 16:56
Show Gist options
  • Save primaryobjects/4d11de474b30bea59038 to your computer and use it in GitHub Desktop.
Save primaryobjects/4d11de474b30bea59038 to your computer and use it in GitHub Desktop.
Funny Plant: Reddit Daily Programmer Challenge #242 2005-11-23
funnyPlant <- function(people, plants) {
fruit <- 0
weeks <- 1
while (fruit < people) {
fruit <- fruit + plants
plants <- plants + fruit
weeks <- weeks + 1
}
weeks
}
https://www.reddit.com/r/dailyprogrammer/comments/3twuwf/20151123_challenge_242_easy_funny_plant/
Description
Scientist have discovered a new plant. The fruit of the plant can feed 1 person for a whole week and best of all, the plant never dies. Fruits needs 1 week to grow, so each weak you can harvest it fruits. Also the plant gives 1 fruit more than the week before and to get more plants you need to plant a fruit.
Now you need to calculate after how many weeks, you can support a group of x people, given y fruits to start with.
Input
15 1
Output
5
Input description
The input gives you 2 positive integers x and y, being x the number of people needed to be fed and y the number of fruits you start with.
Output description
The number of weeks before you can feed the entire group of people.
Explanation
Here you have a table that shows the growth when starting with 1 fruit. It shows when the plant came into existence (is planted) and how may fruit it bears each week
Plant 1 2 3 4 5 6 7 8 9 10 11 12 13 Total # of fruits in a harvest
Week
1 0 - - - - - - - - - - - - 0
2 1 0 - - - - - - - - - - - 1
3 2 1 0 0 0 - - - - - - - - 3
4 3 2 1 1 1 0 0 0 0 0 0 0 0 8
5 4 3 2 2 2 1 1 1 1 1 1 1 1 21
At week 1 we have 1 plant giving 0 fruits, because it has just been planted.
When week 2 comes along we have 1 plant that gives off a fruit and then we use that fruit to plant plant 2.
Then in week 3 we have 2 fruits from plant 1, 1 from plant 2, so we can plant 3 new plants.
Challenge Input
200 15
50000 1
150000 250
Challenge Output
5
14
9
> funnyPlant(200, 15)
[1] 5
> funnyPlant(50000, 1)
[1] 14
> funnyPlant(150000, 250)
[1] 9
# Reddit Daily Programmer Challenge #242 2005-11-23
# https://www.reddit.com/r/dailyprogrammer/comments/3twuwf/20151123_challenge_242_easy_funny_plant/
#
# Description
# Scientist have discovered a new plant. The fruit of the plant can feed 1 person for a whole week and best of all, the plant never dies. Fruits needs 1 week to grow, so each weak you can harvest it fruits. Also the plant gives 1 fruit more than the week before and to get more plants you need to plant a fruit.
# Now you need to calculate after how many weeks, you can support a group of x people, given y fruits to start with.
# Input
# 15 1
# Output
# 5
# Input description
# The input gives you 2 positive integers x and y, being x the number of people needed to be fed and y the number of fruits you start with.
# Output description
# The number of weeks before you can feed the entire group of people.
# Explanation
# Here you have a table that shows the growth when starting with 1 fruit. It shows when the plant came into existence (is planted) and how may fruit it bears each week
# Plant 1 2 3 4 5 6 7 8 9 10 11 12 13 Total # of fruits in a harvest
# Week
# 1 0 - - - - - - - - - - - - 0
# 2 1 0 - - - - - - - - - - - 1
# 3 2 1 0 0 0 - - - - - - - - 3
# 4 3 2 1 1 1 0 0 0 0 0 0 0 0 8
# 5 4 3 2 2 2 1 1 1 1 1 1 1 1 21
# At week 1 we have 1 plant giving 0 fruits, because it has just been planted.
# When week 2 comes along we have 1 plant that gives off a fruit and then we use that fruit to plant plant 2.
# Then in week 3 we have 2 fruits from plant 1, 1 from plant 2, so we can plant 3 new plants.
# Challenge Input
# 200 15
# 50000 1
# 150000 250
# Challenge Output
# 5
# 14
# 9
# Calculate how many weeks until produced fruit equals the number of people, given a starting number of plants.
funnyPlant <- function(people, plants) {
fruit <- 0
weeks <- 1
while (fruit < people) {
fruit <- fruit + plants
plants <- plants + fruit
weeks <- weeks + 1
}
weeks
}
# Calculate how many plants and fruit is produced at a specific week.
numPlants <- function(weeks) {
fruit <- 0
plants <- 1
for (week in seq(weeks)) {
if (week > 1) {
fruit <- fruit + plants
plants <- plants + fruit
}
}
c(plants = plants, fruit = fruit)
}
# Challenge input and output parameters.
challenge <- list(c(people = 200, plants = 15), c(people = 50000, plants = 1), c(people = 150000, plants = 250))
outputs <- c(5, 14, 9)
# Run the function on each input and verify correct output.
sapply(seq_along(challenge), function(index) {
input <- challenge[[index]]
output <- outputs[index]
funnyPlant(input['people'], input['plants']) == output
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment