7 kyu

Two Oldest Ages

8,746 of 35,494jhoffner
Description
Loading description...
Arrays
Algorithms
  • Please sign in or sign up to leave a comment.
  • k108p Avatar

    This comment has been hidden.

  • rsschool_4d3751abbf26b5e7 Avatar

    not bad, TY for the kata

  • PetitLu117 Avatar

    Lua fork

    • added random tests
    • changed function name from camelCase to snake_case (old solutions stay valid)
    • provided input on failure
  • thiccyoshi4568 Avatar

    This comment has been hidden.

  • EndyPlus Avatar

    8 kyu level

  • bloodJelly115 Avatar

    it should be a 8kyu. Anyway, fun enough and thx for it

  • swapnil-up Avatar

    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].

  • ejini战神 Avatar

    Actual and expected are swapped in PHP

  • hobovsky Avatar

    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.

  • Chrono79 Avatar

    The input mutation test in C# is not mentioned anywhere and there is no such test in javascript, python or ruby.

  • ShaksUa Avatar

    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) {

    int [] array = ages;
    Array.Sort(array);
    Array.Reverse(array);    
    int [] result = {array[1], array[0]};
    
    return result;
    

    } }

  • dnbass Avatar

    This comment has been hidden.

  • hoos87 Avatar

    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.

  • arjandhakal Avatar

    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]).

  • ejini战神 Avatar

    Description should be language-agnostic

  • 0xNikki Avatar

    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.

  • NikCSharp Avatar

    This comment has been hidden.

  • Md-Raihan-Alam Avatar

    I submitted the solution in C. But it always showing an error:

    Test Crashed Caught unexpected signal: 6

    Explain me ~~~

  • HellcatW1776 Avatar

    This comment has been hidden.

  • chasoliveira Avatar

    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]

  • davorpa Avatar

    Should tests check if source was mutated?

    Then, it avoids side effects after execute function

    Inmutable tests

    • in Go:

      It("should not mutate source for any input", func() {
        data     := []int{1,5,87,45,8,8}
        expected := []int{1,5,87,45,8,8}
        TwoOldestAges(data)
        Expect(data).To(Equal(expected), 
          "Mutates %v", expected)
      })
      
    • in javascript / typescript

      const { expect } = require('chai');
      ...
      it("Testing for preserve source inmutability", () => {
        let source = [1,5,87,45,8,8];
        let data = [1,5,87,45,8,8];
        twoOldestAges(data);
        expect(data).to.be.deep.equal(source, `Passed values of [${source.join(', ')}] should preserve inmutability`);
      });
      
  • ejini战神 Avatar
  • dummyaccount Avatar

    This comment has been hidden.

  • ejini战神 Avatar

    No random tests in

    • Elixir
    • Go
    • Lua
    • Ocaml
    • Rust
    • Typescript
  • Alsadi77 Avatar

    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

    Please Explain this

  • DJALYNCH Avatar

    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.

  • lloppy Avatar

    This comment has been hidden.

  • user8900535 Avatar

    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

  • dudeibesam14 Avatar

    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.

  • max30272 Avatar

    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.

  • RyanSWest Avatar

    Well my solution works in replit, but not when I push attempt... guess maybe I'll just do it in Python instead.

  • RyanSWest Avatar

    Well I guess I can't do it , at least not in JavaScript, no test cases..

  • AnthoVDO Avatar

    Still not possible to test in javascript ?

  • Luciolla Avatar

    This comment has been hidden.

  • loicboset Avatar

    This comment has been hidden.

  • Sergiu Pop Avatar

    Maybe it can have more random cases, in js I saw only 4. In rest nice kata

  • zizouvb Avatar

    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] >>

  • clcraig Avatar

    C++ and Java translations available. Please review.

  • Sasquatch Avatar

    Are the most concise functions commmonly used in product code? Or is it generally more readable?

  • grndBart Avatar

    This comment has been hidden.

  • Shardul Birje Avatar

    This comment has been hidden.

  • user7147914 Avatar

    PHP Translation

    Please review this :)

  • dinglemouse Avatar

    Kata doesn't actually say the array values are >= 0.

    User supposed to infer this from the word "ages" ?

  • rowcased Avatar
  • jclast Avatar

    JS has no tests

  • stellartux Avatar

    This comment has been hidden.

  • B1ts Avatar

    CFML translation Prolog translation

    Please approve in this order.

  • somethingsomething Avatar

    This is easier than 7kyu

  • clairmont32 Avatar

    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.

  • suzumiyasmith Avatar

    Nim version Test suit does not work!

    test's return type is seq[int] but in actual attamp it is array[2,int]

  • boi87 Avatar

    This comment has been hidden.

  • konkath Avatar

    C++ translation kumited - please check and approve

  • anter69 Avatar

    Updated Python translation kumited -- please check and approve

  • CunningFatalist Avatar

    This comment has been hidden.

  • rkosak237 Avatar

    any ideas how to improve it?

  • kangli_cheng Avatar

    This comment has been hidden.

  • Serg-zefir Avatar

    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); }); })

  • narek13 Avatar

    This comment has been hidden.

  • GiacomoSorbi Avatar

    P3 solutions submitted :)

  • SithFire Avatar

    This comment has been hidden.

  • Eliv Avatar

    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?

  • user4386369 Avatar

    Should probably make your arrays varying lengths, having all test arrays with 5 numbers made it a bit too easy.

  • Mikaeru Avatar

    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].

  • Rainysun Avatar

    This comment has been hidden.

  • eavichay Avatar

    Test for array of one item only.

  • swelltrain Avatar

    This comment has been hidden.

  • fffej Avatar

    Be good if you overrode the prototype of sort() to be very slow to encourage some thought :)

  • foeken Avatar

    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 :)