Page 14 of 14 FirstFirst ... 4121314
Results 131 to 138 of 138

Thread: [Beginner] Programming Challenge: 4

  1. #131
    Join Date
    Sep 2006
    Location
    BC, Canada
    Beans
    347
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Re: [Beginner] Programming Challenge: 4

    It works, it does what is asked of it. It doesn't handle poor user input all that well though.

    PHP Code:
    #!/bin/sh

    target_site="http://laroza.freehostia.com/home/index.php"
    temp_file=/tmp/www.$$

    trap 'rm -f $temp_file' EXIT

    if [ 
    "$1" != "" ]; then 
        target_site
    =$1
    fi

    echo "Attempting to acquire file: $target_site"
    wget -q $target_site -O $temp_file

    if [ $? ]; then
        
    echo "Failed to aquire file: $target_site"
        
    exit 1
    fi

    title
    ="$(grep '<title>' $temp_file | sed 's/<\.title>//' | sed 's/<\/title>//')"

    file_ext=${target_site##*.}
    cp -f $temp_file "$title.$file_ext"

    exit 

  2. #132
    Join Date
    May 2008
    Location
    Stafford
    Beans
    77
    Distro
    Ubuntu 11.04 Natty Narwhal

    Re: [Beginner] Programming Challenge: 4

    Here's my code. Larger than it strictly needs to be, but mostly due to my exception handling and user interface.
    PHP Code:
    #!/usr/bin/python
    #Filename: challenge4.py
    #Author: Robert Orr

    import sys
    import urllib2

    print '\nWelcome to the WebPage Download Program.\nIf you wish to exit, \
    please type exit at the prompt.'
    print '--------------------------------------------------------'
    url raw_input('Please enter the URL you wish to download source code from:\n')

    if 
    url == 'exit':
        print 
    'Thankyou for using this program.'
        
    sys.exit()

    #open the url file if it is a valid url
    try:    
        
    webpage urllib2.urlopen(url)
    except urllib2.URLError:
        print 
    'Please enter a working URL next time.'
        
    sys.exit()
    except ValueError:
        print 
    'Incorrect format. Please enter a URL next time.'
        
    sys.exit()

    #create the local file and transfer the data over.
    localpage file('index.xhtml''w')
    transferdata webpage.read()
    localpage.write(transferdata)

    webpage.close()
    localpage.close()

    print 
    '\nThankyou for using this program.'
    print '%s is now stored in index.xhtml.' url 
    I can resist anything except temptation.

  3. #133
    Join Date
    Jan 2010
    Location
    Not Texas
    Beans
    340
    Distro
    Ubuntu 14.04 Trusty Tahr

    Thumbs down Re: [Beginner] Programming Challenge: 4

    Most basic, in C#, requires a valid html address including http:// ...

    This does virtually no validation, but it works if valid html addresses are entered, 100%. This I compile in Monodevelop, I don't think anything special is required to compile.

    PHP Code:
    using System;
    using System.Net;

    class 
    MainClass
    {
        public static 
    void Main (string[] args)
        {
            
    WebClient client = new WebClient();
            
    int i 0;
            
    string filebase "wgetted - ";
            
    string filename;
            
            foreach(
    string arg in args)
            {
                
    Console.WriteLine("{0}"arg);
                
    client.DownloadFile(argfilebase+i.ToString());
                
    i++;
            }
            
        }


  4. #134
    Join Date
    May 2011
    Beans
    1

    Re: [Beginner] Programming Challenge: 4

    Way too late. Just wanted the comments if anyone had any...

    Code:
    import java.net.*;
    import java.io.*;
    import javax.swing.*;
    
    public class URLWriter {
        public static void main(String[] args) throws Exception {
               
            String inputUrl = JOptionPane.showInputDialog("Please enter the url you wish to read: ");
            URL url = new URL(inputUrl);
            URLConnection uC = url.openConnection();
            BufferedReader in = new BufferedReader(
                                    new InputStreamReader(
                                    uC.getInputStream()));
            String inputLine;
    
            PrintWriter outFile = new PrintWriter("HTMLFILE.txt");    
              
            System.out.println("Writing to file...");
            while ((inputLine = in.readLine()) != null) 
            {
                outFile.println(inputLine);
            }
            System.out.println("\nDone.");
            in.close();
            outFile.close();
        }
    }
    Vers. 1.1

    Code:
    import java.net.*;
    import java.io.*;
    import javax.swing.*;
    
    public class URLWriter {
        public static void main(String[] args) throws Exception {
               
              boolean valid = false;          
              while (!(valid))
              {      
                  try
                  {    
                       String inputUrl = JOptionPane.showInputDialog("Please enter the url you wish to read: ");
                       URL url = new URL(inputUrl);
                       URLConnection yc = url.openConnection();
                       valid = true; // if connection is succesful    
                       BufferedReader in = new BufferedReader(
                                            new InputStreamReader(
                                            yc.getInputStream()));        
                       String inputLine;
                       PrintWriter outFile = new PrintWriter("HTMLFILE.txt");    
                      
                       System.out.println("Writing to file...");
                       while ((inputLine = in.readLine()) != null) 
                       {
                           outFile.println(inputLine);
                       }
                       System.out.println("\nDone.");
                       in.close();
                       outFile.close();            
                      
                  }
                  catch(MalformedURLException e)
                  {
                      JOptionPane.showMessageDialog(null, "Please enter a valid url in the format - http://theurladdresshere/");
    
                  }
              }
              
         }
    }
    Last edited by xSaintFunkyx; May 11th, 2011 at 07:23 PM.

  5. #135
    Join Date
    Nov 2009
    Location
    Ukraine
    Beans
    11

    Re: [Beginner] Programming Challenge: 4

    Written in Ruby:

    PHP Code:
    require 'open-uri'

    module PageScrape

      def self
    .start_scraping(url="http://www.tomayko.com/")
        
    # Either open an url from command line argument 
        # or, if no argument provided, open a default url.
        
    page open(ARGV[0] || url)

        
    # Default filename.
        
    filename "index"

        
    # Parse webpage to find a title. If no title
        # provided, then use default filename.
        
    page.readlines.each do |line|
          
    filename = $if line.match(/<title>(.*)<\/title>/)
        
    end

        page
    .rewind
        File
    .open("#{filename}.xhtml""w+") do |f|
          
    f.puts page.readlines.join
        end
      end
      
    end

    PageScrape
    .start_scraping 

  6. #136
    Join Date
    Sep 2011
    Beans
    10

    Re: [Beginner] Programming Challenge: 4

    I know I'm a little late to the party here, but I'm working on learning Python, and hope to be able to contribute some in the future. This seems like a great way to start towards that goal. Sorry if I'm necro-ing something that I'm not supposed to. I'm not sure if I should do these all sequentially, or jump up to some of the later challenges.

    Here is my Challenge 4. I like to separate things out into functions, but I'm not sure if doing so like I have is good form. I'd love feedback on that. I also tried to get exception handling in there, and it seems to work, but it just doesn't feel quite right...

    Thanks for any feedback! Not sure if anyone will even see this...

    PHP Code:
    from __future__ import print_function
    import urllib2

    def getWeb
    (url):
        try:    
            
    site urllib2.urlopen(url)
            
    source site.read()
            return(
    source)
        
    except urllib2.URLError:
            print(
    "The page was unable to be accessed.")
            
    def writeFile(source):
        try:    
            
    file open("index.xhtml""w")
            
    file.write(source)
            
    file.close()
        
    except IOError:
            print(
    "The file was not able to be saved.")

    def selectSite():
            
    url raw_input("Please enter a URL:")
            return(
    url)
            
    def main():
        
    url selectSite()
        
        while 
    True:    
            try:    
                
    source getWeb(url)
                
    writeFile(source)
                print(
    "The page was successfully saved.")
                break
            
    except ValueError:
                print(
    "That is not a valid URL.")
                
    url selectSite()

    main() 

  7. #137
    Join Date
    May 2007
    Location
    Leeds, UK
    Beans
    1,664
    Distro
    Ubuntu 13.10 Saucy Salamander

    Re: [Beginner] Programming Challenge: 4

    Quote Originally Posted by CoyleTrydan View Post
    I like to separate things out into functions, but I'm not sure if doing so like I have is good form. I'd love feedback on that. I also tried to get exception handling in there, and it seems to work, but it just doesn't feel quite right...
    I found both your programs easy to read and understand largely because of the way you have separated them into individual functions that each have a clear purpose. You could work on the names (functions and variables) a little bit to make the code that calls them read even more clearly but the structure is good IMO.

    You might want to look more closely at your error handling of the file in this submission, in particular whether you need a finally block and whether your error message is accurate. Or consider the 'with' statement.

    In your other submission, I found your checkGuess function and while loop a bit cumbersome. Also, checking for an out of range input would be better handled with simple loops and conditionals rather than assert and exceptions IMO.

    Icing on the cake would be some docstrings and perhaps choose to follow the PEP8 Python style guide more rigorously. Tools like pylint are very useful for advising you about code quality in that respect.

  8. #138
    Join Date
    Mar 2013
    Beans
    1

    Cool Re: [Beginner] Programming Challenge: 4

    Hi all,

    First things first, first post here on Ubuntu forums!

    Second, I know I am way too late to enter the contest, but I am using these beginner exercises to learn C#. Here is what I came up with:


    C# (C-Sharp)
    Code:
    using System;using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Net;
    
    
    namespace ProgramChallenge4
    {
        class ProgramChallenge4
        {
            static void Main(string[] args)
            {
                bool keepRunning = true;
                string url = "";
                while (keepRunning)
                {
                    url = SelectSite();
                    UpdateURL(ref url);
                    try
                    {
                        DownloadSite(ref url);
                        keepRunning = false;
                    }
                    catch (Exception)
                    {
                        Console.WriteLine("You've entered an invalid URL =/");
                    }
                }
                Console.WriteLine(url + " downloaded and saved!");
                Console.ReadLine();
            }
    
    
            public static string SelectSite()
            {
                Console.WriteLine("Please enter a valid URL: ");
                string url = Console.ReadLine();
                return url;
            }
    
    
            public static string UpdateURL(ref string url)
            {
                url = url.Trim();
                if (url.IndexOf("http://", 0) != 0)
                    url = "http://" + url;
                return url;
            }
    
    
            public static string DownloadSite(ref string url)
            {
                    WebClient wc = new WebClient();
                    wc.DownloadFile(url, @"C:\Users\Will\Desktop\Programming Material\Challenges\Program Challenge 4\index.html");
                    return url;
            }
    
    
        }
    }

Page 14 of 14 FirstFirst ... 4121314

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •