6 kyu
Does my number look big in this?
73,920 of 183,564JulianNicholls
Loading description...
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.
Hi all! I am new to Codewars and this is my first kata. Why does it say this when I run my code: File "tests.py", line 1, in from solution import narcissistic ImportError: cannot import name 'narcissistic' from 'solution' Note: I have not changed the beginning of the code at all
Hi, can you show me the code
When you write the code here, be sure to put a spoiler symbol.
you changed the name of the function you changed the input variable to input you don't need to do that you don't need to call the function the testing environment will do it by itself.
Your solutions does a couple of things which do not conform to the requirements of Codewars solutions: https://docs.codewars.com/training/training-example#writing-a-solution
(shameless plug) I also created a browser extension which is supposed to help newbiew with this kind of problems:
This comment has been hidden.
To put code in the correct format, you use Markdown code blocks.
You changed the name of the function to
narcissistic_number
. It should be callednarcissistic
.Thank you so much!
This comment has been hidden.
read the codewars rules on the link given by hobovsky
@big_M Thank you so much for your suggestions!
Incorrect answer for value=7: expected undefined to equal true. what does it mean for the narcissisticc function
It means that for n=7, your solution returns
undefined
, while it should returntrue
. It's because yournarcissistic
is empty.Hello all! my test case hasn't passed but it runs perfectly in my machine. This is first time I am using Codewars. This is regarding my narcissisticc function solution
If your solution seems to work on your local machine but does not pass tests on Codewars, it usually means that you are testing it on your machine incorrectly.
careful BigInt Translation Dart
This comment has been hidden.
1634 is, in fact, a narcissistic number.
1^4 + 6^4 + 3^4 + 4^4 = 1 + 1296 + 81 + 256 = 1634
.Not a kata issue.
this guide may help:
https://docs.codewars.com/training/troubleshooting
resolved , thanks
This comment has been hidden.
You don't need to write a
main
function, you only need to write the narcissistic function, the tests will take care of the main function for you. Also, you can't usescanf
on codewars because you will never receive any input from stdin, so this will cause your program to just wait around forever (actually, until it times out). Also, it's strange to me that you've called them armstrong numbers in your code. If I view your current C solution, it seems like you've replaced your code with the test code for some reason, I would start over by hitting the 'reset' button at the bottom of the screen. You may want to learn the very basics of your chosen language before attempting a yellow kata as well.ERROR
7 is not a
narcissistic number
. 7 pows 3 is 343 not equal 7.do not raise
issues
to ask for help, ask aquestion
instead. your code only works for numbers with less than 5 digits.Buenas con todos, soy nuevo en esto de codewars espero que alguien me puede ayudar, mi codigo pasa todas las prubas pero me sigue saliendo este error ests/Fixture.cs(96,28): error CS1955: Non-invocable member 'Narcissistic' cannot be used like a method. tests/Fixture.cs(102,28): error CS1955: Non-invocable member 'Narcissistic' cannot be used like a method. tests/Fixture.cs(54,37): error CS1955: Non-invocable member 'Narcissistic' cannot be used like a method. tests/Fixture.cs(77,37): error CS1955: Non-invocable member 'Narcissistic' cannot be used like a method. tests/Fixture.cs(28,37): error CS1955: Non-invocable member 'Narcissistic' cannot be used like a method. Agradeceria ayuda
You changed the solution from:
to:
Now tests cannot find your solution and are confused what is a class, what is a namespace, and what is a method.
This comment has been hidden.
But it is not a problem with the kata, it is a bug in your solution.
Oh, can you please help me where I can change, I use pow() function only which returning incorrect values for larger number
If you figured out that the problem is
pow
(and yes, you are right), then you can create your own function, for integers instead of doubles.it should be at 7 kata, maybe 8
This comment has been hidden.
This comment has been hidden.
This comment has been hidden.
This comment has been hidden.
Bash translation
This comment has been hidden.
This comment has been hidden.
you're printing too much to console, just remove the print statements and it will pass
Ohhh thx~
Groovy Translation
Approved
I have used the type __int 128 which allows you to work whit 128bit integers. In fact without it, the randomtest2failed everytime
Attempted this with Python and it doesn't seem to work with very large numbers. The basic tests all turn out correct, as do most of the random ones. Could this be a problem with the input range?
Your solution is buggy, because
math.pow
loses precision for large numbers. It is not a kata issue.Got it, thanks a lot!
Rust user here. I have a rust solution that works on the basic test cases but gives this error on the complex ones "attempt to multiply with overflow". I have tried using both u64 and i64 types in case some tests were very demanding but i got the same error. Any ideas?
Make sure you use
u64
This comment has been hidden.
This comment has been hidden.
This comment has been hidden.
313 is not 729
Wow, helps if I read the problem thanks!
Nim and R has no random tests, and R sample tests do not have
test_that
.TypeScript tests are not considered random: the order of the answer is almost always fixed (as random tests almost always expects
false
). See thisC++ random tests generates false test cases in a laughably weak way it's too easy to deduce the pattern and pass them. See this
In general, the random tests in all languages need to stop cheating the negative input generation by only choosing a small subset of the full input range. Honestly it's pathetic.
Different language versions have wildly different input ranges, which should be normalized. On top of that, some language version have unacceptable input ranges:
n = 50000
n = 1634
n = 8208
n = 4887
(and only positive cases up to371
)u64
, which is too big for this task, since it can cause calculation overflow in certain input range (e.g17999999999999999999
)115132219018763992565095597973971522401
, which is unnecessaryWow!! so wonderful!
This comment has been hidden.
But your code returns the sum of each digit to the corresponding power.
oh seems like i didnt get it right when i read the challenge but thank u for making it clear also can u pls show me ur soluation? (:
You can see all solutions after you solve the kata, or forfeit your award and unlock solutions.
371 is narcissistic: False should equal True... I get this result when I test. If i change my return False to print(value) the test comes back passed and says 371. I dont get what return false has it do this but print lets it pass
hi im just new in here and dont know anyting about coding i wish my self to have motivation every single day.
Hello. Welcome to CodeWars. You may read about on how to write a solution and attempt each kata starting from the lowest difficulty (whites) A.K.A 8kyu / 7kyu. It is also strongly advisable for you to open the official documentation of your language so that you can utilize existing tools to write your solution as it is the fundamentals of learning a new language ! ^^
Happy coding !
Hey everyone i hope yall are having a good day im quite new to coding only doing a codecademy bootcamp. I understand the c++ language but i do not understand how im supposed to read the coding problem and figure out what to do or start with. Even with codecadmey i had the same problem so i used chat gpt as a point of reference but i feel like thats not really helping me i would skim over the code and start the problem and keep on doing it untill i know how to do it without looking at gpt. I just want to know if it is good practice to use chat gpt as a point of reference or should i just figure it out on myself.
Any help would be nice and appreciated i hope all yall have a good day.
ChatGPT may not be the most optimal tool, yes it can be helpful by understanding simple programs with comments, but you should not solely rely on it. Instead, you should probably read this to understand on how to submit solutions to kata and start off with 8kyus. Also, it is strongly recommended that you keep a tab opened for the official documentation for the language that you are currently learning. Happy coding ! ^^
well put!
Hi
hello
This comment has been hidden.
Print the input to help you debug your code.
This comment has been hidden.
Not a kata issue. Your code is wrong, specifically -->
var countDigits = Math.Floor(Math.Log10(value) + 1);
7 is not a narcissistic number
Yes it is. You're raising the digits to the power of 3, you're supposed to raise the digits to the amount of digits in the input number. So,
7
would be7**1
which is7
because the number7
only has one digit.The full test suite uses 1517841543307505039. It is failing. I am using Swift and when I run this in Xcode it's telling me it's not a narcissistic number. Who is correct here?
This comment has been hidden.
This comment has been hidden.
It seems you changed the name of the solution function? It sohuld be named
narcissistic
, but in your solution it isis_narcissistic_number
.Please mark your post as having spoiler content next time.
A good day. Can anyone help me? My code passed all the tests but it returns an error of DEADLYSIGNAL, tried to look for it online and havent quite got what that entails. Thanks in advance :D
Edit: I found the error and tried to reduce inherited values to a minimum. Remade it from scratch and submitted!
I passed all tests, but cannot attempt. Why?
Okay, so I don't yet know why your code passes the sample tests. Most importantly, your code does not acheive the actual goal of the kata, and so it really shouldn't pass all the sample tests. It's something curious, never seen anyone write what you did... you put an assertion into your function, I have no idea why:
Regardless, you should definitely learn to read your error messages:
Your 1st warning reveals
control reaches end of non-void function
because you do not return a value from your function. You do need to return a Boolean value.Your second warning reveals:
all paths through this function will call itself
because your function contains a call to itself with no exception (exit condition), resulting in infinite recursion.Also, take note that this function receives a value, but your function declaration only shows the type
int
without an actual parameter, which is something you removed for some reason.Hi, my code (C) passes the generic test, but i get the next error in the random test
'should_return_uncheated_narcissistity:' 'Caught unexpected signal: SIGSEGV (11). Invalid memory access.'
This line:
char num_str[num];
is very wrong. If the input number is, let's say, on million - do you need an array of a million characters?Thank you! I'll work with that
This comment has been hidden.
This comment has been hidden.
Please use appropriate markdown formatting when posting code. See https://docs.codewars.com/training/troubleshooting#post-discourse.
1634
is narcissistic. Not a kata issue.im supposed to make a function that returns true or false for the given numbers
Yes your function should return if the value is a narcissitic number of false if it isn't
This comment has been hidden.
"expected false to equal true" what does it mean?
That for some value (print it to help you debug your code) your code returned false instead of true.
Hello. I am new to computer stuff. Actually this is my first attempt to code. I wrote this code in VScode and it's working I guess but I don't know how to make it run in here. Please help me, I need guidance.
Here is the code:
value = int(input("Enter an integer: ")) print(value)
len_value = len(str(value)) int_len_value = int(len_value) sum_power = 0
for exponent in str(value): int_exponent = int(exponent) power = int_exponent ** int_len_value print(power) sum_power += power
print(sum_power)
if sum_power == value: print("True") else: print("False")
You need to write a function which
return
s the result. Printing to console can help with debugging, but anything you print will usually be ignored by the tests.OP solved it, closing
This comment has been hidden.
This comment has been hidden.
This comment has been hidden.
I went down the same rabbit hole. math.pow seems to be using floating point arith and with big numbers the precision on a 64bit system is lower compared to a non floating point calc like pow or **.
OP solved it, closing
This comment has been hidden.
Please read this: https://docs.codewars.com/training/troubleshooting#post-discourse
Your current code works, so you realized your mistake.
I think this was explained very poorly but its a good kata
Why? How would you explain it better?
Guys, please help me, how do I run the code in "different modes" so that first Narcissistic Number, and then not Narcissistic Number?
I'm not sure what you mean by that. You don't need to do anything yourself, the tests will do everything for you. You first
if
statement is meaningless.In your code, you need to initialize variable
i
(it is nowundefined
) and your code will pass.This is the point that it requires a different answer to different tests, it requires true for "Narcissistic numbers" and "Not Narcissistic numbers", but how to do variability
I may have found an error, most likely in my code, but for some reason the code works differently here than in the IDE
I mean, I literally told you where the error is, and you're changing other things... Your changes are OK, but they don't fix the bug. Hint: your loop doesn't run at all.
You don't need to do that. You just have to return true/false, and tests will use this value to verify if it's correct.
I have solved the problem!! In one place, for some reason, my number was translated into an array with strings of numbers, and the stupidest thing was that I forgot to assign a value to "i" in "for" aha-ah-ha
Thanks for the help)
Thumbs up for this one! My solution was correct, byt way to complicated. Really nice to see good solutions by you guys that have mastered the art of programming! Inspiring to see better ways to get the job done! I think way to complicated!
Hi,
I get this error Incorrect 'answer for value=153: expected false to equal true'
But in the sample indicate that 153 is narcissisc, it means true.
I'm missing something??
Your code is returning false for that value when the expected one is true. You're doing a wrong math operation in your code, read the description again.
excellent kata . it helps to read the instructions carefully .
I really enjoyed this problem, genuinley stumped me initially. Thanks for suhc a great problem!
This comment has been hidden.
This comment has been hidden.
Strange with Swift. Tests with very large numbers do not pass. In Xcode, for example, the number 35875699062250035 outputs as false. Moreover, if I do print, then the last received number, which is raised to a power, differs by 2 digits - 35875699062250037 Does anyone know what the problem is?
def narcissistic(value ): l = len(str(value)) num= [] new = 0
Extremely fun :)
I'm stuck on this with Swift. My code passes all tests except for very large numbers apparently:
Full Tests XCTAssertEqual failed: ("true") is not equal to ("false") - Should return true for 35875699062250035 XCTAssertEqual failed: ("true") is not equal to ("false") - Should return true for 4498128791164624869 XCTAssertEqual failed: ("true") is not equal to ("false") - Should return true for 4929273885928088826 XCTAssertEqual failed: ("true") is not equal to ("false") - Should return true for 21897142587612075 XCTAssertEqual failed: ("true") is not equal to ("false") - Should return true for 1517841543307505039 XCTAssertEqual failed: ("true") is not equal to ("false") - Should return true for 35641594208964132 XCTAssertEqual failed: ("true") is not equal to ("false") - Should return true for 3289582984443187032
When I logged my output I saw that I have rounding errors, for example: 9 ^ 19 = 1350851717672992089
But Swift returns 1350851717672992000
Has anyone had the same problem? Any tips?
Have you resolved it? I have the same problem
make sure your types can fit your values
your rounding error probably comes from involving f64, python:
likewise, make sure you use a large enough integer type - for example, 1350851717672992000 barely fits in a 64-bit int but not in a 32-bit int, and if you were to go a bit bigger than that you would overflow the 64-bit int as well
my javascript code passes all tests. But when I press the "Attempt" button, I get a log in which one more test is added, and it is suggested to check the number value=32164049651 But, in the task, it is written that only base 10 numbers will be played.
You have not passed all the tests. Your code is failing on that test, as you have found from the error message. So, you can create a sample tests with the number
32164049651
to debug your code. Why do you think this number is not base 10?I believe that this number is not base 10, because, from the task, I understood that in this case the base is the number of digits in the number. the following examples are given in the task:
@alexfan3 Number base is not the same as amount of digits a number have.
A simple definition of number base is the position of which a certain digit would reset to 0 and add the value of the left digit.
For example you can look at binary (base-2) numbers. Let's take number 4 for example:
Starts at number 0:
0
Then we add 1 (current num: 1):
1
Then we add 1 (current num: 2):
2
->10
(why? because we reset the number to 0 and add value to the left once it hits the number base, which is 2)Then we add 1 (current num: 3):
11
Then we add 1 (current num: 4):
12
->20
->100
So, for base 10, it's the same as the number system that we human use on our daily life. It resets the digit to 0 and adds value to the left digit when we hit 9 + 1.
This comment has been hidden.
バイブ🥀🥀🥀
great kata
This comment has been hidden.
IDK what language you are using, but are you taking a string and seperating the characters into an array of numbers?
If so, then your numbers may be encoded as ascii numbers and are being turned into their integer equivalents, which would explain the constant shift.
With so little information, and no code to read, that is all I can guess about.
Actual and expected are swapped in PHP, also assertion messages are not filled in completely for all test cases
Comment on fixed tests completed, but I don't see how actual and expected are swapped? Here
my js code works fine in my pc, but in my attempts I get 7 is not turning TRUE, 7 shouldn't be consider a narcisitic number acording the definition given but browsing the internet I've just found for some reason all 10 numbers from 0 to 9 are considered narcisitic...
all single digits are narcisitic because anything to the first power is equal to itself or
x^1 = x
7 is narcisitic because of the following.
7 has one digit so I sum that one digit raised to the first power
for comparison
Python Invalid syntax in tests(
File "/workspace/default/tests.py", line 10 test.assert_equals(narcissistic(value): ^ SyntaxError: invalid syntax
The examples 7 and 1654 aren't Narcissistic numbers.
But 7 is a narcissistic number? I also cannot see the 1654 being used as an example anywhere, where do you see it?
7^1 == 7 (power 1, because it has 1 digit), so it is a narcissistic number, and 1654 doesn't exist in any example (not in description, and in sample tests there is only 1634, which is same as 1^4 + 6^4 + 3^4 + 4^4).
Even if you were right, you should provide more information why they aren't, and why this is an issue. Currently it isn't.
well the logic they are using here is that "number of time each digit should be multiplied to itself = number of digits in the input"
This comment has been hidden.
This comment has been hidden.
This comment has been hidden.
This comment has been hidden.
Sample Tests has a message mistake in Line 11:
should be "1633 is not narcissistic"
It's important to name the language: Ruby
Fixed.
This comment has been hidden.
Did it in one line, let's goooooo!
In all seriousness, I think I may have just gotten lucky with this one.
Well, this bs is worst I've ever met. Took me some hours to understand how these numbers works. And after understanding, it took me a minute to make a code. So after that, I've googled and asked gpt where this stuff can be used. Got no adequate answers. Useless thing, tbh.
I'm glad you enjoyed it so much.
well, it doesn't work like that, you are learning, you are training yourself in logic, those hours you have been training to undestand it wouldn't be hours if you would have understood the logic.
This comment has been hidden.
This comment has been hidden.
This comment has been hidden.
This comment has been hidden.
Please read this article, which hopefully explains some things: https://docs.codewars.com/training/training-example
Your solution does not work well when run multiple times in a row, for different inputs.
Your solution does not have a form expected by Codewars tests. It's not a kata issue.
Love the name of this kata
JavaSript:all random tests expect False.
Not true, some random tests expect
true
.42678290603 in vscode false , in this kata my code give answer true...
sorry,I'm idiot. I'm badly read discription
Idk y was i doing so many typecasting :|
This comment has been hidden.
I might have complicated the thing but I could pass the tests. Though the attempt result I'm having is:
Time: 3797ms Exit Code: 137
Could be that it's not well optimized?
Your code is mostly OK.
Please see this paragraph: https://docs.codewars.com/training/troubleshooting#maxbuffer
Thanks mate. I was working the solution in my IDE and I forgot to delete/comment the console outputs.
Hello everyone. I'm really new to codding, been trying to pass this challenge but, altough it gives me the propper output in the console, it seems that i'm doing something wrong, since it does not pass the test. Is it apropriate to post some code here, so i can try to understand what the heck is wrong? Thank you all in advance!
Your solution works incorrectly if you run it twice in a row.
Well, i only need to run it once for it to be incorrect. I i try it on the console, it seems fine. Can't actually grasp how i am suposed to know how to correct my code, if i cannot see the solutions or have some kind of feedback on the code itself.
Do you run it in your console twice?
Thanks for your feedback. I wasn't doing that, and thanks to you i mannaged to understand the problem! Thanks!
Well It may help you can post your code if you check the chechbox mark as having spoiler contet if I am right
This comment has been hidden.
Avoid floats, stick to integers. Floats cannot handle some large numbers with sufficient precision, and at some point will return imprecise results.
Question is, why would you even want to use floats? Python can handle very large numbers out of the box, why would you work around this feature?
Haskell: all random tests expect
False
.Fixed
My code passes all the tests except for these: Random_Number_Test
Test Failed Expected: True But was: False
I decided to log the input, output(my sum answer) and ture or false. Please point out why these should be true:
input 1778653589 output 645006974 False
input 199898839 output 1952354824 False
input 671108264 output 194989385 False
input 793241850 output 564227289 False
Thank you
They shouldn't. Either C# suddenly has a problem after
7 000
submitted solutions ( unlikely ), or you're reading the logs wrong. I'm going with the latter.Please mention which language you're using next time, esp. if a kata has multiple.
Closing.
Thank you for looking into this Johan. Noted: yes its C#, will keep that in mind for future reference. I will continue to look into this and provide feedback
Hi, I got a bit busy with work and life hence the dealy, but I ran my code a couple more times until I found only 6 mismatching results.
I then continued to write a small console application to test against these values.
I also manually checked these 6 values.
And this is what I get:
input: 92727
output: 92727
system result: True
ManualJob:
9^5 + 2^5 + 7^5 + 2^5 + 7^5 59049 + 32 + 16807 + 32 + 16807
= 92727
This is True
input: 187435105
output: 178759414
system result: False
ManualJob:
1^9 + 8^9 + 7^9 + 4^9 + 3^9 + 5^9 + 1^9 + 0^9 + 5^9 1 + 134 217 728 + 40 353 607 + 262 144 + 19 683 + + 1 953 125 + 1 + 0 + 1 953 125
= 178 759 414
This is False
input: 24678050
output: 24678050
system result: True
ManualJob:
2^8 + 4^8 + 6^8 + 7^8 + 8^8 + 0^8 + 5^8 + 0^8 256 + 65 536 + 1 679 616 + 5 764 801 + 16 777 216 + 0 + 390 625 + 0
= 24 678 050
This is True
input: 980376545
output: 576257597
system result: False
ManualJob:
9^9 + 8^9 + 0^9 + 3^9 + 7^9 + 6^9 + 5^9 + 4^9 + 5^9 387 420 489 + 134 217 728 + 0 + 19 683 + 40 353 614 + 10 077 696 + 1 953 125 + 262 144 + 1 953 125
= 576 257 604
This is False
input: 1741725
output: 1741725
system result: True
ManualJob:
1^7 + 7^7 + 4^7 + 1^7 + 7^7 + 2^7 + 5^7 1 + 823 543 + 16 384 + 1 + 823 543 + 128 + 78 125
= 1 741 725
This is True
input: 24678051
output: 24678051
system result: True
ManualJob:
2^8 + 4^8 + 6^8 + 7^8 + 8^8 + 0^8 + 5^8 + 1^8 256 + 65 536 + 1 679 616 + 5 764 801 + 16 777 216 + 0 + 390 625 + 1
= 24 678 051
This is True
I'm pretty sure I'm not reading the logs wrong :)
That's what happens when you don't add logs at the beginning, but put them somewhere in the middle. If you put it on first line, you would notice that your code fails for number 0.
This comment has been hidden.
Here are my results. 1 and 2 pass, but where do 3 (43994) and 4 (10162087) even come from? They are not in the sample test (only 7 and 371). Thanks.
Test Results: ~ Narcissistic Function
Im not sure about the format. I inputted working code. I have the program running in Visual Studio. But the "TEST" says it's wrong.
Read this: https://docs.codewars.com/training/troubleshooting and I see a php solution published in javascript.
Thank you
I have the same problem. Mine works on Code Pen but fails the test. Maybe it's because of type conversions, but it wasn't stipulated that we don't do so.
This comment has been hidden.
Please state the language when reporting an issue, this kata has many.
C#, here are few more examples:
Not a kata issue, you're not printing the input always, so, you're confusing the log of one test with the expected answer of a different one. 0 is a Narcissitic number and your code says it isn't.
This comment has been hidden.
Hi @ddm104 and welcome to Codewars!
On the site, you need to return your answer rather than
print()
it. So replace yourprint(...)
stuff withreturn ...
.In this kata, you are asked to return simply
True
orFalse
- you don't need to create the stringstr(x)+" "+"is narcissistic"
etc. Just return a boolean value accordingly - using the variable names from your solution, you should justreturn c
as far as I can tell from reading it.Here are some useful links as you get started on the site:
https://docs.codewars.com/getting-started/solving-kata/
https://docs.codewars.com/training/troubleshooting/
You can also drop by the Codewars Discord - there is a #beginners channel if you want quick replies and help on your first katas. Good luck and see you around!
hi thanks for the help i understand better now :))
I couldn't understand the "True" or "False" part..can anyone please explain me what it is. Thanks in advance
Hi @AgentVenom123 - this kata asks you to return either
True
orFalse
for a given input number. So the True or False part is telling you what you should return from your function.You must return
True
if the input number has the Narcissistic property described in the description - so for example, for153 -> 1^3 + 5^3 + 3^3 = 1 + 125 + 27 = 153
you should returnTrue
.Return
False
if the input number does not have the property.it means return the booleans True or Flase and not the strings 'True' or 'False'
This comment has been hidden.
Should be a 7 or 8kyu kata
Ranks can't be changed especially for old kata like this one
This problem is very similar to "Take a Number And Sum Its Digits Raised To The Consecutive Powers And ....¡Eureka!!"
Too easy WITH changing the value to str. Is there any way finding sollution without changing the data type?
Yes, refer to solution in statistically-typed languages like C
what in randomized_test_2? all test i passed except this.
I just lost. I don't know what should i do there is any place for finding solution?
To forfeit the kata, your level should be at least 1 level below the kata's one. So, keep solving other katas until you reach 7kyu and then come back. Check this out: https://docs.codewars.com/getting-started/solving-kata#unlocking-solutions
quite a variety of solutions, very nice
In C#, the testing is too bad, damn, it made me nervous
Agree, my code works well when tested locally and returns the expected results, but when trying to run the tests on the website I get errors for using the methods like Math.Pow() and Int32.Parse().
This comment has been hidden.
This kata is not meant to be hard and establish solving barriers for warriors! Also, this will invalidate 99.99% of solutions, which is not feasible.
This should be KATA 8, too ez;
How is 7 narcissitic by this definition?, and my code runs well on other editors but im also having errors here
raised to the power of the number of digits in a given base
7^1 = 7 ... 7 in base 10 has 1 digit, so all single digit numbers are "narcissitic"
I have a problem, I wrote the program and it works perfect on my machine with the right output, but when I put it in your IDE I get an eoferror, I know that it's a problem with the input() function, how can I solve this problem?
Don't use the input function. The input function is used to get input from the user. On codewars, your function is called directly from the testing suite. So you don't need to call
narcissistic(x)
. The tests are callingnarcissistic(x)
for you. You also need to return the correct value to the function that called your function, printing the answer doesn't count.Thank you, I thought I needed to enter the values, this is my first time on codewars
don't request user input, just write the code
Loved the challenge, spent half the day on it only to realize my solution could be fitted in 1-3 lines. sadge
We've all been there.
I'm completely lost. How is it that 5**10 is 5? To be a narcissitic number, it should be 5, but my result is 9765625. Where am I getting lost? Can someone guide me?
You take the number and its exponent is the number of digits in said number. That is why any single digit is a Narcisitic number. 5 is 1 digit, therefore 5**1 = 5.
Go translation
D translation
.
This kata is a subject to deduplication process here: https://github.com/codewars/content-issues/issues/102.
Please join the discussion to share your opinions, and help us identify duplicate kata and retire them.
This kata was decided to stay.
my code was not a one line code at first. Then i shrinked it into a one line code to look cooler. I dont think it looks good but this was the first and probabaly last oppurtunity to do it.
Update to Scala 3
Approved
In my opinion this kata could be 8 kyu or 7, as the solution is almost straightforward.
Changing ranks is expensive, not worth it, and not going to happen. Just enjoy the cheap points.
Moreover, consider ranks are shared among languages and can vary a lot depending on each one.
is it necessary to use the function?
Yes. This is what tests always do, on every kata: they control that the function you are given in the trainer returns the right answer for different inputs.
Lua translation!
Approved
Factor translation
Approved
This comment has been hidden.
A problem with your code is not a kata issue, read this: https://docs.codewars.com/training/troubleshooting/
This comment has been hidden.
You're not returning true or false, which is very explicit in the description!
This comment has been hidden.
awesome kata!
in the spirit of diversifying solutions, a nice amendment to the description would be challenging folks to implement the function without converting
value
to a string 😁That's essentially a whole different question at that point.
@Rlaur2 how so? it would be great if you could explain your thinking instead of providing a baseless response. furthermore, simply adding "challenge: complete this kata without converting
value
to a string" to the end of the description would do the trick. anybody who wants to can, and those who don't can just ignore it.i also took a peek at your solution and as i suspected, you solved this kata by converting
value
to a string. i originally completed this kata in typescript, but i completed it in javascript just to show you how it can be done. feel free to take a look if you aren't already familiar with the implementation.This would be a different kata, it's hard do it well, and probably nearly impossible to do in several languages. This is an old kata that has been solved about 100k times, it is not going to be changed now.
Edit (I realize I misread you)
Suggestions of challenges that are not enforced don't make much sense; several have been removed in some old katas, with reason, IMO. So, no. If someone wants to challenge himself, he's still free to do it.
This comment has been hidden.
Don't post solutions in Discourse. Solutions are on the Solutions page for who wants to see them.
Is it cheating to make a list of narcissistic numbers and check if the input is in the list?
No.
i think it defeats the purpose of the kata. you arent writing a code that does the job, you're just predefining the answers to a situation.
non ho capito come risolverlo
This comment has been hidden.
You function must return value. In your code you must return number == value
it doesn't return a value
This comment has been hidden.
I really enjoyed this Kata. While being my first one here it showed me what to expect. Solved with Python
if your results are matching with the test but it is still not letting you pass, consider changing "True" to 1 and "False to 0
Fine Kata, i think it plase - 7 kyu
C#: All tests passing but the random number one? Can we get more details on whats in the random numbers? can these include negatives?
From the description:
The test are givin me 122 in the second test when it should be 371
The test for both javascript and Python are fine, maybe you edited them? Click reset.
Nim: no sample tests
Added
COBOL translation.
approved
am i Stupid, or is 7 not a narcissistic number? the test case say that it should output true, but 7^3 is definitely not 7....
How many digits has 7? It's not 3.
man i should read the question, thanks!
This comment has been hidden.
371 is narcissistic.
The test is failing on a case that should have been successful.
371 is narcissistic: expected false to equal true
language: JavaScript
I think you misunderstand the log. It tells you you return false, while it expects true (so false [your answer] should equal true).
it happens the same with my code in python. I run the same code in Sublime Text3 and the return ouput is True, but here it appears to be False
Have a look there: https://docs.codewars.com/training/troubleshooting/
Closing the question because there is no question: tests in JS expect true for 371, which is correct.
Good kata, atleast i could understand what i need to do.
Really appreciate this Kata for C!
This comment has been hidden.
None of these qualify as kata issues, rather a question. Python has 27k+ completions, so there's no way it's wrong.
TypeScript translation.
Approved (forgot to resolve the other day
I get this eror: "src/Solution.cs(11,36): error CS0103: The name 'Math' does not exist in the current context"
but I think c# has Math fuction. and I use "Math.Pow(x, y)" methode.
try to use "^" sign insted of Math.Pow in C#
You forgot
using System;
to useMath.Pow
^
is the operator for a bitwise XOR. In fact, C# doesn't have an operator for exponentiation :)This comment has been hidden.
You need to return True or False, not the strings 'true' or 'false'.
I have updated the description to make this even clearer than before.
Oh, thanks, my bad, I didn't even try to return the bool.
The description of what a narcissistic number is is wrong. It says the digits are to be raised to the power of the digits in a given base, and goes on to explain that the base is 10. So, power of 10? You should just copy the definition from Wikipedia - "the sum of its own digits each raised to the power of the number of digits" (in the number, not in the base).
You misunderstood what's written there, read it again:
From wikipedia:
See the examples. 153 in base 10, has 3 digits.
If you remove the long distracting parenthesis from the Wikipedia definition, the sentence reads
In the description of this kata, the in a given base is misplaced (is at the end of the sentence) and changes the meaning.
You're right, the description is clear.
.
Enjoyed this one! Amusing name, too.
Liked this kata because of the awesome name hahahh.
what's the problem please !!!
main.cpp:9:5: error: redefinition of 'main' int main(int, const char *[]) { ^ ./solution.cpp:30:5: note: previous definition is here int main(int, const char *[]) { ^ 1 error generated.
You should not provide
main
function, it's already there in tests. You just need to fill in the function stub provided in initial setup.thank you so much bro
For C++, using #include<math.h> lets us use the pow() function.
First of all, for C++ it should be
cmath
, and notmath.h
.Now, what exactly is your suggestion? Users can include
cmath
if they want to, I am not exactly sure what kind of an improvement to the kata you suggest?no need restriction of
pow
functions as this is neither code golf nor implementation-wise katasThis comment has been hidden.
And what is your question?
This comment has been hidden.
This comment has been hidden.
And yet it is.
yeah, I found out after I posted here, but thanks anyway!
This comment has been hidden.
Not a kata suggestion ~~
Language: Ruby I am not being able to add the numbers after i did x.to_i**power to get their power. I split them with the use of to_s and split function and mapped through items and to_i each item and **power. Here, ppower is the length of the number when I converted it into string. Can someone help ??
Paste your code here with markdown code formatting and spoiler flag.
In C++ version set with clang 8 compiler (default option), the compiler will not recognize the function
std::pow
...so even though my solution is correct, it generates a run-time error and cannot pass. The Kata description does not prohibit use of the standard library, so it appears to be an issue with how the#include
s are set in the test runner containingmain
.EDIT/UPDATE: I was able to resolve this issue via
#include <math.h>
in my solution code outside of / immediately preceding my solution inside ofnarcissistic
, i.e.,:I generally prefer not to modify code outside of the solution function's scope when doing coding challenges (which is pretty standard practice on these types of platforms), but leaving this comment here for future reference in case anybody else encounters this for the Kata (and presumably in others). Therefore, be advised that the Kata does not use something like
<bits/stdc++.h>
in the test runner, but rather requires explicit#include
s for specific dependencies.Also, you should better use
<cmath>
instead of<math.h>
in C++Whoops good call, always mix those up--thanks for that clarification! ...also, brain is fried currently (clearly), that would be a compile-time error, not a run-time error
I checked the solution setup and test cases. They're fine. On this website you have to add includes you need by yourself. Also, the whole solution text field is under your control. You can edit it how you wish.
This comment has been hidden.
its not a kata issue. this approach is considered a dishonest cheat and users abusing it get banned.
This comment has been hidden.
This comment has been hidden.
You need to return the result inside the function given in the initial solution (when u entered the trainer) and no need to ask users for their input, this is not a coding project ~~ read this for more info
I've solved it, but c++ compiler on my pc doesn't report any problems and i passed all tests successfully. However, when i upload my code into the website, i don't pass final 2 tests..Why?? (i rebuilt my code so that it had 1 function called narcissistic(int x))
Read this, please: https://github.com/codewars/codewars.com/wiki/Troubleshooting-your-solution
Issue with your code is not a kata issue, you've run into an infinite loop at somewhere in your code
Guys. I wrote and came up with a solution in 20 minutes. I accidentally closed the code and I can't remember the solution for the second day (((((((
This comment has been hidden.
This comment has been hidden.
0 means 'false'
Mmmm... I knew it was equivalent but I didn't interpret it correctly in that context. Thanks!
This comment has been hidden.
I believe you changed the type of your
narcissistic
You probably changed it tonarcissistic :: Int -> Bool
It should benarcissistic :: Integral n => n -> Bool
You can also get away withnarcissistic :: Integer -> Bool
since that's the only input type being specified in the tests.Do not change the type signature in the initial solution.
Closing.
This comment has been hidden.
This comment has been hidden.
Hi, You need to determine if the sum of the digits is a perfect number. To do this, cut out the value then iterate over it. Calculate the sum of the digits raised to the requested power. You get a number that is or is not a narcissistic number (or a plus perfect number).
Thanks
Notem que a potência é fixa (número de dígitos), não me atentei a isso e criei um loop desnecessário dentro de outro para fazer para todas as potências. fazendo com que o tempo excedesse.
This comment has been hidden.
That's a very interesting observation. I'll change it.
I write with JS. I can loop and test for math all work in editor but not here; and i also confuse the question, am i only need to output TRUE or FALSE ? OR what. plz help me explain this :<
Isn't that in the instructions? Is your code returning or just printing it? Read this: https://github.com/codewars/codewars.com/wiki/Troubleshooting-your-solution
It is stated in description "Error checking for text strings or other invalid inputs is not required, only valid positive non-zero integers will be passed into the function." However I'm getting 0 in random tests for C#.
Same here, seems like some error that consistently generates 0 in multiple random tests and expects both true or false in different iterations of them. if (value == 0) return false expects true and if (value == 0) return true expects false in those random tests
Should be fixed
This comment has been hidden.
If you post your solution and mark it as a spoiler, we can look to see why you're timing out. The code pasted here doesn't look quite right.
This comment has been hidden.
This comment has been hidden.
You are mot returning whether the total is equal to the initial value, you are returning whether it is greater than or equal, which is not correct.
Oh I see, I need to stay more composed while understanding the problem. Thankx @JulianNicholls
This comment has been hidden.
For example, in C# you have type BigInteger.
He said the number must be in the 10 base
That was the hardest kata I have solved to date. Unbelievably challenging. I loved it.
this its very close, to take a few more minutes for solve, thanks!
When you think you have done a good job figuring it out for 30m then you submit the kata and see the 1 line of code guy.
feelsbadman t.t
Just keep practicing! you'll do those one liners too;) just dont give up:))
It's still a good job!
HELP ME WITH THIS ERROR PLEASE!!!
main.cpp:64:5: error: conflicting types for 'main' int main(int, const char *[]) { ^ main.cpp:12:5: note: previous definition is here int main() ^ 1 error generated.
MY CODE WORKS FINE ON MY EDITOR .BUT DOESNT GET ACCEPTED WHEN TRYING TO TEST IT HERE.WHAT TO DO?
Not a kata issue, but problem with your code. Closing...
Did you define 'main' function twice??
If you are writing a function you have to name the arguments, if your are declaring a function, there is no need for this. So this would mean you have to write i.e.: int main(int argc, char *argv[]){
}
371 it's not a narcis no? because 3^1+7^1+1^1=10
371 has 3 digits, read the instructions again (and 3 + 7 + 1 is 11 btw).
3^3+7^3+1^3=371
Great Kata!
Hmm. For c# I finished but am getting an arithmetic overflow exception. Not totally sure since I'm just a beginner.
This comment has been hidden.
Don't post solutions in Discourse, it's forbidden.
This comment has been hidden.
Only quantum computers can do that or i'm so stupid
youre not stupid just takes a bit of logic ill give you a hint pow()
C++ "randomized_test_2" generates numbers with leading zeroes from time to time, is that right?
What number is being tested inside of the "randomized_test_2" ? I am failing that edge case.
Not a kata issue:
About what number it is, as the name suggests, it's random, so print it yourself to the console to check what it is.
Hi Guys, I'm trying the run the solution but getting this error - The name 'Math' does not exist in the current context? How could I add the required namespace in the CodeWars IDE? Thanks
Not a kata issue, a
Question
(it is a problem with your code after all). Tryusing System;
.I tried adding the required namespaces - using System; using System.Linq; Still getting the same error. Any idea?
It worked for me. Post your code here using markdown formatting and mark your post as having spoiler content.
This comment has been hidden.
It doesn't throw that error for me, but it doesn't work either, there is something wrong with your code.
Sorted buddy. The namespace was not added properly. Thanks for your reply.
This comment has been hidden.
Next time, please, mark your post as having spoiler content. Read the error message, it even shows the line with the error.
This comment has been hidden.
It's not specified if
0
is a valid input. If it is, there should be tests with it.It is now specified it isn't. Silly, but unambiguous.
2^3+3^3+7^3 = 378 but 378 isn't Narcissistic number. Why? explain pls
378 != 3^3 + 7^3 + 8^3 => 378 != (27 + 343 512)
in your comment you considered the number 237, not 378
thank you
:)
Did not realise we have to include the header files in the editor. Also can anyone tell me why #include<bits/stdc++.h> is not working in this platform? For this reason it was showing invalid identifier when I used pow function.
bits/stdc++.h
is not part of the standard, don't use it. See https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-hThanks for the info
This comment has been hidden.
This comment has been hidden.
Are you calling that function from inside
narcissistic
function?This comment has been hidden.
You need to return the function call to
narc
insidenarcissistic
. If there is no explicit return in your function, the default returned value isNone
. To increase the readability of comments or descriptions, see this page in the github wiki.Thanks Chrono79!
In c++ if you solve the problem using a vector, you have to clear the vector on every test run
Scala translation available for review.
"alr approved some time ago"
Message in third test case for Ruby is wrong:
That code looks like an old version. It was updated to use random values for the non-narcissistic numbers several months ago.
I feel I'm missing something here. 371 is suppose to return true but: 371 3^1 + 7^1 + 1^1 3 + 7 + 1
I get 11...
Finally got it, I was misunderstanding the meaning of Narcissistic number. In the example above it should be: 371 - Exponent here is 3 because there is a total of 3 digits so... 3^3 + 7^3 + 1^1 do the math... you'll get 371 and it matches...
Starter function definition code in Python uses "// Code away" as a comment, but that will give a syntax error if left there. Python comments are "#Code away".
Fixed
This comment has been hidden.
Use spoiler flag next time and read the instructions again:
in Python it should be
True
orFalse
This comment has been hidden.
My code works and passes tests in other editors, but not here. Please see the repl.it link in my code.
Your
result
array is global so it's being shared by multiple test cases. Move it inside the function and that should pass all tests.On the c# version of this kata, I would highly recommend making a minor edit of the argument variable from "value" to "n", "number", or even "val". Value is a built in property in C# (which exists for pretty much any non-enumerable objects), so in general it's not great practice to use that as a variable name due to potential conflicts. In the case of this kata, it does not create conflicts, but this might give newer programmers that don't know any better misleading ideas for variable naming. This is not an urgent fix by any means, but it's definitely a quick and easy one to change if you get the chance to.
Edit: I'm referring not to the test case, but rather the default code structure shown in the "solution" window.
I also noticed in the test case that your numbers are not entirely random. You have a fixed array of integers and randomly select from that list. Instead, I would recommend using rnd.Next(1, x) (x being the max number you want to test) so that it's actually random. In order to generate the entire random test case, you can use linq to populate the container with the following:
using System.Linq; using System.Collections.Generic;
private static int[] nNums = new int[50];
[Test, Description("Random Tests")] private void RandomTests() { Random rnd = new Random(); var randomNums = nNums.Select(x => rnd.Next(1, 999999)); foreach(var n in randomNums) { Assert.AreEqual(n, Kata.Narcissistic(n)); } }
You can use any maximum number or number of tests (in this case I used a max of '999999' and 50 total tests). I also used a similar structure and variable names to make it a bit easier to potentially make the changes. This just ensures the tests themselves are actually random and not just selecting randomly from an array of fixed numbers.
Please, NASM Translation
Please, C Translation
Approved
can negative numbers be considered as narcissists? How do we calculate -153?
I'm thinking (-1)^3 + (-5)^3 + (-3)^3 = -1 + (-125) + (-27) = -153 So I guess only negative numbers with an odd number of digits can be narcissists.
Should change Kata instrunctions, since you have to return a boolean, instead of a number. This is preventing me from completing the kata.
How much clearer than 'you must return true or false' can I be? I even bolded true or false a while ago so that people couldn't miss it.
The JavaScript sample test cases are overly lenient; they allow the function to return any truthy value.
This is because the tests are using
Test.expect
, which is also an issue (they should be replaced byTest.assertEquals
).If it was still possible to edit it, I would have done this a long time ago.
Sample tests were never locked and from some time ago now, you can also edit the real tests too, why don't you give it a try?
Hey, you're right :-)
I have updated the Javascript and CoffeScript tests to use assertEquals.
The Ruby test fixture doesn't seem to have an assertEquals, which is probably why I used expect for JS abd CS in the first place, because I wrote the Ruby version first.
For Ruby, use
Test.assert_equals
Done.
According to Wikipedia, the sequence of increasing narcissistic numbers varies by the base used. In base two, the only narcissistic numbers are zero and 1. As another example, in base ten "six" is a narcissistic number, but in base five, "six" is not a narcissistic number. I didn't see a base specified in the posing of this question. Consequently, I find the question ill-posed.
Duly noted. I have updated the description.
Hi, I published a C++ translation for this Kata.
Thank you.