Forum > Beginners
FreePascal: find all 3-symbol unique substring in string
MarkMLl:
--- Quote from: nixu3 on December 23, 2021, 10:14:59 am ---using "Set Of" in this program - impossible in FCP ?
--- End quote ---
Only for simple types (i.e. individual characters etc.).
Please note that if you want people to help you need to copy the /exact/ error message you're seeing, including line number etc.
Also note that your teacher is probably watching, and if anybody does the entire job for you you'll get zero marks.
Having said that, there's an elegant way of doing this job which doesn't require any fancy language facilities.
* Work through the initial string character-by-character, extracting every three-letter group (i.e. including the overlap) into an array of three-letter strings.
* Sort that array.
* Count the remaining unique strings, skipping or counting duplicates.
MarkMLl
Martin_fr:
--- Quote from: nixu3 on December 23, 2021, 10:14:59 am ---using "Set Of" in this program - impossible in FCP ?
--- End quote ---
For your purpose: Yes, impossible.
There is "TStringList" that can help you.
There are various "classes" to replace it, that may reduce your work by one or maybe two lines of code.
However, unless your teacher gave you the name of them; or unless you truly found and understand them all by yourself => I would really recommend not to go for any of them (hence I do not name them here).
TStringList is a class.
- So you need to "Create" it.
- You can use:
mylist.Add() to add new entries
mylist.IndexOf to check if an entry exists.
Instead of IndexOp, you can go, find the documentation, and read it yourself. TStringList, can be configured to ignore duplicates. Then for your example it does the same work, as the set does.
marcov:
Maybe some tcollection or tstringlist would be working? I suspect there is some hash collection under the pascalabc.net construct too.
dbannon:
Personally, I'd use a stringlist. Probably not the most efficient way to solve the problem but IMHO, the easiest. But, as Mark said, you teacher will have given you some pretty clear hints on how he/she expect you to solve this, please read back over your lecture notes.
https://wiki.freepascal.org/Stringlist - in case stringlist was mentioned.
Davo
Bart:
If you do not want to use the Classes or any other unit you can use an array of string like this:
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---{$mode objfpc}{$h+} type TStringArray = array of string; function IsInList(const S: String; List: TStringArray): Boolean;var i: Integer;begin Result := False; for i := Low(List) to High(List) do if List[i] = S then Exit(True);end; var List: TStringArray; S, Sub: String; i, Idx: Integer;begin List := nil; write('Enter string: '); readln(S); if Length(S) > 2 then begin SetLength(List, Length(S)-2); Idx := -1; for i := 1 to Length(S) - 2 do begin Sub := S[i] + S[i+1] + S[i+2]; if not IsInList(Sub, List) then begin Inc(Idx); List[Idx] := Sub; end; end; SetLength(List, Idx+1); end; writeln('Number of unique substrings of 3 characters: ',Length(List)); for i := Low(List) to High(List) do writeln(i:2,': ',List[i]);end.
There are no comments in the code explaining anything, that's on purpose.
Bart
Navigation
[0] Message Index
[#] Next page
[*] Previous page