ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Usually there will be millions of lines of code .The numbers will be in sorted order but not in a consecutive manner...like after 9849201350 ,9849201400 may appear...The difference between
9849201400 -9849201350 =50 .If the difference between 2 numbers is greater than 10000 then we have to take the numbers encountered as one range.
i,e range 1: 9849201348 to 9849210000 for the above example...
If i follow the rule like taking the first line in the series and adding 10000 and taking it as one range ..But the number may not be present in the file.so shall i have to go linear way or what...Can anyone please help me out in finding a solution
Further, it reeks of elementary C homework. If I am wrong then please explain the real world situation where you need to parse the output in this manner... but I know I am not wrong.
I need to parse a huge file which contain the telecom subscriber data and prepare the ranges of the subscriber data . This is the real world example...
I would try to put all of the values in an array and then try to sort them with a fast sorting algorithm. The you can move down the array with a loop and compare values.
(defconstant +RANGE+ 10000)
(defvar *count* 0)
(defun printrange (x y z)
(setq *count* (1+ *count*))
(format t "Range ~a: ~a to ~a~%" *count* x y)
(values z))
(defun findrange (start current)
(setq a (read *standard-input* nil 'EOF))
(if (eq a 'EOF)
(printrange start current a)
(if (< +RANGE+ (- a start))
(printrange start current a)
(findrange start a))))
(setq b (read *standard-input* nil 'EOF))
(if (eq b 'EOF)
(print "ERROR: Empty File."))
(loop
(setq c (read *standard-input* nil 'EOF))
(if (eq c 'EOF) ; Last value is unmatched not part
(return)) ; of a range. End program.
(setq b (findrange b c))
(if (eq b 'EOF)
(return)))
I was bored... here is the working code... you wanted it in LISP right?
EDIT: Actually, I would love the datafile your instructor gave you... so I could run it with more than the test values and ensure I have it correct. It does produce the output for the input data you gave.
Code:
clisp range.lsp < datafile
Range 1: 9849201348 to 9849210000
Range 2: 9849215000 to 9849220000
EDIT #2: Before anyone points it out... yes the recursion is pointless... although it is one way to solve the problem... I was just playing with LISP today and decided to do it recursive style... besides... that's how LISP likes it.
Originally posted by tamoneya I would try to put all of the values in an array and then try to sort them with a fast sorting algorithm. The you can move down the array with a loop and compare values.
The values are already sorted... this is a toy problem from a programming class. It expects a nearly perfect input. The values are not incremental but they are in sequence.
None of us were born yesterday. Many of us have had dedicated courses on programming. Almost all of us can smell a homework problem from a mile away. Please, do it yourself. You learn more by doing that and we would be doing you a grave disservice if we just gave you the answer.
If it is truly not a homework assignment... use clisp (installed on many Linux distributions and available off the web) and run the program given above. If it was not a homework problem then implementation detail would be minor.
hai frobe its not a homework problem or so...I was trying to solve it ...Really i tried to solve the problem but could not able to do so.thats the reason i'm asking you the help ....As you have already done it so ...thats the reason im asking you so....right now i failed to solve the solution
The Rules The best place for your question is in the forum. We get a large number of emails. If your email contains a technical question we will kindly point you to the forums.
Please do not email your questions. If you have a very specific question regarding this issue, fine someone will help. But we are not going to do it for you. The LISP code was posted above only because it amused me and not as an indication of the help you should expect. No one here is going to give you a complete working example.
I recommend you read the following. I didn't write it but it might help you.
int main() /* Do NOT make main void... it returns an int */
/* It is horrible coding style to expect the compiler to not barf on that */
{
FILE *fp;
static char ch;
char ln[100],
fp=fopen("input.txt","r");
if (fp==NULL)
{
printf ("\n File does not exist....");
exit(0);
}
while (fgets(ln,100,fp)!=NULL)
{
/* How i read 2 lines and store the values */
/* Well, you have one line already (stored in ln)... why not */
/* Read another one? Maybe into a new variable this time? */
strcpy(ch,ln);
}
return 0;
}
int main() /* Do NOT make main void... it returns an int */
/* It is horrible coding style to expect the compiler to not barf on that */
{
FILE *fp;
char ch;
char ln[100];
char str[100];
fp=fopen("input.txt","r");
if (fp==NULL)
{
printf ("\n File does not exist....");
exit(0);
}
while (fgets(ln,100,fp)!=NULL)
{
/* How i read 2 lines and store the values */
/* Well, you have one line already (stored in ln)... why not */
/* Read another one? Maybe into a new variable this time? */
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.