Would this be a difficult program to write? Details inside

Page 2 - Seeking answers? Join the AnandTech community: where nearly half-a-million members share solutions and discuss the latest tech.

Train

Lifer
Jun 22, 2000
13,863
68
91
www.bing.com
did this while on a con call. 20-25 lines, depending on how you count them

NOT TESTED
Code:
using System;
using System.Collections.Generic;
using System.IO;

namespace Cad_Converter
{
    class Program
    {
        static void Main(string[] args)
        {
            // the source file path
            string sourceFile = args[0];
        
            // destination file path
            string destFile = args[1];
        
            // List of started "codes"
            var codes = new List<string>();
        
            string previousCode = "";
        
            foreach (var s in File.ReadAllLines(sourceFile))
            {
                var code = ExtractCode(s);
                if (code == previousCode)
                {
                    File.AppendAllText(destFile, s + nl);
                }
                else
                {
                    var txt = "END" + nl;
                    if (codes.Contains(code))
                    {
                        txt += "CONT ";
                    }
                    else
                    {
                        txt += "BEG ";
                        codes.Add(code);
                    }
                    txt += code + nl + s + nl;
                    File.AppendAllText(destFile, txt);
                    previousCode = code;
                }
            }
        
            // Add another end at the end
            File.AppendAllText(destFile, "END");
        }
        
        private static string nl = Environment.NewLine;

        static string ExtractCode(string line)
        {
            // Grab what is between the quotes
            int start = line.IndexOf('"')+1;
            int len = line.Length - start - 1;
            return line.Substring(start, len);
        }
    }
}
 

Crusty

Lifer
Sep 30, 2001
12,684
2
81
got bored while waiting for a test suite to run

Bonus points for the first person to name the language... it should be very obvious.

Code:
tracked = Hash.new {|h,k| h[k] = false}
tracking = nil

while gets
  line = $_.split

  if line.last =~ /^[A-Z]+\"/
    puts "END" if tracked[tracking]
    tracking = line.last.tr('"','')
    tracked[tracking] = true
    puts "BEG #{tracking}"
    puts line.slice(0..-3).join(" ") + " \"#{tracking}\""
    next
  end

  if line.last !~ /^\"#{tracking}\"$/ && tracking != nil
    puts "END"
    tracking = line.last.tr('"','')
    if tracked[tracking]
      puts "CONT #{tracking}"
    end
  end

  puts $_
end

puts "END" if tracked[tracking]
 
Last edited:

Net

Golden Member
Aug 30, 2003
1,592
2
81
did this while on a con call. 20-25 lines, depending on how you count them

NOT TESTED
Code:
using System;
using System.Collections.Generic;
using System.IO;

namespace Cad_Converter
{
    class Program
    {
        static void Main(string[] args)
        {
            // the source file path
            string sourceFile = args[0];
        
            // destination file path
            string destFile = args[1];
        
            // List of started "codes"
            var codes = new List<string>();
        
            string previousCode = "";
        
            foreach (var s in File.ReadAllLines(sourceFile))
            {
                var code = ExtractCode(s);
                if (code == previousCode)
                {
                    File.AppendAllText(destFile, s + nl);
                }
                else
                {
                    var txt = "END" + nl;
                    if (codes.Contains(code))
                    {
                        txt += "CONT ";
                    }
                    else
                    {
                        txt += "BEG ";
                        codes.Add(code);
                    }
                    txt += code + nl + s + nl;
                    File.AppendAllText(destFile, txt);
                    previousCode = code;
                }
            }
        
            // Add another end at the end
            File.AppendAllText(destFile, "END");
        }
        
        private static string nl = Environment.NewLine;

        static string ExtractCode(string line)
        {
            // Grab what is between the quotes
            int start = line.IndexOf('"')+1;
            int len = line.Length - start - 1;
            return line.Substring(start, len);
        }
    }
}

So your code doesn't solve the problem but it was nice enough for you to write some code to get him going.

I took your code and wrote it in python so he can see what it looks like:

Code:
import csv

class CadConverter:
    
    def run(self, src_file, dst_file):
        codes = []
        new_lines = []
        with open(src_file, 'rb') as csvfile:
            lines = csv.reader(csvfile, delimiter='\n', quoting=csv.QUOTE_NONE)
            previousCode = None
            for row in lines:
                code = row[0].split()[-1].replace('"', '') # extract code
                if code == previousCode:
                    new_lines.append(row[0])
                else:
                    new_lines.append("END")
                    if code in codes:
                        new_lines.append("CONT " + code)
                    else:
                        new_lines.append("BEG " + code)
                        codes.append(code)
                    new_lines.append(row[0])
                    previousCode = code
            new_lines.append("END")
        with open(dst_file, 'w') as f:
            for line in new_lines:
                f.write(line + '\n')
    
def main():
    cadConverter = CadConverter()
    cadConverter.run('cadFile.txt', 'output.txt')

if __name__ == "__main__":
    main()
 
Last edited:

Train

Lifer
Jun 22, 2000
13,863
68
91
www.bing.com
got bored while waiting for a test suite to run

Bonus points for the first person to name the language... it should be very obvious.

Code:
tracked = Hash.new {|h,k| h[k] = false}
tracking = nil

while gets
  line = $_.split

  if line.last =~ /^[A-Z]+\"/
    puts "END" if tracked[tracking]
    tracking = line.last.tr('"','')
    tracked[tracking] = true
    puts "BEG #{tracking}"
    puts line.slice(0..-3).join(" ") + " \"#{tracking}\""
    next
  end

  if line.last =~ /^\"#{tracking}\"$/
    puts $_
    next
  elsif tracking != nil
    puts "END"
    tracking = line.last.tr('"','')
    if tracked[tracking]
      puts "CONT #{tracking}"
    end
  end

  puts $_
end

puts "END" if tracked[tracking]
that would be ruby.
 

Net

Golden Member
Aug 30, 2003
1,592
2
81
How so?

Only problem I see is it will add a BEG DCB line at the beginning, whereas his sample output does not. Easy edge case to fix.

This is what your program outputs:

Code:
END
BEG DCB
NEZ 13000 4860271.330 683259.150 92.391 "DCB"

END
BEG B GO
NEZ 13001 4860271.057 683258.136 92.418 "B GO"

END
BEG GO
NEZ 13002 4860260.096 683260.271 92.683 "GO"

NEZ 13003 4860293.237 683255.697 92.738 "GO"

NEZ 13004 4860318.665 683253.699 92.434 "GO"

END
BEG CB
NEZ 13005 4860319.888 683255.541 92.423 "CB"

END
BEG B EP
NEZ 13006 4860327.588 683250.237 92.557 "B EP"

END
BEG B BD
NEZ 13007 4860332.990 683248.822 91.965 "B BD"

END
BEG BD
NEZ 13008 4860338.890 683270.007 91.919 "BD"

END
BEG EP
NEZ 13009 4860333.877 683272.528 92.534 "EP"

END
CONT GO
NEZ 13010 4860324.544 683274.007 92.337 "GO"

NEZ 13011 4860296.177 683276.034 92.416 "GO"

NEZ 13012 4860272.743 683277.075 92.369 "GO"

END
CONT B EP
NEZ 13013 4860263.201 683277.590 92.347 "B EP"

END
CONT B GO
NEZ 13014 4860260.877 683277.765 92.429 "B GO"

END
CONT GO
NEZ 13015 4860262.766 683301.026 92.175 "GO"

END
CONT EP
NEZ 13016 4860264.814 683300.366 92.038 "EP"

END
BEG GO"
NEZ 13017 4860274.145 683299.075 92.101 "GO" 

END

 

Train

Lifer
Jun 22, 2000
13,863
68
91
www.bing.com
this gets you close, just need to delete the first line of the output.



Code has gotten a little messy, could probably refactor that down and make it more readable.
 

Net

Golden Member
Aug 30, 2003
1,592
2
81
Well you switched my AppendAllText to a A WriteLine, which of course adds an extra line break.

Console.Write would work better.

Uhhh..... No, I used Console.Out.WriteLine. Did you look at the screen shot?

And its more then just a line break that is different with his output. Did you compare against his output?

Your keeping "B Go" etc...
 

Train

Lifer
Jun 22, 2000
13,863
68
91
www.bing.com
Uhhh..... No, I used Console.Out.WriteLine. Did you look at the screen shot?
I got that, the point being one adds a newline at the end, the other does not.

And its more then just a line break that is different with his output. Did you compare against his output?

Your keeping "B Go" etc...
I didn't see that rule at first, my last example above handles it.
 

Net

Golden Member
Aug 30, 2003
1,592
2
81
I got that, the point being one adds a newline at the end, the other does not.

I didn't see that rule at first, my last example above handles it.

it should only add BEG ... above where the line had "B ..." instead of adding a BEG ... if it hasn't seen the code yet.

also the lines that have "B ..." should just be "..."

So "B GO" would be "GO" etc...

Also there is a trailing " on one of the lines at the end: BEG GO"
 
Last edited:

DaTT

Garage Moderator
Moderator
Feb 13, 2003
13,295
118
106
You guys are great! Thanks so much for the help and information.
 

Blueoak

Senior member
Dec 13, 2001
372
0
0
To get some practice my rusty C# skills, I wrote this up last night if you want another C# variation. Seemed to work fine in my testing. It's a console application. The exe accepts the old filename/path and new filename/path as the first two arguments (i.e. SurveyUtility.exe c:\temp\test.txt c:\temp\test2.txt). If the new file already exists, it'll end up overwriting it. I wasn't sure which you'd prefer, overwriting it, appending to it, or something else.

Code:
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;


namespace SurveyUtility
{
   class ConvertFile
   {
      static void Main(string[] args)
      {
         string oldFilePath = args[0];
         string newFilePath = args[1];

         string line;
         string currentCode;
         string previousCode = String.Empty;

         if (File.Exists(oldFilePath))
         {
            using (StreamReader srOldFile = new StreamReader(oldFilePath))
            {
               using (StreamWriter srNewFile = new StreamWriter(newFilePath, false))
               {
                  line = srOldFile.ReadLine();

                  while (line != null)
                  {
                     currentCode = line.Split(new string[] { "\"" }, StringSplitOptions.None)[1];

                     if (previousCode != currentCode && previousCode != "DCB" && previousCode != "CB" && currentCode != "DCB")
                     {
                        srNewFile.WriteLine("END");
                     }
                    
                     if (currentCode.Substring(0, 2) == "B ")
                     {
                        currentCode = currentCode.Substring(2);
                        srNewFile.WriteLine("BEG " + currentCode);
                        line = line.Replace("\"B ", "\"");
                     }
                     else if (previousCode != currentCode && currentCode != "CB" && currentCode != "DCB")
                     {
                        srNewFile.WriteLine("CONT " + currentCode);
                     }

                     srNewFile.WriteLine(line);

                     previousCode = currentCode;
                     line = srOldFile.ReadLine();

                     if (line == null)
                     {
                        srNewFile.WriteLine("END");
                     }
                  }
               }
            }
         }
      }
   }
}
 
Last edited:

dank69

Lifer
Oct 6, 2009
35,602
29,317
136
C# without any hard-coded codes like DCB or CB.

To test on compileonline:
Code:
using System;
using System.Collections.Generic;
using System.IO;

namespace ACAD
{
    class Program
    {
        static void Main(string[] args)
        {
            string sourceFile = "input.txt";
            var codes = new List<string>();
            var code = "";
            string previousCode = "";

            foreach (var s in File.ReadAllLines(sourceFile))
            {
                var finalLine = s.Replace("B ", "");
                code = finalLine.Split('"')[1];
                if (!code.Equals(previousCode ))
                {
                    if (codes.Contains(previousCode))
                    {
                        Console.WriteLine("END");
                    }
                    if (!finalLine.Equals(s))
                    {
                        if (!codes.Contains(code))
                        {
                            codes.Add(code);
                        }
                        Console.WriteLine("BEG " + code);
                    }
                    else if (codes.Contains(code))
                    {
                        Console.WriteLine("CONT " + code);
                    }
                    previousCode = code;
                }
                Console.WriteLine(finalLine);
            }
            if (codes.Contains(previousCode ))
            {
                Console.WriteLine("END");
            }
        }
    }
}

To produce an actual file:
Code:
using System;
using System.Collections.Generic;
using System.IO;

namespace ACAD
{
    class Program
    {
        static void Main(string[] args)
        {
            string sourceFile = @"c:temp\input.txt";
            string destFile = @"c:temp\output.txt";
            var codes = new List<string>();
            var code = "";
            var output = new List<string>();
            string previousCode = "";

            foreach (var s in File.ReadAllLines(sourceFile))
            {
                var finalLine = s.Replace("B ", "");
                code = finalLine.Split('"')[1];
                if (!code.Equals(previousCode))
                {
                    if (codes.Contains(previousCode))
                    {
                        output.Add("END");
                    }
                    if (!finalLine.Equals(s))
                    {
                        if (!codes.Contains(code))
                        {
                            codes.Add(code);
                        }
                        output.Add("BEG " + code);
                    }
                    else if (codes.Contains(code))
                    {
                        output.Add("CONT " + code);
                    }
                    previousCode = code;
                }
                output.Add(finalLine);
            }
            if (codes.Contains(previousCode))
            {
                output.Add("END");
            }
            File.WriteAllLines(destFile, output);
        }
    }
}
 
Last edited:

DaTT

Garage Moderator
Moderator
Feb 13, 2003
13,295
118
106
It really is amazing to see all the different ideas and thoughts from different programmers to accomplish the same task. Thanks a bunch guys.
 

Fayd

Diamond Member
Jun 28, 2001
7,971
2
76
www.manwhoring.com
What would be the best "easy-to-use" preferably free program to use to write this with....Extreme novice here.

python.

you could have learned the line handling and regex necessary to do the job in less time than it took to do those 6000 lines.
 
sale-70-410-exam    | Exam-200-125-pdf    | we-sale-70-410-exam    | hot-sale-70-410-exam    | Latest-exam-700-603-Dumps    | Dumps-98-363-exams-date    | Certs-200-125-date    | Dumps-300-075-exams-date    | hot-sale-book-C8010-726-book    | Hot-Sale-200-310-Exam    | Exam-Description-200-310-dumps?    | hot-sale-book-200-125-book    | Latest-Updated-300-209-Exam    | Dumps-210-260-exams-date    | Download-200-125-Exam-PDF    | Exam-Description-300-101-dumps    | Certs-300-101-date    | Hot-Sale-300-075-Exam    | Latest-exam-200-125-Dumps    | Exam-Description-200-125-dumps    | Latest-Updated-300-075-Exam    | hot-sale-book-210-260-book    | Dumps-200-901-exams-date    | Certs-200-901-date    | Latest-exam-1Z0-062-Dumps    | Hot-Sale-1Z0-062-Exam    | Certs-CSSLP-date    | 100%-Pass-70-383-Exams    | Latest-JN0-360-real-exam-questions    | 100%-Pass-4A0-100-Real-Exam-Questions    | Dumps-300-135-exams-date    | Passed-200-105-Tech-Exams    | Latest-Updated-200-310-Exam    | Download-300-070-Exam-PDF    | Hot-Sale-JN0-360-Exam    | 100%-Pass-JN0-360-Exams    | 100%-Pass-JN0-360-Real-Exam-Questions    | Dumps-JN0-360-exams-date    | Exam-Description-1Z0-876-dumps    | Latest-exam-1Z0-876-Dumps    | Dumps-HPE0-Y53-exams-date    | 2017-Latest-HPE0-Y53-Exam    | 100%-Pass-HPE0-Y53-Real-Exam-Questions    | Pass-4A0-100-Exam    | Latest-4A0-100-Questions    | Dumps-98-365-exams-date    | 2017-Latest-98-365-Exam    | 100%-Pass-VCS-254-Exams    | 2017-Latest-VCS-273-Exam    | Dumps-200-355-exams-date    | 2017-Latest-300-320-Exam    | Pass-300-101-Exam    | 100%-Pass-300-115-Exams    |
http://www.portvapes.co.uk/    | http://www.portvapes.co.uk/    |