Apple and orange tree

Apple and Orange

Sam’s house has an apple tree and an orange tree that yield an abundance of fruit. Sam’s two children, Larry and Rob, decide to play a game in which they each climb a tree and throw fruit at their (Sam’s) house. Each fruit that lands on the house scores one point for the one who threw it. Larry climbs the tree on the left (the apple), and Rob climbs the one on the right (the orange).

Larry climbs the apple tree at point , and Rob climbs the orange tree at point . Sam’s house stands between points and . Values increase from left to right.

You will be given a list of distances the fruits are thrown. Negative distances indicate travel left and positive distances, travel right. Your task will be to calculate the scores for Larry and Rob and report them.

Constraints

  • $1 \le s,t,a,b \le 10^5$
  • $-10^5 \le d \le 10^5$
  • $a \le s \le t \le b$

Input Format

  • Pair numbers containing integers denoting the respective values of s and t .
  • Pair numbers containing integers denoting the respective values of a and b .
  • Integers array denoting the respective distances that each apple falls from point a .
  • Integers array denoting the respective distances that each orange falls from point b .

Sample Input

Sample Output

Explanation

  • The first apple falls at position 5 — 2 = 3
  • The second apple falls at position 5 + 2 = 7
  • The third apple falls at position 5 + 1 = 6
  • The first orange falls at position 15 + 5 = 20
  • The second orange falls at position 15 — 6 = 9

Only one fruit (the second apple) falls within the region between 7 and 11 , so Larry’s score is 1 . Only the second orange falls within the region between 7 and 11 , so Rob’s score is 1 .

Solution Java 7

Pair is just a POJO for containing first and last values.

This solution kind is O(N) since the algorithm performance will grow linearly and in direct proportion to the size of the input data set.

Let’s consider the Java 8 version using lambda

Источник

Перевод песни Oranges on apple trees (A-ha)

Oranges on apple trees

Апельсины на яблонях

Water comes and water goes
Where it ends up no one really knows
Water rises, water falls
Where it comes from no one knows for sure
It seeps from every pore

We have seen the rain before
Not like this — It’s flooding every shore
People come and people go
I can hear their laughter through the door
But no one’s keeping score
Oranges on apple trees
Birds that mate with bumblebees
One big happy family
Endless possibilities
(yeah and more)

Oranges and apple trees
I see your face in front of me
Can’t believe what no one else could see
I feel it deep inside of me
Everything we love shall be released
And everyone will see
Oranges on apple trees (for sure)

I used to be the only one
Not now since you’ve come
You and I have seen the signs
That’s why the wind died
The wind died

That’s why Oranges on apple trees
Birds that mate with bumblebees
Endless possibilities (now)
Oranges on apple trees
All the things that we can see
Multi-gender wannabes
Oranges on apple trees
Birds that mate with bumblebees
One big happy family
Endless possibilities (yeah yeah yeah)
Oranges on apple trees (for sure)

Вода пребывает и вода убывает
Никто на самом деле не знает откуда она берется
Вода поднимается, вода падает
Никто точно не знает откуда она приходит
Кажется, что из каждой поры

Читайте также:  Tpa apple pie дозировка

Нам уже приходилось видеть дождь
Но не такой — он затапливает все берега
Люди приходят и люди уходят
Я слышу их смех за дверью
Но никто не ведет счет
Апельсины на яблонях
Птицы, что поддерживать компанию со шмелями
Одна большая семья
Бесконечные возможности
(Да и даже больше)

Апельсины на яблонях
Я вижу твое лицу перед собой
Не верю, что больше никто этого не видел
Я чувствую это глубоко внутри себя
Все что мы любим будет открыто миру
И все увидят
Апельсины на яблонях (непременно)

Раньше я был только один
Но не сейчас, когда ты пришла
Ты и я видели знаки
Вот почему ветра не стало
Ветра не стало

Вот почему апельсины на яблонях
Птицы, что поддерживать компанию со шмелями
Бесконечные возможности (сейчас)
Апельсины на яблонях
Все те вещи, что мы можем видеть
Герои разных полов
Апельсины на яблонях
Птицы, что поддерживать компанию со шмелями
Одна большая семья
Бесконечные возможности (да да да)
Апельсины на яблонях (непременно)

Источник

Apple and Oranges HackerRank Solution in C, C++, Java, Python

Sam’s house has an apple tree and an orange tree that yield an abundance of fruit. Using the information given below, determine the number of apples and oranges that land on Sam’s house.

In the diagram below:

  • The red region denotes the house, where is the start point, and is the endpoint. The apple tree is to the left of the house, and the orange tree is to its right.
  • Assume the trees are located on a single point, where the apple tree is at point , and the orange tree is at point .
  • When a fruit falls from its tree, it lands units of distance from its tree of origin along the -axis. *A negative value of means the fruit fell units to the tree’s left, and a positive value of means it falls units to the tree’s right.

Given the value of for apples and oranges, determine how many apples and oranges will fall on Sam’s house (i.e., in the inclusive range )?

For example, Sam’s house is between and . The apple tree is located at and the orange at . There are apples and oranges. Apples are thrown units distance from , and units distance. Adding each apple distance to the position of the tree, they land at . Oranges land at . One apple and two oranges land in the inclusive range so we print

Complete the countApplesAndOranges function in the editor below. It should print the number of apples and oranges that land on Sam’s house, each on a separate line.

countApplesAndOranges has the following parameter(s):

  • s: integer, starting point of Sam’s house location.
  • t: integer, ending location of Sam’s house location.
  • a: integer, location of the Apple tree.
  • b: integer, location of the Orange tree.
  • apples: integer array, distances at which each apple falls from the tree.
  • oranges: integer array, distances at which each orange falls from the tree.

The first line contains two space-separated integers denoting the respective values of and .

The second line contains two space-separated integers denoting the respective values of and .

The third line contains two space-separated integers denoting the respective values of and .

The fourth line contains space-separated integers denoting the respective distances that each apple falls from point .

The fifth line contains space-separated integers denoting the respective distances that each orange falls from point .

Print two integers on two different lines:

  1. The first integer: the number of apples that fall on Sam’s house.
  2. The second integer: the number of oranges that fall on Sam’s house.

Источник

MSE Forum

Howdy, Stranger!

It looks like you’re new here. Sign in or register to get started.

Categories

  • 3.5M All Boards
  • 14.6K Coronavirus Support Boards
  • 3.5K Banking & Borrowing
  • 1.2K Reduce Debt & Boost Income
  • 2.3K Spending & Discounts
  • 3.1K Work, Benefits & Business
  • 9.1K Mortgages & Homes
  • 1.5K Life & Family
  • 3.2K Travel & Transport
  • 14.8K Hobbies & Leisure
  • 21K Discuss & Feedback
Читайте также:  Как сбросить айфон через дфу

Free Money Tips Email!

Free Weekly MoneySaving email

Weekly deals and loopholes — spam free!

For all the latest deals, guides and loopholes simply sign up today — it’s spam free!

apple tree and orange tree

need advice asap. i bought a apple and orange tree of Yougarden they arrived Wednesday. after being very good at answering emails when i was going to buy them they have suspiciously dried up now we have? and we have virtually no after care advice & are newbies.

both are small and grown in pots. the apple one is a Discovery Apple tree and is grown in a pot.

We have 2 pots to transfer them over to, one is massive (for discovery apple) 1 smaller for the orange.

Lots of Questions!!
1. how often to water both winter, summer?
2. at what temp does orange tree need to come in?
3. when to transfer to bigger pots? asap? and what soil/compost is required for both? dont want to mess it up!
4. do any need anything added to them other then just plain old water and when etc?

Replies

Personally I’d plant the apple in the ground if possible, but if you want to keep it in a container there is advice here. https://www.rhs.org.uk/advice/profile?pid=321

The orange clearly does need to be in a pot so you can bring it in. There’s advice on growing citrus here https://www.rhs.org.uk/advice/grow-your-own/fruit/citrus and there are plenty of videos and advice online, eg, from specialist nurseries https://citruscentre.co.uk/pages/care-sheets#citrus. (Make sure it’s advice for the UK.) I’ve kept my lemon alive for 20 years, flowers regularly, and get occasional fruit, but nowhere near enough for our G&T needs!

54 ways to ‘DIY it’

According to the MSE Forum

£10 Christmas bonus

For benefits recipients

Lidl ‘£10 off £40 spend’ voucher

Via Metro or Daily Mail. Excludes NI

How this site works

We think it’s important you understand the strengths and limitations of the site. We’re a journalistic website and aim to provide the best MoneySaving guides, tips, tools and techniques, but can’t guarantee to be perfect, so do note you use the information at your own risk and we can’t accept liability if things go wrong.

  • This info does not constitute financial advice, always do your own research on top to ensure it’s right for your specific circumstances and remember we focus on rates not service.
  • We don’t as a general policy investigate the solvency of companies mentioned (how likely they are to go bust), but there is a risk any company can struggle and it’s rarely made public until it’s too late (see the Section 75 guide for protection tips).
  • Do note, while we always aim to give you accurate product info at the point of publication, unfortunately price and terms of products and deals can always be changed by the provider afterwards, so double check first.
  • We often link to other websites, but we can’t be responsible for their content.
  • Always remember anyone can post on the MSE forums, so it can be very different from our opinion.

MoneySavingExpert.com is part of the MoneySuperMarket Group, but is entirely editorially independent. Its stance of putting consumers first is protected and enshrined in the legally-binding MSE Editorial Code.

More tools from

Martin Lewis is a registered trade mark belonging to Martin S Lewis.

Источник

HackerRank: “Apple and Oranges” — Simple Solutions to Hard Problems

This problem stumped me quite well. The following is the problem:

The example given above was quite straightforward, allowing me to find a proper and well written algorithm as soon as I read it. Based on the explanation given by the question, here is the written algorithm to solve this problem:

  1. Create two variables first, to keep the numbers of apple and oranges. For the sake of the tutorial, we will call them apple_landed and orange_landed .
  2. Loop through the array apple.
  3. For every element of the displacement the loop is currently in, add a (distance of the apple tree) to the element, so that it will be the proper displacement in relation to the default (because the displacement in the array is related to the distance between the fallen fruit and its respective tree). Assign this current value to a temporary variable.
  4. After adding the distance of the apple tree to the current element of the loop, I should check if it lands on to the house. This can be done by writing another loop that it will iterate through the position of the house. For example, if the position of my house’s s is 7 and t is 10, it will iterate through 7,8,9, and 10.
  5. If the temporary variable from the 2nd step is equal to one of the iterations from the 3rd step, then add 1 to the variable apple_landed.
  6. Repeat step 1 to 5, but using orange tree this time.
  7. Print apple_landed and orange_landed on two separate lines. Use «\n» to help with adding the new line.
Читайте также:  Смайлик с айфона грустный

That was my initial algorithm. From this algorithm, the following snippet is the algorithm translated to Python3 code:

However, it is silly to think that an algorithm can pass through all crazy test cases that the dear question maker had made. After running it for a single test case and got the green light, I tried to run the whole test case. It failed after succeeding in only 3 test cases, while the other failed cases resulted in a “Terminated due to timeout :(“ error.

A simple Google search showed that a Termination due to timeout error happens when the program runs at a much slower time than expected, which resulted in the program itself being terminated. Besides, who would want to run a slow program? Slow program sucks. This is why I breathe and live in code. I hate slow things. Things that aren’t efficient. I hate waiting. Waiting is the biggest waste of time, especially if it’s waiting for nothing. I aim to make things quicker. To solve problems no one could solve.

Enough with my bravado. The reason a program could run this slow is because of the use of arrays instead of Hash Maps. Huh, interesting enough. For once, I actually learned something from a mistake.

After searching through the ever helpful discussion bar for the first time, it turned out that another user experienced the same problem as me. A user pointed out that what causes the timeout is the algorithm step number 4. Paying my Hackos to see a locked test case revealed that a certain test case has a very high difference between s and t. Imagine iterating through each of those numbers, it would be too much for the computer to handle.

And so, another user proposed a solution, one that is way too simple that made me wonder why I couldn’t think of it in the first place. The solution is: replace iterating over the s and t, and instead see if the number is greater than/equal to s and lower than/equal to t. This is what the final answer looks like:

This small addition to the code passed all the test case.

And it hurts. It hurts and exciting at the same time. To see such simple solution to a problem that could hurt the runtime of a program. It hurts, because obviously I am lacking in proper skills. I haven’t been doing much exercises while all other people are probably banging their back bones over solving 10 algorithm problems a day.

Hopefully, I could apply this life lesson to advance my programming career.

Источник

Оцените статью