A challenge in Python. How to slove this? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

A challenge in Python. How to slove this?

You have k apple boxes full of apples. Each square box of size m contains m × m apples. You just noticed two interesting properties about the boxes: 1.--The smallest box is size 1, the next one is size 2,..., all the way up to size k. 2.--Boxes that have an odd size contain only yellow apples. Boxes that have an even size contain only red apples. => Challenge is to calculate the difference between the number of red apples and the number of yellow apples. Example: For k = 5, the output should be appleBoxes(k) = -15. There are 1 + 3 * 3 + 5 * 5 = 35 yellow apples and 2 * 2 + 4 * 4 = 20 red apples, making the answer 20 - 35 = -15. Disclaimer: - code must be written in python - code must be in a function - function must be named solution - function must take a integer (k) as an argument - function must return a integer - no imports/libraries allowed

11th Sep 2020, 4:52 AM
Vishnu Ram M
Vishnu Ram M - avatar
4 Answers
+ 3
boxes = int(input()) def solution(k): yellow = 0 red = 0 for m in range(1, k+1): m = m*m if m%2: yellow +=m else: red +=m print(red - yellow) solution(boxes)
11th Sep 2020, 9:11 AM
Rik Wittkopp
Rik Wittkopp - avatar
+ 4
https://www.sololearn.com/discuss/1270852/?ref=app This challenge is not fit for the qna so please post it here
11th Sep 2020, 4:58 AM
Nilesh
+ 4
Can you post your attempt so we can see where your problem is. I like the challenge but am hesitant to provide a full answer as it detracts from your learning experience.
11th Sep 2020, 8:53 AM
Rik Wittkopp
Rik Wittkopp - avatar
+ 2
This is not for "qna" I've tried solving this and can't find the answer of this so I'm just asking someone can help me to find this solution. Thank You.
11th Sep 2020, 5:03 AM
Vishnu Ram M
Vishnu Ram M - avatar