8 kyu
String repeat
1,868 of 317,136wichu
Loading description...
Fundamentals
Strings
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.
I tried to use for loop, but there is an error
Haskell original specifies ( by omission ) and tests with negative numbers, which are to be handled as
0
. Other languages do not necessarily test for this.For an 8kyu, asking solvers to figure out what
repeat -1 "ab"
looks like seems like asking too much.IMO kata should specify "non-negative number," and only test with those.
Added
non-negative
in descriptionFixed Haskell tests
Closing ~~
This comment has been hidden.
This comment has been hidden.
There is no problem,
n
is the number of repetitions ands
is the string. In your solution you swaped them, but useds
as a number andn
as a string, there was no need for that.function repeatStr (n, s) { return ''; }
describe("Tests", function() { it ("Basic tests", function() { assert.strictEqual(repeatStr(3, ""), "**"); assert.strictEqual(repeatStr(5, "#"), "#####"); assert.strictEqual(repeatStr(2, "ha "), "ha ha "); }); });
repeatStr(3, "*") ->>>> repeatStr (n, s) in here n equals 3, s equal *
Yes, and that's ok. In your code you did this:
And had to use n as a string and s as a number to make sense of that change, because the tests called your function with the first argument being a number and the second one a string.
My solution passes the test but when I press the "Attempt" button, I get the following: Test Results: green SolutionTest • 0 Repeat The red border appears, but nothing else happens... Any ideas?
Your code is failing the test with 0 repetitions.
OP solved it, closing
This comment has been hidden.
Not a kata suggestion. Please don't post solutions in kata's discourse.
This comment has been hidden.
This comment has been hidden.
the logic for the method is badly written. it should return it once at 0- if you repeat it 1 time, it should print the string teice, 2 repeats should print 3 etc. but this example show 6 printing 6 times- so this isnt even a program of repeating its a program that asks for the number of times to print and then prints it that number of times, which is different than printing it a minimum of one time and then the number specifies the number of repeats which intuitively should be seperate from the initial one (which by definition would not be a repeat) so stringRepeat(0, a) should return "a" not "". the idea of repetion seems to be a confusing topic for the creator of the problem and it is designed in a way that contradicts itself and has very buggy tests.
it made this test:
@Test public void test0kata() { assertEquals("", Solution.repeatStr(0, "kata")); }
the tests and problem should assume the String value being passed is printed once- or else it didnt exist in order to be repeated and had no business being passed as an argument in the first place.
testing for assertEquals("", Solution.repeatStr(0, "kata")); instead of testing for assertEquals("kata", Solution.repeatStr(0, "kata")); (I.E. a default base value of the String passed said/printed at least once doesnt actually check if it was repeated it checks if it was ever said which is different. To check if it was repeated you need the 0 value to return the string one time and assume the first iteration is when you pass it as an argument. That shows it being repeated 0 times. if it was repeated 0 times and also never printed one time, it doesnt exist so you dont need to test for the case of repeating something that is never being said once because then it by nature would obviously not be repeated and therfore never passed as an argument in the first place.
since this is a repeater we should assume that the lowest number accepted, 0, should correlate to the least amount of times it could be said/printed in order to exist- which in this case is 1.
long story short this problem doesnt test your program as a repeater- it tests it as a printer-
-to repeat once has to have a base from which to repeat- -therfore the return of this method at 0 repetitions should be the passed string one time. -if you want your solution to work make sure the numbers correlate to number of prints not number of repetions
if it is a printer then yes, 0 copies should have 0 value- but if it is a repeater, 0 should mean print only once without any repetition.
This kata is using this logic:
Just do the challenge wether it's right or wrong "logic" for you. Don't know why your last remark is necessary, but next time, try to tell that to your client/boss that their request is so childish and lack a basic logical foundation.
i did- in 4 mins- but had to read through the tests they made to relaize they didnt want repetion but just number of times printed which is actually a completely different problem than what they labeled it as.
IDK, requirements of this task seem to conform to at least one commonly recognized interpretation of "repeating a string":
I wonder if they all know that they are wrong :)
ah i see here:
repeat public String repeat(int count)
Returns a string whose value is the concatenation of this string repeated count times. If this string is empty ***or count is zero then the empty string is returned.
i thought logically if the return is a concatonation of the string repeated 'count' times then "string" + nothing else would still make "string" but java says it returns empty string so thats right. seems counter-inuitive though but i guess java says if u dont repeat it any times it returns an empty string not that string once. obviously java.lang is right, but it seems like they might could use a conterversial new intern with some bright ideas (jk)
This comment has been hidden.
also,
src == ""
does not do what you think it does. In C, usestrcmp()
to compare strings. Using==
compares their addresses in memoryExpected: equal to "#####" Actual: "***#####" cool, that's just cool.
That looks like you're using a global var and that's the result of the 1st and 2nd test together. You had that problem in another kata too, please, don´t use global vars, read the troubleshooting docs.
This comment has been hidden.
This comment has been hidden.
The most straight-forward way I can think of solving it in Haskell leads to Timeout Error
You need to think of a more straight-forward way. Or a more complex one. Anyway, a way which does not turn out to be very, very, very ineffective one.
This comment has been hidden.
"Non-negative" is not specified. I don't know exactly what "
a
, repeated exactly-1
times," looks like, but that's your problem. Closing this issue, because you made it a "your code" issue instead of a kata issue.Missing edge cases (
s = "", n = 0
,s = "", n = 5
ands = "I", n = 0
) inClojure
CS
Crystal (Missing 1st and 2nd)
C# (Missing 1st)
D
Dart
Elixir
Factor (Missing 1st and 2nd)
Go
Haskell
Java (Missing 1st)
JS
Julia
Kotlin (Missing 1st)
Lua
NASM (Missing 1st and 2nd)
Nim
Perl
PHP
Python
R
Racket
Reason
Ruby
Rust
Shell
Solidity (Missing 1st and 2nd)
SQL
Swift (Missing 1st and 2nd)
TS (Missing 1st and 2nd)
Haskell random tests guarantee the first random test will be that first case, and it is exceedingly unlikely the other two cases will not come up.
QuickCheck generates random cases of increasing possible size, it starts with
0
, and there are then 99 chances of generating a case empty in either argument. I have not done the math, but getting 99 non-empty cases seems unlikely.The negative numbers might be a problem though - addressing that separately.
Added across all languages :-) (After 1 year, by me still ~~
This comment has been hidden.
Elm translation translation added :)
This comment has been hidden.
it is strange that if n = 0 then the string is empty
This comment has been hidden.
Please refrain of posting your solutions in kata's discourse.
Ok. Sorry, mark both my solutions as a spoilers, i think i forgot to do that. ;) Or u can delete the somments, i allow you
Already did it, but the idea is not every user posting a solution in discourse, you can post your solution if it doesn't work following this guideline when requesting some help. Read this and this to help you get how things work here. Welcome and have fun.
This comment has been hidden.
Read this: https://docs.codewars.com/training/troubleshooting/#expected-the-same and this: https://docs.codewars.com/training/troubleshooting/#post-discourse
Hey, I'm trying to solve this with java using recursion. I'm a beginner level. Anyways, It's not allowing me to remove the static from the method saying 'detected non-static method' in the errors when testing. Is this intentional so they don't want us to solve it using factorization or not? I'm confused.
You cannot remove
static
, because tests were created in a way to expect the solution function to be static. If you removestatic
, tests wouldnt be able to call your solution.But even then, nothing stops you from using the static function recursively. Static functions can be recursive. if you feel you need to remove the
static
to create a recursive solution, you are doing something wrong.Not a kata issue, you should not change the initial interface of the solution.
Thanks for clarifying that! I'm still a student in universities and we haven't learned about tests yet, so I didn't know about that part. Anyways, thank you for the clarifications!
What a friendly website I found xd
Hello there.. nice Kata in the first place. But unfortunately for Swift I get the following err which seems to reference to the test files:
=> Fatal error: Can't form Range with upperBound < lowerBound: file Swift/ClosedRange.swift, line 335
My Solution works in a Xcode Playground and the example test runs smoothly. Can I contribute to a solution here?
Your solution crashes for tests like
repeatStr(0, "ABC")
. Your solution is buggy, it's not a kata issue.This comment has been hidden.
Not a kata issue.
Hint: your code doesn't handle
n = 0
correctly.Yeah I tried my answer in VS code and it worked....I dont know why it didnt work out with the test
This comment has been hidden.
That's not how you report an issue, state the language and what the problem is about. Read this: https://docs.codewars.com/training/troubleshooting/#kata-bug
That's a problem with your code, not a kata issue.
OCaml translation (author gone)
Approved
ii test the function at remix and its work fine but dont work at SOLIDITY editor here?
On Ruby, I keep getting the error: Expected: "***", instead got: "*"
Have tried the code in a different interpreter and it runs fine, anyone getting the same error message?
Your function can only return once, not multiple times as you want with your code. Maybe in the other interpreter you used
puts
orprint
instead ofreturn
.This comment has been hidden.
I've tried your code in repl.it and it doesn't work there either. Try changing it, it won't work.
Thanks for the help man! Solved.
This comment has been hidden.
I positively confirm it correctly works for C.
Yeah, I don't know what I did 😅
All my tests passed and the output on the random tests looked exactly as it should. Missing null pointer maybe? But surely then the other tests would have failed too? No idea what happened 😅
Yeah sorry to say that, but there's more than one thing wrong in your solution :)
sizeof(src)
andsizeof(src)*count
do not work the way you think, and cause your buffers to be waaay too smallreturn "";
will cause crashes for some inputsstrcat(clean_string, "\0");
is either wrong, or redundant (depending on what's already in theclean_string
)Feel free to join Codewars Discord and ask for help in
#c
channel, definitely there will be someone willing to help!Thank you 😅
Hint, you can write answer in one string.
Actually really help me thank u!!
It sure did help me though. Thanks
There is a compilation error with the pragma but the kata doesnt say what kata to use instead
For C++, there is an assertion for "null" and it expects to equal "".
The problem here is that this null is a literal string instead of a NULL value. I had to make a separate condition for "null" in my solution for it to work.
So for this assertion, in reality should be "null" and expecting "null", or NULL and expecting "".
Requests output of n, s, res, but accepts s, n, res.
A good one. Optimal difficulty for 8 kyu.
But your code doesn't work, isn't it?
The fastest kata in my memory. 3s and done)
Hello, this error is appearing, can you help me with what I'm missing in the code?
"expected 'BrunoBrunoBruno' to equal '***'"
You should use your function's argument
s
, not"Bruno"
.thank you, now it works.
This comment has been hidden.
This comment has been hidden.
an error with the kata
No.
Yes, this kata can't be solved because tests are written wrong. The error is:
ReferenceError: Test is not defined at Context. (test.js:13:5) at processImmediate (internal/timers.js:464:21)
Click
Reset
, try again.something happend !! here there is a problem with this kata
What happened?!!
I'm trying to submit my kata but dosen't work but it's work in console dev tools Error : ReferenceError: Test is not defined at Context. (test.js:15:5) at processImmediate (internal/timers.js:464:21)
Do you have Node 12 or Node 14 selected when you submit? Tests indeed use
Test
while they should not, but for some reason they work for me.I will take a look tomorrow.
Node 14 ok as you want thank you again
Please try with Node 12 and see if it works.
Solidity version should be updated to 0.8.12 or above, where simpler (and more practical) solutions are available. Also, the "multiply" function signature is missing its visibility "public".
Language version suggestions are not kata suggestions. Also, see https://discord.com/channels/846624424199061524/846630620444229632/1012233459483279370 - it's available.
Suggestion should be "update kata to use current language version."
Now it's available!
Solidity version huge outdated, so skip this kata.
Language version updated after fork approved
This comment has been hidden.
My apologiez, I did not know it would make the code like that
This comment has been hidden.
there is a problem with this kata, it seems ?
You're going to have to be more precise than that if you want someone to be able to help out - it seems you are working in JavaScript where this kata has been solved 70,000+ times: what problem do you encounter?
Are there any error messages that you can share, do they occur for specific inputs etc. etc. ?
for beginners there should be a solution with explanation and logic. I think it will them grow.
Submit your annotated solution, and people will be able to read that after solving or forfeiting.
That way, you can make available what you think should be available. ( Please do this for all languages. )
This comment has been hidden.
Please, include in the kata that if n equals 0 then the function should return an empty string ''
Why add it? Isn't it obvious? What should be returned instead?
Well, a given string repeat
0
times is definitely gonna be empty (same logic to all numbers multiplied by0
will be0
) Hence, closingD translation
Approved!
This kata is a subject to deduplication process here: https://github.com/codewars/content-issues/issues/109.
Please join the discussion to help us identify duplicate kata and retire them.
This kata was decided to stay.
This comment has been hidden.
This comment has been hidden.
Read this please: https://docs.codewars.com/training/troubleshooting
Not a kata issue.
This comment has been hidden.
Good learning exercise for beginners.
Not a suggestion ^^
When running my program I am getting incorrect results for the Random tests after the first test. I believe it is not freeing the memory between each random test, like it is in the preset tests.
I have the same issue
SQL translation
This comment has been hidden.
The description of this kata should really include that 'n' can be 0.
Why? It's elementary that it can.
I agree, but it's a Kata specifically for those new to programming.
Hello!
I'm getting an error that I believe has nothing to do with my code. The following is the error.
Traceback (most recent call last): File "tests.py", line 2, in from solution import repeat_str ImportError: cannot import name 'repeat_str' from 'solution' (/workspace/default/solution.py)
Copy your code, click Reset, paste it and try again. Make sure 3.8 is the selected Python version in the trainer.
This comment has been hidden.
you allocate an arbitrary size. why 100 chars ? what if your function is called to repeat 30 times a 10-chars long string ? other problem : you are breaking the function's contract by returning its input (a constant string) if
count
is 0. the tests expect you to allocate memory withmalloc()
, so that the returned pointer can befree
d. that would not be a correct solution anyway : you have to return the empty string whencount
is 0 (repeating something 0 times results in nothing)This comment has been hidden.
you code in its current form passes the tests (I tried several times), so I'm not sure what your problem is.
However, there are still several problems:
malloc
ed string every time. It seems you are getting away with this because the tests suite does not test this case (or else your code would crash for freeing a string literal)less serious issues:
strcpy(dest, "")
is equivalent to just*dest = '\0'
strcat()
, it does it for youstrlen()
is a loop despite not needing to do that; you can know what is the current length of the string at each iteration, because you are always concatenating the same stringcheers :)
Description should be language agnostic. I didn't update it myself in COBOL's translation because I'm not sure BF should require a specific note (though a comment note in solution set up is more advisable). Currently it's an uninteresting mess, several languages miss a specific block.
done by someone
All languages should include the examples from the description (
repeatStr(6, "I") // "IIIIII", repeatStr(5, "Hello") // "HelloHelloHelloHelloHello"
) as fixed tests. Many, if not most, don't.Added across all languages :-)
COBOL translation (author inactive).
approved
This comment has been hidden.
I'm trying to solve it with Clojure (and clojure.string/join function), but I'm getting a stacktrace saying "Unable to resolve symbol: string in this context". Is codewars broken?
This is not a kata issue: an issue is a bug in the kata, not a difficulty to solve it. I mark your comment with a spoiler flag since it reveals a key to solve the kata. You may need to import something to do what you intent.
This comment has been hidden.
@akar-0 hey, it is not resolved.
You don't use it correctly.
@akar-0, you are wrong. I was importing it correctly, but the variable in the template code is named "strng" (without "i") for some weird reason, so this was the source of the problem. Thanks for nothing anyway.
I am a complete noob at this language, meaning I literally just passed this one as my very first Clojure kata (
Completed on 1st Attempt:1
), yay! (and this language totally mysifies me, just had to do some research) So, no it is not broken.@rowcased congrats and welcome to the wonderful Lisp world! Codewars' solver is not broken, but there's definitely a typo in the template solution ("string" -> "strng").
I imagine I probably missed something before diving in but none of the learning tools I used in school ever had me use return for code validation(even though its probably better that way), and a note would have saved a few minutes of head scratcing. Maybe this comment will save somebody else from that, though.
This comment has been hidden.
School is overrated. Rick and Morty
Unclear suggestion (which code validation are you talking about?). Maybe suggest something to your school.
I do not understand, what is supposed to be returned, if n = 0?
A string with zero repetitions of the given string.
Thank you!
This comment has been hidden.
I started to get SEGDEV from testing. So I commented everything in the function (because my code works under a real compiler) and I still get SEGDEV ?? I am starting to doubt the assertion code being used to valide our code.
An
issue
is reserved for when there is a clear problem with the kata. The C kata is working fine. If you're not sure what's going on, post as aquestion
instead. You're getting aSEGDEV
probably from memory mis-management. You need to return a dynamically allocated, null-terminated C-string as a result, which will be freed by the testing suite.Well, that was a 'clear problem' with he kata as when no instruction are executed, there should not be any SEGDEV error. Now if your assertion/testing code is doing a free operation without validation, I can't do nothing on that part as I didn't code it.
This comment has been hidden.
This kata used obsolete setup for F# which did not support your syntax, but now it got updated and your solution works.
This comment has been hidden.
Not a kata issue.
Please use spoiler flag when you post code, and use markdown tags to format it.
Please read this: https://docs.codewars.com/training/troubleshooting/
The outputs are exactly the same as what the test cases require but the tests fail anyway. I'm using Python if that helps.
I checked the Python version of the kata and it seems to work well. Most probably your solution is not correct. Please see this bullet point for a potential cause: https://docs.codewars.com/training/troubleshooting#.print-vs-return or see if the linked article has anything helpful.
You're probably not returning anything, and without seeing your code it's impossible to tell what's wrong for sure. In any case, not a kata issue.
Perl translation kumited
Approved
This comment has been hidden.
Print the input and read the error messages.
This comment has been hidden.
Catch that case with an if. The error message is very clear about what's wrong with your code.
This comment has been hidden.
Please use a spoiler flag next time you give elements of a solution.
You must return a value, your function currently returns nothing. There's no point in calling the function inside your code as you do at the end.
See if this can help you: Troubleshooting Your Solution.
No one is here to teach you programming, this is a very basic kata you can find the whole JavaScript documentation online.
Thank you very much for the answer! Sorry, next time I'll mark my messages as question. First day at codewars, sorry for noob question.
No problem, welcome :) But you have to practice and learn by yourself, for many 8kyus katas in JS you'll find easily the answers online (and for this one in particular).
This comment has been hidden.
Please say which language you're using (I can't even know if your code is syntactally correct). You do not modify the string by printing it, so it does not work (you always return the original string), and you don't have to call the function inside your code as you do at the end, tests do that by themselves.
nice
This comment has been hidden.
This comment has been hidden.
Read about the methods you use, that won't mutate the string, it returns a new one, you should assign the result to something, for instance, to
result
var.This comment has been hidden.
ok I managed to fix my code, I had to put the size argument to malloc on rdi register before calling malloc, I was thinking that would go through the stack
This comment has been hidden.
This comment has been hidden.
why does c++ have so few completions, so ez lol
yolo
yolo
yolo
This comment has been hidden.
yolo!
agregame ruben
In C# need to use different method names Test solution = repeatStr Attempt = RepeatStr
Click
Reset
, I see RepeatStr in both places.This comment has been hidden.
Read https://docs.codewars.com/training/troubleshooting#expected-the-same
not sure how this solves the issue.
when I run "Test.assert_equals(repeat_str(3, ""),"**")" through my solution, the test gives me the following report:
"Expected: "***", instead got: 3"
but when I run the same test through ruby in (for example) replit, I do get the expected output. I still think there is an error somewhere.
The problem is in your code, it is printing and returning, it prints
"***"
and returns3
it should only return"***"
In repl.it you see this:
***3
Where did the 3 come from? I can't wrap my head around that one.
I managed the kata with an oldskool (not really best practise like) code, but I'm still puzzled about the "3". Anyway, thanks for the assistance! Appreciated!
This comment has been hidden.
I don't know how to contact mods but the test works only for method repeatStr() but the "Attempt" only works with for RepeatStr()!!!!!
fix if you are interested
What language?
C#. to be more clear, the method names are not the same for test/attempt
thanks for the response!
Fixed
This comment has been hidden.
I had to create two methods (repeatStr, RepeatStr) due to an error in the tests for c#, just copied and pasted into each to pass.
Different variables during test and debugging, developers, pay attention to this!
repeatStr and RepeatStr for C# tests
Different variables during test and debugging, developers, pay attention to this!
when you get setbacks in life never let it go to waste just understand that you are not prepared yet and it's necessary maybe cry and then over just get back up and do it again....
This comment has been hidden.
add ; at the end of each lines.
This comment has been hidden.
The NASM translation should probably note in the details that count can be zero.
"an integer" can't be zero? be glad you're not getting negative numbers.
In C# there's a typo in the namespaces, top one has the first letter capitalized while the bottom one doesn't, if anyone else is having issues in other languages I suspect it may be the same reason
Updated C++ translation
Bad method name in tests on C#
This filling, when I'm writing everything right, but functions name is wrong (sorry for my blodyhell english).
Hello, I tested my code on my end and it works. I got an code.cs(1,7)error cs0246: The type of namespace name 'system' could not be found. Are you missing an assembly reference? I look up error and it says to update nuget packages? Please advise.
You'd need to give some more details (your code or the exact error message), because namespace
System
is definitely present and should be available (althoughsystem
is not).This comment has been hidden.
Please mark your post as having spoiler content next time.
using system;
is not the same asusing System;
You have a typo there, and that's what hobovsky told you before.
My apologies I mark spoiler moving forward. I will work with this. Thank you.
This comment has been hidden.
Not a kata issue, your code should return the string, not print it.
Please read this: https://github.com/codewars/codewars.com/wiki/Troubleshooting-your-solution
This comment has been hidden.
No random tests in
C# Fork with random tests (approved)
Haskell now has random tests.
Added for Typescript
someone added for java
Added for F#
This comment has been hidden.
This comment has been hidden.
This comment has been hidden.
You're leaving too much room for interpretation/guessing, there's no way to know what you're doing. Start with showing the code that you're using instead of code that you're not using, and, put
before and after your code to keep the formatting intact.
Mark your post as containing spoilers.
This comment has been hidden.
should be fixed
Hello! I have good result in TEST button, but I have error in ATTEMPT. Swift.
Time: 2308ms Exit Code: 132 Test Results: SolutionTest STDERR Fatal error: Can't form Range with upperBound < lowerBound: file Swift/ClosedRange.swift, line 335 Current stack trace: 0 libswiftCore.so 0x00007f03531d8610 swift_reportError + 50 1 libswiftCore.so 0x00007f035324bea0 swift_stdlib_reportFatalErrorInFile + 115 2 libswiftCore.so 0x00007f0352f3124e + 1397326 3 libswiftCore.so 0x00007f0352f30da7 + 1396135 4 libswiftCore.so 0x00007f0352f30a83 + 1395331 5 libswiftCore.so 0x00007f0352f304f0 assertionFailure(::file:line:flags:) + 511 6 test 0x000055594843f546 + 38214 7 test 0x000055594843fee2 + 40674 8 test 0x000055594843ff23 + 40739 9 test 0x000055594843ff94 + 40852 10 libXCTest.so 0x00007f0353509bec + 257004 11 libXCTest.so 0x00007f035350994d + 256333 12 libXCTest.so 0x00007f0353508960 XCTAssertEqual(:::file:line:) + 91 13 test 0x000055594843fe83 + 40579 14 test 0x000055594843fa32 + 39474 15 test 0x000055594843fabc + 39612 16 test 0x00005559484409b1 + 43441 17 libXCTest.so 0x00007f0353502911 + 227601 18 libXCTest.so 0x00007f035350297c + 227708 19 libXCTest.so 0x00007f03535028f4 + 227572 20 test 0x000055594843b810 + 22544 21 test 0x000055594843d6c4 + 30404 22 libXCTest.so 0x00007f0353501270 XCTestCase.invokeTest() + 55 23 libXCTest.so 0x00007f0353500f60 XCTestCase.perform(:) + 150 24 libXCTest.so 0x00007f0353504be0 XCTest.run() + 150 25 libXCTest.so 0x00007f0353502e70 XCTestSuite.perform(:) + 160 26 libXCTest.so 0x00007f0353504be0 XCTest.run() + 150 27 libXCTest.so 0x00007f0353502e70 XCTestSuite.perform(:) + 160 28 libXCTest.so 0x00007f0353504be0 XCTest.run() + 150 29 libXCTest.so 0x00007f0353502e70 XCTestSuite.perform(_:) + 160 30 libXCTest.so 0x00007f0353504be0 XCTest.run() + 150 31 test 0x000055594843acc9 + 19657 32 test 0x000055594843f720 + 38688 33 libc.so.6 0x00007f0351411ab0 __libc_start_main + 231 34 test 0x000055594843a71a + 18202
I just solved the kata in Swift and encountered no problems with it, so I believe problem lies somewhere (probably in incorrectly constructed range) in your solution.
This comment has been hidden.
You should add a 'return' statement AFTER the loop, so all paths return some value.
But your code is incorrect:
will only run once and return the string immediately, so you shouldn't use return in a for loop.
Is any tips you can give me? I know the point of the exercise is to try to solve of it yourself but I am stuck. Also do you have any good Java tutorials to recommend?
OP solved it, closing
This comment has been hidden.
Kata rank is shared amongst all languages, it is pretty easy in some other languages.
Oh that makes sense. Yeah, in Python it's a joke.
A beginner should know for loops
Yeah that wasn't the problem, it was getting to know malloc. I haven't really been exposed to it, have just done the Harvard free CS50 course so far.
R translation added. Thank you for reviewing :).
Approved
Expected and actual are flipped in PHP.
fixed
Using PHP, when I do the ATTEMPT I get this: Failed asserting that two strings are equal. Expected: 'a' Actual : '' When I run my code on my Ampps localhost server it does successfully return 'a'. Could there be a bug in the ATTEMPT testing? Please advise.
It could, but I checked the PHP version and it seems to be good. My solution passed all tests.
Well, I trust my IDE more than this Kata test so I'm not going to waste my time trying to find an error. Unless you don't mind me sending you my code.
See the issue I created above, the expected and actual value are shown flipped in php.
In python: In the description, the function is named
repeatString
but in the solution box and in test cases, it'srepeat_str
This kata is for the very beginners, and they can be very confused about such things.
True. Also the parameter names in the description do not match the code that is given. That was extra confusing for me.
the description is common to all languages and hence has that typo
but nevertheless, the test cases and solution setup follow snake-case, so nothing drastic needs to be changed
In C,
Either the test crashes or I also get this error:
The expression strcmp(result, "hello hello hello ") == 0 is false.
And when I run it else where it is not return false.
I'm using malloc - is that the problem?
Cheers,
This comment has been hidden.
This comment has been hidden.
it's a matter of memory management. first thing: printf your output before your return statement to see what you're actually returning. this will give you a start on what you need to fix
Factor traslation
Reason translation
Racket translation
approved
Thank you. 谢谢
should test for negative repeat integers being specified Eg:
assertEquals("Hello", repeatStr(-1, "Hello"))
Removed from the sample tests in Java, I think it was the last place where negative numbers remained.
aaaa None should equal 'aaaa'
hello hello hello None should equal 'hello hello hello '
abcabc None should equal 'abcabc'
I got this as my output. Isn't what I have technically correct
You're not returning anything.
(Instead of printing the result you should
return
it in the function)python makes this look like a joke lmao
It's supposed to be super duper easy. ;)
There is a Dart translation waiting. However, it was using an old version of Dart (old translation!), so I forked and updated it.
This comment has been hidden.
it's best to post any code with a spoiler tag.
as for your code, JUST TO BEGIN WITH: think very carefully about the effect of just this one action:
var n = 0;
... oh yeah, and then reread the description carefully and consider what they are asking you to returnYour "solution" has nothing to do with the actual task.
First of all, you are modifying the value of the argument passed to the function, which you shouldn't. With your loop, you will always repeat the string 6 times, no matter the input. Second, the function is supposed to return the new string, but it seems that you've deleted it from the passed arguments.
This comment has been hidden.
Not a kata issue, your function should return the result instead of printing it.
This comment has been hidden.
This comment has been hidden.
yeah for loop is beautiful in Python
This comment has been hidden.
Change language version to 2.4/ES6.
Thanks for sharing!
are there issues with the haskell version? the tests work fine, but when attempting to submit there is an unspeciied error. exit code 1
Not an issue, but question. Haskell version is working fine, or it'd not have almost 600 completions now.
Kotlin Translation
This comment has been hidden.
Not a kata issue, use question when it's a problem with your code. Also read about using proper markdown so your code doesn't lose indentation amongst another things. In CW your functions should return the result, not print it, so store the string in a var and return it at the end instead of calling your function again (that's why it never stops, you're using recursion like that and it never ends).
thank you
Julia translation kumited
This comment has been hidden.
Because here your functions should return, not print the results.
Got it :-) Many thanks
This comment has been hidden.
Not so similar, return inside a loop, exits the loop and the function, it isn't repeating at all.
where can i get some help on the test code?
This comment has been hidden.
This comment has been hidden.
The
result
should be a chunk of allocated memory, not an array.This comment has been hidden.
Don't use global variables ;-)
This comment has been hidden.
Because you're not repeating a string but returning it?
return
will exit the loop at its first iteration. So your code basically does the same asreturn s;
This comment has been hidden.
Not an issue. What's the problem if many language has the exact functionality as built-in?
Refactoring a function by using it in the refactor there is no challenge, what i say...
good exeercise
Unable to submit my code. Not button shown on screen.
same problem, use Ctrl + Enter to submit. Or just restore (un-maximise)
The Scala version of this kata hasn't been implemented...
Looks like a CW bug and not a kata bug. I'll report it to the github repo.
https://github.com/Codewars/codewars.com/issues/1359
;-)
This comment has been hidden.
can I get help with my below question ?
This comment has been hidden.
Don't use echo, use return.
This comment has been hidden.
sizeof(src) doesn't do what you think it does, it's returning the size of the pointer type. You want strlen(src) instead.
Also, malloc isn't required to return initialized memory. It might have junk data in it, if there is junk data then strcat will do strange things and possibly segfault. You should use calloc instead, which returns 0-initialized memory.
Thanks!
This comment has been hidden.
Bash. Told to repeat Exactly n times. Unit tests accept n Or More times.
Fixed
This comment has been hidden.
In the Go translation, the argument
repititions
is spelled incorrectly, it should berepetitions
.Fixed
This comment has been hidden.
Because your function doesn't
return
.This comment has been hidden.
That's Ruby right? I forgot Ruby doesn't need an explicit return line, well, print in Ruby sends whatever you put there to the console, you should read how to repeat a string in Ruby (store that in a var if you want, not required tho). It's pretty easy in Ruby
Duplicate of https://www.codewars.com/kata/repeatit
This comment has been hidden.
The malloc function returns uninitialized memory. If this happens to have a 0 in the first character then it will work, if it doesn't then it won't work. So the same code may work on your computer, but on Codewars will not. You should be using calloc instead which writes 0's to the memory it returns. Unless you're allocating extremely large regions of memory there's no practical reason to use malloc anymore.
This comment has been hidden.
No.
This comment has been hidden.
Expected and actual values are inverted in the tests, you're returning str when n is 0 with your code.
This comment has been hidden.
This comment has been hidden.
This comment has been hidden.
no curly brackets, no print in ruby it's puts(don't need it for this problem)
https://ruby-doc.org/core-2.2.2/Integer.html#method-i-times
This comment has been hidden.
you're not working with java, there... Java initial soluiton is:
C problem. Even though I'm returning the true value and checking it at some other places, in the "attempt" section (first tests all pass), just one of those all tests fail. Just one test fails. And I couldn't find the reason, checking everything, there is no any problem in the return value.
when i took cr_assert() out of the example test, it ran all the tests fine
This is not the only problem with C here. C has problems in this website.
Not a kata issue.
The most probably reason is you segfaulted somewhere and the program crashed.
This comment has been hidden.
way too easy for a 8kyu
Elixir translation here
I did this kata in PHP and it works, but I still get a comment like this: Failed to confirm that '***' matches expected null. Why is this happening?
I'm having the same issue using PHP.
Same here. Run the code elsewhere and it worked as expected.
Edit: I found out the issue: do not use an echo within the function, use return.
I use return but the problem still continues :(
This comment has been hidden.
repeat
torepeat_str
.string
andtime
parameters are the wrong way around in your function. Press reset to reset the function back to its original form.string
is already "string" type, therefore you don't need to cast to a string.Thanksfor the description. . I am a complete newbie
Go Translation submitted as well! Please accept :)
I'm using C language, when click "Attempt", the output is always blank. More over, I'm using night theme, which is dark theme, but the output area is white. Can't see the result.
Yeah im getting the same problem too and im using java language. I guess its just a bug.
Same problem here. Unable to attempt test cases or submit actual test cases. Must be a recursive bug.
Not a kata issue
All tests passed in PHP however on click on Attempt below error is returned
Failed asserting that two strings are equal. Expected: 'b' Actual : ''
In Swift3 ,my code is working in run sample test but when I click on Attempt It shows me given error: "Can't form Range with upperBound < lowerBound". I think it is not working when giving negative value as input.
In PHP, when running the exemple test:
class MyTestCases extends TestCase { public function testThatSomethingShouldHappen() { $this->assertEquals(repeatStr(3, "**"), "****"); $this->assertEquals(repeatStr(2, "@"), "@@"); $this->assertEquals(repeatStr(1, "!"), "!"); } }
It show me this error: Expected: "**********" Actual: "***"
I'm assuming you have something appending $str to $str multiple times. Remember that the second time you do that, $str is now '', resulting in '****' not '*'.
The expected value is '' and the value you have put in is "*******". Wording should be the other way.
This comment has been hidden.
passed all test except the test with negative values.Any suggestions?
These functions are against Swift naming conventions.
func repeatStr(_ string: String, _ n: Int) -> String
. Global functions, the termStr
, blank external labels andn
as an internal label are all against Swift naming guidelines.So you are saying, dont name variables, that are within a def, important names that have other uses? Example dont create a variable named Str, Int, Float etc
A more Swift name would be:
or you could use a method on String
In Java there are tests with negative repeat count, which is undocumented.
Removed
CoffeeScript Translation Kumited! Please Accept :D
Accepted.
TY! :D
Is this Kata broken in JavaScript? The test passes offline, and I log the correct answer to the console, but I get this error when the tests run online: Expected: ''***'', instead got: 'undefined'.
And you are
return
ing, notconsole.log()
in the result, right?Crystal translation done, enjoy!
This comment has been hidden.
Fixed it, felt slightly hacky
Javascript variant of Kata needs either at least 10 distinct fixed tests or some randomly generated tests; 3 fixed tests is not enough.
Test added.
TypeScript-Translation kumited!
https://www.codewars.com/kumite/5819ca315d23581e03001246
Please check and approve!
https://www.codewars.com/kata/reviews/57f78c4be0da82645b00009c/groups/58186d18d14fc3ad04000020 talk about random cases
C translation kumited. :)
yuhu my first Kata with one string code!
Same ! :D
@wichu
,As
@PilgrimShadow
and@AcesOfGlory
have already mentioned, this kata is a duplicate. I'm not sure why you have been making so many simple duplicate katas these days, the only reason I can guess is for easy honor points.Not checking for duplicate katas doesn't really help Codewars with new content.
Yep - checking for dups again.
I appreciate your effort but this issue doesn't seem to be resolved. This kata is still a duplicate...
A kata asking a user to repeat a string already exists.
Just learning Haskell, just learning..
What's with all your small program duplicates recently?
Re-published. Please try again. Thanks, suic
Try again, i dont see 'accept' button.
Is there anything wrong with my Python translation?
Hi, Python translation kumited. If you like it, please approve.