• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

How to find occurences of a number in a text file - any algorithm for this?

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Supposed I have a text file having the results of the 6/49 lottery in it:
49-05-13-42-18-45
43-24-36-16-39-22
32-48-05-18-34-29
13-14-07-41-48-39
47-05-17-13-37-49
06-23-34-21-46-37
06-42-13-40-11-37
39-44-40-21-32-20
17-26-38-43-01-02
49-13-42-36-40-10

The following output must be written to a text file with filename, "occurrences.txt"?
49 - 2 times
49 appears with 13 and 42 - 2 times

I think to approach this problem, the following has to be done:
1) Convert the input text file into a machine-readable format
2) Split the items inside the file into separate fields
3) Convert all the numbers in the text file into integers (since the numbers in the text file are obviously strings, am I right?)
4) Count the occurrences of each individual number in the text file
5) Count the number of times each number appears in a trio of oft-occurring numbers
6) Transfer those recorded occurrences to an output text file

Any idea on the algorithm to go about this?
Just the algorithm - I'm not asking for any source code...
 
lowercase baba
Posts: 12856
52
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't necessarily need to convert them into integers...After all you may someday want to parse a file with input like:

AB-24-Z7-1L-62-0O

etc.  in other words, the fact that they look like integers now may not matter.  What you are really looking for is repeated 2-character patterns.  If you know they will always an only ever be 2-digit integers, then you can maybe take a few shortcuts.  You'd know they can only be 0-99.  That suggests you might be able to use an array with 100 elements to hold your count of each value found.

Now....Why 49?  is it because it's the first element in the first row, or is there some other criteria?  I don't think it's because it appears the most often, because I see 40 in there three times.

 
Bartender
Posts: 3959
155
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My idea is to build a Map<Integer, List<LineNumbers>> from the input. To determine how many times two integers appear as pair, you simply take the intersection of the two corresponding lists. Don't know about C++, but in java there's a handy method for that.
 
Piet Souris
Bartender
Posts: 3959
155
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to the Ranch. Enjoy the stay!
 
Ranch Hand
Posts: 179
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Fudgie - you've posted in the C/C++ forum, so presumably you are going to be writing your code in one of those; can you confirm which one? The intersection of corresponding lists approach will require a bit more effort in C
Cheers
John
 
Fudgie Braun
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

John Matthews wrote:Hi Fudgie - you've posted in the C/C++ forum, so presumably you are going to be writing your code in one of those; can you confirm which one? The intersection of corresponding lists approach will require a bit more effort in C
Cheers
John



I will be coding in C++.
By the way, is pseudo-code the same as algorithm?
 
Fudgie Braun
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

fred rosenberger wrote:You don't necessarily need to convert them into integers...After all you may someday want to parse a file with input like:

AB-24-Z7-1L-62-0O

etc.  in other words, the fact that they look like integers now may not matter.  What you are really looking for is repeated 2-character patterns.  If you know they will always an only ever be 2-digit integers, then you can maybe take a few shortcuts.  You'd know they can only be 0-99.  That suggests you might be able to use an array with 100 elements to hold your count of each value found.

Now....Why 49?  is it because it's the first element in the first row, or is there some other criteria?  I don't think it's because it appears the most often, because I see 40 in there three times.



Oh, as for the "49", I'm only showing how the output would look like after looking through the entire list for the occurrence of that number (and other numbers I'm interested in).

By the way, you mentioned "parse a file"?
 
Fudgie Braun
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Piet Souris wrote:My idea is to build a Map<Integer, List<LineNumbers>> from the input. To determine how many times two integers appear as pair, you simply take the intersection of the two corresponding lists. Don't know about C++, but in java there's a handy method for that.



By the way, can a Java pseudo-code about the solution better help in making a C++ source code version?
 
Fudgie Braun
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Piet Souris wrote:And welcome to the Ranch. Enjoy the stay!



Oh, I forgot - thanks for welcoming me here in this forum...
 
Saloon Keeper
Posts: 12027
257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Fudgie Braun wrote:By the way, is pseudo-code the same as algorithm?


Pseudo-code is code that is used to express abstract concepts more concretely without worrying about whether the code follows the grammar of any particular programming language. It's often a mixture of human language and programming language.

An algorithm can be expressed using pseudo-code, but an algorithm can also be expressed using an animated video or by explaining it using the spoken word.

Think of the abstract concept "electricity". You can explain electricity in a book, or a YouTube video, or in a college lecture, but none of those things IS electricity. You can explain an algorithm using pseudo-code, but pseudo-code is not an algorithm.
 
fred rosenberger
lowercase baba
Posts: 12856
52
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Parsing simply means reading in the data, and figuring out what parts are what.  Any modern language will have built-in tools to help you do this.  

One way you could try would be to read in the file a line at a time and save it to a string.  Then you can use various String methods to break apart that string into the pieces. In your case, you're going to break it apart by the dashes (and discarding them).  You'd be left with an array of six elements.

You could convert each to an integer type, or just continue using them as strings.

 
Stephan van Hulst
Saloon Keeper
Posts: 12027
257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Imagine the problem "My car is almost out of fuel".

An algorithm to solve that problem is "fill tank at a gas station".

Here's pseudo-code expressing that algorithm:
 
Fudgie Braun
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

fred rosenberger wrote:Parsing simply means reading in the data, and figuring out what parts are what.  Any modern language will have built-in tools to help you do this.  

One way you could try would be to read in the file a line at a time and save it to a string.  Then you can use various String methods to break apart that string into the pieces. In your case, you're going to break it apart by the dashes (and discarding them).  You'd be left with an array of six elements.

You could convert each to an integer type, or just continue using them as strings.



So you're saying I need to put the input text file into an array?
But before I put that into an array, I need to change each line into a string then put all those into the array?
 
Marshal
Posts: 69495
277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Fudgie Braun wrote:. . .  Java pseudo-code . . . help in making a C++ source code version?

Pseudo‑code is independent of implementation language.
 
fred rosenberger
lowercase baba
Posts: 12856
52
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Fudgie Braun wrote:So you're saying I need to put the input text file into an array?
But before I put that into an array, I need to change each line into a string then put all those into the array?


No.  You asked what parsing was, and I was trying to explain that, using your problem as an example.

What you need to do is figure out your algorithm.  Forget about java, arrays, parsing...just think in English (or whatever your preferred natural language is...).  start describing what you need to do, step by step.  then go back and refine each step, making them more detailed and explicit.  For example:

open the file
as long as there are more lines in the file
 read a line
 look at each number in the line
 add 1 to a counter for each number found on that line

When the steps are clear enough, start coding.  Code the smallest amount you can, then compile, test and debug.  I generally go 2-3 lines AT MOST before i recompile.  So my first go would be simply trying to open the file, then close it, without getting an error.  I would not try reading it, printing it, or doing anything else...literally just open and close without error.

THEN I'd go back and try reading a line and printing it.
THEN i'd go back and try reading each line, and printing each, one after the other.
THEN I"d go back and try adding anotehr tiny piece...
etc.
 
No one can make you feel inferior without your consent - Eleanor Roosevelt. tiny ad:
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic