comp.lang.ada
 help / color / mirror / Atom feed
From: Simon Wright <simon@pushface.org>
Subject: Re: Need Help On Ada95 Problem
Date: Fri, 10 Feb 2012 08:47:20 +0000
Date: 2012-02-10T08:47:20+00:00	[thread overview]
Message-ID: <m262ff3wiv.fsf@pushface.org> (raw)
In-Reply-To: 553ceec3-ec34-41de-9723-0dc342379cfe@vv9g2000pbc.googlegroups.com

Shark8 <onewingedshark@gmail.com> writes:

>> Complete the function Encrypt(PIN) that takes a 4-digit PIN (as a
>> string) and returns the corresponding 4-letter code (also as a
>> string).
>>
>> Note: It would be possible to convert digits to letters using a giant
>> IF statement, but don't do this. Instead, the letter wheel is given to
>> you as a string, so use each digit to read the appropriate letter from
>> the string.
>
> Alright this is actually a simple problem; there are several ways to
> go about this,
> but let's play to Ada's strengths and use types.
>
> So start by describing things in terms of types.
>
>       Type Cypher_Character is Private;
>       Type Pin_Character is Private;
>
>       Type Pin_String is Array(1..4) of Pin_Character;

Really, this is overkill.

> Reading the problem the note says:
> It would be possible to convert digits to letters using a giant
> IF statement, but don't do this. Instead, the letter wheel is given to
> you as a string, so use each digit to read the appropriate letter from
> the string.
>
> Meaning that they're hinting strongly that you use the CASE statement.

No, they want you to use the encoding as an array.

But I think it would be better to use a character mapping:

   The_Ring : constant Ada.Strings.Maps.Character_Mapping
     := Ada.Strings.Maps.To_Mapping ("0123456789", "UROVALTINE");

   function Encode (PIN : String) return String
   is
   begin
      return Ada.Strings.Fixed.Translate (PIN, The_Ring);
   end Encode;

OK, if you encode 9537s you get ELVIs ...

But we don't know what the professor feels about students who are too
clever for their own good and have clearly been reading ahead in the
textbook! (or asking for help on the net ...)



  parent reply	other threads:[~2012-02-10  8:47 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-02-09  1:03 Need Help On Ada95 Problem Will
2012-02-09  1:51 ` Robert A Duff
2012-02-09  2:01 ` Shark8
2012-02-10  1:36   ` BrianG
2012-02-10  2:22     ` Shark8
2012-02-10  5:32       ` Alex
2012-02-10 15:19         ` Shark8
2012-02-10 15:45           ` Alex
2012-02-10 20:07           ` Robert A Duff
2012-02-12 19:40             ` Will
2012-02-12 19:42               ` Will
2012-02-12 22:26               ` Robert A Duff
2012-02-13  0:41                 ` Will
2012-02-13  0:43                 ` Nasser M. Abbasi
2012-02-10  5:45       ` Alex
2012-02-10 13:29         ` Robert A Duff
2012-02-10 13:50           ` Alex
2012-02-10 14:30             ` Robert A Duff
2012-02-10 15:32               ` Alex
2012-02-10 20:03                 ` Robert A Duff
2012-02-13  3:28       ` BrianG
2012-02-10  8:47   ` Simon Wright [this message]
2012-02-09  3:38 ` Alex
     [not found]   ` <90a10801-440b-4a31-88d0-f0f7c17137f1@eb6g2000vbb.googlegroups.com>
2012-02-09 13:35     ` Alex
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox