AI writes a code for b4a

Hamied Abou Hulaikah

Well-Known Member
Licensed User
Longtime User
Wow
URL; https://www.perplexity.ai/
Question: b4a code get x times of any number in 3,4,6,90,4,6,7,7,4
Answer:
B4X:
Dim arr() As Int = Array As Int(3, 4, 6, 90, 4, 6, 7, 7, 4)
Dim count As Map
count.Initialize

For Each num As Int In arr
    If count.ContainsKey(num) Then
        count.Put(num, count.Get(num) + 1)
    Else
        count.Put(num, 1)
    End If
Next

For Each key As Int In count.Keys
    Log("The value " & key & " appears " & count.Get(key) & " times in the array.")
Next
 

Hamied Abou Hulaikah

Well-Known Member
Licensed User
Longtime User
Also regex pattern was nightmare for me, but with these AI chatbots, it simplify it extremely.
for example:
Q: what is b4a regex of email address in string "everyone can email me to "[email protected] I'll respond within 2 hours, thanks."
A:
B4X:
Dim text As String = "everyone can email me to \"[email protected] I'll respond within 2 hours, thanks."
Dim pattern As String = "[\w.-]+@[\w.-]+\.[\w]+"

Dim regex As Regex
regex.Initialize(pattern, False)

Dim match As Matcher
match = regex.Matcher(text)

If match.Find Then
    Dim email As String
    email = match.Group(0)
    Log("The email address is: " & email)
Else
    Log("No email address found.")
End If

WOW
 

apti

Member
Wow
URL; https://www.perplexity.ai/
Question: b4a code get x times of any number in 3,4,6,90,4,6,7,7,4
Answer:
B4X:
Dim arr() As Int = Array As Int(3, 4, 6, 90, 4, 6, 7, 7, 4)
Dim count As Map
count.Initialize

For Each num As Int In arr
    If count.ContainsKey(num) Then
        count.Put(num, count.Get(num) + 1)
    Else
        count.Put(num, 1)
    End If
Next

For Each key As Int In count.Keys
    Log("The value " & key & " appears " & count.Get(key) & " times in the array.")
Next
perplexity is garbage. worse than chat gpt. I just tried it out and all it does is give invalid flawed code. when you tell it the errors it just repeats them. anybody have something better that works?
 

emexes

Expert
Licensed User
Longtime User
Grok started off on a different (and reasonable) tack but a few hints brought it around to the OP interpretation:

https://x.com/i/grok/share/eFhhzeolesvkvMfnwVnCawKMc

B4X:
Sub Activity_Create(FirstTime As Boolean)
    Dim numbers() As Int = Array As Int(3, 4, 6, 90, 4, 6, 7, 7, 4)
    Dim countMap As Map
    countMap.Initialize
    
    ' Count occurrences of each number
    For Each num As Int In numbers
        If countMap.ContainsKey(num) Then
            countMap.Put(num, countMap.Get(num) + 1)
        Else
            countMap.Put(num, 1)
        End If
    Next
    
    ' Display results with "occurs" and "time" or "times" based on count
    Dim result As String
    For Each key As Int In countMap.Keys
        Dim count As Int = countMap.Get(key)
        Dim timeWord As String
        If count = 1 Then
            timeWord = "time"
        Else
            timeWord = "times"
        End If
        result = result & "Number " & key & " occurs " & count & " " & timeWord & CRLF
    Next
    
    Log(result)
    Msgbox(result, "Number Counts")
End Sub
 

Hamied Abou Hulaikah

Well-Known Member
Licensed User
Longtime User
Currently my AI favorite is Calude sonnet 3.7 inside windsurf IDE.
It cost me 10$/month
 
Top