Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Calculate weight of word  (Read 9561 times)

0 Members and 1 Guest are viewing this topic.

Linux711

    Topic Starter


    Mentor

    Thanked: 59
    • Yes
    • Programming Blog
  • Certifications: List
  • Computer: Specs
  • Experience: Experienced
  • OS: Windows 7
Calculate weight of word
« on: October 12, 2011, 09:48:10 AM »
I am trying to alphabetize by generating a weight of a word, saving those numbers, and then comparing which numbers are greater. Don't ask me why I need to do this way.

So I am trying to come up with a function that calculates the value of a certain word. I am using the asc function to get the ascii value, but I am not good enough at math to figure out how to calculate this.

Here is what I have:

Code: [Select]
Dim byt As Integer
Dim acc As Integer

If txt = "" Then
    lbl = "0"
Else
    For i = 1 To Len(txt)
        byt = Asc(Mid(txt, i, 1))
        acc = acc + (byt / i)
    Next i
   
    lbl = Str(acc)
End If
YouTube

"Genius is persistence, not brain power." - Me

"Insomnia is just a byproduct of, "It can't be done"" - LaVolpe

Salmon Trout

  • Guest
Re: Calculate weight of word
« Reply #1 on: October 12, 2011, 10:56:40 AM »
Don't ask me why I need to do this way.

Why do you need to do it this way?

reddevilggg



    Expert

    Thanked: 69
  • Experience: Beginner
  • OS: Windows 7
Re: Calculate weight of word
« Reply #2 on: October 12, 2011, 12:15:34 PM »

Why do you need to do it this way?

*whispers* because its homework ??
11 cheers for binary !

Salmon Trout

  • Guest
Re: Calculate weight of word
« Reply #3 on: October 12, 2011, 12:27:27 PM »
I am trying to alphabetize by generating a weight of a word

It's not really clear what the "weight" of a word is supposed to mean. Maybe if you made that clearer? Do you mean that some letters are "heavier" than others e.g. A weighs 1 unit and Z weighs 26 units? Something like that?


Linux711

    Topic Starter


    Mentor

    Thanked: 59
    • Yes
    • Programming Blog
  • Certifications: List
  • Computer: Specs
  • Experience: Experienced
  • OS: Windows 7
Re: Calculate weight of word
« Reply #4 on: October 12, 2011, 01:04:04 PM »
Quote
*whispers* because its homework ??

To be honest, yes, but that's only about 10% of it. The rest I already have completed.

Quote
It's not really clear what the "weight" of a word is supposed to mean.

Ok. I'll try to be clearer. What I need is a algorithm that will take a word and return a value of that word, so that it can be put in alphabetical order. 'A' weighs whatever the ASCII value of 'A' is, etc. So I began by just adding all the ASCII values together, but that was not correct because the first character takes presidence over the rest because "aa" would come before "apple". Do you see what I mean now?
YouTube

"Genius is persistence, not brain power." - Me

"Insomnia is just a byproduct of, "It can't be done"" - LaVolpe

Salmon Trout

  • Guest
Re: Calculate weight of word
« Reply #5 on: October 12, 2011, 01:19:18 PM »
It's not as simple as you seem to think it is.

these are in alphabetical order

a
aa
aaa

You can't just "sort" one string. The whole essence of sorting is comparison.


Linux711

    Topic Starter


    Mentor

    Thanked: 59
    • Yes
    • Programming Blog
  • Certifications: List
  • Computer: Specs
  • Experience: Experienced
  • OS: Windows 7
Re: Calculate weight of word
« Reply #6 on: October 12, 2011, 01:25:29 PM »
Quote
You can't just "sort" one string. The whole essence of sorting is comparison.

True, but you could generate a value based on that string, right?
YouTube

"Genius is persistence, not brain power." - Me

"Insomnia is just a byproduct of, "It can't be done"" - LaVolpe

Salmon Trout

  • Guest
Re: Calculate weight of word
« Reply #7 on: October 12, 2011, 01:49:25 PM »
True, but you could generate a value based on that string, right?

So what value would the string "a" have?

Linux711

    Topic Starter


    Mentor

    Thanked: 59
    • Yes
    • Programming Blog
  • Certifications: List
  • Computer: Specs
  • Experience: Experienced
  • OS: Windows 7
Re: Calculate weight of word
« Reply #8 on: October 12, 2011, 01:53:16 PM »
Probably 97, but I'm not sure because my algorithm doesn't work. Any help?
YouTube

"Genius is persistence, not brain power." - Me

"Insomnia is just a byproduct of, "It can't be done"" - LaVolpe

Salmon Trout

  • Guest
Re: Calculate weight of word
« Reply #9 on: October 12, 2011, 02:18:53 PM »
Probably 97, but I'm not sure because my algorithm doesn't work. Any help?

You are using the word algorithm incorrectly. There are sorting algorithms, sure, but like I said, you can't sort one string.


Salmon Trout

  • Guest
Re: Calculate weight of word
« Reply #10 on: October 12, 2011, 02:46:27 PM »
Quote
my algorithm doesn't work

This is a great big fat clue.


Salmon Trout

  • Guest
Re: Calculate weight of word
« Reply #11 on: October 12, 2011, 02:48:43 PM »
"aa" would come before "apple"

... and "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaa" would come before "ab". Do you see the difficulty now?



Geek-9pm


    Mastermind
  • Geek After Dark
  • Thanked: 1026
    • Gekk9pm bnlog
  • Certifications: List
  • Computer: Specs
  • Experience: Expert
  • OS: Windows 10
Re: Calculate weight of word
« Reply #12 on: October 12, 2011, 04:44:53 PM »
May I but in? Think of the letters as being part of a number system from some space aliens. For this example, we shall only  lower case letters from a to z. The letter am by itself, has a value of 1. the Letter z, by itself, has a value of 26. Also, the hyphen will be given a value of 0. (More of that latter).
These space aliens  have asymmetrical bodies with with fingers and toes that add up to 27. So they count things in groups of 27 or a power of 27. The have adopted the letters a-z and the hyphen for their number system.
So in their notation:
1 = a
b = 2
and so on.
Now look at this:
a-  represents 27, but
aa  represents 28  and
ba  represents 55 also
b-a represents 1459
We told you earlier the hyphen in their language represents a zero, it is a plaice holder.

Myself, I can not do this in batch code. I would have to use VB-script. Or maybe Python.

It is a form of base conversion. Using base 27 as the source. Computers don not work in base 27, so it needs to be in a common base that is used in computing. Likely a 64 bit long integer.



Salmon Trout

  • Guest
Re: Calculate weight of word
« Reply #13 on: October 12, 2011, 11:52:55 PM »
Geek isn't getting it either.

BC_Programmer


    Mastermind
  • Typing is no substitute for thinking.
  • Thanked: 1140
    • Yes
    • Yes
    • BC-Programming.com
  • Certifications: List
  • Computer: Specs
  • Experience: Beginner
  • OS: Windows 11
Re: Calculate weight of word
« Reply #14 on: October 13, 2011, 05:58:37 AM »
I am trying to alphabetize by generating a weight of a word, saving those numbers, and then comparing which numbers are greater.
It won't work.

What you are basically trying to do is generate a hash. The problem with a hash is that a collision is inevitable,especially with one so simple and comprised of so few bits. and in the case of sorting by a hash any collisions will end up sorting together.

The only time I've needed to so anything remotely similar to this was when I was writing a Anagram search program that could find all anagrams of a given word from a dictionary list. Rather  than a sequential search and compare, I sorted the list of words according to the sorted list of their letters; that is, the word "parent" would be sorted as "aenprt". So would "entrap". This means that all anagrams of a given word would end up being sorted contiguously.


However, what you are trying to do, again, is hash the word, and then sort based on that hash. Since a hash will always lose some information, there are going to be collisions.

Quote
Likely a 64 bit long integer.
limited to 14 characters. - I think, my math may be a bit rusty, I got that from log base 27 of 18,446,744,073,709,600,000 (~2 to the 64th power); (and it's been a while since I've dealt with logarithms). And that is for a unsigned 64-bit long, too.


Doable, maybe. But as a general purpose sort algorithm? fairly useless. Especially since it can only represent the alphabetic characters and in a single case (lower case or upper case). Additionally, it doesn't give any sort of speed advantage since surely hashing (which will pretty much be what is being done) the characters into a 64-bit integer is going to be more work than simply comparing two strings, particularly since the latter case only has to compare the strings up to the point where they differ; it's a O(n+1) operation where n is the number of letters that are the same at the beginning of either string. Hashing (or converting base, or whatever you want to call it) will always be O(n) for the length of the string. Add to this the fact that VB6 (which it appears they are using) doesn't support 64-bit integers except by faking it with the scaled 64-bit integer that is the Currency Type, and you add a whole new meaning to the word "overcomplicated". Not to mention said 64-bit scaled integer is in fact a signed value.
I was trying to dereference Null Pointers before it was cool.