Problem of the Day
A new programming or logic puzzle every Mon-Fri

File Stats

We've made it to problem number 50!

This week's problems seem to be very word processor oriented. Given a file, print out the word count and the line count. For bonus points print out extra information about the file such as file size, file type, created date, and last modified date.

Permalink: http://problemotd.com/problem/file-stats/

Comments:

  • Nick Krichevsky - 10 years, 7 months ago

    Got it in 20 lines of Python. Sadly, date creation/modification date are identical on UNIX based systems, but according to the docs it should work just fine on Windows.

    from os.path import getsize,getctime,getmtime
    from time import ctime
    f=open("file.txt","r")
    d=f.readlines()
    lineCount=0
    wordCount=0
    charCount=0
    for line in d:
        lineCount+=1
        for word in line.split():
            wordCount+=1
        for char in line:
            charCount+=1
    print "Properties of file.txt"
    print "Filesize:",getsize("file.txt")
    print "Time created:",ctime(getctime("file.txt"))
    print "Time modified:",ctime(getmtime("file.txt"))
    print "Lines:",lineCount
    print "Words:",wordCount
    print "Chars:",charCount
    

    reply permalink

  • Ben - 10 years, 7 months ago

    C#

    using System;
    using System.IO;
    using System.Linq;
    
    class Program
    {
        static void Main(string[] args)
        {
            string path = "file.txt";
            string[] lines = File.ReadAllLines(path);
            FileInfo info = new FileInfo(path);
    
            Console.WriteLine("Lines:" + lines.Count());
            Console.WriteLine("Words:" + lines.Select(x => x.Split(new [] { '.', '?', '!', ' ', ';', ':', ',' }, StringSplitOptions.RemoveEmptyEntries).Length).Sum());
            Console.WriteLine("createDate:" + info.CreationTime);
            Console.WriteLine("lastModified:" + info.LastWriteTime);
            Console.WriteLine("filetype:" + info.Extension);
            Console.WriteLine("fileSize: " + info.Length + "B");
            Console.ReadLine();
        }
    }
    

    reply permalink

  • Nick Krichevsky - 10 years, 7 months ago

    Really like your idea of using a split function on the words here!

    reply permalink

  • Pufe - 10 years, 7 months ago

    wc FILENAME comes with almost every unix shell.

    reply permalink

Content curated by @MaxBurstein