digitalmars.D.learn - Caesar Cipher
- Mario (20/20) Feb 11 2018 Hello there! I know deep Java, JavaScript, PHP, etc. but as you
- Cym13 (8/28) Feb 11 2018 Your mistake has little to do with D, and more with Ceasar (which
- Mario (3/36) Feb 11 2018 Got it, I'll give it a try with an if. Will post later the result
- Seb (3/7) Feb 11 2018 If you want to cheap, have a look at
- Mario (3/11) Feb 11 2018 Thank you for that, but actually it's using stuff that didn't
- Cym13 (5/19) Feb 11 2018 I think the less advanced way (in term of features) would be
- Mario (3/23) Feb 11 2018 Yeah, if it's similar to Js will be a good choice (sure it ain't
- Seb (7/21) Feb 11 2018 Wel, the DLang Tour grew organically and its typically hard for
- Mario (4/26) Feb 11 2018 Sure! Also, I'm from Spain so I'd like to contribute translating
- DanielG (1/1) Feb 11 2018 Here's a newbie-friendly solution: https://run.dlang.io/is/4hi7wH
- Era Scarecrow (13/21) Feb 11 2018 If you take Z (25) and add 10, you get 35. You need to have it
Hello there! I know deep Java, JavaScript, PHP, etc. but as you all probably know, that's high-level and most of them only use the heap memory. So I'm new to the wonderful world of low-level and the stack-heap. I started a week ago learning D (which by the moment is being easy for me) but I'm facing a big problem: I don't know how to do the exercise of https://tour.dlang.org/tour/en/basics/arrays . I really have been looking on forums and Google but I found this in Java https://stackoverflow.com/questions/10023818/shift-character-in-alphabet which is actually not the best due to Java uses other ways. My code is something like this: char[] encrypt(char[] input, char shift) { auto result = input.dup; result[] += shift; return result; } What's wrong? I mean, I know that z is being converted into a symbol, but how should I fix this? Thanks on forward.
Feb 11 2018
On Sunday, 11 February 2018 at 18:01:20 UTC, Mario wrote:Hello there! I know deep Java, JavaScript, PHP, etc. but as you all probably know, that's high-level and most of them only use the heap memory. So I'm new to the wonderful world of low-level and the stack-heap. I started a week ago learning D (which by the moment is being easy for me) but I'm facing a big problem: I don't know how to do the exercise of https://tour.dlang.org/tour/en/basics/arrays . I really have been looking on forums and Google but I found this in Java https://stackoverflow.com/questions/10023818/shift-character-in-alphabet which is actually not the best due to Java uses other ways. My code is something like this: char[] encrypt(char[] input, char shift) { auto result = input.dup; result[] += shift; return result; } What's wrong? I mean, I know that z is being converted into a symbol, but how should I fix this? Thanks on forward.Your mistake has little to do with D, and more with Ceasar (which is unfortunate IMHO): this cipher is usually defined only on the 26 letters of the alphabet and seeing the result of the assert at the end of the code it's the case here. So while you're working on a full byte (256 values) you should restrict yourself to the 26 lowercase ascii alpha characters. Give it a try :)
Feb 11 2018
On Sunday, 11 February 2018 at 18:28:08 UTC, Cym13 wrote:On Sunday, 11 February 2018 at 18:01:20 UTC, Mario wrote:Got it, I'll give it a try with an if. Will post later the result so more people can get a solution with the most basic stuff.Hello there! I know deep Java, JavaScript, PHP, etc. but as you all probably know, that's high-level and most of them only use the heap memory. So I'm new to the wonderful world of low-level and the stack-heap. I started a week ago learning D (which by the moment is being easy for me) but I'm facing a big problem: I don't know how to do the exercise of https://tour.dlang.org/tour/en/basics/arrays . I really have been looking on forums and Google but I found this in Java https://stackoverflow.com/questions/10023818/shift-character-in-alphabet which is actually not the best due to Java uses other ways. My code is something like this: char[] encrypt(char[] input, char shift) { auto result = input.dup; result[] += shift; return result; } What's wrong? I mean, I know that z is being converted into a symbol, but how should I fix this? Thanks on forward.Your mistake has little to do with D, and more with Ceasar (which is unfortunate IMHO): this cipher is usually defined only on the 26 letters of the alphabet and seeing the result of the assert at the end of the code it's the case here. So while you're working on a full byte (256 values) you should restrict yourself to the 26 lowercase ascii alpha characters. Give it a try :)
Feb 11 2018
On Sunday, 11 February 2018 at 18:01:20 UTC, Mario wrote:Hello there! I know deep Java, JavaScript, PHP, etc. but as you all probably know, that's high-level and most of them only use the heap memory. [...]If you want to cheap, have a look at https://github.com/dlang-tour/core/issues/227
Feb 11 2018
On Sunday, 11 February 2018 at 18:31:35 UTC, Seb wrote:On Sunday, 11 February 2018 at 18:01:20 UTC, Mario wrote:Thank you for that, but actually it's using stuff that didn't appear yet (I understand how it works but I'd like to know why XD)Hello there! I know deep Java, JavaScript, PHP, etc. but as you all probably know, that's high-level and most of them only use the heap memory. [...]If you want to cheap, have a look at https://github.com/dlang-tour/core/issues/227
Feb 11 2018
On Sunday, 11 February 2018 at 18:50:25 UTC, Mario wrote:On Sunday, 11 February 2018 at 18:31:35 UTC, Seb wrote:I think the less advanced way (in term of features) would be using a foreach loop (which haven't been introduced either yet). But yeah, the example isn't well choosen, and that's why an issue was oppened for it ;)On Sunday, 11 February 2018 at 18:01:20 UTC, Mario wrote:Thank you for that, but actually it's using stuff that didn't appear yet (I understand how it works but I'd like to know why XD)Hello there! I know deep Java, JavaScript, PHP, etc. but as you all probably know, that's high-level and most of them only use the heap memory. [...]If you want to cheap, have a look at https://github.com/dlang-tour/core/issues/227
Feb 11 2018
On Sunday, 11 February 2018 at 18:55:14 UTC, Cym13 wrote:On Sunday, 11 February 2018 at 18:50:25 UTC, Mario wrote:Yeah, if it's similar to Js will be a good choice (sure it ain't the best in D, but I will try it)On Sunday, 11 February 2018 at 18:31:35 UTC, Seb wrote:I think the less advanced way (in term of features) would be using a foreach loop (which haven't been introduced either yet). But yeah, the example isn't well choosen, and that's why an issue was oppened for it ;)On Sunday, 11 February 2018 at 18:01:20 UTC, Mario wrote:Thank you for that, but actually it's using stuff that didn't appear yet (I understand how it works but I'd like to know why XD)Hello there! I know deep Java, JavaScript, PHP, etc. but as you all probably know, that's high-level and most of them only use the heap memory. [...]If you want to cheap, have a look at https://github.com/dlang-tour/core/issues/227
Feb 11 2018
On Sunday, 11 February 2018 at 18:50:25 UTC, Mario wrote:On Sunday, 11 February 2018 at 18:31:35 UTC, Seb wrote:Wel, the DLang Tour grew organically and its typically hard for an experienced D user to realize what has been introduced and what not, so we are really grateful for any feedback you have -> https://github.com/dlang-tour/english/issues And of course, you can always press on "edit" and improve it directly ;-)On Sunday, 11 February 2018 at 18:01:20 UTC, Mario wrote:Thank you for that, but actually it's using stuff that didn't appear yet (I understand how it works but I'd like to know why XD)Hello there! I know deep Java, JavaScript, PHP, etc. but as you all probably know, that's high-level and most of them only use the heap memory. [...]If you want to cheap, have a look at https://github.com/dlang-tour/core/issues/227
Feb 11 2018
On Sunday, 11 February 2018 at 18:55:44 UTC, Seb wrote:On Sunday, 11 February 2018 at 18:50:25 UTC, Mario wrote:Sure! Also, I'm from Spain so I'd like to contribute translating the "Learn" site (I think it might be useful for new members, even if most of the community knows English :P).On Sunday, 11 February 2018 at 18:31:35 UTC, Seb wrote:Wel, the DLang Tour grew organically and its typically hard for an experienced D user to realize what has been introduced and what not, so we are really grateful for any feedback you have -> https://github.com/dlang-tour/english/issues And of course, you can always press on "edit" and improve it directly ;-)On Sunday, 11 February 2018 at 18:01:20 UTC, Mario wrote:Thank you for that, but actually it's using stuff that didn't appear yet (I understand how it works but I'd like to know why XD)Hello there! I know deep Java, JavaScript, PHP, etc. but as you all probably know, that's high-level and most of them only use the heap memory. [...]If you want to cheap, have a look at https://github.com/dlang-tour/core/issues/227
Feb 11 2018
Here's a newbie-friendly solution: https://run.dlang.io/is/4hi7wH
Feb 11 2018
On Sunday, 11 February 2018 at 18:01:20 UTC, Mario wrote:char[] encrypt(char[] input, char shift) { auto result = input.dup; result[] += shift; return result; } What's wrong? I mean, I know that z is being converted into a symbol, but how should I fix this?If you take Z (25) and add 10, you get 35. You need to have it identify and fix the problem, namely removing 26 from the result. Assuming anything can be part of the input (and not just letters), we instead do the following: auto result = input.dup; foreach(ref ch; result) { if (ch >= 'A' && ch <= 'Z') ch = ((ch+shift-'A') % 26) + 'A'; } Alternatively if you do where every character is defined for switching (and those not changing are the same) it could just be a replacement & lookup.
Feb 11 2018