Recent

Author Topic: Please help  (Read 10675 times)

q250

  • New Member
  • *
  • Posts: 20
Please help
« on: November 12, 2023, 09:21:29 pm »
Hello everybody. I have problem: I need to write program but iam not programmer. Full algorithm of actions:

Program read any file and check if it not has .cmprs extension, if it not has it check it size, if it equal to 8192 bit then read it in binary and check if it not consist of only zeroes or ones. If all checks is true then do subroutines 1, else check if it has .cmprs extension and it size is smaller than 8192 bit, if true then do subroutines 2, else print “error” and HALT.

Subroutines 1: open file as binary text string, ie all 1 and 0 now become text character. Delete from that string from left all zeroes and one one, that one included, if we get empty string use exemption rule. Declare variable N that equal to number of bits of resulting string. Declare variable D10 that equal to that string value in base10, ignoring all zeroes from left, example string 0101 has integer value 5, string 101 also has integer value 5. Declare variable A that equal to N + D10. Now add 1 to A, ie A:=A+1. Now declare variable B that equal to Floor from log(A) base 2. Now declare variable R that equal to R:=A – 2^B. Now construct binary text string that equal to R in binary and add as much 0 from left as much it needed to make it overall size equal B, example B=3 R=2: firstly get string from R = 10, then add so much zeroes, in our case only one, and final answer 010. Now save that string with name of original file, add low dash and old extension letters, and .cmprs extension and HALT. Exemption rule: just write 0 in new string, save that string with name of original file, add low dash and old extension letters, and .cmprs extension and HALT.

Subroutines 2: : open file as binary text string. Declare variable N that equal to number of bits of resulting string. Declare variable D10 that equal to that string value in base10, ignoring all zeroes from left, example string 0101 has integer value 5, string 101 also has integer value 5. Declare variable A that equal to N + D10. Now subtract 1 from A, ie A:=A-1. If A turn out to be 1 then use exemption rule. Now declare variable B that equal to Floor from log(A) base 2. Now declare variable R that equal to R:=A – 2^B. Now construct binary text string that equal to R in binary and add as much 0 from left as much it needed to make it overall size equal B. Now add to that string one one from left, and add even further to left as much zeroes it needed to make overall string size equal to 8192. Now save that string as binary file under old file name, removing .cmprs extension from it and replace low dash with dot, ie if for example old name was music_mp3.cmprs now it become music.mp3 and HALT. Exemption rule: just write 1 in new string and as much 0 needed to make overall size equal to 8192, now save that string as binary file under old file name, removing .cmprs extension from it and replace low dash with dot and HALT.

If you interested in context and want to know what this program does let me know.

jamie

  • Hero Member
  • *****
  • Posts: 7884
Re: Please help
« Reply #1 on: November 12, 2023, 09:50:17 pm »
Let's see, its Sunday for me, nice homework assignment, hope you do well when it's due.
The only true wisdom is knowing you know nothing

TRon

  • Hero Member
  • *****
  • Posts: 4377
Re: Please help
« Reply #2 on: November 12, 2023, 10:00:35 pm »
There is a special jobs sub-forum for job offers and requests.
Today is tomorrow's yesterday.

jamie

  • Hero Member
  • *****
  • Posts: 7884
Re: Please help
« Reply #3 on: November 12, 2023, 10:47:18 pm »
There is a special jobs sub-forum for job offers and requests.

You are just too easy going, my NICE lever is stuck atm. :o

The only true wisdom is knowing you know nothing

Joanna

  • Hero Member
  • *****
  • Posts: 1464
Re: Please help
« Reply #4 on: November 13, 2023, 01:29:40 am »
First things first..
Although it is not a bad idea to write down what you want to do in English, you are far from finished.

1. Learn enough about pascal language to get started.
2. Organize Your thoughts into well defined tasks. All I can see is a large block of disorganized text.  ;D
3. Translate from English to pascal.

« Last Edit: November 13, 2023, 02:15:04 am by Joanna »

cdbc

  • Hero Member
  • *****
  • Posts: 2921
    • http://www.cdbc.dk
Re: Please help
« Reply #5 on: November 13, 2023, 08:18:27 am »
Hi
Ok, I'll chime in to get you started  :D
Code: Pascal  [Select][+][-]
  1. program TextCmp;
  2. {$mode objfpc}{$H+}
  3. uses sysutils;
  4.  
  5. var
  6.   S: string;
  7.   Infile: textfile;
  8.  
  9. begin
  10.   (* TAG... Now you're IT*)
  11. end.
  12.  
Have fun  8-)
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE6/QT6 -> FPC Release -> Lazarus Release &  FPC Main -> Lazarus Main

tr_escape

  • Sr. Member
  • ****
  • Posts: 437
  • sector name toys | respect to spectre
    • Github:
Re: Please help
« Reply #6 on: November 13, 2023, 08:23:08 am »
Ok.

I asked chatGPT sorry :)

Quote
Absolutely, I can help you transform this logic into Pascal code. It seems to involve various file operations, string manipulations, and binary conversions. Let's break down the logic and convert it into Pascal syntax.

For instance, you might start with opening and checking the file's extension and size:

```pascal
Code: Pascal  [Select][+][-]
  1. program FileProcessor;
  2.  
  3. uses
  4.   SysUtils;
  5.  
  6. var
  7.   fileName, fileExtension: string;
  8.   fileSize: Int64;
  9.  
  10. begin
  11.   // Code to get the file name and its extension
  12.  
  13.   fileName := 'example'; // Replace 'example' with the actual file name
  14.   fileExtension := ExtractFileExt(fileName);
  15.  
  16.   if fileExtension <> '.cmprs' then
  17.   begin
  18.     // Check file size
  19.     fileSize := FileSize(fileName);
  20.    
  21.     if fileSize = 8192 then
  22.     begin
  23.       // Read file in binary
  24.       // Check if it consists only of 0s or 1s
  25.       // Perform Subroutines 1
  26.     end
  27.     else
  28.     begin
  29.       // Check for .cmprs extension and its size
  30.       // Perform Subroutines 2
  31.     end;
  32.   end
  33.   else
  34.   begin
  35.     // Print "error" and HALT
  36.     writeln('Error');
  37.     Halt;
  38.   end;
  39.  
  40. end.
```

This is a basic structure where you'd need to implement file operations, binary reading, and the specific logic defined in your description. You can continue by building the logic for Subroutines 1 and 2 as defined in your initial description. If you want help with a particular part or have specific questions, feel free to ask!

q250

  • New Member
  • *
  • Posts: 20
Re: Please help
« Reply #7 on: November 13, 2023, 03:47:29 pm »
write down what you want to do
i want that program take on input string from left column and output string from right colomn and vice-versa. here exaples for cases N = 4 and N = 5. i describe programm with N = 8192. also how to turn on long arithmetic?
https://imageupload.io/en/a4U93vHeM2Jc2Tg

Bart

  • Hero Member
  • *****
  • Posts: 5755
    • Bart en Mariska's Webstek
Re: Please help
« Reply #8 on: November 13, 2023, 04:56:37 pm »
Program read any file and check if it not has .cmprs extension, if it not has it check it size, if it equal to 8192 bit then read it in binary and check if it not consist of only zeroes or ones. If all checks is true then do subroutines 1, else check if it has .cmprs extension [snip]
Maybe it's just english language problem, but if I reads and interpret this as a programmer, the else condition will never be met, since the else belongs to the branch that only handles files that do not have a .cmprs extension ...

Subroutine 1 baffles me.
Let's say I have a binary file of 1024 bytes.
You want me to rranslate those bytes into a string representation of the binary values?
But if the file starts with 0x00 0xFF, must that be interpreted as 0000000011111111 or as 1111111100000000 (e.g. either 2 bytes or 1 word, and in the latter case: big-endian or little-endian, and of course this byte sequence can also be interpreted as dword or qword or 128-bit or 256-bit or 512-bit or 1024-bit values???)

Quote
Delete from that string from left all zeroes and one one, that one included
Not sure what that means.

Quote
Now declare variable B that equal to Floor from log(A) base 2. Now declare variable R that equal to R:=A – 2^B. Now construct binary text string that equal to R in binary and add as much 0 from left as much it needed to make it overall size equal B
What is the purpose of all of this?
Or is it just the teacher pestering the students.

After that I did not not bother reading routine2.

Quote
i want that program take on input string from left column and output string from right colomn and vice-versa
I don't see the algorithm behind the conversion from left to right. The right to left conversion is undecidable if the right is empty string (should it be converted to 0000 or 1111?).

Quote
also how to turn on long arithmetic?
Meaning what exactly?
Big integers?

Bart


Bart

  • Hero Member
  • *****
  • Posts: 5755
    • Bart en Mariska's Webstek
Re: Please help
« Reply #9 on: November 13, 2023, 07:34:19 pm »
An example might improve things a little.
Given a file: 'test.cmprs', it's size is 16 bytes.
It's contents are :
$00 $01 $02 $03 $04 $05 $06 $07 $08 $09 $0A $0B $0C $0D $0E $0F
Reading that as a binarys string would give:
Code: [Select]
00000000000000010000001000000011000001000000010100000110000001110000100000001001000010100000101100001100000011010000111000001111
N would be 64.
D10 would be a big integer, not fitting into the largest integer that fpc supports natively: 5233100606242806050955395731361295 if I'm not mistaken.
A would be 5233100606242806050955395731361295 +16 - 1 = 5233100606242806050955395731361310
B = Log2(A), which I think turns out to be the number (X) of binary digits after the left most '1' (I may be off-b-one here).
2^B would then become a 1 followed by X zero's.
In effect we have masked out all 1's after the left most 1 we found.

R := A - 2^B: the original number - the number with all 1's zeroed out: so it leaves the masked out 1's,
So we end up with a 1 with 15*8-1 zero's in this example
We prepend as meuch zero's as to get a string with length 8192.
Now we save that string as a binary file: i don't know what that means.
Should we write it as 1024 consecutive bytes, where the last 16 bytes are $01 followed by 15 times a $00?
(And I'm not even going to try to understand the filenaming rules here).

IIUC:
For most numbers (if you see the file as a large 8192-bit number) the net eefect will be that the result will be the same as finding the first 1 digit, changing that to a 0 and writing it back.
Problems may arise if the first step actually changes where the first 1 digit is (imaging the original number being '00011111111111111111111111111111111111111111111111111'. Adding anything will change the position of the left most 1 bit).

Doing all this maipulating strings, converting to and from some integer type seems rather a dumb idea.
Having a good biginteger library that supports binary and/or operations as well as bsr/bsf operations and/or something like LeadingZeros function would make this a lot easier I think.

Still none the wizer as to the purpose of all of this though.

Bart


q250

  • New Member
  • *
  • Posts: 20
Re: Please help
« Reply #10 on: November 13, 2023, 10:10:14 pm »
Big integers?
yes, we need that. 1Kb long integers more precisely.
Maybe it's just english language problem, but if I reads and interpret this as a programmer, the else condition will never be met, since the else belongs to the branch that only handles files that do not have a .cmprs extension ...
ok, lets rephrase this part. we load file and check if it eligible for subroutines 1, or subroutines 2 or, if not eligible for any, print "error" and stop. for subroutines 1(code) file must: not have .cmprs extension; have size exactly  8192 bit; it not consist of only zeroes or ones. for subroutines 2(decode) file must: have .cmprs extension; it size is smaller than 8192 bit.
You want me to rranslate those bytes into a string representation of the binary values?
yes.
But if the file starts with 0x00 0xFF, must that be interpreted as 0000000011111111 or as 1111111100000000 (e.g. either 2 bytes or 1 word, and in the latter case: big-endian or little-endian, and of course this byte sequence can also be interpreted as dword or qword or 128-bit or 256-bit or 512-bit or 1024-bit values???)
honestly i dont know. i dont know what 0xFF even means. only i know that modern computers work in binary, and interpretation must be consistent, ie if we interpret this value in some way in subroutines 1, then we must interpret this value same way in subroutines 2.
Not sure what that means.
examples for this procedure, ignoring everything else: string 001100, this procedure would give us string 100;  string 101100, this procedure would give us string 01100;  string 000011, this procedure would give us string 1; string 000001, this procedure would give us empty string; string 100000, this procedure would give us string 00000; string 111110, this procedure would give us string 1111; strings 000000 or 111111 is not valid inputs, in case of subroutines 1 and if we used number 6 insted of 8192.
What is the purpose of all of this?
lets constuct set of all binary strings, start with empty. now assign natural numbers to it. it would look like this - Ø-1; 0-2; 1-3; 00-4; 01-5 and so on. call it table M, on left column binary strings, on right natural numbers. purpose of all of this is - to find integer value for that string, add one to it, then find what binary string stand assigned to that new integer value. then we simply save that string with full old name, replacing dot with low dash in old extention, adding .cmprs. examples: test should become test.cmprs; test .rar should become test_rar.cmprs.
Or is it just the teacher pestering the students.
no, its not my school homework. aslo not school boy, not even college student.
I don't see the algorithm behind the conversion from left to right.
but i do, and trying to explain it to everyone.
The right to left conversion is undecidable if the right is empty string (should it be converted to 0000 or 1111?)
yes, it is. that reason we simply dont use this values.

Bart

  • Hero Member
  • *****
  • Posts: 5755
    • Bart en Mariska's Webstek
Re: Please help
« Reply #11 on: November 13, 2023, 10:58:50 pm »
Without knowing something more about the context, this is going nowhere IMO.
What are these files you want to convert: what does it's content actually mean.

Quote
i dont know what 0xFF even means. only i know that modern computers work in binary

OK, so it seems you are almost completely agnostic about how programs save files in a computer...

B.t.w "0xFF" means Hexadecimal FF, which equals Decimal 255 (or Binary 11111111), it's just a way of notating that most programmers will understand.
In Pascal we use the dollarsign ($) as a prefix to denote hexadecimal values, so we would write $FF.

The fact that computers "work in binary" may or mat not have any relevance to the thing you are trying to accomplish (which I stll do not understand).

Given that you can see every file on disk as being "binary", does not mean that this is in general the most logical way to "look at" the file.
For example a file that starts with the 4 bytes 65 66 67 68 69 might be easier to understand if you know that this sequence represent the string 'ABCD' (wihtout the quotes).
On the other hand, this file might actually represent some data like: a char (A), a Word (composed of 2 bytes, it represents the value 17218) and another Word (17732).
So, wthout knowing the the purpose (and therefore the internal structure) of the file in question, parsing and processing it is a PITA.

And I still really do not understand why you want to convert everything to a string and then do calculations with/on it.

Obviously the files in question do have some meaning, as the converted files will also have.
It just would be really helpfull to know what this is.
What program creates these files?

Bart
« Last Edit: November 13, 2023, 11:01:15 pm by Bart »

q250

  • New Member
  • *
  • Posts: 20
Re: Please help
« Reply #12 on: November 14, 2023, 12:13:08 am »
Given a file: 'test.cmprs', it's size is 16 bytes.
we find bug. this file is eligible for subroutines 2, but when we will try to save this we will not do that, becouse we assume that every file that go in subroutines 1 have extention, need to make exeption to naming rule. but if we ignore that bug then give me moment i will compute this step by step.
Without knowing something more about the context, this is going nowhere IMO.
What are these files you want to convert: what does it's content actually mean.
it data compression. take all, exept two, 1Kbytes files, and give out files that at least 1 bit smaller. here context:
https://getshared.com/fDHUtsFh
wthout knowing the the purpose (and therefore the internal structure) of the file in question, parsing and processing it is a PITA.
no, we dont care about internal structure, only binary.

Fibonacci

  • Hero Member
  • *****
  • Posts: 1077
  • Behold, I bring salvation - Unleashed Pascal
    • fibo.gg
Re: Please help
« Reply #13 on: November 14, 2023, 12:50:25 am »
Unleashed Pascal: async/await, parallel for, match, tuples, string interpolation, inline vars, autofree, no-RTTI & tons more. Star on GitHub

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2269
  • Fifty shades of code.
    • Delphi & FreePascal
Re: Please help
« Reply #14 on: November 14, 2023, 01:02:50 am »
So you want to do this:

https://en.wikipedia.org/wiki/Sloot_Digital_Coding_System

right?
I dont think so. Remark his first post, he want to compute exact 1kb (1024 byte/8192 bit).

@OP, your explainations are way to vague and after rephrasing its even more going off track.
As already mentioned, write down exactly what you want to achieve, the only two things that are currently clear is that you want to check for fileextension and filesize.

Before you rephrase, take a look on Pascals Integer types and its sizes, since what you write is currently with normal data types impossible to do in one step.

Append some example file(s) so we better understand what you try to compute.
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

 

TinyPortal © 2005-2018