Sver
Full Member
Posts: 145
|
Post by Sver on Sept 15, 2020 11:26:38 GMT -5
'Comparing two strings, with %
dim a$(65) dim b$(65)
var$ = "Liberty basic"
for i=1 to len(var$) a$(i) = mid$(var$,i,1) next i
print var$ input "Start writing: "; text$
for k=1 to len(text$) b$(k) = mid$(text$,k,1) next k
for q=1 to len(text$) for m=1 to len(var$) if a$(m) = b$(q) then counter = counter + 1 exit for end if next m next q
print "" print counter percent$ = using("##.##", counter*100/len(var$)) print percent$+Chr$(37) end
|
|
|
Post by tsh73 on Sept 15, 2020 15:44:53 GMT -5
Could be made a bit shorter / likely faster
'Comparing two strings, with %
var$ = "Liberty basic"
print var$ input "Start writing: "; text$
for q=1 to len(text$) counter = counter + (instr(var$, mid$(text$,q,1))>0) next q
print "" print counter percent$ = using("##.##", counter*100/len(var$)) print percent$+Chr$(37) end
|
|
Sver
Full Member
Posts: 145
|
Post by Sver on Sept 16, 2020 1:31:44 GMT -5
Yes, a little bit shorter. Thanks ! Actually, it's comparing characters, but somethings usefull.
House and esuoH give a 100% match.
|
|
|
Post by Chris Iverson on Sept 16, 2020 2:41:14 GMT -5
"house" and "eeeee" give 100% match.
Try this. This also takes strings of different length into account. (Both previous codes will report strings of differing lengths as matching, i.e. "horse" and "horsey" will be counted as 100% match.)
'Comparing two strings, with %
var$ = "house"
print var$ input "Start writing: "; text$
min = min(len(var$), len(text$)) max = max(len(var$), len(text$))
'We count to the minimum length, because there's no point comparing 'data that doesn't exist for q=1 to min counter = counter + (mid$(var$, q, 1) = mid$(text$, q, 1)) next q
print "" print counter
'we calculate the percentage using the larger string length. 'doing this automatically reduces the percentage proportionally 'if the strings are different lengths. (Each longer character will 'be counted as a non-matching character in a string of "max" length.) percent$ = using("##.##", counter*100/max) print percent$+Chr$(37) end
|
|
Sver
Full Member
Posts: 145
|
Post by Sver on Sept 16, 2020 3:00:20 GMT -5
Hmm, I didn't test it very well. But House and ou is 0% It looks easy.
|
|
|
Post by Chris Iverson on Sept 16, 2020 3:10:14 GMT -5
Is that not correct? What answer are you looking for there?
Maybe I misunderstood the question.
|
|
Sver
Full Member
Posts: 145
|
Post by Sver on Sept 16, 2020 3:15:56 GMT -5
Is that not correct? What answer are you looking for there? Maybe I misunderstood the question. House and ou Expected: 40% match
|
|
Sver
Full Member
Posts: 145
|
Post by Sver on Sept 16, 2020 5:22:49 GMT -5
'Comparing two strings, with %
dim a$(65) dim b$(65)
var$ = "house" print var$
input "Start writing: "; text$
'swap if len(var$)<len(text$) then temp$=var$ var$=text$ text$=temp$ end if
for i=1 to len(var$) a$(i) = mid$(var$,i,1) next i
for k=1 to len(text$) b$(k) = mid$(text$,k,1) next k
for q=1 to len(text$) for m=1 to len(var$) 'var langste if a$(m) = b$(q) then counter = counter + 1 a$(m)="|" 'delete found character , eeeee works exit for end if next m next q
print "" print counter percent$ = using("##.##", counter*100/len(var$)) print percent$+Chr$(37) end
'testing 'house and house 100% 'house and eeeee 20% 'after delete first found 'house and houses 83,3% 'house and oe 40% '---
'hoose and o 20% 'hoose and oo 40% 'hoose and hooose 83,3%
|
|
|
Post by tsh73 on Sept 17, 2020 9:55:11 GMT -5
There is different ways to count "similarity" Here we have "letters contained" My stab at same rules
'Comparing two strings, with %
while 1 read var$ if var$="XYZZY" then end
read text$ read score
'var$ = "house" print var$,
'input "Start writing: "; text$ print text$ print "expected score: ",score,
'swap - make text$ shorter one if len(var$)<len(text$) then temp$=var$ var$=text$ text$=temp$ end if maxLen=len(var$)
counter=0 for q=1 to len(text$) j=instr(var$, mid$(text$,q,1)) if j then counter = counter + 1 var$=left$(var$,j-1);mid$(var$,j+1) 'remove letter so not to count it twice end if next q
'print "" 'print counter percent$ = using("##.##", counter*100/ maxLen) print percent$+"%" wend end
'testing 'house and house 100% 'house and eeeee 20% 'after delete first found 'house and houses 83,3% 'house and oe 40% '---
'hoose and o 20% 'hoose and oo 40% 'hoose and hooose 83,3%
data "house","house", 100 data "house","eeeee", 20 data "house","houses", 83.3 data "house","oe", 40 data "hoose","o",20 data "hoose","oo",40 data "hoose","hooose",83.3 data "XYZZY"
|
|