|
Post by jarychk on Jul 17, 2022 1:29:59 GMT
This one is still open to user input problems happening but if only alphabet given and nothing else, program example finds if two strings are anagram or not anagram. I spent more than 2 hours on this; maybe 3 hours. I would not pass coding interviews.
dim string1$(75) dim string2$(75) abc1$="" abc2$=""
PRINT "Tell your two strings and program will analyze to find if anagrams." INPUT "First String? ";raww1$ INPUT "Second string? ";raww2$
if len(raww1$)<>len(raww2$) then print "Bad from the start. Program ending." end end if
LET lcww1$=lower$(raww1$) LET lcww2$=lower$(raww2$)
for i=1 to len(lcww1$) string1$(i)=mid$(lcww1$,i,1) string2$(i)=mid$(lcww2$,i,1) print string1$(i);" ";string2$(i) 'checks well next i
'SORTING HERE sz=len(lcww1$) flagswitched=1 while flagswitched=1 flagswitched=0 for i0=1 to sz-1 if string1$(i0)>string1$(i0+1) then let tmp$=string1$(i0) let string1$(i0)=string1$(i0+1) let string1$(i0+1)=tmp$ flagswitched=1 end if next i0 wend sz=len(lcww2$) flagswitched=1 while flagswitched=1 flagswitched=0 for i0=1 to sz-1 if string2$(i0)>string2$(i0+1) then let tmp$=string2$(i0) let string2$(i0)=string2$(i0+1) let string2$(i0+1)=tmp$ flagswitched=1 end if next i0 wend
REM use the two string arrays to build the aphabetic sored each abc1$ and abc2$ 'reordering each aphabetically for i0=1 to len(lcww1$) abc1$=abc1$+string1$(i0) abc2$=abc2$+string2$(i0) next i0
print print "Program reached through several steps, but is still unfinished as a program. " print abc1$;" against ";abc2$ print
flaganag=1 for i2=1 to sz if mid$(abc1$,i2,1)<>mid$(abc2$,i2,1) then flaganag=0 next i2
if flaganag=1 then print "Yes, anagrams." else print "NOT anagrams." end if
END
|
|
|
Post by honkytonk on Jul 17, 2022 9:18:30 GMT
Other way
a$="abcdefg" b$="agbecfd" for x=1 to len(a$) for y=1 to len(b$) if mid$(a$,x,1)=mid$(b$,y,1) then mark=mark+1 next y next x if mark=len(b$) and mark=len(a$) then notice, "Anagram !"
|
|
|
Post by tsh73 on Jul 17, 2022 14:01:25 GMT
Honkytonk, your program fails on
a$="abcdeefg" b$="agbeecfd"
(does not tell it is anagram)
|
|
|
Post by tsh73 on Jul 17, 2022 14:09:50 GMT
Jarych, you can use build-it SORT and for checking if sorted stuff match, you just can check arrays (no need to build string)
dim string1$(75) dim string2$(75) abc1$="" abc2$=""
PRINT "Tell your two strings and program will analyze to find if anagrams." INPUT "First String? ";raww1$ INPUT "Second string? ";raww2$
LET lcww1$=lower$(raww1$) LET lcww2$=lower$(raww2$)
N=len(lcww1$) if N<>len(raww2$) then print "*Different length*." end end if
for i=1 to N string1$(i)=mid$(lcww1$,i,1) string2$(i)=mid$(lcww2$,i,1) print string1$(i);" ";string2$(i) 'checks well next
'SORTING HERE sort string1$(),1,N sort string2$(),1,N
flaganag=1 for i=1 to N if string1$(i)<>string2$(i) then flaganag=0 next
if flaganag=1 then print "Yes, anagrams." else print "NOT anagrams." end if
END
|
|
|
Post by jarychk on Jul 17, 2022 21:01:03 GMT
Jarych, you can use build-it SORT and for checking if sorted stuff match, you just can check arrays (no need to build string) dim string1$(75) dim string2$(75) abc1$="" abc2$=""
PRINT "Tell your two strings and program will analyze to find if anagrams." INPUT "First String? ";raww1$ INPUT "Second string? ";raww2$
LET lcww1$=lower$(raww1$) LET lcww2$=lower$(raww2$)
N=len(lcww1$) if N<>len(raww2$) then print "*Different length*." end end if
for i=1 to N string1$(i)=mid$(lcww1$,i,1) string2$(i)=mid$(lcww2$,i,1) print string1$(i);" ";string2$(i) 'checks well next
'SORTING HERE sort string1$(),1,N sort string2$(),1,N
flaganag=1 for i=1 to N if string1$(i)<>string2$(i) then flaganag=0 next
if flaganag=1 then print "Yes, anagrams." else print "NOT anagrams." end if
END
I THOUGHT I could use the JB's built-in SORT function or command, and I DID TRY the SORT command, but it did not do anything! Really. I tried it. It did not work. I then rewrote part of my program and specifically wrote a section of code to perform the sorting, I believe as a 'bubble' sort. NOW this way, the program works. If you want, and if I still did not change the previous 'version' of this program, I could post its code for you to look and think about.
|
|
|
Post by jarychk on Jul 17, 2022 21:11:00 GMT
This is the first version of this anagram checking program which uses SORT. This example program is UNFINISHED and in some manner does not fully work. The better version is the one posted yesterday my first post on the topic.
Here is the program which I tried to rely on JB's SORT, which I say does not work:
'anagramyesorno.bas july 16,2022 'failure. sort does not work and no result after. 'Improved, but one of the checks shows a fail.
dim string1$(75) dim string2$(75) abc1$="" abc2$=""
PRINT "Tell your two strings and program will analyze to find if anagrams." INPUT "First String? ";raww1$ INPUT "Second string? ";raww2$
if len(raww1$)<>len(raww2$) then print "Bad from the start. Program ending." end end if
LET lcww1$=lower$(raww1$) LET lcww2$=lower$(raww2$)
'must relearn how to sort. 'Trying the lazier way instead: for i=1 to len(lcww1$) string1$(i)=mid$(lcww1$,i,1) string2$(i)=mid$(lcww2$,i,1) print string1$(i);" ";string2$(i) 'checks well next i ' print "Is fail to sort? " sort string1$(), 1,len(lcww1$) sort string2$(), 1,len(lcww2$) 'not to differ since both string same size
rem a check loop only look at sort results rem why this loop fail to show these results? for c=1 to len(lcww$) print string1$(c);" ";string2$(c) next c ' 'reordering each aphabetically for i0=1 to len(lcww1$) abc1$=abc1$+string1$(i0) abc2$=abc2$+string2$(i0) next i0
print print "Program reached through several steps, but is still unfinished as a program. " print abc1$;" against ";abc2$
END
|
|
|
Post by tsh73 on Jul 17, 2022 21:22:44 GMT
rem a check loop only look at sort results rem why this loop fail to show these results? for c=1 to len(lcww$) print string1$(c);" ";string2$(c) next c
Because you don't have lcww$ in your code it cames empty, "" and len(empty) is 0 So FOR c=1 to 0 doesn't actually do a thing.
|
|
|
Post by jarychk on Jul 18, 2022 1:10:54 GMT
rem a check loop only look at sort results rem why this loop fail to show these results? for c=1 to len(lcww$) print string1$(c);" ";string2$(c) next c
Because you don't have lcww$ in your code it cames empty, "" and len(empty) is 0 So FOR c=1 to 0 doesn't actually do a thing. Maybe I see? I made a brief listing of a couple of variables being lcww 1$ and lcww 2$. Suddenly I wrote a piece of code with wrong spelling for a variable, therefore empty string... Thanks. I do though like the fact I recreated a sorting routine with my own skill.
|
|
|
Post by honkytonk on Jul 18, 2022 16:31:25 GMT
Honkytonk, your program fails on a$="abcdeefg" b$="agbeecfd"
(does not tell it is anagram) But yes, he says it's an anagram
|
|
|
Post by tsh73 on Jul 18, 2022 18:55:25 GMT
>But yes, he says it's an anagram may be you have some other version But version posted in post #2 did not.
I made it show - len(a$) and len(b$) is 8, but mark ends to be 10...
|
|
|
Post by honkytonk on Jul 18, 2022 22:39:22 GMT
>But yes, he says it's an anagram may be you have some other version But version posted in post #2 did not. I made it show - len(a$) and len(b$) is 8, but mark ends to be 10... On my machine (XP) mark=7; len(a$)=7; len(b$)=7->=> Notice,'It's anagram'
|
|
|
Post by tsh73 on Jul 19, 2022 0:40:08 GMT
example I posted
a$="abcdeefg" b$="agbeecfd" has len() =8 Letter E is doubled, and it confuses your program.
|
|
|
Post by plus on Jul 19, 2022 2:12:48 GMT
No Sort Anagram testing by anaCoding words:
test$(0) = "grmaana" test$(1) = "angiogram" test$(2) = "naagrma" test$(3) = "telgram" test$(4) = "gramana" AC$ = anaCode$("anagram") for i = 0 to 4 if AC$ = anaCode$(test$(i)) then print test$(i);" is an anagram of 'anagram'" else print test$(i);" is NOT an anagram of 'anagram'" end if next
Function anaCode$ (wrd$) Dim L(26) w$ = Upper$(wrd$) For i = 1 To Len(wrd$) p = Asc(Mid$(w$, i, 1)) - 64 ' A=1, B=2... L(p) = L(p) + 1 Next For i = 1 To 26 rtn$ = rtn$; Str$(L(i)) Next anaCode$ = rtn$ End Function
|
|