7 kyu
Two Oldest Ages
8,746 of 35,494jhoffner
Loading description...
Arrays
Algorithms
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.
This comment has been hidden.
It is related to this issue: https://www.codewars.com/kata/511f11d355fe575d2c000001/discuss#63191a62e03dafd1c7a871bd
You're probably mutating the input in your code, removing those 109 and then the expected result is different.
not bad, TY for the kata
Haskell translation
Lua fork
camelCase
tosnake_case
(old solutions stay valid)This comment has been hidden.
There are several problems in your code.
Not a kata issue.
8 kyu level
it should be a 8kyu. Anyway, fun enough and thx for it
If there are two or more oldest age, then return both of them in array format, would've been better formatted as
If there are two or more oldest age, then still return only an array of two repeated numbers. For example: [99,99,98] should return [99,99].
Thanks for your suggestion, but the design of the kata won't be changed now, it has been the same for more than ten years and changing it would invalidate all submitted solutions.
Actual and expected are swapped in PHP
fixed
This kata is a subject to deduplication process here: https://github.com/codewars/content-issues/issues/163.
Please join the discussion to help us identify duplicate kata and retire them.
The input mutation test in C# is not mentioned anywhere and there is no such test in javascript, python or ruby.
Can someone help me to understand what is going on? Even after copy ages --error;
Random Tests (100 assertions)
Test Failed
User mutated the input array!
Expected and actual are both <System.Int32[20]>
Values differ at index [0]
Expected: 3
But was: 117
using System;
using System.Collections;
public class LargestTwo {
public static int[] TwoOldestAges(int[] ages) {
} }
That's not a copy, that's another reference to the same array.
Ok, it make sense. Thanks. So many questions to this mutation test..
This comment has been hidden.
Don't change the input array. Sorting it as you did, mutated it in place.
I've tried to use the Array.Sort method and I get this message "The name 'Array' does not exist in the current context". I've testing this solution VS and it works fine. Anyone know what I am missing?
Thanks.
Without knowing what you are doing nor the language you are using it's impossible to answer. I strongly advice you to read carefully the documentation to have more chance to receive a helpful answer : https://docs.codewars.com/training/troubleshooting/
This comment has been hidden.
You're missing some
using
.you need to add the namespace using System;
It is not clear what is the right solution when there are two ages of same value. Also, a test case where in the input where second oldest age comes before the oldest age should be added, for example([1,5,47,87,8]).
Description should be language-agnostic
^^
In the C version, the function does not return anything, it is just a pointer that you should fill with the right values. Moreover, you do not initialize the given array with nul. Some novice programmers might strugle when they see the results.
there is a comment in the initial code :
it is also clear that the function returns nothing, since its a
void
functioni'm not sure what you mean by
which array are you talking about ?
result
is an array of 2int
s. its values cannot be initialized withNULL
Do you have a suggestion to reword the existing comment ? Would it have helped you if the initial code included something like
to demonstrate what you have to do ?
This comment has been hidden.
Well I solved it with dynamic memory allocation using malloc, create the array with it and it might help
I used C#, not C
Also, just found out a better solution
I submitted the solution in C. But it always showing an error:
Test Crashed Caught unexpected signal: 6
Explain me ~~~
C version of the kata requires you to return a dynamically allocated, two-element array (which sucks, and I will probably change it, but that's how it is now).
Do you return a pointer to a dynamically allocated buffer?
Tnx for info but I have already solved it with dynamic memory allocation
This comment has been hidden.
In Python you should return a list, your code is returning a tuple.
The instructions say that need to return the two oldest ages, but the test tries to validate the same age:
should return 89 and 89 for input []int{19,5,43,13,75,89,43,89,25,49} Test Failed Log Expected <[2]int>: [75, 89] to equal <[2]int>: [89, 89]
No. There are two 89 in the array, so it's ok:
If there are two or more oldest age, then return both of them in array format.
Should tests check if source was mutated?
Then, it avoids side effects after execute function
Inmutable tests
in Go:
in javascript / typescript
R translation
Thanks :)
This comment has been hidden.
Because of this:
Right, totally forgot ab that, cheers!
No random tests in
This comment has been hidden.
Added random tests for typescript
Added random tests for OCaml
Added random tests for Lua in this fork
Test Failed User mutated the input array! Expected and actual are both <System.Int32[20]> Values differ at index [0] Expected: 43 But was: 118
Random Tests (100 assertions) Log max is:113 max2 is:95 Test Failed Expected and actual are both <System.Int32[2]> Values differ at index [0] Expected: 99 But was: 0 ???????????????????????????
Post your method here with correct markdown format and spoiler flag.
This comment has been hidden.
problem solved!!!
Just incase other people can't read just like me: return the array in order of [secondHighest, highest]. I kept returning the two highest in the wrong order for some test cases.
This comment has been hidden.
Doesn't show all tests, which is useless, and it doesn't pass test because of it and can't even tell where it is wrong
Read this: https://github.com/codewars/codewars.com/wiki/Troubleshooting-your-solution#print-input
Print the input yourself.
But I already did this, it works fine for tests I can see in the terminal, and all random I could think of, but there are few one that are hidden, and they say that mine are wrong, which I can't tell why, as the tests are hidden
Print the input inside your function, not the tests suite.
I'm really new to programming, but I managed to "complete" this with my IDE's help. I still don't understand some things. I'm solving this in C.
So from what I gather, what I returned was a hexadecimal pointer whose 8-digit address was the same number value as the second-oldest age?
Is there even any way to check the oldest age from what I returned?
I'm sorry, I just really don't understnad this one.
you are allocating your own array instead, this is why it does not work
This is one of the ancient katas and uneditable. So, there won't have local test in some languages. Please just hit "Attempt" to do the submit test.
Well my solution works in replit, but not when I push attempt... guess maybe I'll just do it in Python instead.
Well I guess I can't do it , at least not in JavaScript, no test cases..
no SAMPLE tests. Just hit 'attempt' to run all tests.
Still not possible to test in javascript ?
Still not possible...
Just go for 'Attempt'.
This comment has been hidden.
You have mutated the original array, also missing curly brackets in some parts
This comment has been hidden.
Hi, your code is fine. There are no sample tests in js, and an issue has already been raised for this. Just click submit and your code will pass the kata.
This kata seems to be lacking any example test solutions so the only way to test if it works is by pressing the attempt button
... or you can add sample tests by yourself while working on your solution.
Hope this helps for some copy-paste test cases:
Maybe it can have more random cases, in js I saw only 4. In rest nice kata
For typescript, example in the instruction << two_oldest_ages( 4, {1, 2, 10, 8} ) // should return {8, 10} >> should be change to << two_oldest_ages([1, 2, 10, 8]) // should return [8, 10] >>
C++ and Java translations available. Please review.
Updated Java to JUnit 5 and approved.
C++ approved by someone else.
Are the most concise functions commmonly used in product code? Or is it generally more readable?
Depends. With simple exercises like this one, compact solutions are used in production. However, with more complex algorithms, code golfing is more of a past time. Self documenting and legible code is more desireable for professinal production code, as it's easier to read and maintain.
This comment has been hidden.
Ps: this is what I get, when it crashes:
Test Crashed
Exit code: 0
Signal code: 6
STDERR *** Error in `./test': munmap_chunk(): invalid pointer: 0x00007ffedf8ade08 *** ======= Backtrace: ========= /lib/x86_64-linux-gnu/libc.so.6(+0x777e5)[0x7fe8043ab7e5] /lib/x86_64-linux-gnu/libc.so.6(cfree+0x1a8)[0x7fe8043b8698] ./test[0x4018c7] ./test[0x400ff4] /usr/lib/libcriterion.so(criterion_internal_test_main+0xaf)[0x7fe804bfa29f] ./test[0x400fc8] /usr/lib/libcriterion.so(run_test_child+0xb0)[0x7fe804bf8100] /usr/lib/libcriterion.so(run_worker+0x36)[0x7fe804bf9986] /usr/lib/libcriterion.so(spawn_test_worker+0x140)[0x7fe804bf9b00] /usr/lib/libcriterion.so(+0x9487)[0x7fe804bf9487] /usr/lib/libcriterion.so(run_next_test+0x16c)[0x7fe804bf966c] /usr/lib/libcriterion.so(criterion_run_all_tests+0x23e)[0x7fe804bf863e] /usr/lib/libcriterion.so(main+0x30)[0x7fe804bf7720] /lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xf0)[0x7fe804354830] ./test[0x400ed9] ======= Memory map: ======== 00400000-00402000 r-xp 00000000 08:01 715603 /home/codewarrior/test 00602000-00603000 r--p 00002000 08:01 715603 /home/codewarrior/test 00603000-00604000 rw-p 00003000 08:01 715603 /home/codewarrior/test 01ce4000-01d05000 rw-p 00000000 00:00 0 [heap] 7fe8034a5000-7fe8034bb000 r-xp 00000000 08:01 283444 /lib/x86_64-linux-gnu/libgcc_s.so.1 7fe8034bb000-7fe8036ba000 ---p 00016000 08:01 283444 /lib/x86_64-linux-gnu/libgcc_s.so.1 7fe8036ba000-7fe8036bb000 rw-p 00015000 08:01 283444 /lib/x86_64-linux-gnu/libgcc_s.so.1 7fe8036bb000-7fe8036bc000 ---p 00000000 00:00 0 7fe8036bc000-7fe803ebc000 rwxp 00000000 00:00 0 7fe803ebc000-7fe803f2a000 r-xp 00000000 08:01 283495 /lib/x86_64-linux-gnu/libpcre.so.3.13.2 7fe803f2a000-7fe80412a000 ---p 0006e000 08:01 283495 /lib/x86_64-linux-gnu/libpcre.so.3.13.2 7fe80412a000-7fe80412b000 r--p 0006e000 08:01 283495 /lib/x86_64-linux-gnu/libpcre.so.3.13.2 7fe80412b000-7fe80412c000 rw-p 0006f000 08:01 283495 /lib/x86_64-linux-gnu/libpcre.so.3.13.2 7fe80412c000-7fe804133000 r-xp 00000000 08:01 283504 /lib/x86_64-linux-gnu/librt-2.23.so 7fe804133000-7fe804332000 ---p 00007000 08:01 283504 /lib/x86_64-linux-gnu/librt-2.23.so 7fe804332000-7fe804333000 r--p 00006000 08:01 283504 /lib/x86_64-linux-gnu/librt-2.23.so 7fe804333000-7fe804334000 rw-p 00007000 08:01 283504 /lib/x86_64-linux-gnu/librt-2.23.so 7fe804334000-7fe8044f4000 r-xp 00000000 08:01 283416 /lib/x86_64-linux-gnu/libc-2.23.so 7fe8044f4000-7fe8046f4000 ---p 001c0000 08:01 283416 /lib/x86_64-linux-gnu/libc-2.23.so 7fe8046f4000-7fe8046f8000 r--p 001c0000 08:01 283416 /lib/x86_64-linux-gnu/libc-2.23.so 7fe8046f8000-7fe8046fa000 rw-p 001c4000 08:01 283416 /lib/x86_64-linux-gnu/libc-2.23.so 7fe8046fa000-7fe8046fe000 rw-p 00000000 00:00 0 7fe8046fe000-7fe804716000 r-xp 00000000 08:01 283498 /lib/x86_64-linux-gnu/libpthread-2.23.so 7fe804716000-7fe804915000 ---p 00018000 08:01 283498 /lib/x86_64-linux-gnu/libpthread-2.23.so 7fe804915000-7fe804916000 r--p 00017000 08:01 283498 /lib/x86_64-linux-gnu/libpthread-2.23.so 7fe804916000-7fe804917000 rw-p 00018000 08:01 283498 /lib/x86_64-linux-gnu/libpthread-2.23.so 7fe804917000-7fe80491b000 rw-p 00000000 00:00 0 7fe80491b000-7fe8049ea000 r-xp 00000000 08:01 532168 /usr/lib/x86_64-linux-gnu/libsqlite3.so.0.8.6 7fe8049ea000-7fe804bea000 ---p 000cf000 08:01 532168 /usr/lib/x86_64-linux-gnu/libsqlite3.so.0.8.6 7fe804bea000-7fe804bed000 r--p 000cf000 08:01 532168 /usr/lib/x86_64-linux-gnu/libsqlite3.so.0.8.6 7fe804bed000-7fe804bef000 rw-p 000d2000 08:01 532168 /usr/lib/x86_64-linux-gnu/libsqlite3.so.0.8.6 7fe804bef000-7fe804bf0000 rw-p 00000000 00:00 0 7fe804bf0000-7fe804c07000 r-xp 00000000 08:01 1064586 /usr/lib/libcriterion.so 7fe804c07000-7fe804e07000 ---p 00017000 08:01 1064586 /usr/lib/libcriterion.so 7fe804e07000-7fe804e08000 r--p 00017000 08:01 1064586 /usr/lib/libcriterion.so 7fe804e08000-7fe804e09000 rw-p 00018000 08:01 1064586 /usr/lib/libcriterion.so 7fe804e09000-7fe804e0c000 r-xp 00000000 08:01 283434 /lib/x86_64-linux-gnu/libdl-2.23.so 7fe804e0c000-7fe80500b000 ---p 00003000 08:01 283434 /lib/x86_64-linux-gnu/libdl-2.23.so 7fe80500b000-7fe80500c000 r--p 00002000 08:01 283434 /lib/x86_64-linux-gnu/libdl-2.23.so 7fe80500c000-7fe80500d000 rw-p 00003000 08:01 283434 /lib/x86_64-linux-gnu/libdl-2.23.so 7fe80500d000-7fe805227000 r-xp 00000000 08:01 532025 /lib/x86_64-linux-gnu/libcrypto.so.1.0.0 7fe805227000-7fe805426000 ---p 0021a000 08:01 532025 /lib/x86_64-linux-gnu/libcrypto.so.1.0.0 7fe805426000-7fe805442000 r--p 00219000 08:01 532025 /lib/x86_64-linux-gnu/libcrypto.so.1.0.0 7fe805442000-7fe80544e000 rw-p 00235000 08:01 532025 /lib/x86_64-linux-gnu/libcrypto.so.1.0.0 7fe80544e000-7fe805451000 rw-p 00000000 00:00 0 7fe805451000-7fe805559000 r-xp 00000000 08:01 283457 /lib/x86_64-linux-gnu/libm-2.23.so 7fe805559000-7fe805758000 ---p 00108000 08:01 283457 /lib/x86_64-linux-gnu/libm-2.23.so 7fe805758000-7fe805759000 r--p 00107000 08:01 283457 /lib/x86_64-linux-gnu/libm-2.23.so 7fe805759000-7fe80575a000 rw-p 00108000 08:01 283457 /lib/x86_64-linux-gnu/libm-2.23.so 7fe80575a000-7fe805780000 r-xp 00000000 08:01 283386 /lib/x86_64-linux-gnu/ld-2.23.so 7fe80596f000-7fe805976000 rw-p 00000000 00:00 0 7fe80597e000-7fe80597f000 rw-p 00000000 00:00 0 7fe80597f000-7fe805980000 r--p 00025000 08:01 283386 /lib/x86_64-linux-gnu/ld-2.23.so 7fe805980000-7fe805981000 rw-p 00026000 08:01 283386 /lib/x86_64-linux-gnu/ld-2.23.so 7fe805981000-7fe805982000 rw-p 00000000 00:00 0 7ffedf88f000-7ffedf8af000 rwxp 00000000 00:00 0 [stack] 7ffedf8af000-7ffedf8b0000 rw-p 00000000 00:00 0 7ffedf910000-7ffedf913000 r--p 00000000 00:00 0 [vvar] 7ffedf913000-7ffedf915000 r-xp 00000000 00:00 0 [vdso] ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0 [vsyscall]
It's enough to post the first line of the error, which exactly states the problem... no need for entire stack trace.
Not a kata issue, but problem with your code, so, closing...
@grndBart ~ you need to dynamically allocate memory
This comment has been hidden.
MDN page for Array#shift
As you can see, it doesn't take any arguments, and it returns an element, not an array, so there's no way you can call
.reverse()
on that.JS has no sample tests, but has tests when you hit 'attempt'. The issue about it is already raised below.
PHP Translation
Please review this :)
'alr approved some time ago'
Kata doesn't actually say the array values are >= 0.
User supposed to infer this from the word "ages" ?
The array will always include at least 2 items
C Translation
JS has no tests
Reraised above with more info
This comment has been hidden.
CFML translation Prolog translation
Please approve in this order.
Please approve this fork of CFML translation. Thanks!
This is easier than 7kyu
Is not the argument in the example a slice not an array? An array would have an integer with the number of elements it can hold whereas a slice does not and is (mostly) flexible.
Nim version Test suit does not work!
test's return type is seq[int] but in actual attamp it is array[2,int]
This comment has been hidden.
This comment has been hidden.
C++ translation kumited - please check and approve
Added random tests. See above.
Updated Python translation kumited -- please check and approve
Thanks for approving!
This comment has been hidden.
It seems to work now.
any ideas how to improve it?
This comment has been hidden.
replace simple test on that: /// /// import {twoOldestAges} from './solution'; import {assert} from "chai";
describe('twoOldestAges', () => { it('should return two old ages', () => { assert.equal(twoOldestAges([1, 2, 10, 8])[0], 8); assert.equal(twoOldestAges([1, 2, 10, 8])[1], 10); }); })
This comment has been hidden.
Elixir translation here
Crystal translation here
P3 solutions submitted :)
This comment has been hidden.
Thanks SithFire. TS and Elixir kata are likely to be a bit buggy for the next few days while we work out the kinks. This one should be fixed now.
Yep, it works now. Thank you very much.
The description is lacking: 1)Will the array always have 2 or more numbers? How to act if not? 2)It says 3 examples, but only gives out 1... 3)If two highest numbers are equal, should we return both? Or the second biggest different age?
fixed
Should probably make your arrays varying lengths, having all test arrays with 5 numbers made it a bit too easy.
I find conditions a bit awkward because for an array like [0, 1, 1, 2, 2] the output will be [2, 2]. But my inner voice tells me that it's got to be [1, 2].
I'd make an issue out of this comment. The wording in the task description is not clear.
This comment has been hidden.
"The returned value should be an array in the format [second oldest age, oldest age]."
The "numbers passed in" are the input. The input can be in any order, the output has requirements on the order.
Test for array of one item only.
This comment has been hidden.
It's not too hard to get the answer in a single pass-through the array (and to do so in a way that scales to finding the k-biggest elements, or k-smallest)
I guess Ruby's use case isn't for this kind of fine-tuning, but w/e
Be good if you overrode the prototype of sort() to be very slow to encourage some thought :)
What if the age array is empty? If you use hard indices you will get [nil,nil] as a result, where an empty array would be preferable. I suggest adding a case for this :)
I think that since 2 items are requested, an error must be raised