Jump to page: 1 2
Thread overview
Caesar Cipher
Feb 11, 2018
Mario
Feb 11, 2018
Cym13
Feb 11, 2018
Mario
Feb 11, 2018
Seb
Feb 11, 2018
Mario
Feb 11, 2018
Cym13
Feb 11, 2018
Mario
Feb 11, 2018
Seb
Feb 11, 2018
Mario
Feb 11, 2018
DanielG
Feb 11, 2018
Era Scarecrow
February 11, 2018
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.
February 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 :)
February 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
February 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:
>> 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

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)
February 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:
>> 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 :)

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.
February 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:
>> 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
>
> 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)

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 ;)
February 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:
>> 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
>
> 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)

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 ;-)
February 11, 2018
Here's a newbie-friendly solution: https://run.dlang.io/is/4hi7wH

February 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.

February 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:
>> On Sunday, 11 February 2018 at 18:31:35 UTC, Seb wrote:
>>> 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
>>
>> 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)
>
> 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 ;)

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)
« First   ‹ Prev
1 2