8 kyu
Twice as old
24,296 of 79,927petrosernivka
Loading description...
Fundamentals
Mathematics
View
This comment has been reported as {{ abuseKindText }}.
Show
This comment has been hidden. You can view it now .
This comment can not be viewed.
- |
- Reply
- Edit
- View Solution
- Expand 1 Reply Expand {{ comments?.length }} replies
- Collapse
- Spoiler
- Remove
- Remove comment & replies
- Report
{{ fetchSolutionsError }}
-
-
Your rendered github-flavored markdown will appear here.
-
Label this discussion...
-
No Label
Keep the comment unlabeled if none of the below applies.
-
Issue
Use the issue label when reporting problems with the kata.
Be sure to explain the problem clearly and include the steps to reproduce. -
Suggestion
Use the suggestion label if you have feedback on how this kata can be improved.
-
Question
Use the question label if you have questions and/or need help solving the kata.
Don't forget to mention the language you're using, and mark as having spoiler if you include your solution.
-
No Label
- Cancel
Commenting is not allowed on this discussion
You cannot view this solution
There is no solution to show
Please sign in or sign up to leave a comment.
Java:
backward compatability code is applied, but the initial solution setup boilerplate still uses
PascalCase
for method's naming conventionfixed, and upgraded to JUnit 5, and added assertion messages
I hate math 😭
so do i man
This comment has been hidden.
There is this requirement:
You sohuld always return a positive value, for both future and past times.
This comment has been hidden.
Because you should return the result, and you're always returning 0. Note that your code is wrong, btw, and the results are not the expected ones.
It was the hardest 8kyu to me ;)
Test for python is incorrect, no?
test.assert_equals(twice_as_old(55,30) , 5)
55+5 = 60, 30+5 = 35, 60 is not twice 35.
Father 50, son 25, 5 years ago. The test is fine.
Ahhhh, damnit. Thanks.
is this definitely 8 kata?
Nice task for Python, teaches a useful function.
I work with Go. I wrote a cycle that checks if dad's age is twice of his son's. When I run this code on my local machine it works perfectly but when I try to execute it on Codewars, it just times out. That kata is broken
The kata is not broken. Please don't raise issues so lightly in the future. See https://docs.codewars.com/training/troubleshooting#timeout and https://docs.codewars.com/training/troubleshooting#post-discourse.
I used an else/if statement, no way for this statement to lead to a negative value. But in the random tests, they are telling me the code returns a negative number. I tried a combo of -/+ on both parameters, father younger than son, both -/- and in no scenario do I get a return of a (-) value. Something wrong with this one but I cannot see the values they tried to test.
there are no tests for Haskell, annoying
This comment has been hidden.
This comment has been hidden.
Resolving: The
suggestion
tag is for suggestions to improve the kata, not tips for solving the kata. 😉There is a wrong test in the Golang tests for this kata
Which test?
Expect( TwiceAsOld(55,30)).To(Equal(5)) should be Expect( TwiceAsOld(55,25)).To(Equal(5)) instead
There is another one when I hit the attempt button but I can't see the test case
You can print those arguments to console. I believe it's
fmt.Println()
for Golang.Expect( TwiceAsOld(55,30)).To(Equal(5))
-> that's correct.This comment has been hidden.
This comment has been hidden.
The c++ sample tests confuse the reader, does the kata want you to find how many years ago the father was twice the age his son was at the tiem of does it want you to find when the father was double the son's current age? The samples are all over the place.
eg.
36-7 is supposed to result in 22
22-1 is supposed to eresult in 20
edit: this is a redditor site, I encountered spacing issues
This comment has been hidden.
Haskell:
No sample tests
User has to write their own function / definition (which is not aligned with the task)
fixed
Is this a trick question?
no
Maybe it's just me, but if I am 30 years old and my child is 10 years old and someone asked, "In how many years will you be twice as old as your child?" My answer would be : 10 BUT if someone asked, "How old will you be when you are twice as old as your child?" My answer would be : 40
This is great!!!!!!
To avoid ambiguity, could you add some indication like: "Сalculate how many years ago the father was twice as old as his son CURRENTLY IS"? It should be clearly stated that the current age of the son is the point of reference and remains CONSTANT.
This comment has been hidden.
This comment has been hidden.
You forgot
using System;
. Not a kata issue.The ruby sample tests are not passing
Copied and pasted your code and they passed just fine. Not a kata issue. Click Reset in case you have messed them up.
poor translation of the exercise
In two years from now apparently I will still be as old as I am today
this is good
These test cases don't make sense: test.assert_equals(twice_as_old(36,7) , 22) test.assert_equals(twice_as_old(55,30) , 5)
The first one, the son's age remains fixed for this to happen. 36-22 = 14. 14/2 = 7 The second one, the son's age moves for this to happen. 55-5 = 50. 30-5(moving suns age)= 25. 50/2 = 25
Hi - first of all, I agree that the description/sense of the kata is a bit confusing.
But anyways, in description we have: ...no matter if it was in the past or it is in the future...
So the first example corresponds to +22 years in future, where father will be 58 years old, and son will be 29 years old, and indeed we have 58 = 2 * 29.
Agreed created the solution and then came across that the test cases aren't even working
i agree to you
The equation for this doesnt make sense. The question says now or in the future. As the son gets older multiplying it by 2 means that number will be larger than the age of the father. Causing a negative interger.
No, you misunderstood something, I don't know how you get a negative number but that's wrong. It says how many years ago (past) or in how many years (future). And the answer is always a positive number. Not a kata issue.
Not sure why my code is not working. I get an error that says "None should be equal to 0". Everything else looks good apparently. Ran this on my IDE and no issues there smh
Because your function is not returning anything for certain condition and the default returned value in Python is
None
. Read this: https://docs.codewars.com/training/troubleshooting/Thanks Chrono! This helped me alot :)
This comment has been hidden.
This comment has been hidden.
This comment has been hidden.
The description should say the ages would be absolute value.
And Javascript's random test have some problem.
The error message would be same.
Testing for dad's age: 94 and son's age: 59
It should work for random inputs too: expected -33 to equal 33
Testing for dad's age: 84 and son's age: 49
It should work for random inputs too: expected -33 to equal 33
Testing for dad's age: 99 and son's age: 79
It should work for random inputs too: expected -33 to equal 33
Testing for dad's age: 23 and son's age: 2
It should work for random inputs too: expected -33 to equal 33
Fixed.
Oh, thanks.
I was just still looking for, why the test looks fine but output is wrong.
And you fixed it all.😁
Update to Scala 3
.
cool
Am I the only one thniking that the answers are weird. Using a code editor everything works fine but, then inhere. I fail everytime.
Not correct test: it("Testing for dad's age: 55 and son's age: 30", function(){ Test.assertEquals(twiceAsOld(55,30) , 5); });
The test is fine. Read the posts below, it's been already asked and answered.
Im a little confused looking at the parameters. What is the dads value and what is the sons value ? I can see that the first int in the sample parameter probably is dadYearS ? but what is sonYears ?
current age of his son (years)
who made this crap? please rephrase this
this absolutely needs to be rephrased in some way, the description is completely useless for solving the kata, it's asking the wrong question
Let's try to rephrase a bit:
Given
find the year, Y, in which the father will be (if Y is in the future) or was (if Y is in the past) twice the age of his son.
Return the difference between Y and the current year.
that does read a lot better than the original yeah
Massive help, thanks for that.
Мне очень понравилась эта задача! Через 8 лет мой отец будет старше меня в два раза O_O
This comment has been hidden.
This comment has been hidden.
Oh, you're right! 🤦🏽♀️ Thank you!
This comment has been hidden.
I always just check the test and make logic from there. It's heaves easier that way and saves a lots of times than readin a stupid discription.
Recommendation: Use mathematics first to establish and solve equations.
easy but poorly worded question
Make more tests like 55:dad / 30:son, its bring more clear solution for this kata, its very already a good one but can be better
This comment has been hidden.
k
This comment has been hidden.
Not a suggestion.
This comment has been hidden.
Not a suggestion.
This comment has been hidden.
Thank you, sir. I've finally grasped.
Quite simple, but interesting task, although description could be more detail
Terrible freaking instructions! kata is simple, but it is not about both of their ages increasing. They are static numbers you are doing the basic math difference calculations about.
description does not say their age is increasing
This comment has been hidden.
C translation
Thanks :)
Lua translation
Approved
This comment has been hidden.
Please use spoiler flag when posting code. This is not very pertinent, the solution page is here to get the solutions, feel free to comment your solutions there if you want.
please excuse me, I'm rookie here. How can I mark my comment as a spoiler? I can't edit it somewhy
No problem, I marked it for you. Now you can see each time you post a message you have a little box
Mark as having spoiler content
you can tick under it.I saw the box, but it was too late after posting - then I just couldn't edit the first comment to tick it. Thank you!
COBOL translation
After a long week, approved :)
thanks :)
is it right for (55,30) ? shouldn't it (55,25) ?
i'm asking myself the same question
If dad has 55 and son has 30, then never dad would still have 55 and son would be 5 years younger...
What a terrible instruction
No, the tests are terrible
Umm, if the son is 30 years old, and father is 55 years old, there won't be a time when he was twice as older as his son, because he isn't older now
Father 50, son 25, 5 years ago.
Сalculate how many years ago the father was twice as old as his son. (or in how many years he will be twice as old). While he has not been 60 yet, he will be in 5 years.
It made my brain strain, but the solution is simple :)
This comment has been hidden.
Approved :)
'years ago'
This comment has been hidden.
Good kata with bad description
The description's wording is ambiguous. It should explicitly state that the expected number is the number of years since/until the father will be twice as old as his son currently is. The other interpretation (where time is considered to have passed for both of them) would not make sense for a majority of test cases (as the son wouldn't have been born for most).
Could you elaborate about that?
"Testing for dad's age: 36 and son's age: 7" The expected answer is 14 and it's in the future, when the father has 58 and the son 29. I think all those cases when you say it wouldn't make sense are the same.
This comment has been hidden.
This comment has been hidden.
Yeah, you're correct. I got confused because I expected different maths. My bad.
Thank you for this comment! I couldn't wrap my head around the kata as the description wasn't clear enough for me.
I instantly got it after reading this comment. The description is really vague.
This comment has been hidden.
Not an issue, it was 5 years ago when they had 50 and 25.
This comment has been hidden.
Not a kata issue. What if it was in the past? You'll have an infinite loop, and that's causing the time out.
This comment has been hidden.
Yes, he will be twice as old in 2 years.
Oh wow that's so weird I didn't realise. All the solutions seem to only deal with the static age of the son but it looks like it doesn't matter as long as you understand the maths. I understood the question, just not the maths needed.
Go init code still misses "package kata" on the first line.
Fixed
Hi all,
In golang, I test and all pass.
When I attempt, the basic tests pass, but the random fails.
How can I see the arguments used, so I can deal with this case?
https://github.com/codewars/codewars.com/wiki/Troubleshooting-your-solution#how-can-i-see-which-input-causes-my-solution-to-fail
This comment has been hidden.
Approved, thanks :)
Hi everyone, I got the following error message trying to run my code in the site. The code runs fine on my own machine(as far as I can tell) so I don't know what needs fixing. I'm a big newbie btw. Any suggestions? Thanks in advance... Emmet STDERR Traceback (most recent call last): File "tests.py", line 2, in from solution import twice_as_old File "/workspace/default/solution.py", line 24, in f_age =int(input()) EOFError: EOF when reading a line
Do not use 'input()' or anything like that, just write the function provided and use function arguments instead of reading input from console.
Thanks for the reply. Much appreciated. I just used a list to hard-code the values instead as the console couldn't import it's own modules. My code works now in the window, but doesn't seem to be accepted as a solution. Iĺl just move on...Thanks again...Emmet
You have to take the values the test will give you, not harcode it either. you also have to return the value, not print it.
Use return with the final result and take the variables (dad_years_old, son_years_old) instead of hardcoded ones.
so fun
This comment has been hidden.
Me too :(
brute force gang
Go translation Solution setup was missing a 1st line : package kata I added it since, but it does not yet appear. maybe translation needs re-approval.
Did you fork or edit the go translation?
I edited it
Perhaps it's a caching issue. But at the moment, it doesn't appear to be edited.
GO: sample tests:
No package declaration found.
Hi, Go translation Solution setup was missing a 1st line : package kata
It looks resolved in the Go translation.
Go Translation and Haxe Translation and OCaml Translation
Approved by author
Did he check before approving? ~~)
Sort of annoying when the Kata IDE times out so easily while in my own IDE the solution takes milliseconds to produce an answer. Obviously I'll just need to solve another way, but seems like the IDE is so touchy lol
mistake in js, test №3: 'it("Testing for dad's age: 55 and son's age: 30", function(){ Test.assertEquals(twiceAsOld(55,30) , 5); });' after 5y, son=35, dad=60(must be 70, i think).
It was 5 years ago when the dad had 50 and the son 25. The test is ok.
The phrasing is confusing for what is actually a simple and fun task. It is important to clarify that the son's age is a constant. I would suggest
Your function takes two arguments:
How many years ago was the father double his son's current age? (or indeed how many years from now?)
Your interpretation of the task is wrong.
Ahh.. fair enough. In that case, the correct interpretation is that they are both aging (much more logical) but the task wording is quite awkward. A clearer sentence would be:
Сalculate how many years it takes until the father is twice his son's age (or how many ago this already happened).
Your wording is more obscure, and includes grammatical mistakes. No, it's not better at all.
WOW, Thank you for that constant hint!
it is a little confused by the description
This comment has been hidden.
This comment has been hidden.
You have the right to your opinion. Thank you for your feedback)
Spoiler for hinting at the solution
Kotlin Translation
Please, review and appove/comment
This comment has been hidden.
Hi!
"or in how many years he will be twice as old":
This comment has been hidden.
And thank you for your feedback! ))
in one of the java's tests there is one error " assertEquals(15, TwiceAsOld.TwiceAsOld(45,30));" this won't work because 60 != 45
Hi
30 = 15*2
yes you are right,but that is not the question, if dad years are 45 and son years are 30 in 15 years would be 60 and 45 then 45*2= 70 and not 60
Read the instructions again.
Oh sorry, then is my bad but how the first text son has 0 years i was thinking in the other way
Ruby tests 4 & 5 have the wrong string explanation text:
Should read:
Dad is 42, Son is 21 Dad is 58, Son is 29
Fixed it. Thank you.
.
This comment has been hidden.
You should
return
, notprint
.Nobody's argued about the ranking except you. And there's a lot shorter,
O(1)
solution which works for any possible and impossible ages of the father and son, so it's your mistake overcomplicating the task.This comment has been hidden.
Fixed it. Thank you.
Random test generates dad younger than son, is it correct?
What is the programming language?
Java. The age generation is wrong. In the random tests:
Java
Fixed it. Thank you.
PHP setup is wrong.. doesn't have a dollar sign for arguments, very confusing to newbies
Fixed it. Thank you.
why dad_years_old = 55 son_years_old =30 twice as old equal 5? so dad_years_old = 60 and son_years_old =35 it's not twice as old
This comment has been hidden.
This comment has been hidden.
The Haskell one doesn't even have the base test case file set up, it is left as the template. It also generates negative ages, son older than father.
It's my first kumite, made some stupid mistakes :( Now fixed.
Haskell kumited.
This looks to generate all kinds of cases such son older than the father, ages are negative .
@Nlaa could you fix it?
It's my first kumite, made some stupid mistakes :( Now fixed.
Java. Argument order in sample tests is wrong again. I've left the explanation in author's fork in case he decides to fix it himself.
Click reset, I had already fixed it some hours ago.
NASM translation ready.
approved by someone
This comment has been hidden.
Please, make good suggestions. Nobody's going to rewrite 7 versions of a kata because you'd like output to be slightly different.
Please, explain why you think my suggestion isn't good enough. Both father and son are aging simultaneously - that's why I thought it would be better to consider it in this kata.
He already explained that. You'd be invalidating a -lot- of existing, valid solutions just becuase you want a output string.
Invalidating solutions is not a problem, users do not lose honor because of this, but rewriting a kata to change output format? Lol, good luck finding a user who's gonna waste their time on such a worthless idea.
It may sound surprising to you, but if the user does not address this fact, (s)he won't be able to solve the kata.
And you're not suggesting to "consider the fact they're aging", you're asking to change the output from integer to string, which only makes this kata harder because you have to output the correct string i.e. consider the singular/plural use of word "year(s)" and choose the correct tense for the message (Past Simple, Future Simple). This would be harder than finding the answer itself, you know?
In my suggestion I explained algorithm, not it's implementation. I don't care about actual output and what you're going to check in final tests.
Currently for input father's age = 30, son's age = 0 correct answer is 0. In my suggestion correct answer would be 30 (father will be 60 when his son will be 30). In my second example I explained case "from the past" - currently father is 53 and son is 27, but a year ago father was twice as old as his son.
In your suggestion you were talking nonsense. And now you're talking nonsense as well. How on earth did you get
0 * 2 = 30
???You're right. I think I just misunderstood final tests or my coffee wasn't strong enough this morning. Anyways, sorry for wasting your time.
I get this error when i try to run my code Parse error: syntax error, unexpected ',', expecting variable (T_VARIABLE) in /home/codewarrior/run.php on line 4
that's coming from your own code:
in /home/codewarrior/run.php on line 4
, so not an issue.Issue
: problem in the kata itself (description, wrong tests, wrong internal solution...)Suggestions
: well, I guess that part is clearQuestion
anything else that is related to you having a problem solving a kata.When you post issues:
cheers
C++ translation. Please, review and approve.
Java. Wrong
actual
/expected
argument order in tests.Also, please check the translation comments before approving it. I've pointed this problem out earlier.
Yeah, I was wondering about that. I was hoping that somebody fixed my janky ass random tests fix...
Fixed.
ah... then you can reject my fork of the java version, plz x)
https://www.codewars.com/kumite/5b86aab1ced43c8e7e00004f?sel=5b87eb89ced43c368b000183
It's been rejected by the kata's author.
There is now also an Elixir translation in the translations dialogue.
Also a thank you to petrosernivka... as this is a very good Kata to get my feet wet on making translations.
Rejected by someone
[link to fork]
There is now also a PHP translation in the translations dialogue
stop spamming... just announce it all at once
There is now also a Ruby translation in the translations dialogue.
There is now a Ruby translation in the translations dialogue
There is now also a Javascript translation in the translations dialogue.
I've forked your translation (was working on it at the same time). Take a look.
You removed all the charm, character and jankiness xD
I mean that purely as an odd compliment xD
If it is as a compliment, I'll take it ;)
There is now also a Javascript translation in the translations dialogue.
There is now also a Javascript translation in the translations dialogue.
I did up a C translation (not certain how to link those)
There is also a Java translation that somebody else did up.
Rejected 6 years ago
[link to fork]
A description in plain english would be a good idea. For now... :/
This is my first kata and I speak a bit in english)). Thanks for the comment)
Might still contain some mistakes (I'm not english either) but I guess that will be a bit easier to understand.
Thanks!
I think this sounds more natural though.