4 kyu
Vigenère Cipher Helper
3,407 of 8,424jacobb
Loading description...
Algorithms
Ciphers
Security
Object-oriented Programming
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.
The test for c.encode("it's a shift cipher!") is broken. Actual value for assertion should be "xt'k s ovzii cahdsi!" in Java.
No:
It's been already discussed 4 years ago: https://www.codewars.com/kata/52d1bd3694d26f8d6e0000d3/discuss#6050b777d2342a000e60697f
'yiuzsrzheb' should equal 'yiuzsrzhot' i dont get it tf is going on?
Print the input, and check why your code is returning a wrong answer.
I just had an error and I don't know how to fix it.
Testing with katakana alphabet Password of 'カタカナ' 'タコタナ' should equal 'タモタワ'
The kata itself was fun to solve. However, I believe that requirements like capability to handle non-sequential alphabets (as katakana from the tests where next character is not always previous + 1) shall be explicit in the description. It is easy to solve but extremely frustrating to figure out. At least it shall be included as an example or a public test case. Otherwise, this becomes "guess what I wanted" type.
It's there because I find that it's reflective of real-world scope considerations where shortcuts that don't follow the original definition are liable to fail, particularly when working with Unicode. Would you be able to provide feedback on the definition specifically?
Edit to add, I think that working with date/time formats is the best example of what you're describing--while Unicode encoding variants are pretty much across the board, you-need-to-assume a Unicode encoding instead of say ASCII, Latin-1, or what-have-you these days.
This comment has been hidden.
It was fun!
But it's kind of daunting to spend an hour and see that the answer in the solutions is so compact while yours is so long :p
How do you even come up with those super smart solutions ?
Practice and also being able to review and understand other people's solutions. The less compact solutions aren't necessarily better or worse--they provide more detail into the underlying logical structure of the solution, whereas the more compact solutions take advantage of abstractions of the underlying logical structure of the solution--which may or may not perform better.
This kata is not correct. There is an error in the test: Time: 724ms Passed: 1 Failed: 0 Test Results: Log: rovwsoiv CODEWARS
//This implementation correctly encrypts and decrypts messages using the Vigenère cipher based on the key and alphabet provided.
How does the above mean that there is an error in tests?
From the description:
expected 'wafflesw' to equal 'waffles', is this correct?, getting this error on the test cases
The tests (at least in javascript) are fine.
Chai + assert for CoffeeScript
This also adds Katakana random tests since the only issue I could find with them was Python 2.x specific?
I think, this kata is 5 kyu (or even 6)
not a suggestion (ranks aren't changed anymore)
Kata tests broken.
No, they aren't broken:
Your code is wrong, read the description again.
You are right, I'm not very bright today. My apologies
Scala translation
TypeScript translation (literal translation of JavaScript)
This kata is broken. When I log decoded message before returning I get correct string 'codewars', but when I run a test I get an error:
expected 'orrvnqc' to equal 'CODEWARS'
The kata is fine, read the description again, specially at the end. The alphabet is not case-insensitive.
This comment has been hidden.
Yep, they should fix it so it doesn't work
Hi guys,
This kata have a problem:
Server Execution Error: Test class name must end with 'Test'. Request Error: Request failed with status code 422 Please fix the issue and try again.
Updated to PHP 8.0, try again.
I belive there is an error in the test cases for JavaScript. My code passes the first test but fails on the second test with a password of pizza. My code gives the correct letters up untill the last 2. I verified the correct answer with a cypeher solver online as well.
That's a problem with your code, not a kata issue. The tests are fine. And your code fails some sample tests too.
2 random test cases only show failed
Hi,
Not an issue.
Which ones? inputs? Expected output? your output?
Most likely your code fails only in rare edge cases that are not present in the fixed tests.
Closing
What is difference between Laint and katakana alphabet?
It works fine on Latin alphabet so I dont have idea why it doesn't work on katakana
Katakana uses multi-byte characters in UTF-8.
Some of the tests seem broken, all of them pass but a few
Python 3.11
Quoted from 3 months ago: 1`I don't know whether there is an issue with this Kata or an issue with my code but for the Python solution I get the correct answer when I run my code in a different program but when I copy the same code into Codewars, it says I got the answer wrong.
Examples are: "xt'k o hwixl qzewej!" should equal "xt'k o vwixl qzswej!" "it's a ghift cidher!" should equal "it's a shift cipher!"`
"I don't know whether there is an issue with this Kata or an issue with my code" is exactly opposite of the "ISSUE" flag. Why marking a post with ISSUE if it's not known if there is an issue?
And it's difficult to tell what is the problem with your code if we do not know your code.
It's a problem with your code, it doesn't work with chars that aren't in the alphabet. Try in your own IDE with sentences, not single words, for
"hello world"
your code returnsweddk world
. Not a kata issue.It ran the test c.decode(c.encode('token')) and the expected output was token, the password was 'attackatdawn' and the alphabet was normal. I ran my code in pycharm and it worked and got the right output 'token' but the kata said that an error occurred. List index out of range.
PHP Sample Tests are wrong!
My code is case sensitive, but I get:
testThatVigenèreCipherlsCaseSensitive
Failed asserting that two strings are identical
Expected: CODEWARS
Actual: ROVWSOIV
All other tests are passed.
Read the description again:
The tests are fine.
Thank you for the propmpt response
How are we suppose to handle katakana (Japanese) alphabet? All my tests passed except with katakana alphabet.
A bit tricky, but the Description contains link to Wikipedia, where you can even find JavaScript or Ruby code for the solution.
Forked ruby translation, including new test framework, Ruby 3.0 and a cleaner description -- please check and approve!
i do not know why i keep getting an error eventhough i managed to solve it on my machine. it's just resulting to another word.
My solution passed tests but when comes to test in submit my code during encode goes index out of range at line 46, care somebody to explain me why? I feel hopeless and I dont know what is wrong
Hi @Your1Chloe - as the error message states, you have an IndexError, which as you say yourself means that your index goes out of range, and Python tells you it's happening at line 46.
Since this is a 4 kyu kata it's intended that you should troubleshoot a bit, otherwise it's too easy ;)
But to give you a little hint/general advice for debugging: while your code isn't finished, try separating all the variables you use - this allows you to print each one individually to console, which in turn allows you to pinpoint where the error is coming from.
Specifically, in your code:
instead of trying to debug stuff like
self.alphabet.find(self.key[i])
(which appears inside yourdecode
function, after thefor i in range (...)
stuff) you should create a variable called for examplesk = self.key[i]
and then right after,print(sk)
.This will already give you a much more targeted error message which explains what is going wrong (think about what happens when the length of the
key
is shorter than the length oftext
).many thanks I will go get some sleep and then will edit my code even tho I few times had to debug I was lost here but now it seems more obvious once again thanks
it was very easy, this should be 8 kata
If you like harder kata's, try this one
python fork with new test framework
Approved, thanks!
Description is misleading. It says
Assume the key is repeated for the length of the text, character by character.
. They are not, don't assume that. YOU need to make them the same length.You misterpreted the description. The key IS repeated throughout the message, but you don't need to make them the same length. A little bit of modulo math.. et voíla!
Hi - my code is passing all tests except the two javascript ones with the 'pizza' key. -- 'yiuzsrzheb' should equal 'yiuzsrzhot' 'javascrizl' should equal 'javascript' -- any ideas what my issue could be?
I see there's a similar issue with a jupyter notebook problem below...nevermind. Thanks
Did you solve your problem? I have the same issue.
Yes, got it to work. If I remember correctly it was something to do with "Check if your solution passes two successive calls with different inputs. It's sometimes the case that the solution contains some global variables or static members and a stale state is carried over from one invocation to another." from https://docs.codewars.com/training/troubleshooting/ but not 100% sure
This comment has been hidden.
Not too difficult, maybe not a 4 kyu, but I really enjoyed the kata nontheless. Nice one :D
I liked this kata. She pushed me to study classes and several other interesting methods to solve it. Although I spent 1-2 hours on it, it seemed to me easy for 4kyu
Getting Test class name must end with 'Test'. Even with default code where I didn't change anything.
After removing character è from the test class name - tests are running and are green. But pressing button
Attempt
again shows theGetting Test class name must end with 'Test'.
error.PHP 7.4
You could try using 7.0 until it is fixed.
oh, thanks. With 7.0 it works.
All my English encodings are correct. And Half of my Japanese encodings are correct. How in the world? Why am I missing four random Japanese words? Could someone review?
Python tests are fine:
A problem with your code is not a kata issue, please, read this: https://docs.codewars.com/training/troubleshooting
This comment has been hidden.
I thought the same. But eventually solved it!
Everything works in English, passes tests. In katakana, it encodes and decodes to the original! Works amazing, but it won't pass the "test" for katakana. What's up with that? Can someone pass my code?
Have you tried using regexes? That is what I attempted... and yep, if my regex says work with a-z, then it will work well in English. And won't do anything in katakana.
do noy hard code the value of alphabet as 26, instead use the length method for your preferred programming language
Before php 7.4 % works bad with negative numbers. PHP 8.0 server throws 422
Nice Kata, but a little easy for 4KYU, imo. It almost requires nothing but simple lang. features.
Almost pass all cases except katakana
I don't know whether there is an issue with this Kata or an issue with my code but for the Python solution I get the correct answer when I run my code in a different program but when I copy the same code into Codewars, it says I got the answer wrong.
Examples are: "xt'k o hwixl qzewej!" should equal "xt'k o vwixl qzswej!" "it's a ghift cidher!" should equal "it's a shift cipher!"
Probably a problem with your code, read this: https://docs.codewars.com/training/troubleshooting
A
all tests passed except this thit Password of 'カタカナ' expected 'タコタナ' to equal 'タモタワ' what the heck is the shit . I tried just return the str it shows Password of 'カタカナ' expected 'カタカナ' to equal 'タモタワ' is that means PW is カタカナ and is should decode or encode カタカナ to タモタワ? and btw should I do it in the katakana to tamotawa way i mean k convert to t something like that or do it in the Japanese char list way ? coz theres no way Japanese used the abc just like English right? im just simply adding the index of PW str am I doing it wrong ? since others all passed I just cant figure it out
Yes, you can log stuff to console to see this better.
It should be handled the same way, I think.
I am not sure if I understand you, but I believe this is your problem. The katakana character set used by this kata is not a consistent sequence of codepoints, and you need to use actual definition of the alphabet to check what a character shifted for
x
positions is. You cannot just convert a charater to its code point, addx
to it, and convert it back to a character - this wont work. For the Latin alfabet, it's true that'c' + 3 == 'f'
, but it's a coincidence which does not hapen for the katakana alphabet in this kata.ahh thx dude ,this shit had drive me .I was so stupid and thought there were only 26 chars in the abc, so I just typed number 26 in my code ,after seeing ur solution I changed my 26 to abc.longth and it passed .:)
thx man for your reply, I was stupid too with this 26 symbols
This comment has been hidden.
there is no need to get katakana alphabet set if u do the others proply the katakana one should be ok check the rest code if theres some mistake
This comment has been hidden.
Read the description again:
It IS a problem with your code, not a kata issue. Use
Question
label next time.Read the error message, it clearly points to where the problem is.
That will give you a similar error.
Hi i have problem with 'pizza' - 'javascript' in python, all test passed but javascript no. Mess. - Traceback (most recent call last): File "tests.py", line 48, in test.assert_equals(c.encode('javascript'), 'javascript') File "/workspace/default/solution.py", line 11, in encode zipped = zip([self.alphabet.index(i) if i.isalpha() else i for i in text], File "/workspace/default/solution.py", line 11, in zipped = zip([self.alphabet.index(i) if i.isalpha() else i for i in text], ValueError: substring not found Could anyone advise me where the problem is? In jupyter 'javascript' with key = "pizza" are 'yiuzsrzhot'. Thanks
Not a kata issue, that error comes from your code. Read this: https://docs.codewars.com/training/troubleshooting
i get the same but in my ide it still works
No random tests for php.
Are all the python tests correct? I managed to get all tests right, but 4, where the text was with letters from the katana alphabet. I tried to figure out my mistake, but I ended up with a suspicion that there must be a letter missing from the end of the alphabet or something... Can you confirm that they are all correct? Thanks in advance!
They are correct, I just submitted a Python solution without any issues. Btw my solution does not bother at all with UTF.
This comment has been hidden.
All Python tests are correct.
Hint: Vigenère encryption/decryption was invented way before ASCII :-)
This comment has been hidden.
This comment has been hidden.
Vigenere Code ELI5 from reddit
https://www.reddit.com/r/explainlikeimfive/comments/1bhg7n/eli5_vigenere_code/
dont really understand the description shift is derived by applying a Caesar shift to a character with the corresponding index of the key in the alphabet.
my secret code i want to secure" // message "passwordpasswordpasswordpasswor" // key
Anyone have any gentle pointers as to why I'm getting IndexErrors for the first two katakana test cases? I've fixed the obvious things, like making sure that the alphabet is correctly ordered and not hard-coding the zero point for the alphabet to be 97. Thanks in advance
Are you accounting for the fact that the characters are multibyte?
There are no versions of this available for versions of Node greater than 10. Node v10 has been EOL for over a year.
Node v18 is enabled now. Closing.
kind of easy for a 4kyu if you understand how the cipher works still, quite an interesting kata that taught a new cipher!
i agree. compared to other 4 kyus i didnt take quite a long time to
I was struggling to find out why katana alphabet didn't work. Then I realized that I hard coded 26 as length of alphabet xD
Great kata!!
Hello! Python 3.8 have a problem with: "it's a shift cipher!". My result is "xt'k s ovzii cahdsi!", but in test it's should equal "xt'k o vwixl qzswej!" I've used online calcs to check my variant (for exhample https://planetcalc.com/2468/). Is that issue, or is it only my mistake? Thanks in advance!
If someone will have this problem: the KEY-counter does not include characters outside the alphabet. For exhample you can check it with pencil and paper.
This comment has been hidden.
This was an absolute blast to figure out. I sent the description and Wiki article to a friend who said, and I quote, "my eyes are glossing over, what am I reading".
That being said, it took me like 3 days of work because I wrote the encoding function first, it passed the test suites, and then I realized different alphabets were a requirement. As usual, since I've only been learning Python for a little over a month, my solution is messy as can be.
Excellent kata!
I have to say that it's an easy solution for a problem that seems complex when written on paper... nice kata! thanks for the katakana at the end to mess with some beginner's assumptions
This comment has been hidden.
I had that same issue : help : alphabet length ;-)
Thx buddy, just now looked it again and finally got it... damn I feel stupid again =P...
doe anyone know why am i getting this return in Js?
c.decode("rovwsoiv") "codewars"
c.encode("codewars") "codewarsrovwsoiv"
It's like it's concatenating the previous answers given...
This comment has been hidden.
solution
is never being reset, so it'll keep adding values every time you call your class methods.yup, i noticed that..
Then why you don't make it local to your methods instead and initialize it there? Or why don't you use a string?
This comment has been hidden.
This comment has been hidden.
It's a problem with your code, and unless we see it, we can't help you.
I run my code in my spyder Idle and it encodes "it's a shift cipher!" and decodes "xt'k o vwixl qzswej!" correctly but in here I got the message says "xt'k o hwixl qzewej!" should equal "xt'k o vwixl qzswej!" "it's a ghift cidher!" should equal "it's a shift cipher!" I wonder what's going on. This is from my spyder idle's results "xt'k o vwixl qzswej!""it's a shift cipher!"
Interesting Kata!
There is an issue with one of the tests: Cipher is "xt'k o vwixl qzswej!", key is "password". It says expected output should be: "it's a shift cipher!" but I think the real cipher should be: "xt'k s ovzii cahdsi!" to make output looks like expected. At least my code and criptii site (https://cryptii.com/pipes/vigenere-cipher) says so. Please, correct me if I'm wrong.
criptii says:
xt'k o vwixl qzswej!
->it's w ziruw qhaaqs!
(notxt'k s ovzii cahdsi!
), but criptii uses a different algorithm.This topic saved my day!!!
katakana alphabet? seriously? that's why I like this site
This comment has been hidden.
This comment has been hidden.
I don't understand why I get this error. All the tests were passed, even the ones with the Japanese alphabet. But I get a "list index out of range error" in the test with the Japanese alphabet. How is that possible? How can the test be successfull if I get that error? Besides, I tested the same code on vsc and I get no error with both latin and japanese alphabets.
how to handle katakana strings?? Anyhelp?
There's no really special handling needed actually. As long as you do not couple your solution with some specific alphabet (like Latin), then any alphabet can be handled by generic solution, if you appropriately implement operations like 'get num value of a char' and 'get character X positions away from Y, wrapping if necessary'.
Thanks.
Learn a lot from tackling with katakana alphabet Nice kata!
This comment has been hidden.
In JS, kata uses following set of characters for alphabet:
var abc = 'アイウエオァィゥェォカキクケコサシスセソタチツッテトナニヌネノハヒフヘホマミムメモヤャユュヨョラリルレロワヲンー';
Was that needed?(I just completed this kata after seeing the comment on dashboard) [Python]
Thank you, hobovsky.
This comment has been hidden.
Not an issue. Your solution is wrong.
Okay, is there a reason that I'm not able to reproduce the failing test cases? edit: nevermind. I figured out why it must be wrong.
I'm writing in python 3.6.0 and I have a strange problem. When I press attempt button, in Password of 'password' I have some wrong characters in the last case but when I added same tests in example tests everything is right.
Not an issue. If your code is failing, it is wrong.
Hello! PHP 7.4 produces this error:
I selected PHP 7.4 because I used an arrow function. When I selected PHP 7.0, all the tests turned green.
Fixed. Please reset or rename the sample test's class name to end with
Test
.This was a really interesting kata, I just translated it into C++. The way C++ handles Unicode made it a bit more challenging than Javascript (more fun).
'asodavwt' should equal 'pancakes'
key: pizza
That's all I'm failing on in python. Looking at online encoders/decoders I see asodavwt equals lkpeagou, which is what I get in my code. Only when I change to the variant beaufort cipher do I get "pancakes".
you can try yourself here https://cryptii.com/pipes/vigenere-cipher
I wrote using python 3.6.0.. When running the attempts, I got the wrong answers when password is 'password' and 'pizza' although when running it in JupyterNotebook, I got the correct answers.
Code in the comments.
This comment has been hidden.
Apparently it changed my key so that its pattern is different from the one I wrote in JupyterNotebook. E.g. 'pizzapizza' became 'pizzapizpi'. And I don't know why it is like that.
Your code is mutating the class key in each test, your code only works ok in Jupyter because you're running a test at a time.
Answered and I see you've passed the kata, don't forget to close the Question.
Thank you so much!
Same here although i don't change key anywhere in code it's 'pizzapizpi'. Only in this case rest tests are fine
Even made specific function to adjust passwords still test mess it up it works for first 2 cases in pizza just not "javascript"
Just encountered an issue with test #3 + #4 in PHP, it expects "pncakes" which is wrong, it should be expecting "pancakes"
This comment has been hidden.
PHP Sample Tests seem to be broken - expected values are missing some letters.
Example:
While sample tests are written as:
It could be that whoever did the PHP translation may have accidentally transposed the expected/actual arguments in the test cases.
Fixed
I'm a bit at a loss. I'm passing most tests (19), but get strange error in the pizza section. I fail when the tests ask to encode 'asodavwt' expecting the result pancakes. However encoding this string produces 'pncakes' and the test fails
Is this a problem with my code or is there currently a slight typo in the tests?
The tests in javascript are ok and 'asodavwt' should return 'pancakes'. Encoding or decoding can't change the amount of letters. It seems it's a problem with your code.
Thank You, have to get back to it then :)
This comment has been hidden.
This was one of the best katas I have encountered, it went smooth.
This was... easy...
Java translation with full random tests.
Approved it myself.
This comment has been hidden.
It's not a kata issue, it's a problem with your code, use
Question
label next time:Tried your code in repl.it and it's returning wrong results. If it helps to debug your code, try this in the sample tests:
I dont understand what you mean I have been trying really hard to solve this one
You have a 26 in your code, where does it come from? Don't hardcode that value.
I really liked to solve this kata. I pass all tests here except "pizza" with "javascript". But on my console the same code returns the right expectations "javascript => yiuzsrzhot". So is there something wrong with the test? It will be very frustating for other users to solve the kata and not get rewarded.
Well, there's already 1000+ JS completions, so it's very likely a problem with your code =/ Try debugging it with
console.log()
s or post your code here and mark it as spoiler, so someone could help you.Ok, sorry... that was my fault. Don´t know why I don´t get the I idea to use console.log in the browser... just on my console. Maybe to less coffee :) Thx
You solved it?
This comment has been hidden.
This comment has been hidden.
Damn Python 2.7 encodings...
I have nearly got cancer because of that -_-
Did you get it solved without writing special cases for it? I'm stuck on this katakana but I don't really want to write a special case for it.
There is no need to write special cases. You should remember though about 'decode("utf-8")' and 'encode("utf-8")' methods. UTF-8 is your friend and make sure that you make use of its help at every point.
This comment has been hidden.
Sorry for the trouble, in description it was asked to create VigenèreCipher Class but in tests it needed VigenereCipher, But It would be a lot better if it threw a class not found error instead of this vague one.
It's interesting. I used the e-grave when I originally implemented this in Javascript because I thought it was interesting that Javascript supported that. I guess whoever implemented the python version didn't clarify that their implementation didn't use e-grave. What do you think the best wording would be to the description to clarify that?
The problem is that the python description uses a screenshot of some other language which accepts VigenèreCipher.
Really like this one! It's have a perfect balance between complexity and simplicity. My only suggestion is maybe make description is more explicit? I was need to read the Wikipedia before start because I don't understand how cypher work. Or maybe it's just me.
I'm looking atht explination and just don't understand how it works. I'm going to have to study the wiki for this one as well.
Solved for everything except the katakana cases (Python)
I have trouble with the katakana alphabet... everything else works fine and I have no Idea why that is not working. My first Idea was that one katakana letter is made out of more than one char as python sees it... pls help
When key ='pizza', both the running sample test and Chrome passed, but attempt failed. What happened?
I translated it for Kotlin ! Can you validate it?
Can we open this to Python 3? Sigh
Reminder: Python 2 will be retired in 2 months, see: https://pythonclock.org/ So it is completely reasonable to not only open for but switch all katas to Python 3.
I have decoded , encoded and have passed some tests.Why is there such a mistake? test.assert_equals(c.encode('\xe3\x82\xab\xe3\x82\xbf\xe3\x82\xab\xe3\x83\x8a'), '\xe3\x82\xbf\xe3\x83\xa2\xe3\x82\xbf\xe3\x83\xaf') File "/home/codewarrior/solution.py", line 27, in encode num=new_dict[self.key[i]]%26+new_dict[text[i]] KeyError: '\xe3'
Would this kata be opened for contributors? I was hoping to translate to swift, and kotlin.
I am trying to figure out why my encoding is wrong with 'it's a shift cipher!' my output is = "xt'k s ovzii cahdsi!", but the test says as follows:
````xt'k s ovzii cahdsi!" should equal "xt'k o vwixl qzswej!"```
I ran the same string here https://www.dcode.fr/vigenere-cipher, and gave the same output like mine.
xt'k s ovzii cahdsi!"
If we follow rules for encoding letter a from plaintext to letter s from key, should be encoded to s. The next letter s from plaintext to letter w from key, should be encoded to o. If I keep going by encoding with paper vigenere table my encoding is correct, so not sure what am I doing wrong.
Usually it's different specs on what to do when encountering symbols outside of the alphabet.
This comment has been hidden.
I have a bit of a problem, and I'm scratcching my head a little here. My code passes most of the test cases, inluding the Unicode. Howeer, there's a test case where I don't think I agree with the test:
The alphabet is abc...xyz, the key is 'password'. The string to encode is "xt'k s ovzii cahdsi!" and the test asserts it should encode to "xt'k o vwixl qzswej!".
Looking at the first character, frankly, I am struggling to see how "x" + "p" encodes to "x". Am I missing something blindingly obvious?
You're misinterpreting the test output. Hint: "x" + "p" => "i"
Ah, blindingly obvious, indeed.
Solved?
Solved indeed, thank you.
All tests passed, but I'm still having the unicode error. What about migrating to python3.6 ?
Oh my god! I don't believe i have finally beat damn katakana. Don't forget to decode everything connected with katakana characters and encode your output.
I'm still stuck there... I did encode the input but I already get an error there..
This comment has been hidden.
My program has 22 lines and when submitting I get this.
File "main.py", line 39 SyntaxError: Non-ASCII character '\xe3' in file main.py on line 39, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details
So, appearently there's something wrong about the backend testing script.Same here. According to the link given by the error the author should just add someting like
# -*- coding: utf-8 -*-
on the first or second line. I've tried to add this at the beggining of my code, it doesn't work. Too bad 'cause I've spent some time in it and I can't get my points. Hope it'll be fixed soon.After some tests, adding
in test cases throws the same error. Simply adding
# -*- coding: utf-8 -*-
on the first line of the test cases fixed this.If someone could edit to add that snippet it would be great :)
Fixed :)
thanks !
I'm getting the opposite error:
output.encode('utf-8')
returns a Decode error:UnicodeDecodeError: 'ascii' codec can't decode byte 0xe3 in position 0: ordinal not in range(128)
I decode all my utf-8 inputs, but it's failing on the last encode.
(using python 2.7)
Check closely all the places where you're decoding/encoding strings. It could be that you've already encoded that
output
string back to utf-8 elsewhere, which would explain why string's bytes are not in ascii range. In other words, you might be trying to encode the same string twice.All of these superfluous encoding/decoding issues could be get rid of if the tests themselves simply declared these strings as unicode in the first place.
I did, still no dice... May just have to give up on this one.
Why is Python 3.x not available for this one? At least the supplied strings should be unicode e.g. prefix them with
u
in the tests, like this:u'カタカナ'
. That will make it a 4-character string instead of 12-character.Updated the example test cases for all languages, increased the number of random tests (where possible), and fixed up the description a little.
This comment has been hidden.
Look like smth is broken. I'm receiving next error:
it's a shift cipher! <- sequence to be ciphered "xt'k o hhaxp txpzwn!" should equal "xt'k o vwixl qzswej!" <- error message
The problem is that my code returns "xt'k o vwixl qzswej!" in my local IDE..
ok, python. So that's not it...
Check that you do not alter the key during the executions because it's reused from one test to the other without being changed.
Yes, it's pyton. Sorry for not fully described inputs ;) Actually I'm checking the size of key during every encryption/decryption and fit it to the length of text to be ciphered.
I am not sure if it is just me, but the PHP template was missing the fact that the key and alfabeth came through a constructor that was not mention at all. When I got that in place it all worked a lot better.
Should be part of the start template.
In katakana alphabet test, in python the sentence key.decode("utf-8") raise the error: UnicodeEncodeError: 'ascii' codec can't encode characters in position 5-8: ordinal not in range(128)
*sigh* I thought I got rid of the encoding problems. Could you paste your solution here (surrounded by a line of three backticks (```) and marked as "having spoiler content")?
This comment has been hidden.
This comment has been hidden.
This kata uses Python 2.7.
'some text'.decode('utf-8')
returns aunicode
object. When you try to print that Python tries to encode it in ASCII. If thatunicode
object contains symbols that cannot be encoded in ASCII, Python raises aUnicodeEncodeError: 'ascii' codec can't encode characters in position 0-32: ordinal not in range(128)
.Tip: For printing values while debugging you might want to consider using
repr
.There is one test that caused all this grief in Python:
I tested all solutions with votes on "Best Practices" or "Clever". All of them failed the final test, except one which handled this test as a special case (if x: return y). This special case handling is also done in all of the newest 50+ solutions.
The most vexing part is that you can copy the tests into the sample tests of the kata trainer, click "Run Sample Tests" and all tests pass. You can also click "Validate Solution" in the Kata Creator and all tests pass. But when you click on "Attempt" in the kata trainer, that one problematic test fails.
With some monkey patching I could confirm that in the sample tests
'ドモアリガトゴザイマス'
is passed (correctly) asbut on an Attempt it is passed as
I have no clue what's going on. To fix this kata I have replaced all literal katagana strings with manually UTF-8 encoded strings. I tested some of the accepted solutions and now they pass again.
Are you sure that your changes are taken in account? Because seeing the number of completions, you shouldnt have been able to mosify the tests, right?
Did you try to fork the python translation (if it exists...)? Because if I remember well, it worked for another kata that had too much completions (not sure, but it worths the try...)
I don't know the rules for when modification is disabled. But I am sure that my changes are applied, I can see them when I click on "Show Kata Test Cases". And the accepted solutions that previously failed are now passing.
No, I did not fork the translation. The problem only occurred on an "Attempt", not on "Run Sample Tests" and not in the "Kata Creator". I had no other choice than to perform open heart surgery and edit the kata directly, or I wouldn't have seen if my changes worked.
'just tried it again: it works like a charm!! \o/ (maybe power users can now edit old katas too??)
The katana alphabet makes it impossible to do it in python
Fixed. Thanks for reporting.
No, it is not fixed!
This kata has been solved 231 times in Python. There was a problem (see my comment above) but I fixed it and now all solutions pass again. I checked a few of these solutions, they look OK and do handle the katakana alphabet.
Did you read the note in the description (
The Python implementation presently requires Unicode conversion, i.e. input.decode('utf-8'), output.encode('utf-8')
)? If you still think that there's a bug, please post your solution (surrounded by a line with three backticks ```) and mark is as "having spoiler content".Tests are set up improperly in PHP.
By printing out the start states for the tests I can recreate them in my preview tests and pass them all, but I fail on the main:
Example: First test key: password alphabet: abcdefghijklmnopqrstuvwxyz string: codewars Should expect: acosaecs Real Test: Failed asserting that two strings are equal. Expected: 'acosaecs' Actual : 'rovwsoiv' even though prints of my return value show I'm returning acosaecs not rovwsoiv.
Seems as though you've hardcoded the response....
As far as I know "expected" and "actual" are switched in the PHP test cases. That means, that the test cases expect 'rovwsoiv' (which is indeed the correct encryption), while you provide it with 'acosaecs'. I have seen this issue in multiple katas so far, it might be an issue of PHPtest, but i can't say that for sure.
I believe it might be a good idea to update the Python version of this Kata to Python 3 in order to ease the Unicode handling.
To everyone who has seen the issues with Python 2.7.6 and the Katakana tests:
I am not the original author, but my rank allowed me to attempt to edit the Kata. When I did so, the test cases look absolutely correct.
I ran the original author's solution against those cases, and they all pass without any sort of hack.
I copied in my own solution code, without the hack for test # 20 that allowed me to submit my solution ... and it passed the tests.
It seems as though there's a difference when the test cases are run in the Kata authoring editor, versus running them in the training editor.
I will report this to the admins.
You need to choose the correct language version there before running the tests.
I did. Python 2.7.6 was selected, both in the authoring interface, and in the training interface, when doing the above tests.
I just re-ran them again, modifying test cases and my solution code, to ensure that I was testing what I thought I was: Solution code that validates in the authoring interface, fails in the training interface.
I can confirm the difference between the Kata Trainer and the Kata Creator. Please let us know if you ever find out what's causing it. I'm very curious. But for now I replaced all literal katagana strings with manually utf-8 encoded strings and that issue is fixed.
Remove python as a language choice or remove the failing python test. Really lame to leave broken for a language you have listed.
Hello, when trying to pass the Katakana tests, I have an error on the fourth or fith test. However, the output of my code is
ドモアリガトゴザイマス
(which looks valid) and the expected string isドモアリガトゴザイマ���
which seems invalid to me. Am I missing something?Same issue found. I think you are right. Invalid test case it is.
There was a bug and its cause is unclear. I fixed it by rewriting the tests.
Rewrote my Python solution in Javascript and worked without drama. Still unclear what the issue is with the Python version, but I recommend solving it in a different language.
Katakana tests for python still seem to be messed up.
Agreed. There are comparatively few python solutions, maybe that's why.
bruuhh same :/ the test suite is messed up
I seem to be in the same hole everyone else is: failing on test 20 after lots of python 2.7 unicode jiu jitsu.
'\xe3\x83\x89\xe3\x83\xa2\xe3\x82\xa2\xe3\x83\xaa\xe3\x82\xac\xe3\x83\x88\xe3\x82\xb4\xe3\x82\xb6\xe3\x82\xa4\xe3\x83\x9e\xe3\x82\xb9' should equal '\xe3\x83\x89\xe3\x83\xa2\xe3\x82\xa2\xe3\x83\xaa\xe3\x82\xac\xe3\x83\x88\xe3\x82\xb4\xe3\x82\xb6\xe3\x82\xa4\xe3\x83\x9e\xef\xbf\xbd\xef\xbf\xbd\xef\xbf\xbd'
I can't tell if other people got past this or how to push on. Is it just me?
Same hole..... And i am now hoping that you were able to go past it haha... If yes, hints would be appreciated ..
Hi Gwiji,
When I realized this is a problem specific to unicode on python 2.7, I made a note to myself to appreciate python 3.5 more and moved on. 206 python solutions have been achieved, but there are lots of questions about the validity of the tests. I suggest testing your solution using a python 2.7 environment and a more accessible alphabet like Greek or French if you have more spine than me.
Harry
Same error here, please fix the test if possible.
I figured out it's okay to have a print in the solution, which then shows up nicely as a log window in the test run. For this test we are supposed to decode:
ド オ カ セ ガ ヨ ゴ ザ キ ア ニ
The backshift is:
10 20 10 26 10 20 10 26 10 20 10
Which decodes to:
ド モ ア リ ガ ト ゴ ザ イ マ ス
Which is what I have. The solution the test wants appears to be longer, which doesn't make sense to me because the code and decode have the same number of characters. Unless I'm missing something, there's a problem with the test.
I got the same error, and got sucked in it for a long time. What does \xef\xbf\xbd mean?
I'm pretty ignorant about unicode, so I tried a little Stackoverflow R&D.
"The leading \x escape sequence means the next two characters are interpreted as hex digits for the character code"
Source: http://stackoverflow.com/questions/2672326/what-does-a-leading-x-mean-in-a-python-string-xaa
Using \x doesn't explicitly tell you the encoding, where \u does.
Source: http://stackoverflow.com/questions/33415388/whats-the-difference-between-hex-code-x-and-unicode-u-chars
Naively trying '\xef\xbf\xbd' using the default utf-8 in the python3 repl, I get '�'. Hmm. That's all I've got.
When I first fell into the testing hole there were 206 python solutions. Now there are 209. Someone must have figured it out.
This comment has been hidden.
Same goes for PHP:
Log Key: password Alphabet: abcdefghijklmnopqrstuvwxyz String to encode: CODEWARS Encoded string: ROVWSOIV
✘ Failed asserting that two strings are equal. Expected: 'ROVWSOIV' Actual : 'CODEWARS'
So:
What can I say, as a person who translated that, it was working for sure (it's completed 200+ times in Python). So something changed in meantime and there is no possibility to fix it. @jacobb, do you have such possibility?
Thanks for responding @adam-tokarski. It's a nice kata. In the spirit of learning I'll try rewriting my python solution in javascript--which I suck at--and see how that goes.
The issues are now properly described in the description (although the unicode tests in python can still cause a headache...)
I think there's something wrong with the final test (the Japanese one) for Python.
'\xe3\x83\x89\xe3\x83\xa2\xe3\x82\xa2\xe3\x83\xaa\xe3\x82\xac\xe3\x83\x88\xe3\x82\xb4\xe3\x82\xb6\xe3\x82\xa4\xe3\x83\x9e\xef\xbf\xbd\xef\xbf\xbd\xef\xbf\xbd'
- 39 bytes\ufffd
, unicode's "replacement character"!Please fix the test / explain why I'm wrong... I want to complete this kata!
Hey, just wondering whether I'm misunderstanding something or there's an error in the Python test cases? I keep passing all the tests but getting this issue:
Traceback: in in encode ValueError: "'" is not in list
I messed around with my own test cases and found I got the same error when I set up the password key as "'password'" instead of "password". So I used key.strip("'") to get rid of the ' but I'm still encountering the error.
Man why don't you just change the testing suite for Python?
Great Kata but very frustrating to get through it and get stopped by utf-8 encoding bugs in the tests.
The utf-8 issue is now properly described in the description, along with the solution.
(Python 2.7) I can't get one of the Katana tests to work. My output makes sense, however, the desired output from the test is requesting a longer string than the input.
Exactly what I found. Bummer
I'm having a problem that I don't understand. After wrangling with the unicode conversion for a while, I have results that are mostly right, but a couple of characters are off. '\xe3\x82\xbf\xe3\x82\xb3\xe3\x82\xbf\xe3\x82\xa2' should equal '\xe3\x82\xbf\xe3\x83\xa2\xe3\x82\xbf\xe3\x83\xaf'. Specifically, what should be \x83 is being encoded as \x82, even though there is a properly encoded \x82 character. The majority of the characters being correct makes me think my logic is correct, but I'm still encountering a unicode bug somwhere. Any insights would be greatly appreciated!
This comment has been hidden.
keep passing 20 only failing at one, I think my code is good and the problem may be in the test cases I could just write into it if this line "???? then this line ??? but I dont want to half ass it can someone check there code that has passed it and get back to me I think it is the 21 subission yests case that has error In python I keep forgetting each languages has differenct code for test cases, see if your python code is working if it has worked befor thanks in advance
As
dimmk0
andgabbek
explain in the Questions section, the last test is failing despite having the correct answer.I seem to be having the same issue, get the error:
'\xe3\x83\x89\xe3\x83\xa2\xe3\x82\xa2\xe3\x83\xaa\xe3\x82\xac\xe3\x83\x88\xe3\x82\xb4\xe3\x82\xb6\xe3\x82\xa4\xe3\x83\x9e\xe3\x82\xb9' should equal '\xe3\x83\x89\xe3\x83\xa2\xe3\x82\xa2\xe3\x83\xaa\xe3\x82\xac\xe3\x83\x88\xe3\x82\xb4\xe3\x82\xb6\xe3\x82\xa4\xe3\x83\x9e\xef\xbf\xbd\xef\xbf\xbd\xef\xbf\xbd'
If I'm doing something wrong, I'd like to work it out. But it looks like the test may be incorrect?
Can someone see if there is a problem here?
It's definitely a typo in the test. A little debugging shows that tests come in pairs - 1st for encode(), 2nd for decode(). Here are my logs for the 2 tests. As you can see, everything works just as expected, bt as sztamas mentioned, decoding test expects ��� instead of the last character. Can only wait for resolving this issue
Upd: passed the last test with a placeholder. Will remove that from the solution as soon as the test gets fixed
Fixed.
I write Python first and fail a lot of times, I spent lot time to debug it and copy raw code in local to test it. I think of the last test case have encode error. I am confused why 100+ pass Python version? Are they mapping its wrong answer specially? Anyway I give up Python and choose JavaScript to pass it to check those test case.
I remember that some time ago there was a diffrent version of test-suite - without this katakana alphabet.
Hi, one test with katakana is failed. I have add some debug lines to trace, and what I see? Test string is successfully decoded! But test failed for some reason. Whats wrong?
String to encode : ドモアリガトゴザイマス, length: 33 encoded : ドオカセガヨゴザキアニ, length 33
✔ Test Passed
String to decode : ドオカセガヨゴザキアニ, length 33 decoded : ドモアリガトゴザイマス, length 33
✘ '\xe3\x83\x89\xe3\x83\xa2\xe3\x82\xa2\xe3\x83\xaa\xe3\x82\xac\xe3\x83\x88\xe3\x82\xb4\xe3\x82\xb6\xe3\x82\xa4\xe3\x83\x9e\xe3\x82\xb9' should equal '\xe3\x83\x89\xe3\x83\xa2\xe3\x82\xa2\xe3\x83\xaa\xe3\x82\xac\xe3\x83\x88\xe3\x82\xb4\xe3\x82\xb6\xe3\x82\xa4\xe3\x83\x9e\xef\xbf\xbd\xef\xbf\xbd\xef\xbf\xbd'
You're not alone, I have the very same issue :) In earlier test you encode it to this string, while decoding result is of length 39 instead of 33, which is odd - I guess we'll have to wait for someone more knowledgeable. Edit: It seems that most of solutions have the exact same problem (I've checked 10+ and all of them had this issue); something most likely happened to the last test as your answer is indeed correct :)
I have this exact issue :(. Although, because I'm checking my length after the decoding the
len()
result gives me 11 for all of them. I wish this was fixed. Would love to complete this kata.I've got the same problem. Which is frustrating since I was proud that I'd figured out encoding for the katakana with python.
Same here for me…
+1
Same problem here. Following kata author's "hint" from six months ago re: using decode on all string inputs, and encode on all outputs, but still failing this one test. Debugged by printing out the lengths of the input/output both before and after encode/decode, and it all looks good to me ...
+1
+1 It really looks like the expected string is wrong in the test!
actual: ドモアリガトゴザイマス expected: ドモアリガトゴザイマ���
Would be great if someone could fix this.
+1
PHP Translation Kumited - please accept :D
donaldsebleung or stɛffan153: could you fix the swap of actual and expected in PHP translation?
'\xe3\x82\xab\xe3\x82\xbf\xe3\x82\xab\xe3\x83\x8a' should equal '\xe3\x82\xbf\xe3\x83\xa2\xe3\x82\xbf\xe3\x83\xaf' what is this problem encoding issue or bug? For me this is python nightmare kata. To much encoding jokes.
It might be helpful to print the input after decoding, and also printing the output before encoding to make visual inspection easier.
Also there's no spec in the description describing what we should do if the key is not in ascii.
The number of shift is determined by the position of the key in the input alphabet. I first thought it's determined by its distance to ascii 'a'. Then I thought it's determined by the unicode distance to the first character in the alphabet. I think it should be put in the description.
Instructions are still not mentioning which Unicode encoding to expect. (It seems to be UTF-8.)
Well, I don't know if this was in response to your post, but I put "i.e." followed by the actual code you would use and the context of input and output. I'm hoping that's clear enough. If not, let me know.
I am still very confused by this, having tried many combinations of str.encode and str.decode, I am still getting UnicodeEncodeError and UnicodeDecodeError. All the tests with just ASCII characters pass for me.
This comment has been hidden.
2 minutes to write the code. 2 hours to solve the unicode conversion. upgrade to python 3 please :(
The encryption was quite easy and straightforward but the decryption was a bit tricky ... anyway, great Kata, loved it <3 :D
man, seriously what's wrong with you... no mention about the unicode part.... totally wrong description!
Last line in the kata description
In Javascript and Coffeescript, where Unicode support is built in by default, it's expected to be supported as a best practice. Python does not use Unicode by default. For that reason, it's cited in the description.
Python - my code works in the initial tests, but on hitting submit:
Testing with lowercase latin alphabet Password of 'password' Traceback: in in encode NameError: global name 'key' is not defined
Are the tests initiating the test data correctly?
Yea, they're initiating correctly. My guess is that you're trying to refer to a global variable instead of encapsulating the key and alphabet in the constructor.
This comment has been hidden.
Same underlying concept, but it's a different implementation. The behavior of what to do when encountering characters not in the alphabet isn't prescribed formally in the definition of a Vigenere cipher. Any way I can make the description more clear?
The description (now) is clear about the cycling of the password.
If you want to make a kata about unicode conversion, then make one that is about unicode conversion. Or at least have the decency to mention it in the instructions. A**hole
You're entirely right. I have updated the description. This kata was originally implemented in Javascript, which doesn't have nearly the same character encoding issues Python has with Unicode. However, the Unicode tests were there to encourage the generalization of the concept of alphabets. I've written some Python code back in the day, but never anything that would have needed to be Unicode aware until recently. Someone introduced a translation, it looked okay based on my past experience with Python, which wasn't enough experience, so I approved this. Usually kata go through a user-testing process that would weed this kind of issue out. This wasn't done when kata translations were introduced. I contacted info@codewars.com about this issue (and mentioned this kata specifically) about two weeks ago. I haven't received a response. I apologize for approving the Python translation.
Meanwhile, you there jhoffner?
Sorry for the language was very unhappy at the time of the comment, shouldv'e taken a walk around the block and then commented.
Kata does not specify that unicode data will be passed. Specifying in description that input is utf-8 encoded bytes, and that decryption should be performed per decoded symbol and not per byte, would be helpful. =)
jacobb upgraded description about unicode inputs.
katakana why you hate me so much D:
How come 'ilxfpwv' should equal 'waffles' with the key being 'password'? (Python)
I have the same problem. A lot of people are having trouble with unicode support, but this is the third test and the alphabet and key do not require unicode support.Can anyone spare a hint?
I'm thinking it could be a wording issue with the test cases, but "ilxfpwv" is incorrect, and it's looking for waffles. Due to the third and fourth letters being the identical in the key ("password") and identical in the word (independently), the third and fourth letters of the solution should also be identical (also indepedently).
Yea, when I wrote this I hadn't realized, that it was not trying to get 'ilxfpwv' encoded into 'waffles'. It was actually telling me that instead of getting 'waffles' my code was yielding 'ilxfpwv'. silly mistake :D
I try to transfer code to "unicode" by: key = key.decode("utf-8") the result is UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-3: ordinal not in range(128)
But it work well on my computer. with python 2.7. Does anyone know why?
The problem is somewhere else.
But after I comment this line, There is no UnicodeEncodeError.
Problem happens on katakana And I use chardet.detect to detect the code infomartion, the result is 'utf-8' So I think decode("utf-8") is correct?
I decided to spend some more time researching this today rather than remaining oblivious. The test cases expect default-type strings for I/O. The kata editor input, meanwhile, is in utf8. I.e. you need to convert all strings from ASCII to unicode and back. You can totally blame me for approving the kata translation. Meanwhile, I decided to investigate reconciling this, but ran into some issues (not limited to test cases being locked), so I've contacted Codewars about that.
I try to convert all strings to unicode by using str.decode("utf-8") but it fails.
I was wondering how you convert katakana?
I think there may be some weird string issues with Codewars' implementation of Python, and I'm still trying to figure out what those issues are. I will give out that in the solution, all strings provided to the class use the decode method with 'utf-8' as an argument, and then, with the final output, run 'encode' on then transformed string with 'utf-8' as an argument.
This comment has been hidden.
Nah, it makes more sense to go on a hybrid approach when a version breaks backwards compatibility.
In which case maybe just add Python3 as a separate language.
I like the idea of adding them as seperate. I personally only use 2 when I have to. It is fun writing tools that have to work with multiple versions of Python and it's libraries. It'd be fun to have the ability to submit an answer and have it tested against both versions of Python to see if you can make one program work for both.
Oh yeah, my first answer would have worked fine in Python3. It took some serious fiddling to get it to work right with the unicode test.
This comment has been hidden.
I agree, totally frustrating ... I've spent way more time on the unicode thing than on the algorithm, because this was the first time I've encountered unicode in python.
It definitely sucks that Unicode is a fundamental in programming that Python never made any easier before v3.
Oy. Unicode with python. Still, lots of fun. I liked it!
this was a really fun Kata. seemed a little easy for a 4 Kyu kata, though.
The description of the cypher was a little confusing though. I had to go to wikipedia and read the page to really understand how the cipher worked.
Maybe if it explained the cipher something like:
A Caesar cipher shifts all letters in the string n spaces. A Vigenère cipher shifts each individual letter in the string n space, where n is calculated as "index of the original letter in the string + index of the letter in the key"
A Caesar encrption can use a shift value of 3, turning 'dog' (3,14,6) into 'grj' (6,17,9) But Vigenère uses the key 'pup' (15,20,15) to turn 'dog' (3,14,6) into 'siv' (18,8, 21).
After re-reading the description, it seems like it could be made more clear. However, I'm not sure if your suggested changes to the description accomplish that. I'll be sleeping on it, particularly in regard to this sentence and the following example recommended previously "The shift is derived by applying a Caesar shift to a character with the corresponding index of the key in the alphabet."
Edit: In regard to difficulty, it was determined by the beta process, and it seems to fall in line with other cipher katas. It should also be noted that, although this was published before Python was supported on Codewars, that this kata is much harder in Python because Unicode support sucks in Python 2.x.
Maybe my description wasn't the best. I understood really well what the Caesar was and how it worked. I just wasn't seeing its relationship to the Vigenère. And yeah, "The shift is derived by applying a Caesar shift to a character with the corresponding index of the key in the alphabet." now makes sense.
I had to look at the example on wikipedia and manually perform an encryption before I understood exactly how the cypher worked. And the best way that it worked in my head was, "oh, it's basic addition and subtraction."
I didn't realize the difficult was the same across languages. Interesting.
I am getting "Process was terminated. It took longer than 6000ms to complete"... My solution which worked for the simple testcases relies on arrays. Is that the problem?
You might have an infinite loop somewhere? None of the test cases are particularly computationally intensive.
Thanks! Yes, I had a while loop with a semicolon after the condition lol. For some reason it wasn't a problem when running tests...
I just cannot go through the 5th test in katakana using python and don't know how to figure it out. Will there be any hint?
Unicode is definitely hairy to work with in Python 2.x/ The decode and encode methods on strings help bring about a solution.
actually i use encode/decode to do the job, so i can go though the first 4 test in katakana, but don't actually know what's wrong with the 5th
Probably the biggest difference for that one from the previous one is to respect a character codepoint followed by a diacritic codepoint as combined, a single character. I.e. a regular character + a diacritic should be treated as a single character rather than a regular character followed by a diacritic character.
Domo, domo arigato gozaimasu (:P).
You guys are really good at japanese hahah.
I am not sure whether the diacritic character you said just means the dakuten/handakuten. If so, I wonder what the alphabet is provided? If alphabet provided as 'ァアィイゥウェエォオカガキギクグケゲコゴサザシジスズセゼソゾタダチヂッツヅテデトドナニヌネノハバパヒビピフブプヘベペホボポマミムメモャヤュユョヨラリルレロヮワヰヱヲンヴヵヶヷヸヹヺ・ーヽヾヿ', it seems whether the combination of regular character and dakuten/handakuten is not really concerned, as they run as different unicode character, u'\u30db\u30dc\u30dd' represented for 'ホボポ' for instance.
What my program's outcome of that test is 'ドモアリガトゴザイマス' while the answer provided is 'ドオカセガヨゴザキアニ'...
So I still stucked at the question and once suspected whether my algorithm of encrytion has a substle mistake enable me go through every test before the 5th one in katakana.
So sorry for my asking so many questions without making any break throuugh till now....
I just put my outcome in google translate and it turns out as 'Domo Thanks Gozaimasu' the same as what you hinted. And the answer should be 'De Oca Sega prognosis Zaki Ani'.
If the test is running the encode function, does it means that my program did not run? Or if the test is running the decode function, it seems more reasonable to get a readable sentence.
Yea, dakuten and handakuten are diacritics. It appears that you're solution is outputting the test's input rather than the output.
Weird because:
The alphabet is provided to the constructor: アイウエオァィゥェォカキクケコサシスセソタチツッテトナニヌネノハヒフヘホマミムメモヤャユュヨョラリルレロワヲンー
Valuable infomation! The alphabet I provided above is just for testing whether my program can deal with katakana and did not test the 'domo domo arigato gozaimasu' one. So it is not related to my weird output. In fact, my 'output' is just the one appear in the exception, 'A' in the sentence "'A' should be 'B'", shown after my submission. So, if there is not mistakes in the test case, it will be my program's responsibility. After all, I take it for granted that I only need to do the shift job.
Thank you so much for the instruction!
Well, perhaps I got some different opinion....
As shown in the Unicode Standard v8.0(http://www.unicode.org/charts/PDF/U30A0.pdf), katakana should take the range of 30A0-30FF, which I think that words like 'ホボポ' should be considered as three different basic character in katakana. And if I do not make any mistake, the dakuten and handakuten's unicode character is 309b and 309c, which seems not be considered as an isolated part in katakana by the definition of katakana in the unicode standard document.
Furthermore, It can be more reasonable not to take katakana as an isolated object to treat with in the general cipher method. Let's say, the alphabet provided is 'アイウエオァィゥェォカキクケコサシスセソタチツッテトナニヌネノハヒフヘホマミムメモヤャユュヨョラリルレロワヲンー' so it means that characters with dakuten/handakuten is not a member of the alphabet and we do not need to make any cipher at them. We can seperate the regular character and diacritic to left part and right part and we can use '+' to cocatenate them so it is reasonable under this circumstance, but what about other language have diacritic above the regular character, like latin?
This comment has been hidden.
This comment has been hidden.
This kata was originally coded for Javascript, which somehow manages to make it easier to work with Unicode than Python 2.x, despite its reputation (which is slowly being shed) for not having much in the way of Unicode support. The purpose of requiring Unicode for this kata is to help the people completing it be able to generalize the algorithm to any set of symbols rather than whatever alphabet they're familiar with. The fact that Python 2.x doesn't make it particularly easy to work with Unicode is something I see as a major flaw in Python 2.x. A lot of places still exclusively use Python 2.x over Python 3 (which are effectively two separate languages to me at this point). Meanwhile, if I'm writing code in any language, knowing how to work with a Unicode format is pretty high up on my list of things I want to know about that language.
Oh, I agree with you. It was useful to have to figure out how to do it in Python 2. Makes me appreciate the innate unicode support of Python 3.
I don't think the level is correct - this isn't a 4kyu kata.
I'm almost there, but I can't figure out how to properly index into a multibyte string to get a multibyte char. I can manually verify the correct solution provided by the output for the katakana test. And I am getting the write indices into the katakana alphabet for the encoded message. However, I can't properly build the string from the katakana alphabet, encoded += alphabet[idx] only gets 1 byte (should be 3). I have yet to find any useful examples of such indexing on the internet. I would greatly appreciate any hints, pointers and/or references.
Thanks.
Try decoding/encoding the string/your results.
I'm stuck on the katakana section. When I look at the alphabet, everything seems fine (it even prints in katakana), but things go funny when printing out my tabula recta. This is the first line, which should just be the alphabet in initial order:
['\xe3', '\x82', '\xa2', '\xe3', '\x82', '\xa4', '\xe3', '\x82', '\xa6', '\xe3', '\x82', '\xa8', '\xe3', '\x82', '\xaa', '\xe3', '\x82', '\xa1', '\xe3', '\x82', '\xa3', '\xe3', '\x82', '\xa5', '\xe3', '\x82', '\xa7', '\xe3', '\x82', '\xa9', '\xe3', '\x82', '\xab', '\xe3', '\x82', '\xad', '\xe3', '\x82', '\xaf', '\xe3', '\x82', '\xb1', '\xe3', '\x82', '\xb3', '\xe3', '\x82', '\xb5', '\xe3', '\x82', '\xb7', '\xe3', '\x82', '\xb9', '\xe3', '\x82', '\xbb', '\xe3', '\x82', '\xbd', '\xe3', '\x82', '\xbf', '\xe3', '\x83', '\x81', '\xe3', '\x83', '\x84', '\xe3', '\x83', '\x83', '\xe3', '\x83', '\x86', '\xe3', '\x83', '\x88', '\xe3', '\x83', '\x8a', '\xe3', '\x83', '\x8b', '\xe3', '\x83', '\x8c', '\xe3', '\x83', '\x8d', '\xe3', '\x83', '\x8e', '\xe3', '\x83', '\x8f', '\xe3', '\x83', '\x92', '\xe3', '\x83', '\x95', '\xe3', '\x83', '\x98', '\xe3', '\x83', '\x9b', '\xe3', '\x83', '\x9e', '\xe3', '\x83', '\x9f', '\xe3', '\x83', '\xa0', '\xe3', '\x83', '\xa1', '\xe3', '\x83', '\xa2', '\xe3', '\x83', '\xa4', '\xe3', '\x83', '\xa3', '\xe3', '\x83', '\xa6', '\xe3', '\x83', '\xa5', '\xe3', '\x83', '\xa8', '\xe3', '\x83', '\xa7', '\xe3', '\x83', '\xa9', '\xe3', '\x83', '\xaa', '\xe3', '\x83', '\xab', '\xe3', '\x83', '\xac', '\xe3', '\x83', '\xad', '\xe3', '\x83', '\xaf', '\xe3', '\x83', '\xb2', '\xe3', '\x83', '\xb3', '\xe3', '\x83', '\xbc']
I don't know why it no longer prints in katakana. Not only does that seem weird, but the symbols in this first line look like they're repeating in a very strange fashion. For example, "\xe3" seems to show up 56 times in a given alphabet which appears to have only 60 characters. How can that be?
When I build each line of my table, I just do line.append(self.alphabet[counter]). Even printing out a single character from the alphabet by doing "print self.alphabet[0]" gives me something other than katakana. Can someone help me see what's going wrong here?
Hint: There are more letters, numbers, and symbols in the world than you can represent with a single byte.
Your hint has helped, and I've been reading up on using Unicode with Python, but I still can't seem to get my code right. The best I've been able to come up with is prefacing my code with:
#!/usr/bin/env python
-- coding: utf-8 --
and I've put .encode(encoding='UTF-8',errors='strict') after every instance of a string character such as:
alphabet[0].encode(encoding='UTF-8',errors='strict')
But it still fails with the Japanese characters, giving me:
Traceback: in in init UnicodeDecodeError: 'ascii' codec can't decode byte 0xe3 in position 0: ordinal not in range(128)
I'm not sure where else I could add the .encode method that would make any sense. Am I even on the right track here?
To be honest, I had no idea that Python didn't seamlessly handle Unicode data, so that's something I'm going to want to look into; I'm now using Python in lieu of Perl for certain projects at work. The Python translation was contributed by someone else, and I haven't actually written a solution for this kata in Python.
There are a couple of solutions that have
import normalize from unicodedata
, but don't reference any normalize function in their code, such that it could be an artifact of debugging.All of the present solutions are using encode and decode with just the string 'utf-8'.
I've just added a Python translation, waiting for approval!
Adam, any chance you could offer me some clues about how to handle these Japanese characters in Python? My various attempts to .encode(), .decode(), unicode(), using UTF-8, and UTF-16 have all resulted either in no change, or different errors in different parts of the program. I've been reading about Python and Unicode for the past few days, and don't feel like I've gotten any closer to solving this thing.
Unfortunately, sometimes unicode is somewhat tricky in Python, imho. In my solution, I've used both decode() and encode(). It worked with UTF-8.
I think I may have just come across part of why I'm having so much trouble, and I can't believe I didn't pick up on it sooner. I'm trying to use Python's built-in .encode() and .decode() functions, but the class methods in the program are overwriting them.
Is it possible to change the way the kata is set up in Python to use class methods with slightly different names to avoid this? Maybe encode_ or something?
I'm afraid it may be not possible, as is not in Beta and is approved. I wonder how encode() could collide here?
Good one, a nice teacher for basic (old, historic) encryption practices. I accidentally hardcoded a 26 for the alphabet size instead of using abc.length, and could not find my error for awhile. Silly me...
This comment has been hidden.
Any hint on how to handle the katana' alphabet? I tried to use the "abc" provided in input as the alphabet used. Thus the first letter would be the zero reference and the last letter the end reference. But it doesn't work. I can't find where are these damn chinese character on the asci codes.
I think I'm having issues with the capital/non capital letters.
This comment has been hidden.
Do you mean shift should be done latin chars "abcdef...." ? what about many katakana chars that do not have match with latin chars... any clues ?
Ah, never mind got it too...silly me...hardcoded the number of alphabets...
yep, shift needs to assume the order of the alphabet provided.
Marking question as resolved.
You should really add in the description that you will be testing against Unicode characters (the katakana alphabet test cases). That really messed me up, had I known before hand I would have coded it differently.
Fun kata, I enjoyed it!
Just responded to rooc, but you could check out my similar Vigenere Autokey Cipher kata in beta to help get it approved faster.
worthy kata!
If you liked this kata, my Vigenere Autokey Cipher is still hanging out in beta. It's similar but with a twist that makes it harder to decode.
http://www.codewars.com/dojo/katas/52d2e2be94d26fc622000735
Added a CoffeeScript version.
Omg, laoris is working on my first published kata. I hope the person is creative in his/her hello world sense more than in his/her block of code sense.
This comment has been hidden.
No problem. Marked as ready now.
The example test code is missing the
y
in it's alphabet, causing errors where there are none.Edit: Otherwise, this is a decent kata!
Meanwhile, not really sure if it should rank higher than the Caesar Cipher Helper kata. I think the difference in difficulty level is negligible enough to keep it the same rank.
I needed to throw in a dynamic alphabet because I'm worried that people going through cipher katas won't understand that an alphabet can be generalized to just a list of symbols.
Added the autokey version that Vigenère actually did create as another kata. It's pretty easy if you've already solved this kata.