#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    3
    Rep Power
    0

    A few simple python programs


    Hello all. I have some Python homework that I need some help with. I have been sick the past few days to attend class and am a little behind. This is a second level python course, and I wasn't able to take the first level either. Hopefully you can provide me with some solutions and I am sure I can learn from the solutions. Here are all of the mini questions. Please use as "basic" of functions and code as possible, as we are not expected to know any complex codes. Thanks for all help, I am sure to a professional, this would take only a few minutes.


    Exercises

    Solve the following small programming exercises by writing Python programs. Some of the exercises are stated vaguely: this has been done on purpose to make you think about possible solutions. Clarifications will be given in Course Discussion, in the labs and in class if necessary. The exercises are only roughly ordered by difficulty.

    All input is from standard input and all output is to standard output.
    (currenttime.py)
    Your program should print the date and time of its execution in the following format
    Printed on the Dss day of M, Y at HH:MM.
    where
    D is one or two digits of the day with no leading zeros,
    ss is one of st, nd, rd, th, whichever appropriate for D,
    M is the English name of the month,
    Y is four digits of the year, and
    HH and MM are the two digits for the hour and the minute respectively, printed with leading zeros.
    You can use the datetime module in Python.
    Sample output
    Printed on the 26th day of July, 2012 at 12:37.
    (blanks.py)
    Your program replaces sequences of one or more consecutive identical whitespace characters with a single character identical to the replaced ones. At the end your program prints one additional line with the maximum number of consecutive identical whitespace characters in the input.
    Sample input
    ababa

    ab ba

    xxxxxxxxxxxxxxxxxxx
    that is it followed by a lot of spaces .
    no dot at the end
    Output for sample input
    ababa
    ab ba
    xxxxxxxxxxxxxxxxxxx
    that is it followed by a lot of spaces .
    no dot at the end
    The largest run of consecutive whitespace characters was 47.
    (convert.py)
    Write a program which converts numerals from one base to another. Possible bases are between 2 and 62 and the following characters are to be used as digits
    0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
    The digits for base n are the first n characters in the above string.
    Input to you program consists of a sequence of lines which should be processed one by one. Each line contains two decimal numbers giving the source and the target base followed by a sequence of digits in the source base. Convert the input number expressed in the source base to the corresponding numeral in the target base.
    Sample input
    7 8 666
    8 7 666
    10 16 273
    16 10 111
    16 8 ffff
    62 53 hgsdflkztyZTYWc10SAZ
    62 23 hgsdflkztyZTYWc10SAZ
    62 4 hgsdflkztyZTYWc10SAZ
    62 10 hgsdflkztyZTYWc10SAZ
    Output for sample input
    526
    1164
    111
    273
    177777
    6lMFwJNk0a8rl8PeuAAqs
    hhadm3c8fea6jde4286821l9d0
    21130132133021323030003200120232310202021011002003333133131
    196163792841084734239402885221709789
    (tally.py)
    This program counts how many integer numbers occur on input, after treating all non-digit characters as whitespace. The last line of input contains one single word END. The output consists of the number and the sum of the integers in the format as indicated in the sample.
    Sample input
    Energy content of fuels KJoule/g KJoule/cc

    Hydrogen 124.7 8.7
    Coal C28H42 32.2 41.8
    Wood C.32H.40O.22 17.5 14.2
    Methane gas 61.1 .0044

    Gasoline1 30.9e6 (J/liter))) ; => 3.090e+10

    And -.23 here are some - 3.090e+10 negative -3.090e-10
    numbers.
    END
    Output for sample input
    There are 34 integers in the input summing up to 915.
    (maplesyrup.py)
    Peter has n bottles of maple syrup. He empties them one by one keeping all the bottle caps. For each k > 1 bottle caps he can get a new full bottle of maple syrup.
    How many bottles of maple syrup can Peter have in total?
    Input is a sequence of lines. Each line contains two integer numbers giving the values of n and k.
    For each line of input, output one integer number on a separate line giving the maximum number of maple syrup bottles that Peter can have.
    Sample input
    4 3
    10 3
    100 5
    1000000000 8
    Output for the sample input
    5
    14
    124
    1142857142
    (commperm.py)
    This question is optional (Bonus marks).
    Given two strings of lowercase letters, a and b, print the longest string x of lowercase letters such that there is a permutation of x that is a subsequence of a and there is a permutation of x that is a subsequence of b.
    Input consists of pairs of lines. The first line of a pair contains a and the second contains b. Each string is on a separate line and consists of at most 1,000 lowercase letters.
    For each subsequent pair of input lines, output a line containing x. If several x satisfy the criteria above, choose the first one in alphabetical order.
    Sample input
    pretty
    women
    walking
    down
    the
    street
    Output for the sample input
    e
    nw
    et
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,931
    Rep Power
    481
    Code:
    '''
        command `python -m doctest thisfile.py' to run doctests.
        
    '''
    
    BLANKIN = '''ababa
    
    ab           ba 
    
                                          xxxxxxxxxxxxxxxxxxx
    that is it followed by a lot of spaces                         .
                                                   no dot at the end
    '''
    
    def blanks(a):
        '''
            >>> blanks(BLANKIN)
            ababa
            ab ba 
             xxxxxxxxxxxxxxxxxxx
            that is it followed by a lot of spaces .
             no dot at the end
            The largest run of consecutive whitespace characters was 47.
        '''
        result = []                 # hold the resultant string output as a list.
        previous = ''               # store the prior character.
        max_run_of_white_space = 0
        current_run_of_white_space = 0
        for c in a:                 # sequentially process each input character
            if not c.isspace():
                result.append(previous)
                max_run_of_white_space = max(max_run_of_white_space, current_run_of_white_space)
                current_run_of_white_space = 0
            elif c == previous:     # identical space character
                current_run_of_white_space += 1
            else:                   # a new kind of space character
                result.append(previous)
                max_run_of_white_space = max(max_run_of_white_space, current_run_of_white_space)
                current_run_of_white_space = 1 
            previous = c            # update the prior character
        max_run_of_white_space = max(max_run_of_white_space, current_run_of_white_space)
        'Lambert Electronics, LLC.  USA, NY.'
        print(''.join(result))
        print('The largest run of consecutive whitespace characters was {}.'.format(max_run_of_white_space))
    
    
    
    def tally():
        '''
               NB. result verified in j.  www.jsoftware.com    
               A=:0 :0
            #Energy content of fuels KJoule/g KJoule/cc
            #
            #Hydrogen 124.7 8.7
            #Coal C28H42 32.2 41.8
            #Wood C.32H.40O.22 17.5 14.2
            #Methane gas 61.1 .0044
            #
            #Gasoline1 30.9e6 (J/liter))) ; => 3.090e+10
            #
            #And -.23 here are some - 3.090e+10 negative -3.090e-10
            #numbers.
            #END
            )
               (#,+/)".-.@(e.&Num_j_)`(,:&' ')}A
            34 915
        '''   
    
    
    def commperm_oompa_loompa(a, b):
        '''
            Bonus
            >>> commperm_oompa_loompa('pretty', 'women')
            'e'
            >>> [commperm_oompa_loompa(*args) for args in (('walking', 'down'), ('the', 'street'))]
            ['nw', 'et']
        '''
        # While this function passes the tests of the examples,
        # it is probably incorrect.  For instance,
        # 'aa' != commperm_oompa_loompa('aa', 'aa')
        # Also, given that the problem statement specified maximum line length---
        # which is stupid for a python program---we determine that the question
        # was probably copied verbatim from an internet programming challenge and
        # that solutions in many languages are probably also available on the
        # internet.
        # Specifying a maximum line length is merely nice to programmers using
        # languages requiring array size declarations, and it means "the program
        # is about "common permutations", and not about "dynamic memory allocation".
        c = list(set(a).intersection(set(b)))
        c.sort()
        return ''.join(c)
    I must be a novice. That took a long time. Well over an ahem order of magnitude longer than "a couple of minutes".
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2005
    Posts
    616
    Rep Power
    65
    I will be sending my homework to
    b49P23TIvg
    from now on!
    Real Programmers always confuse Christmas and Halloween because Oct31 == Dec25
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    3
    Rep Power
    0
    Thank you so much for your reply. Can anyone else help me out on the remainder of the problems?
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2009
    Posts
    516
    Rep Power
    34
    I'm going to start using oompa_loompa instead of foo-bar.
  10. #6
  11. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,931
    Rep Power
    481
    commperm is the wrong name because it doesn't solve the entire problem.

    ..._slave , ..._worker , ..._backend ,
    ah! ..._oompa_loompa

    Without understanding what a commperm is, I wrote code to pass the tests. Tests define code.


    The acceptable response to "do my homework I've made no effort" is "No one wants a bad programmer on their team. Learn it or take up a different vocation." and to "The class is over my head." would be "Take the introductory class first. Drop this class while the semester is still fresh and take a different course."

    I hope that this person really has just recovered from illness at the beginning of class (I don't believe it though) and that the person is really smart, and that an example or two will show the way.
    Last edited by b49P23TIvg; September 19th, 2013 at 08:45 PM.
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo