Collection in Smalltalk

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
14 messages Options
Reply | Threaded
Open this post in threaded view
|

Collection in Smalltalk

4l3x
Hi guys,

I am just beginner in Smaltalk and I have some problem with collection in smalltalk.
For this problem I have to define the value of an alphabetic character based on its location in the alphabet. So ‘a’ and  ‘A’ each have value 1, ‘b’ and ‘B’ each have 2, etc. Non-alphabetic characters can be considered to have value 0.
The value of a word is the sum of the value of all its characters. So ‘Smalltalk’ has value 101 and ‘abc’ has value 6.
A dollar word is a word whose value is 100.  I want to add a method to the String class that returns all the dollar words in the string.

So for example if the input is 'SmallTalk' the result is ('SmallTlk' 'SmllTlk').

Please kindly help me guys. Really appreciate your help.

Thanks.


_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Collection in Smalltalk

Marcin Tustin
You need to break this down into its component parts:
Write a method that tells you score of a character
Write a method that tells you the score of a string (hint: use inject:into:)
Write a method that tells you the dollar words (hint: use select: and splitOn:)

2008/9/24 Alex Chi <[hidden email]>
Hi guys,

I am just beginner in Smaltalk and I have some problem with collection in smalltalk.
For this problem I have to define the value of an alphabetic character based on its location in the alphabet. So 'a' and  'A' each have value 1, 'b' and 'B' each have 2, etc. Non-alphabetic characters can be considered to have value 0.
The value of a word is the sum of the value of all its characters. So 'Smalltalk' has value 101 and 'abc' has value 6.
A dollar word is a word whose value is 100.  I want to add a method to the String class that returns all the dollar words in the string.

So for example if the input is 'SmallTalk' the result is ('SmallTlk' 'SmllTlk').

Please kindly help me guys. Really appreciate your help.

Thanks.


_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners



_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Collection in Smalltalk

cedreek
In reply to this post by 4l3x
Hi,

As a start you can do:

alphabet := $a to: $z.
coll indexOf: $c    returns 4

to have both upper and lower case:
alphabet indexOf: yourCaracter asLowercase

then you iterate on your string collection and add each result...

not sure about the $ word, but it could be once you detect the sum to be 100...

hth :)

Cédrick


but maybe you could reverse the collection so as to have ($a -> 1  ...
 $z -> 26)

2008/9/24 Alex Chi <[hidden email]>:

> Hi guys,
>
> I am just beginner in Smaltalk and I have some problem with collection in
> smalltalk.
> For this problem I have to define the value of an alphabetic character based
> on its location in the alphabet. So 'a' and  'A' each have value 1, 'b' and
> 'B' each have 2, etc. Non-alphabetic characters can be considered to have
> value 0.
> The value of a word is the sum of the value of all its characters. So
> 'Smalltalk' has value 101 and 'abc' has value 6.
> A dollar word is a word whose value is 100.  I want to add a method to the
> String class that returns all the dollar words in the string.
>
> So for example if the input is 'SmallTalk' the result is ('SmallTlk'
> 'SmllTlk').
>
> Please kindly help me guys. Really appreciate your help.
>
> Thanks.
>
>
> _______________________________________________
> Beginners mailing list
> [hidden email]
> http://lists.squeakfoundation.org/mailman/listinfo/beginners
>
>


--
Cédrick

_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Collection in Smalltalk

cedreek
> alphabet := $a to: $z.
> coll indexOf: $c    returns 4
>
 returns 3 !
_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Collection in Smalltalk

4l3x
In reply to this post by 4l3x
Thanks. I'll try to start with the indexing table for each character first.
Anyway do you know how to split string based on more than 1 character, for example I want to split string based on some punctuation. I have tried using tokenBasedOn, but still can not figured out how to use it for more than 1 separator.

----- Original Message ----
From: Cédrick Béler <[hidden email]>
To: A friendly place to get answers to even the most basic questions about Squeak. <[hidden email]>
Sent: Wednesday, September 24, 2008 40:44 PM
Subject: Re: [Newbies] Collection in Smalltalk

Hi,

As a start you can do:

alphabet := $a to: $z.
coll indexOf: $c    returns 4

to have both upper and lower case:
alphabet indexOf: yourCaracter asLowercase

then you iterate on your string collection and add each result...

not sure about the $ word, but it could be once you detect the sum to be 100...

hth :)

Cédrick


but maybe you could reverse the collection so as to have ($a -> 1  ...
$z -> 26)

2008/9/24 Alex Chi <[hidden email]>:

> Hi guys,
>
> I am just beginner in Smaltalk and I have some problem with collection in
> smalltalk.
> For this problem I have to define the value of an alphabetic character based
> on its location in the alphabet. So 'a' and  'A' each have value 1, 'b' and
> 'B' each have 2, etc. Non-alphabetic characters can be considered to have
> value 0.
> The value of a word is the sum of the value of all its characters. So
> 'Smalltalk' has value 101 and 'abc' has value 6.
> A dollar word is a word whose value is 100.  I want to add a method to the
> String class that returns all the dollar words in the string.
>
> So for example if the input is 'SmallTalk' the result is ('SmallTlk'
> 'SmllTlk').
>
> Please kindly help me guys. Really appreciate your help.
>
> Thanks.
>
>
> _______________________________________________
> Beginners mailing list
> [hidden email]
> http://lists.squeakfoundation.org/mailman/listinfo/beginners
>
>



--
Cédrick


_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Collection in Smalltalk

Randal L. Schwartz
In reply to this post by 4l3x
>>>>> "Alex" == Alex Chi <[hidden email]> writes:

Alex> So for example if the input is 'SmallTalk' the result is ('SmallTlk'
Alex> 'SmllTlk').

Presuming the input came from someone who didn't know that Smalltalk
doesn't have a capital T. :)

--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<[hidden email]> <URL:http://www.stonehenge.com/merlyn/>
Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc.
See http://methodsandmessages.vox.com/ for Smalltalk and Seaside discussion
_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Collection in Smalltalk

Yoshiki Ohshima-2
In reply to this post by Marcin Tustin
At Wed, 24 Sep 2008 22:39:53 +0100,
Marcin Tustin wrote:
>
> Write a method that tells you score of a character

  Yes.

> Write a method that tells you the score of a string (hint: use inject:into:)

  Yes.

> Write a method that tells you the dollar words (hint: use select: and splitOn:)

  This part is not really a Smalltalk question, I think.  It is a
problem of searching in a power set and finding the all solutions.  It
is not exactly a simple task, because the length of string may be
fairly big, You would need to do some dynamic programming to pick 100
a's from a 10,000 characters string.

http://en.wikipedia.org/wiki/Dynamic_programming

-- Yoshiki
_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Collection in Smalltalk

cedreek
In reply to this post by 4l3x
have a looks at method name containing split
use the method finder
or
alt+shift+w when split selected...

You should be able to find ;)

Cédrick

2008/9/25 Alex Chi <[hidden email]>:

> Thanks. I'll try to start with the indexing table for each character first.
> Anyway do you know how to split string based on more than 1 character, for
> example I want to split string based on some punctuation. I have tried using
> tokenBasedOn, but still can not figured out how to use it for more than 1
> separator.
>
> ----- Original Message ----
> From: Cédrick Béler <[hidden email]>
> To: A friendly place to get answers to even the most basic questions about
> Squeak. <[hidden email]>
> Sent: Wednesday, September 24, 2008 40:44 PM
> Subject: Re: [Newbies] Collection in Smalltalk
>
> Hi,
>
> As a start you can do:
>
> alphabet := $a to: $z.
> coll indexOf: $c    returns 4
>
> to have both upper and lower case:
> alphabet indexOf: yourCaracter asLowercase
>
> then you iterate on your string collection and add each result...
>
> not sure about the $ word, but it could be once you detect the sum to be
> 100...
>
> hth :)
>
> Cédrick
>
>
> but maybe you could reverse the collection so as to have ($a -> 1  ...
> $z -> 26)
>
> 2008/9/24 Alex Chi <[hidden email]>:
>> Hi guys,
>>
>> I am just beginner in Smaltalk and I have some problem with collection in
>> smalltalk.
>> For this problem I have to define the value of an alphabetic character
>> based
>> on its location in the alphabet. So 'a' and  'A' each have value 1, 'b'
>> and
>> 'B' each have 2, etc. Non-alphabetic characters can be considered to have
>> value 0.
>> The value of a word is the sum of the value of all its characters. So
>> 'Smalltalk' has value 101 and 'abc' has value 6.
>> A dollar word is a word whose value is 100.  I want to add a method to the
>> String class that returns all the dollar words in the string.
>>
>> So for example if the input is 'SmallTalk' the result is ('SmallTlk'
>> 'SmllTlk').
>>
>> Please kindly help me guys. Really appreciate your help.
>>
>> Thanks.
>>
>>
>> _______________________________________________
>> Beginners mailing list
>> [hidden email]
>> http://lists.squeakfoundation.org/mailman/listinfo/beginners
>>
>>
>
>
>
> --
> Cédrick
>
>
> _______________________________________________
> Beginners mailing list
> [hidden email]
> http://lists.squeakfoundation.org/mailman/listinfo/beginners
>
>


--
Cédrick

_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Collection in Smalltalk

Herbert König
In reply to this post by 4l3x
Hello Alex,


AC> Thanks. I'll try to start with the indexing table for each character first.
AC> Anyway do you know how to split string based on more than 1
AC> character, for example I want to split string based on some
AC> punctuation. I have tried using tokenBasedOn, but still can not
AC> figured out how to use it for more than 1 separator.

String>>findTokens: is your friend here.

I have a code fragment which reads

tokens := comment asLowercase findTokens: mySeparators

As I use this fairly often, someplace I have code to initialise
mySeparators like:

mySeparators := Array new: 13
mySeparators at: 1 put: $- ; at: 2 put: $( .....
and so on.



Cheers,

Herbert  

_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Collection in Smalltalk

Randal L. Schwartz
>>>>> "Herbert" == Herbert König <[hidden email]> writes:

Herbert> Hello Alex,
AC> Thanks. I'll try to start with the indexing table for each character first.
AC> Anyway do you know how to split string based on more than 1
AC> character, for example I want to split string based on some
AC> punctuation. I have tried using tokenBasedOn, but still can not
AC> figured out how to use it for more than 1 separator.

String> findTokens: is your friend here.

Herbert> I have a code fragment which reads

Herbert> tokens := comment asLowercase findTokens: mySeparators

Herbert> As I use this fairly often, someplace I have code to initialise
Herbert> mySeparators like:

Herbert> mySeparators := Array new: 13
Herbert> mySeparators at: 1 put: $- ; at: 2 put: $( .....
Herbert> and so on.

You can just write that as a literal:

  mySeparators := #($- $().

--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<[hidden email]> <URL:http://www.stonehenge.com/merlyn/>
Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc.
See http://methodsandmessages.vox.com/ for Smalltalk and Seaside discussion
_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Collection in Smalltalk

Bert Freudenberg

Am 25.09.2008 um 06:40 schrieb Randal L. Schwartz:

>>>>>> "Herbert" == Herbert König <[hidden email]> writes:
>
> Herbert> Hello Alex,
> AC> Thanks. I'll try to start with the indexing table for each  
> character first.
> AC> Anyway do you know how to split string based on more than 1
> AC> character, for example I want to split string based on some
> AC> punctuation. I have tried using tokenBasedOn, but still can not
> AC> figured out how to use it for more than 1 separator.
>
> String> findTokens: is your friend here.
>
> Herbert> I have a code fragment which reads
>
> Herbert> tokens := comment asLowercase findTokens: mySeparators
>
> Herbert> As I use this fairly often, someplace I have code to  
> initialise
> Herbert> mySeparators like:
>
> Herbert> mySeparators := Array new: 13
> Herbert> mySeparators at: 1 put: $- ; at: 2 put: $( .....
> Herbert> and so on.
>
> You can just write that as a literal:
>
>  mySeparators := #($- $().


Err, what's wrong with using a String?

        mySeparators := '-(...'

- Bert -


_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Collection in Smalltalk

Herbert König
In reply to this post by Randal L. Schwartz
Hello Randal,


Herbert>> mySeparators := Array new: 13
Herbert>> mySeparators at: 1 put: $- ; at: 2 put: $( .....
Herbert>> and so on.

RLS> You can just write that as a literal:
RLS>   mySeparators := #($- $().

actually yes but I really have 13 separators (ugly) and I may have
done it that way if it were numbers.

The first line even reads:
mySeparators at: 1 put: ' ' asCharacter;
so I understand the whole thing when I read it later. $. and $; makes
me stumble and $  is on the verge of unreadable to me.

Btw I just changed that to .. put: Character space.
' ' asCharacter is clear but it itches me to count the blanks anyway.

Details, details :-))

--
Cheers,

Herbert  

_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Collection in Smalltalk

Bert Freudenberg

Am 25.09.2008 um 08:33 schrieb Herbert König:

> Hello Randal,
>
>
> Herbert>> mySeparators := Array new: 13
> Herbert>> mySeparators at: 1 put: $- ; at: 2 put: $( .....
> Herbert>> and so on.
>
> RLS> You can just write that as a literal:
> RLS>   mySeparators := #($- $().
>
> actually yes but I really have 13 separators (ugly) and I may have
> done it that way if it were numbers.
>
> The first line even reads:
> mySeparators at: 1 put: ' ' asCharacter;
> so I understand the whole thing when I read it later. $. and $; makes
> me stumble and $  is on the verge of unreadable to me.
>
> Btw I just changed that to .. put: Character space.
> ' ' asCharacter is clear but it itches me to count the blanks anyway.
>
> Details, details :-))


In that case I'd suggest

        Character separators, '-('


- Bert -


_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Collection in Smalltalk

Herbert König
In reply to this post by Bert Freudenberg
Hello Bert,


BF> Err, what's wrong with using a String?

BF> mySeparators := '-(...'

oops, yes. Even the blank looks good in there.

I just haven't groked yet that Strings are Collections :-)


Cheers,

Herbert  

_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners