Page 1 of 4 123 ... LastLast
Results 1 to 10 of 127

Thread: [Beginner] Programming Challenge: 3

Hybrid View

  1. #1
    Join Date
    Apr 2007
    Beans
    14,781

    [Beginner] Programming Challenge: 3

    The last challenge had more entries fail than succeed, which shows that the challenge was harder than it looks. User input is a pain. I recommend everyone go try that just for the experience of the hell of dealing with users. Interestingly, the best user is a programmer, so it was actually unfair for me to test. I am sure I could find an average person to crash each one of the "working" versions and take the entire operating system with it, however, I cannot be that clueless.

    Here is the last challenge: http://ubuntuforums.org/showthread.php?t=881152

    For this one, I ask non beginners to not give solutions that give away too many hints to those trying.


    The Challenge:
    Now that we dealt with user input, lets deal with program output. All put output to a prompt or in one case a GUI. Lets put somethings out to files. While we are at it, lets read from files also.

    This is important! Follow the following instructions to the letter
    Make a file named "bhaarat.text" with this content:
    Code:
    1. Assamese/Asomiya
    2. Bengali/Bangla
    3. Bodo
    4. Dogri
    5. Gujarati
    6. Hindi
    7. Kannada
    8. Kashmiri
    9. Konkani
    10. Maithili
    11. Malayalam
    12. Manipuri
    13. Marathi
    14. Nepali
    15. Oriya
    16. Punjabi
    17. Sanskrit
    18. Santhali
    19. Sindhi
    20. Tamil
    21. Telugu
    22. Urdu
    Write a program that reads this file, and takes the languages that begin with "H" or "S" and writes them to another file, named "out.text". The output can be more flexible. It can be one line (with spaces between names) or on a new line. I don't want the original numbers, but extra credit goes to those who number the lines correctly (begin with 0, if you are a geek)

    Then have this program write "23. English" to the end to continue the list in bhaarat.text.

    In short:
    • Read bhaarat.text and read the language names beginning with an "S" or "H", and write them (only the language names!) to a file named out.text
    • Output can be flexible, however, it cannot include the original numbers and some sort of whitespace should separate the words (newlines if you are going for extra credit). Extra credit for correctly numbered lines.
    • The language "English" and its number and period is then added to the original file in continuation of the list.


    Extra Credit
    • Line numbers in output
    • Use of the native script for (any) languages (shows unicode support). You can get the script for Hindi from its wikipedia page. Either version will work. I will be using gedit, unicode enabled for reading with standard packages.


    Rules:
    Do not copy code, but you can obviously read it

    Try not to comment on other submissions. It will be hard to judge changing entries. If you edit your code, post the new version and make it clear there is a new code.

    How it will be judged
    This task is very technical. There is no user input to deal with, and I will be using the correct file, so feel free to exclude error handling, unless you forsee errors.

    • It has to do what it is specified.


    As before, try to follow the following (although I think it won't come down to judging on this, as the challenge will be tricky for beginners):
    • Clean code. Readable code is a must for being a programmer who wishes to possibly interact with others. Some languages can be more readable than others, so it will be judged on the effort of writing clean code, not which language is easier to read by design.
    • Logical code. Be a programmer, not a code monkey
    • Commented code. If something is possibly unclear (like an obscure use of math), let the readers know. Do not over comment, and let the code speak for itself whenever you can. Have logical variable names and function names. (Excessive commenting is a minus factor)
    • Working code. It has to work as it is posted. The codw will be read on the forum, so remember to use this guide to paste code if you don't know how: http://ubuntuforums.org/showthread.php?t=606009


    Hints:
    • Most languages have all the functions you need to do this, so explore standard libraries.
    • You will have to readlines (hint, that is the name of the function you may be using in the std lib), and writelines. Also, you will be getting rid of line numbers, so you'll have to deal with string functions or regular expressions (Python users, split() will do this for you with no problem, explore it)
    • Using this challenge to try out a new language brings you bonus points, so please state when you are using the language for the first time if you are.

  2. #2
    Join Date
    Jan 2006
    Beans
    Hidden!
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Re: [Beginner] Programming Challenge: 3

    read and rip

    Code:
    #!/usr/bin/perl
    #
    #       untitled.pl
    #       
    #       Copyright 2008 Vyacheslav Goltser <slavikg@gmail.com>
    #       
    #       This program is free software: you can redistribute it and/or modify
    #       it under the terms of the GNU General Public License as published by
    #       the Free Software Foundation, either version 3 of the License, or
    #       (at your option) any later version.
    #       
    #       This program is distributed in the hope that it will be useful,
    #       but WITHOUT ANY WARRANTY; without even the implied warranty of
    #       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    #       GNU General Public License for more details.
    #       
    #       You should have received a copy of the GNU General Public License
    #       along with this program.  If not, see <http://www.gnu.org/licenses/>.
    #       
    
    use strict;
    use warnings;
    
    open IN, "bhaarat.text" or die $!; #+>> means read/append
    open OUT, ">out.text" or die $!;
    
    my $counter = 0;
    
    my @input = <IN>;
    close IN;
    
    print OUT map { $counter++.". ".$1."\n" if (/((?:S|H).*)$/) } @input;
    
    close OUT;
    
    # what is the point of this?
    open IN, ">>bhaarat.text" or die $!;
    print IN "23. English\n";
    close IN;
    The code is a mix of procedural and functional programming
    I am infallible, you should know that by now.
    "My favorite language is call STAR. It's extremely concise. It has exactly one verb '*', which does exactly what I want at the moment." --Larry Wall
    (02:15:31 PM) ***TimToady and snake oil go way back...
    42 lines of Perl - SHI - Home Site

  3. #3
    Join Date
    Sep 2006
    Beans
    2,914

    Re: [Beginner] Programming Challenge: 3

    Quote Originally Posted by slavik View Post

    print OUT map { $counter++.". ".$1."\n" if (/((?:S|H).*)$/) } @input;
    Code:
    print  map { $.++.". ".$1."\n" if (/((?:S|H).*)$/) } @input;

  4. #4
    Join Date
    Jul 2005
    Location
    Ontario, Canada
    Beans
    366
    Distro
    Ubuntu 7.04 Feisty Fawn

    Re: [Beginner] Programming Challenge: 3

    I tried to make it clear, but it is Haskell.
    Since Haskell can be so esoteric, I don't feel I'm giving
    away much.

    Code:
    -- By Jessehk
    -- On Saturday, August 9th 2008
    
    module Main where
    
    import System.IO
    
    -- Given an ordered list of languages,
    -- return a list of valid languages with
    -- the numbers stripped.
    extractLangs :: [String] -> [String]
    extractLangs lines = filter validLang $ map (head . tail . words) lines
        where validLang = flip elem ['H', 'S'] . head
    
    -- Return an enumerated list
    -- of an array of strings.
    enumerate :: [String] -> String
    enumerate items = unlines $ zipWith makeItem [1..] items
        where makeItem n s = show n ++ ". " ++ s
    
    main :: IO ()
    main = do
      contents <- readFile "bhaarat.text"
      writeFile "out.text" $ enumerate $ extractLangs (lines contents)
      appendFile "bhaarat.text" "23. English"
    Run with either

    Code:
    runhaskell <file>.hs
    or

    Code:
    ghc --make <file>
    ./<file>
    Last edited by Jessehk; August 14th, 2008 at 04:42 PM. Reason: replaced hand-written function with library alternative.

  5. #5
    Join Date
    Nov 2007
    Beans
    706
    Distro
    Ubuntu 9.04 Jaunty Jackalope

    Re: [Beginner] Programming Challenge: 3

    C++

    v0.1
    v0.2 (added code to append to bhaarat.text)
    v0.3 (added code to print appropriate line numbers to out.text)
    PHP Code:
    #include <iostream>
    #include <fstream>
    using namespace std;

    #define IGNORE_LIMIT 10
    #define MAX_READ_LENGTH 15

    int main(){
        
    // create pointers & open files for reading and writing.
        
    ifstream read_ptrread_ptr.open("bhaarat.text"ios::in);
        
    ofstream write_ptrwrite_ptr.open("out.text"ios::out);

        
    char lang[MAX_READ_LENGTH]; // string to store extracted language text.

        
    if (read_ptr.is_open()){
            
    int line_number;
            while (!(
    read_ptr.eof())){
                
    read_ptr.ignore(IGNORE_LIMIT' ');  // ignore line until first space.
                
    read_ptr.get(langMAX_READ_LENGTH); // write text to string 'lang'.
                
    read_ptr.ignore(IGNORE_LIMIT'\n'); // go to the next line.
                
    if (lang[0] == 'S' || lang[0] == 'H'){
                    
    write_ptr << line_number << ". " << lang << endl
                    
    line_number++;
                }
            }
            
    // close opened files
            
    read_ptr.close();
            
    write_ptr.close();
            
            
    // create pointer for appending to file; write text; close file.
            
    ofstream write_ptrwrite_ptr.open("bhaarat.text"ios::app);
            
    write_ptr << "\n23. English";
            
    write_ptr.close();
            
            
    cout << "Operation completed successfully.\n" <<
                    
    "Press Enter to continue...\n"getchar(); return (0);
        }
        
    cout << "Error opening file.\n"getchar(); return (1);

    Last edited by Sinkingships7; August 13th, 2008 at 09:58 AM.
    Programming is an art. Learn it. Live it. Love it.

  6. #6
    Join Date
    Apr 2007
    Location
    Germany
    Beans
    239
    Distro
    Ubuntu 10.04 Lucid Lynx

    Thumbs down Re: [Beginner] Programming Challenge: 3

    Another Haskell solution (I hope it works this time ):
    PHP Code:
    import System.IO

    main 
    = do
      
    content <- readFile "bhaarat.text"
      
    writeFile "out.text" sieve content
      appendFile 
    "bhaarat.text" "23. English\n"
      
    sieve :: String -> String
    sieve s 
    =  unlines enumerate (filter beginsWithBorS words s) [1..]

    enumerate :: [String] -> [Int] -> [String]
    enumerate [] = []
    enumerate (s:ss) (i:is) = (show i ++ ". " ++ s) : enumerate ss is

    beginsWithBorS 
    :: String -> Bool
    beginsWithBorS s
      
    head s == 'B' || head s == 'S' True
      
    otherwise False 
    Last edited by conehead77; August 13th, 2008 at 10:26 AM.
    NEVER use a command given to you before asking and knowing exactly what it does. Make sure you know what it is that you're telling your system to do before doing it; some commands can be very harmful to your system or leave you vulnerable to attack.

  7. #7
    Join Date
    Oct 2007
    Location
    Portugal
    Beans
    277

    Re: [Beginner] Programming Challenge: 3

    Hi all!
    I saw this challenge and decided to participate.
    It's my 1st coding in python, so please cut me some slack. =-)
    I did some Perl programming in the past, but python 'feels' a lot better (at least for me).
    Anyway, here's my entry, hope I'm not too late.
    Code:
    #!/usr/bin/python
    
    #Open the needed files.
    infile = open("bhaarat.text")
    outfile = open("output.text",'w')
    
    #Read the infile, strip the numbers and write
    #the appropriate languages to the outfile.
    for lines in infile:
        indx = lines.find(" ")
        lines = lines[(indx + 1):]
        if lines.startswith('H') or lines.startswith('S'):
            outfile.write(lines)
    
    #Close all files.    
    outfile.close()
    infile.close()
    
    #Add '23. English' to the infile.
    append = open("bhaarat.text",'a')
    append.write('23. English')
    append.close()
    Since the closing is today I didn't find the time to number the output entries. I also didn't have the time to read trough the whole thread, but I hope I didn't miss anything important.
    Let me know what you think when it's over. =-)

    BTW, LaRoza, this is a great idea!

  8. #8
    Join Date
    Jan 2006
    Beans
    Hidden!
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Re: [Beginner] Programming Challenge: 3

    Quote Originally Posted by ghostdog74 View Post
    Code:
    print  map { $.++.". ".$1."\n" if (/((?:S|H).*)$/) } @input;
    I don't get it ...
    I am infallible, you should know that by now.
    "My favorite language is call STAR. It's extremely concise. It has exactly one verb '*', which does exactly what I want at the moment." --Larry Wall
    (02:15:31 PM) ***TimToady and snake oil go way back...
    42 lines of Perl - SHI - Home Site

  9. #9
    Join Date
    Sep 2006
    Beans
    2,914

    Re: [Beginner] Programming Challenge: 3

    Quote Originally Posted by slavik View Post
    I don't get it ...
    no need for $counter.

  10. #10
    Join Date
    Jan 2006
    Beans
    Hidden!
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Re: [Beginner] Programming Challenge: 3

    I don't think it would be a good idea to use $.
    I am infallible, you should know that by now.
    "My favorite language is call STAR. It's extremely concise. It has exactly one verb '*', which does exactly what I want at the moment." --Larry Wall
    (02:15:31 PM) ***TimToady and snake oil go way back...
    42 lines of Perl - SHI - Home Site

Page 1 of 4 123 ... LastLast

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
  •