5 kyu

Closest and Smallest

110 of 3,343g964
Description
Loading description...
Fundamentals
Sorting
  • Please sign in or sign up to leave a comment.
  • migmcc Avatar

    in my vscode this exercise do well. I don't understand why there is an eror in here.

  • ahmet_popaj Avatar

    Nice kata, doing these type of coding challenges for fun is always rewarding.

  • KKOKU Avatar

    Precision: New to c# $$$$ I am having an error System.FormatException : Input string was not in a correct format. This is on the unit test code line cdecode.Helper.Array2DToString(ClosestWeight.Closest(s)) The code itself run without any error and i have not change the output type so i am returning int[][] and here the return return new int[][] { result[index], result[index+1]}; I guess the Helper method is from Nunit frawework but quick search unsuccessful. So what could cause the cdecode to throw error ?

  • zjt-code Avatar

    very good kata!!!

  • alaska_north Avatar

    Does anyone know what could be happening in this situation? I received the correct answer on my local env, but when I test it here, it returns a reversed array. For example:

    Expected: '[[10, 1, 154], [10, 9, 37]]', but I got: '[[10, 9, 37], [10, 1, 154]]'

    On my machine, I normally get: '[[10, 1, 154], [10, 9, 37]]'

  • saudiGuy Avatar

    python new test framework is required. updated in this fork

  • trashy_incel Avatar

    C fork

    • initial code does not crash anymore
    • it is explained what kind of pointer to return
    • better assertion messages
    • removing the reference solution from preloaded
  • pulaas Avatar

    Very nice Kata. Good for learning Scala. Thank you!

  • MarkShcerbakov Avatar

    Very good Kata! Thanks to author!

  • ejini战神 Avatar

    C#:

    Sample tests throw an error despite the correct code passes random tests

    System.FormatException : Input string was not in a correct format.

  • rustkae Avatar

    Hi, my implementation in rust passes all basic_tests, however it fails with the random tests, please can someone explain me why this string "478117 478 234955 249 761812 53 178807 396 728614 909 825796 928 157818 742 810150 842 330106 381 51786 167 742499" returns the following:

    assertion failed: (left == right) left: "[(13,14,742)(13,17,330106)]", right: `"[(13,13,742)(13,16,330106)]

    As you can see the indexes are off by one, I can't figure out what I'm doing wrong, I think the correct answer is "[(13,13,742)(13,16,330106)]", is this is a mistake in the test suite for rust? or am I missing something?

  • soypaduano Avatar

    The random test are marking me as wrong when its not How is this possible? When I try local I have the expected result. Please help!

  • stanislavrepin Avatar

    This comment has been hidden.

  • qddamage Avatar

    Spend 3 days on this. I like this kata

  • alexc19 Avatar

    Nice 5-kyu kata, I came up with a simple comparison function and let Python do the work.

  • SergeyFM Avatar

    Nice kata about programming, makes you think through the language. A breath of fresh air in between all those gotcha katas.

  • user1430804 Avatar

    amazing challenge !

  • Joec05 Avatar

    so i wrote the code and tested it one by one and works. but then test results show: "TypeError: Reduce of empty array with no initial value". someone care to explain?

  • sachinkhatri Avatar

    hmph..done at last.

  • 0bfu5cat3d Avatar

    Can someone explain the task more clearly? I get the summing the digits to find the "weight" part, but after that its not clear what its trying to say. Any help would be appreciated.

  • user4452518 Avatar

    had to write like 10 helper functions but i did it! thanks for the awesome kata

  • user9644768 Avatar

    Why output is string in perl not nested anonymous arrays?

  • RES-TophWells Avatar

    The random tests in Python could contain trailing spaces, e.g. "24 4820 10380 21 259 1 " I didn't expect this, and nothing in the description or the standard tests led me to expect it. It could be nice to add a specific test case for it.

  • rudimadima Avatar

    While performing this task, I learned that the JS sort function works differently in modern browsers and in the old Node.js. I had to write an additional sorting condition.

    By the way, it calls "unstable sort algorithms". Stable sorting - only Chrome 70+, Firefox 79+, Safari 10.1+.

  • xjyxiw Avatar

    testing(closest("89998 187 126159 175 338292 89 39962 145 394230 167 1"), [[13, 3, 175], [14, 9, 167]]) Can someone explain why? My resutl is [[16, 1, 187], [17, 5, 89]], i dont know why its bad, weights are the same but in my reuslt index are closer to 0

  • robert wang Avatar

    Funny kata to practicing sort. Thanks.

  • chustos96 Avatar

    maybe it's my english but i think it could be better explained

  • Zordid Avatar

    There's even more completely nonsense in this Kata. In the Notes, you find a hint that for empty string "" you should return something - but what about the case where only ONE number is in the string? This Kata needs serious rework. Please.

  • Zordid Avatar

    I find this Kata to be particulary hard to understand and read. It's not pleasant and well defined at all.

    For example, the third criterium reads: "and with the smallest indices (or ranks, numbered from 0) in strng"

    What exactly is that? We are talking about two numbers... so, is the sum of their indices to be minimized? If index 1 and 1000 AND 2 and 10 are closest by all other rules, does 1 and 1000 win over 2 and 10?

    Sorry, but I dislike this Kata already before I start to program...

    Also, the Kotlin translation lacks proper use of the right datatypes - if TWO things are to be returned, one should pick a Pair, not an Array.

  • mjjbycicle Avatar

    small typo- "because 2000 and 103 have for weight 2 and 4, (their, not ther) indexes in strng are 4 and 0."

  • HolyShaq Avatar

    The random tests' input contain a trailing whitespace which I don't think was intentional.

    It's not uniform to the basic tests that have no trailing whitespace with their inputs.

  • sebc Avatar

    To change the string into an vector of long long values I use std::stoll. My code works well for the test cases, but if I do Attemp stoll throws an exception. I deletet the non digit letters at the beginning of the Random test. Also the exception is thrown when the code tries to convert the last number of the string in the random test into a long long value. Has anyone experienced the same problem?

  • xgf Avatar

    Test for (462835, 148, 467467, 128, 183193, 139, 220167, 116, 263183, 41, 52) is broken! Please fix this test. For correct answer [ (13, 1, 148), (13, 5, 139) ] there is an error: caught std::exception, what(): vector::_M_range_check: __n (which is 4294967294) >= this->size() (which is 0). If swap element of answer, there is an error: Expected: equal to [ (13, 1, 148), (13, 5, 139) ] Actual: [ (13, 5, 139), (13, 1, 148) ].

  • DmytroKurovskyi Avatar

    It was crazy kata but I solved IT! ^_^

  • SteveJune Avatar

    OK, solved!

  • SteveJune Avatar

    In my C solution the test always says: Test Crashed Caught unexpected signal: 6 Completed in 0.0000ms STDERR setup.c:46:21: warning: comparison of integers of different signs: 'int' and 'unsigned long' [-Wsign-compare] for (int i = 0; i < strlen(str); i++) { ~ ^ ~~~~~~~~~~~ 1 warning generated. munmap_chunk(): invalid pointer

    On my computer it runs perfectly, also with valgrind, I cannot find a memory leak. I checked the mallocs, but everything seems to be fine. Maybe I should increase/decrease the size of the allocated memory?

  • heron1 Avatar

    Well I spent more time trying to understand the question than coding, hacking it together piece by piece as my understanding of the question evolved. I notice a lot of problems on this site are like that. Briefer, simpler to understand questions but with complicated solutions would be a far nicer thing to encounter.

  • papay Avatar

    (Sorry for my English, I am Russian)has complete that task in JS, BUT what is required to return:

    "Output:

    an array of two arrays, each subarray in the following format: [number-weight, index in strng of the corresponding number, original corresponding number instrng]" The second required number in subarrays is INDEX IN STRING!, not in array, but tests contain exactly indexes of arrays, which is returned as result of using method "split" in JS (or smth like that on other languages). Tell me why I'm wrong ^_^.

  • Tue Avatar

    Currently using a function to compare values using an iter() to keep one step ahead. When testing I get:

    STDERR Traceback (most recent call last): File "main.py", line 8, in testing(closest(""), []) File "/home/codewarrior/solution.py", line 9, in closest lowest_weights = closest_pairs(lst, 0) File "/home/codewarrior/solution.py", line 28, in closest_pairs next(ahead) StopIteration

    The code works when I run it on my computer. Does anybody know why this happens and how to remedy it?

  • thiagomajesk Avatar

    This comment has been hidden.

  • amalliar Avatar

    C version of random tests (№22, but may be more...) is BUGGED:

    Error. Expected 
    {{13, 6, 1831}, 
     but got 
    {{13, 6, 1831}, {13, 12, 12235}}
    

    Screenshot: https://imgur.com/a/BNz5Vok

    Please fix it asap @g964 as this is the second time I'm getting a broken random tests version for C in
    your kata (first one being k-Primes and I'm not the only one there).

    edit: the problem is strng: '12719 14978 18158 6578 17606 18577 16018 17959 14215 16124 12484 ' in test 22 (+) has an extra space at the end of it.

  • tmwaddel Avatar

    No matter what I try I get the following error (this is the error right after a reset):

    main.cpp:8:68: error: out-of-line definition of 'closest' does not match any declaration in namespace 'Closest' std::vector<std::tuple<int, unsigned int, long long>> Closest::closest(const std::string &strng) ^~~~~~~ 1 error generated.

    Am I missing something? Sorry if this is a basic question but I'm not too familiar with namespaces and am not sure where to look. I've been searching for several hours now. Thanks for any help!

  • don.wei99 Avatar

    My result is like that Array ( [0] => Array ( [0] => 8 [1] => 5 [2] => 134 )

    [1] => Array
        (
            [0] => 8
            [1] => 7
            [2] => 62
        )
    

    ) But the test always show Failed asserting that two arrays are equal. Expected: Array ( ) Actual : Array ( 0 => Array (...) 1 => Array (...) )

    I cann't understand the reason. By the way, I codes by php

  • solitude Avatar

    This comment has been hidden.

  • dinglemouse Avatar

    This comment has been hidden.

  • fujunnnn Avatar

    i tested in my vs2013 is passed ,but here i am not passed ,what's happen ?

  • lynxlynxlynx Avatar

    Shell is missing sample tests. This time I needed 10 before succeeding. :O

  • KongQingXin12 Avatar

    This comment has been hidden.

  • ThekhoN Avatar

    All the sample test cases are passing. But on Attempt, 180 Passed, 41 Failed, 1 Error. Example of failure - Expected: '[[9, 7, 135], [9, 9, 126]]', instead got: '[[9, 7, 135], [9, 10, 36]]'

    Is it possible to get the arguments for this example?

  • colwem Avatar

    Why can't I use the String module in OCaml?

  • PranavRaina Avatar

    i passed all the tests even random ones but i am getting this STDERR: Traceback: in in tests in closest ValueError: invalid literal for int() with base 10: '' i cant find out what this error is for.. help is much appreciated.

  • nathannli Avatar

    This comment has been hidden.

  • CNAmira Avatar

    Why I always get

    *** Error in `/home/codewarrior/solution': munmap_chunk(): invalid pointer: 0x00000000004020a3 ***

    I've test my code on my computer and it works without any problem.

  • kr4Y Avatar

    On Attempt

    testRandom

    Log

    Random Tests

    input: 213661 145 482403 149 163732 130 14079 46 209766 121 61

    Error: Call to undefined function weight()

    What is the function weight()?

  • stormhaul Avatar

    This Kata's tests are invalid for the specifications in PHP. It creates a test in the random tests every time that doesn't follow the expected output. EX) Failed asserting that two arrays are equal. Expected: Array ( 0 => Array ( 0 => 1 1 => 10 2 => '100' ) 1 => Array ( 0 => 1 1 => 3 2 => '10' ) ) Actual : Array ( 0 => Array ( 0 => 1 1 => 3 2 => 10 ) 1 => Array ( 0 => 1 1 => 10 2 => 100 ) )

    As you can see. The test's expected output expects two numbers with the same weights to not be sorted by index, which is a requirement of the previous tests.

  • user609111 Avatar

    hey, for python, in the random string tests, in some cases the numbers repeat (not the weights). How am I supposed to deal with this? I modified my code to ignore repeats, but then it fails in that way too.

  • kevinchannon Avatar

    I think that the c++ tests have the wrong values in them. For example, the second test case looks like this:

    s = "456899 50 11992 176 272293 163 389128 96 290193 85 52"; e1 = std::make_tuple(13, 9, 85); e2 = std::make_tuple(14, 3, 176); e = {e1, e2}; dotest(s, e);

    The smallest weight is given by the "50" value at index 1 in the string. It should have weight 5, right? There are three or four of the tests that have this issue.

    Also, the syntax of the starting function is incorrect. The namespace qualifier is not required if you're defining the function inside the namespace. It starts as this:

    std::vector<std::tuple<int, unsigned int, long long>> Closest::closest(const std::string &strng) { // your code }

    It should be:

    std::vector<std::tuple<int, unsigned int, long long>> closest(const std::string &strng) { // your code }

    The current implementation doesn't build because of this.

  • Yuemsam Avatar

    Expected: equal to [ [unsupported type], [unsupported type] ] Actual: [ [unsupported type], [unsupported type] ] why it happened this???

  • neopedro Avatar

    This comment has been hidden.

  • TGeo Avatar

    Fun Kata! Thanks g964!

  • safiir Avatar

    JavaScript's test cases occurs the issue like:

    Expected: '[undefined, undefined]', instead got: '[[7, 15, 7], [7, 19, 124]]'
    
  • tmikeschu Avatar

    (JS) I pass all of the example test cases, but when I run the full suite with random tests, the expected value is [undefined, undefined] on every single test.

  • hoifong Avatar

    there is a bug in test;

  • erid_nord Avatar

    This comment has been hidden.

  • alexgertz Avatar

    Hi, Didn't find how to report a cheat solution so I thought I would write here. The solution in Ruby by Poilon is clearly not trying to solve the actual problem but "cheat the way through".

    I don't know enough Ruby to understand how it works but maybe it can be prevented with tests.

  • hammenm Avatar

    Hi.

    I have an issue with this Kata in Python.

    The code I wrote works on an external IDE but not when I try to submit the code here. I get the error :

    ValueError: invalid literal for int() with base 10: ''
    
  • UnknownToken Avatar

    This comment has been hidden.