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

Computer Info

Welcome back to another awesome week! Registered users will now notice the ability to delete comments. Always feel free to suggest features though the suggest a problem page.

We have a fun problem to start off our week. Today's goal is to create a program that prints out information about the computer it is running on. Depending on your language/libraries of choice this problem can have you digging deep. The below are the minimum requirements for the program:

  • Operating System
  • Total Ram
  • Processor Model
  • Hard Disk Size
  • Free Space on Hard Disk

For bonus points add other information about the computer such as screen resolution or a list of all connected USB devices.

Permalink: http://problemotd.com/problem/computer-info/

Comments:

  • Ethan Glover - 10 years, 7 months ago

    Unfortunately, it doesn't seem Java supports USB, I was able to do the rest though. :)

    Here are the results: http://imgur.com/bwlIAl7

    import java.io.File;
    import java.awt.*;
    
    public class ComputerInfo {
        public static void main(String[] args) {
            // Display OS
            System.out.println("Operating System: " + System.getProperty("os.name"));
    
            // Display RAM
            com.sun.management.OperatingSystemMXBean bean =
                (com.sun.management.OperatingSystemMXBean)
                java.lang.management.ManagementFactory.getOperatingSystemMXBean();
            long memory = bean.getTotalPhysicalMemorySize();
            long gb = (long)(Math.pow(2,30));
            System.out.println("Memory: " + memory / gb + " GB");
    
            // Display processor model
            System.out.println("Processor: " + System.getenv("PROCESSOR_ARCHITECTURE"));
    
            // Display Hard Disk Size
            File file = new File("c:");
            long totalSpace = file.getTotalSpace();
            System.out.println("Disk Space: " + totalSpace / gb + " GB");
    
            // Display free space
            long freeSpace = file.getFreeSpace();
            System.out.println("Free Disk Space: " + freeSpace / gb + " GB");
    
            // Display screen resolution
            Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
            double width = screenSize.getWidth();
            double height = screenSize.getHeight();
            System.out.println("Resolution = " + (int)width + " x " + (int)height);
        }
    }
    

    reply permalink

  • Nick Krichevsky - 10 years, 7 months ago

    Requires libusb, pyusb, and psutil, but it works :). I couldn't get the device name on OSX due to a libusb error that I couldn't delve into, so I just gave the product id.

    import platform
    from psutil import virtual_memory, disk_usage
    from subprocess import Popen, PIPE
    import usb
    
    output = str(Popen("xrandr |grep '*' ",stdout=PIPE,shell=True).communicate()[0])
    reso = ""
    for item in output.split():
        if 'x' in item:
            reso=item
            break
    print "OS:",platform.platform()
    print "RAM:",int(virtual_memory().total)/1073741824," GB" #1073741824 is the amount of bytes in a gigabyte
    print "Processor:",platform.processor()
    print "HDD Space:",disk_usage('/').total/1073741824," GB"
    print "Free HDD space:",disk_usage('/').free/1073741824," GB"
    print "Resoloution:",reso
    print "Connected usb devices"
    busses = usb.busses()
    i = 1
    for bus in busses:
        print "Bus ",i
        i+=1
        for device in bus.devices:
            print "\t Device product Number:",device.idProduct
    

    reply permalink

Content curated by @MaxBurstein