Jump to content

Count the number of each letter


Recommended Posts

  • Moderators

civilcalc,

This is one way to do it: :mellow:

#include <Array.au3>

$sData = "Amber" & @CRLF & _
         "Azure" & @CRLF & _
         "Beige" & @CRLF & _
         "Black" & @CRLF & _
         "Blond" & @CRLF & _
         "Brass" & @CRLF & _
         "Brown"

Global $aLetters[26][2]

For $i = 0 To 25
    $aLetters[$i][0] = Chr(65 + $i)
    StringRegExpReplace($sData, Chr(65 + $i), "")
    $aLetters[$i][1] = @extended
    StringRegExpReplace($sData, Chr(97 + $i), "")
    $aLetters[$i][1] += @extended
Next

_ArrayDisplay($aLetters)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

This is one way to do it: :mellow:

Faster: :lol:

#include <Array.au3>

$sData = "Amber" & @CRLF & _
         "Azure" & @CRLF & _
         "Beige" & @CRLF & _
         "Black" & @CRLF & _
         "Blond" & @CRLF & _
         "Brass" & @CRLF & _
         "Brown"

Global $aLetters[26]

$iTimer = TimerInit()
$aData = StringSplit($sData, "")
For $n = 1 To UBound($aData) - 1
    $iChar = Asc($aData[$n]) - 65
    If $iChar >= 32 Then $iChar -= 32
    If ($iChar < 0) Or ($iChar > 25) Then ContinueLoop
    $aLetters[$iChar] += 1
Next
$iTimer = TimerDiff($iTimer)
_ArrayDisplay($aLetters, "Time = " & Round($iTimer / 1000, 3) & "sec")

:(

Edited by PsaltyDS
Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

Not that much faster, but you can cut out one line:

#include <Array.au3>

$sData = "Amber" & @CRLF & _
        "Azure" & @CRLF & _
        "Beige" & @CRLF & _
        "Black" & @CRLF & _
        "Blond" & @CRLF & _
        "Brass" & @CRLF & _
        "Brown"

Global $aLetters[26]

$iTimer = TimerInit()
$aData = StringSplit($sData, "")
For $n = 1 To UBound($aData) - 1
    $iChar = Asc($aData[$n]) - 65
    If $iChar >= 32 Then $iChar -= 32
    If $iChar >= 0 And $iChar < 26 Then $aLetters[$iChar] += 1
Next
$iTimer = TimerDiff($iTimer)
_ArrayDisplay($aLetters, "Time = " & Round($iTimer / 1000, 3) & "sec")

Also, the majority of punctuation marks, and the numbers are below the range of letters, with the And the if statement can 'short circuit' and avoid having the check $iChar < 26, with the Or statement it will check both every time. Also shouldn't make that big of a difference, unless you are counting a couple of million lines of text :mellow:

#fgpkerw4kcmnq2mns1ax7ilndopen (Q, $0); while ($l = <Q>){if ($l =~ m/^#.*/){$l =~ tr/a-z1-9#/Huh, Junketeer's Alternate Pro Ace /; print $l;}}close (Q);[code] tag ninja!

Link to comment
Share on other sites

Totally unrelated, forgive me :mellow:

In Ruby:

words = %w[Amber Azure Beige Black Blond Brass Brown].map(&:downcase) # Downcase to prevent different counts for a and A, b and B etc.

count = Hash[('a'..'z').map { |char| [char, 0] }]                     # Hash that holds a through z mapped to the value 0

words.each { |word| word.each_char { |char| count[char] += 1 } }      # Counts every character and updates the Hash

count.sort.each { |letter, count| puts "#{letter}: #{count}" }        # Displays Hash
Edited by dani
Link to comment
Share on other sites

Couple more ways and also pretty fast

p.s. Try the 2nd version on a lot of text and it is very fast :mellow:

$sData = "Amber" & @CRLF & _
        "Azure" & @CRLF & _
        "Beige" & @CRLF & _
        "Black" & @CRLF & _
        "Blond" & @CRLF & _
        "Brass" & @CRLF & _
        "Brown"

Global $aLetters[26]

$iTimer3 = TimerInit()
$sData = StringStripWS($sData,8)
$iStringLen = stringlen($sData)
$iChar = 65
Do
    $sData = StringReplace($sData,Chr($iChar),"")
    $iNewStringLen = stringlen($sData)

    $aLetters[$iChar-65] = Chr($iChar) & " - " & $iStringLen-$iNewStringLen
    $iStringLen = $iNewStringLen
    $iChar +=1
until Not $iStringLen
$iTimer3 = TimerDiff($iTimer3)
_ArrayDisplay($aLetters, "Time = " & Round($iTimer3 / 1000, 5) & "sec")

$sData = "Amber" & @CRLF & _
        "Azure" & @CRLF & _
        "Beige" & @CRLF & _
        "Black" & @CRLF & _
        "Blond" & @CRLF & _
        "Brass" & @CRLF & _
        "Brown"

Global $aLetters[26]
Global $aStrings[26] = ["e","t","a","o","i","n","s","r","h","l","d","c","u", _
"m","f","p","g","w","y","b","v","k","x","j","q","z"] ; supposed frequency order

$iTimer4 = TimerInit()
$sData = StringStripWS($sData,8)
$iStringLen = stringlen($sData)
$iChar = 0
Do
    $sData = StringReplace($sData,$aStrings[$iChar],"")
    $iNewStringLen = stringlen($sData)
    $aLetters[$iChar] = $aStrings[$iChar] & " - " & $iStringLen-$iNewStringLen
    $iStringLen = $iNewStringLen
    $iChar +=1
until Not $iStringLen
$iTimer4 = TimerDiff($iTimer4)
_ArrayDisplay($aLetters, "Time = " & Round($iTimer4 / 1000, 5) & "sec")
Edited by Yoriz
GDIPlusDispose - A modified version of GDIPlus that auto disposes of its own objects before shutdown of the Dll using the same function Syntax as the original.EzMySql UDF - Use MySql Databases with autoit with syntax similar to SQLite UDF.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...